comparison service/mqtt_to_rdf/candidate_binding.py @ 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 22d481f0a924
children 20474ad4968e
comparison
equal deleted inserted replaced
1637:ec3f98d0c1d8 1638:0ba1625037ae
5 from prometheus_client import Summary 5 from prometheus_client import Summary
6 from rdflib import BNode, Graph 6 from rdflib import BNode, Graph
7 from rdflib.term import Node, Variable 7 from rdflib.term import Node, Variable
8 8
9 from inference_types import BindableTerm, BindingUnknown, Triple 9 from inference_types import BindableTerm, BindingUnknown, Triple
10
10 log = logging.getLogger('cbind') 11 log = logging.getLogger('cbind')
11 INDENT = ' ' 12 INDENT = ' '
13
14
15 class BindingConflict(ValueError):
16 pass
17
12 18
13 @dataclass 19 @dataclass
14 class CandidateBinding: 20 class CandidateBinding:
15 binding: Dict[BindableTerm, Node] 21 binding: Dict[BindableTerm, Node]
16 22
19 return f'CandidateBinding({b})' 25 return f'CandidateBinding({b})'
20 26
21 def apply(self, g: Union[Graph, Iterable[Triple]], returnBoundStatementsOnly=True) -> Iterator[Triple]: 27 def apply(self, g: Union[Graph, Iterable[Triple]], returnBoundStatementsOnly=True) -> Iterator[Triple]:
22 for stmt in g: 28 for stmt in g:
23 try: 29 try:
24 bound = ( 30 bound = (self.applyTerm(stmt[0], returnBoundStatementsOnly), self.applyTerm(stmt[1], returnBoundStatementsOnly),
25 self.applyTerm(stmt[0], returnBoundStatementsOnly), 31 self.applyTerm(stmt[2], returnBoundStatementsOnly))
26 self.applyTerm(stmt[1], returnBoundStatementsOnly),
27 self.applyTerm(stmt[2], returnBoundStatementsOnly))
28 except BindingUnknown: 32 except BindingUnknown:
29 log.debug(f'{INDENT*7} CB.apply cant bind {stmt} using {self.binding}') 33 log.debug(f'{INDENT*7} CB.apply cant bind {stmt} using {self.binding}')
30 34
31 continue 35 continue
32 log.debug(f'{INDENT*7} CB.apply took {stmt} to {bound}') 36 log.debug(f'{INDENT*7} CB.apply took {stmt} to {bound}')
43 return term 47 return term
44 48
45 def addNewBindings(self, newBindings: 'CandidateBinding'): 49 def addNewBindings(self, newBindings: 'CandidateBinding'):
46 for k, v in newBindings.binding.items(): 50 for k, v in newBindings.binding.items():
47 if k in self.binding and self.binding[k] != v: 51 if k in self.binding and self.binding[k] != v:
48 raise ValueError(f'conflict- thought {k} would be {self.binding[k]} but another Evaluation said it should be {v}') 52 raise BindingConflict(f'thought {k} would be {self.binding[k]} but another Evaluation said it should be {v}')
49 self.binding[k] = v 53 self.binding[k] = v
50 54
51 def copy(self): 55 def copy(self):
52 return CandidateBinding(self.binding.copy()) 56 return CandidateBinding(self.binding.copy())
53 57