Mercurial > code > home > repos > homeauto
comparison service/mqtt_to_rdf/inference.py @ 1652:dddfa09ea0b9
debug logging and comments
author | drewp@bigasterisk.com |
---|---|
date | Sun, 19 Sep 2021 13:19:20 -0700 |
parents | 20474ad4968e |
children | e7d594c065d4 |
comparison
equal
deleted
inserted
replaced
1651:20474ad4968e | 1652:dddfa09ea0b9 |
---|---|
81 augmentedWorkingSet = self._myWorkingSetMatches | 81 augmentedWorkingSet = self._myWorkingSetMatches |
82 else: | 82 else: |
83 augmentedWorkingSet = list( | 83 augmentedWorkingSet = list( |
84 applyChunky(self.prev.currentBinding(), self._myWorkingSetMatches, returnBoundStatementsOnly=False)) | 84 applyChunky(self.prev.currentBinding(), self._myWorkingSetMatches, returnBoundStatementsOnly=False)) |
85 | 85 |
86 log.debug(f'{INDENT*6} {self}.advance has {augmentedWorkingSet=}') | 86 log.debug(f'{INDENT*6} --> {self}.advance has {augmentedWorkingSet=} {self._current=}') |
87 | 87 |
88 if self._advanceWithPlainMatches(augmentedWorkingSet): | 88 if self._advanceWithPlainMatches(augmentedWorkingSet): |
89 log.debug(f'{INDENT*6} <-- {self}.advance finished with plain matches') | |
89 return | 90 return |
90 | 91 |
91 if self._advanceWithFunctions(): | 92 if self._advanceWithFunctions(): |
92 return | 93 log.debug(f'{INDENT*6} <-- {self}.advance finished with function matches') |
93 | 94 return |
94 log.debug(f'{INDENT*6} {self} is past end') | 95 |
96 log.debug(f'{INDENT*6} <-- {self}.advance had nothing and is now past end') | |
95 self._pastEnd = True | 97 self._pastEnd = True |
96 | 98 |
97 def _advanceWithPlainMatches(self, augmentedWorkingSet: Sequence[Chunk]) -> bool: | 99 def _advanceWithPlainMatches(self, augmentedWorkingSet: Sequence[Chunk]) -> bool: |
98 log.debug(f'{INDENT*7} {self} mines {len(augmentedWorkingSet)} matching augmented statements') | 100 log.debug(f'{INDENT*7} {self} mines {len(augmentedWorkingSet)} matching augmented statements') |
99 for s in augmentedWorkingSet: | 101 for s in augmentedWorkingSet: |
117 def _advanceWithFunctions(self) -> bool: | 119 def _advanceWithFunctions(self) -> bool: |
118 pred: Node = self.lhsChunk.predicate | 120 pred: Node = self.lhsChunk.predicate |
119 if not isinstance(pred, URIRef): | 121 if not isinstance(pred, URIRef): |
120 raise NotImplementedError | 122 raise NotImplementedError |
121 | 123 |
124 log.debug(f'{INDENT*6} advanceWithFunctions {pred}') | |
125 | |
122 for functionType in functionsFor(pred): | 126 for functionType in functionsFor(pred): |
123 fn = functionType(self.lhsChunk, self.parent.graph) | 127 fn = functionType(self.lhsChunk, self.parent.graph) |
128 log.debug(f'{INDENT*7} ChunkLooper{self._shortId} advanceWithFunctions, {functionType=}') | |
129 | |
124 try: | 130 try: |
131 | |
125 out = fn.bind(self._prevBindings()) | 132 out = fn.bind(self._prevBindings()) |
126 except BindingUnknown: | 133 except BindingUnknown: |
127 pass | 134 pass |
128 else: | 135 else: |
129 if out is not None: | 136 if out is not None: |
227 if done: | 234 if done: |
228 break | 235 break |
229 | 236 |
230 def _debugChunkStack(self, label: str, chunkStack: List[ChunkLooper]): | 237 def _debugChunkStack(self, label: str, chunkStack: List[ChunkLooper]): |
231 log.debug(f'{INDENT*5} {label}:') | 238 log.debug(f'{INDENT*5} {label}:') |
232 for l in chunkStack: | 239 for i, l in enumerate(chunkStack): |
233 log.debug(f'{INDENT*6} {l} curbind={l.currentBinding() if not l.pastEnd() else "<end>"}') | 240 log.debug(f'{INDENT*6} [{i}] {l} curbind={l.currentBinding() if not l.pastEnd() else "<end>"}') |
234 | 241 |
235 def _checkPredicateCounts(self, knownTrue): | 242 def _checkPredicateCounts(self, knownTrue): |
236 """raise NoOptions quickly in some cases""" | 243 """raise NoOptions quickly in some cases""" |
237 | 244 |
238 if self.graph.noPredicatesAppear(self.myPreds): | 245 if self.graph.noPredicatesAppear(self.myPreds): |
239 log.info(f'{INDENT*2} checkPredicateCounts does cull because not all {self.myPreds=} are in knownTrue') | 246 log.debug(f'{INDENT*3} checkPredicateCounts does cull because not all {self.myPreds=} are in knownTrue') |
240 return True | 247 return True |
241 log.info(f'{INDENT*2} checkPredicateCounts does not cull because all {self.myPreds=} are in knownTrue') | 248 log.debug(f'{INDENT*3} checkPredicateCounts does not cull because all {self.myPreds=} are in knownTrue') |
242 return False | 249 return False |
243 | 250 |
244 def _assembleRings(self, knownTrue: ChunkedGraph, stats) -> List[ChunkLooper]: | 251 def _assembleRings(self, knownTrue: ChunkedGraph, stats) -> List[ChunkLooper]: |
245 """make ChunkLooper for each stmt in our LHS graph, but do it in a way that they all | 252 """make ChunkLooper for each stmt in our LHS graph, but do it in a way that they all |
246 start out valid (or else raise NoOptions)""" | 253 start out valid (or else raise NoOptions). static chunks have already been confirmed.""" |
247 | 254 |
248 log.info(f'{INDENT*2} stats={dict(stats)}') | 255 log.info(f'{INDENT*2} stats={dict(stats)}') |
249 log.info(f'{INDENT*2} taking permutations of {len(self.graph.patternChunks)=}') | 256 log.info(f'{INDENT*2} taking permutations of {len(self.graph.patternChunks)=}') |
250 for i, perm in enumerate(itertools.permutations(self.graph.patternChunks)): | 257 for i, perm in enumerate(itertools.permutations(self.graph.patternChunks)): |
251 stmtStack: List[ChunkLooper] = [] | 258 stmtStack: List[ChunkLooper] = [] |
262 stmtStack.append(elem) | 269 stmtStack.append(elem) |
263 prev = stmtStack[-1] | 270 prev = stmtStack[-1] |
264 else: | 271 else: |
265 return stmtStack | 272 return stmtStack |
266 if i > 5000: | 273 if i > 5000: |
267 raise NotImplementedError(f'trying too many permutations {len(self.graph.patternChunks)=}') | 274 raise NotImplementedError(f'trying too many permutations {len(chunks)=}') |
268 | 275 |
269 log.debug(f'{INDENT*6} no perms worked- rule cannot match anything') | 276 log.debug(f'{INDENT*6} no perms worked- rule cannot match anything') |
270 raise NoOptions() | 277 raise NoOptions() |
271 | 278 |
272 def _advanceAll(self, stmtStack: List[ChunkLooper]) -> bool: | 279 def _advanceAll(self, stmtStack: List[ChunkLooper]) -> bool: |