Mercurial > code > home > repos > homeauto
changeset 1638:0ba1625037ae
don't crash, just skip the rule if there's a BindingConflict (no clear test case yet)
author | drewp@bigasterisk.com |
---|---|
date | Mon, 13 Sep 2021 01:53:32 -0700 |
parents | ec3f98d0c1d8 |
children | ae5ca4ba8954 |
files | service/mqtt_to_rdf/candidate_binding.py service/mqtt_to_rdf/inference.py |
diffstat | 2 files changed, 14 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/service/mqtt_to_rdf/candidate_binding.py Mon Sep 13 01:36:06 2021 -0700 +++ b/service/mqtt_to_rdf/candidate_binding.py Mon Sep 13 01:53:32 2021 -0700 @@ -7,9 +7,15 @@ from rdflib.term import Node, Variable from inference_types import BindableTerm, BindingUnknown, Triple + log = logging.getLogger('cbind') INDENT = ' ' + +class BindingConflict(ValueError): + pass + + @dataclass class CandidateBinding: binding: Dict[BindableTerm, Node] @@ -21,10 +27,8 @@ def apply(self, g: Union[Graph, Iterable[Triple]], returnBoundStatementsOnly=True) -> Iterator[Triple]: for stmt in g: try: - bound = ( - self.applyTerm(stmt[0], returnBoundStatementsOnly), - self.applyTerm(stmt[1], returnBoundStatementsOnly), - self.applyTerm(stmt[2], returnBoundStatementsOnly)) + bound = (self.applyTerm(stmt[0], returnBoundStatementsOnly), self.applyTerm(stmt[1], returnBoundStatementsOnly), + self.applyTerm(stmt[2], returnBoundStatementsOnly)) except BindingUnknown: log.debug(f'{INDENT*7} CB.apply cant bind {stmt} using {self.binding}') @@ -45,7 +49,7 @@ 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}') + raise BindingConflict(f'thought {k} would be {self.binding[k]} but another Evaluation said it should be {v}') self.binding[k] = v def copy(self):
--- a/service/mqtt_to_rdf/inference.py Mon Sep 13 01:36:06 2021 -0700 +++ b/service/mqtt_to_rdf/inference.py Mon Sep 13 01:53:32 2021 -0700 @@ -14,7 +14,7 @@ from rdflib.graph import ConjunctiveGraph, ReadOnlyGraphAggregate from rdflib.term import Node, Variable -from candidate_binding import CandidateBinding +from candidate_binding import BindingConflict, CandidateBinding from inference_types import BindingUnknown, ReadOnlyWorkingSet, Triple from lhs_evaluation import functionsFor, lhsStmtsUsedByFuncs @@ -339,7 +339,10 @@ key = tuple(sorted(bound.binding.binding.items())), b self.rhsBnodeMap.setdefault(key, BNode()) - bound.binding.addNewBindings(CandidateBinding({b: self.rhsBnodeMap[key]})) + try: + bound.binding.addNewBindings(CandidateBinding({b: self.rhsBnodeMap[key]})) + except BindingConflict: + continue # for lhsBoundStmt in bound.binding.apply(bound.lhsStmtsWithoutEvals()): # log.debug(f'{INDENT*6} adding to workingSet {lhsBoundStmt=}')