# HG changeset patch # User drewp@bigasterisk.com # Date 1630995623 25200 # Node ID bcfa368e54986bb980aa2b0b62fa486d1326e334 # Parent 97b2c3cfdb83f0653137d68b01c8d2869fc6adf5 change a Graph.__sub__ to Set.difference in verify() for a big speedup diff -r 97b2c3cfdb83 -r bcfa368e5498 service/mqtt_to_rdf/candidate_binding.py --- a/service/mqtt_to_rdf/candidate_binding.py Mon Sep 06 23:04:34 2021 -0700 +++ b/service/mqtt_to_rdf/candidate_binding.py Mon Sep 06 23:20:23 2021 -0700 @@ -1,5 +1,5 @@ from dataclasses import dataclass -from typing import Dict, Iterator +from typing import Dict, Iterable, Iterator, Union from prometheus_client import Summary from rdflib import BNode, Graph @@ -16,7 +16,7 @@ b = " ".join("%s=%s" % (k, v) for k, v in sorted(self.binding.items())) return f'CandidateBinding({b})' - def apply(self, g: Graph) -> Iterator[Triple]: + def apply(self, g: Union[Graph, Iterable[Triple]]) -> Iterator[Triple]: for stmt in g: try: bound = (self._applyTerm(stmt[0]), self._applyTerm(stmt[1]), self._applyTerm(stmt[2])) @@ -35,6 +35,5 @@ def addNewBindings(self, newBindings: 'CandidateBinding'): for k, v in newBindings.binding.items(): if k in self.binding and self.binding[k] != v: - raise ValueError( - f'conflict- thought {k} would be {self.binding[k]} but another Evaluation said it should be {v}') - self.binding[k] = v \ No newline at end of file + raise ValueError(f'conflict- thought {k} would be {self.binding[k]} but another Evaluation said it should be {v}') + self.binding[k] = v diff -r 97b2c3cfdb83 -r bcfa368e5498 service/mqtt_to_rdf/inference.py --- a/service/mqtt_to_rdf/inference.py Mon Sep 06 23:04:34 2021 -0700 +++ b/service/mqtt_to_rdf/inference.py Mon Sep 06 23:20:23 2021 -0700 @@ -48,6 +48,8 @@ else: self.nonStaticRuleStmts.add(ruleStmt) + self.nonStaticRuleStmtsSet = set(self.nonStaticRuleStmts) + self.evaluations = list(Evaluation.findEvals(self.graph)) def __repr__(self): @@ -194,7 +196,8 @@ def verify(self, workingSet: ReadOnlyWorkingSet) -> bool: """Can this bound lhs be true all at once in workingSet?""" - boundLhs = self.binding.apply(self.lhs.nonStaticRuleStmts - self.usedByFuncs) + rem = cast(Set[Triple], self.lhs.nonStaticRuleStmtsSet.difference(self.usedByFuncs)) + boundLhs = self.binding.apply(rem) if log.isEnabledFor(logging.DEBUG): boundLhs = list(boundLhs)