Mercurial > code > home > repos > homeauto
comparison service/mqtt_to_rdf/inference.py @ 1620:92f8deb59735
log layout
author | drewp@bigasterisk.com |
---|---|
date | Wed, 08 Sep 2021 18:42:53 -0700 |
parents | 3a6ed545357f |
children | da235054caa0 |
comparison
equal
deleted
inserted
replaced
1619:e24058ae4806 | 1620:92f8deb59735 |
---|---|
100 self._logCandidates(orderedVars, orderedValueSets) | 100 self._logCandidates(orderedVars, orderedValueSets) |
101 log.debug(f'{INDENT*3} trying all permutations:') | 101 log.debug(f'{INDENT*3} trying all permutations:') |
102 if not orderedValueSets: | 102 if not orderedValueSets: |
103 yield CandidateBinding({}) | 103 yield CandidateBinding({}) |
104 return | 104 return |
105 | |
105 if not orderedValueSets or not all(orderedValueSets): | 106 if not orderedValueSets or not all(orderedValueSets): |
106 # some var or bnode has no options at all | 107 # some var or bnode has no options at all |
107 return | 108 return |
108 rings: List[Iterator[Node]] = [itertools.cycle(valSet) for valSet in orderedValueSets] | 109 rings: List[Iterator[Node]] = [itertools.cycle(valSet) for valSet in orderedValueSets] |
109 currentSet: List[Node] = [next(ring) for ring in rings] | 110 currentSet: List[Node] = [next(ring) for ring in rings] |
110 starts = [valSet[-1] for valSet in orderedValueSets] | 111 starts = [valSet[-1] for valSet in orderedValueSets] |
111 while True: | 112 while True: |
112 for col, curr in enumerate(currentSet): | 113 for col, curr in enumerate(currentSet): |
113 currentSet[col] = next(rings[col]) | 114 currentSet[col] = next(rings[col]) |
114 log.debug(repr(currentSet)) | 115 log.debug(f'{INDENT*4} currentSet: {repr(currentSet)}') |
115 yield CandidateBinding(dict(zip(orderedVars, currentSet))) | 116 yield CandidateBinding(dict(zip(orderedVars, currentSet))) |
116 if curr is not starts[col]: | 117 if curr is not starts[col]: |
117 break | 118 break |
118 if col == len(orderedValueSets) - 1: | 119 if col == len(orderedValueSets) - 1: |
119 return | 120 return |
238 def applyRule(self, workingSet: Graph, implied: Graph, stats: Dict): | 239 def applyRule(self, workingSet: Graph, implied: Graph, stats: Dict): |
239 for bound in self.lhs.findCandidateBindings(ReadOnlyGraphAggregate([workingSet]), stats): | 240 for bound in self.lhs.findCandidateBindings(ReadOnlyGraphAggregate([workingSet]), stats): |
240 log.debug(f'{INDENT*3} rule has a working binding:') | 241 log.debug(f'{INDENT*3} rule has a working binding:') |
241 | 242 |
242 for lhsBoundStmt in bound.binding.apply(bound.lhsStmtsWithoutEvals()): | 243 for lhsBoundStmt in bound.binding.apply(bound.lhsStmtsWithoutEvals()): |
243 log.debug(f'{INDENT*5} adding {lhsBoundStmt=}') | 244 log.debug(f'{INDENT*4} adding {lhsBoundStmt=}') |
244 workingSet.add(lhsBoundStmt) | 245 workingSet.add(lhsBoundStmt) |
245 for newStmt in bound.binding.apply(self.rhsGraph): | 246 for newStmt in bound.binding.apply(self.rhsGraph): |
246 log.debug(f'{INDENT*5} adding {newStmt=}') | 247 log.debug(f'{INDENT*4} adding {newStmt=}') |
247 workingSet.add(newStmt) | 248 workingSet.add(newStmt) |
248 implied.add(newStmt) | 249 implied.add(newStmt) |
249 | 250 |
250 | 251 |
251 class Inference: | 252 class Inference: |
277 | 278 |
278 bailout_iterations = 100 | 279 bailout_iterations = 100 |
279 delta = 1 | 280 delta = 1 |
280 stats['initWorkingSet'] = cast(int, workingSet.__len__()) | 281 stats['initWorkingSet'] = cast(int, workingSet.__len__()) |
281 while delta > 0 and bailout_iterations > 0: | 282 while delta > 0 and bailout_iterations > 0: |
283 log.debug('') | |
282 log.info(f'{INDENT*1}*iteration ({bailout_iterations} left)') | 284 log.info(f'{INDENT*1}*iteration ({bailout_iterations} left)') |
283 bailout_iterations -= 1 | 285 bailout_iterations -= 1 |
284 delta = -len(implied) | 286 delta = -len(implied) |
285 self._iterateAllRules(workingSet, implied, stats) | 287 self._iterateAllRules(workingSet, implied, stats) |
286 delta += len(implied) | 288 delta += len(implied) |