comparison service/mqtt_to_rdf/candidate_binding.py @ 1664:1a7c1261302c

logic fix- some bindings were being returned 2+; some 0 times
author drewp@bigasterisk.com
date Mon, 20 Sep 2021 23:19:08 -0700
parents 20474ad4968e
children a2347393b43e
comparison
equal deleted inserted replaced
1663:a0bf320c70fe 1664:1a7c1261302c
19 @dataclass 19 @dataclass
20 class CandidateBinding: 20 class CandidateBinding:
21 binding: Dict[BindableTerm, Node] 21 binding: Dict[BindableTerm, Node]
22 22
23 def __repr__(self): 23 def __repr__(self):
24 b = " ".join("%s=%s" % (k, v) for k, v in sorted(self.binding.items())) 24 b = " ".join("%r=%r" % (var, value) for var, value in sorted(self.binding.items()))
25 return f'CandidateBinding({b})' 25 return f'CandidateBinding({b})'
26 26
27 def apply(self, g: Graph, returnBoundStatementsOnly=True) -> Iterator[Triple]: 27 def apply(self, g: Graph, returnBoundStatementsOnly=True) -> Iterator[Triple]:
28 for stmt in g: 28 for stmt in g:
29 try: 29 try:
30 bound = ( 30 bound = (
31 self.applyTerm(stmt[0], returnBoundStatementsOnly), # 31 self.applyTerm(stmt[0], returnBoundStatementsOnly), #
32 self.applyTerm(stmt[1], returnBoundStatementsOnly), # 32 self.applyTerm(stmt[1], returnBoundStatementsOnly), #
33 self.applyTerm(stmt[2], returnBoundStatementsOnly)) 33 self.applyTerm(stmt[2], returnBoundStatementsOnly))
34 except BindingUnknown: 34 except BindingUnknown:
35 log.debug(f'{INDENT*7} CB.apply cant bind {stmt} using {self.binding}') 35 if log.isEnabledFor(logging.DEBUG):
36 log.debug(f'{INDENT*7} CB.apply cant bind {stmt} using {self.binding}')
36 37
37 continue 38 continue
38 log.debug(f'{INDENT*7} CB.apply took {stmt} to {bound}') 39 if log.isEnabledFor(logging.DEBUG):
40 log.debug(f'{INDENT*7} CB.apply took {stmt} to {bound}')
39 41
40 yield bound 42 yield bound
41 43
42 def applyTerm(self, term: Node, failUnbound=True): 44 def applyTerm(self, term: Node, failUnbound=True):
43 if isinstance(term, (Variable, BNode)): 45 if isinstance(term, (Variable, BNode)):
52 for k, v in newBindings.binding.items(): 54 for k, v in newBindings.binding.items():
53 if k in self.binding and self.binding[k] != v: 55 if k in self.binding and self.binding[k] != v:
54 raise BindingConflict(f'thought {k} would be {self.binding[k]} but another Evaluation said it should be {v}') 56 raise BindingConflict(f'thought {k} would be {self.binding[k]} but another Evaluation said it should be {v}')
55 self.binding[k] = v 57 self.binding[k] = v
56 58
59 def subtract(self, removeBindings: 'CandidateBinding'):
60 for k, v in removeBindings.binding.items():
61 if k in self.binding:
62 del self.binding[k]
63
57 def copy(self): 64 def copy(self):
58 return CandidateBinding(self.binding.copy()) 65 return CandidateBinding(self.binding.copy())
59 66
60 def contains(self, term: BindableTerm): 67 def contains(self, term: BindableTerm):
61 return term in self.binding 68 return term in self.binding