Mercurial > code > home > repos > homeauto
comparison service/mqtt_to_rdf/inference.py @ 1661:00a5624d1d14
cleanups and optimizations
author | drewp@bigasterisk.com |
---|---|
date | Sun, 19 Sep 2021 16:51:51 -0700 |
parents | d8e5b2232474 |
children | 1a7c1261302c |
comparison
equal
deleted
inserted
replaced
1660:31f7dab6a60b | 1661:00a5624d1d14 |
---|---|
46 | 46 |
47 This iterator is restartable.""" | 47 This iterator is restartable.""" |
48 lhsChunk: Chunk | 48 lhsChunk: Chunk |
49 prev: Optional['ChunkLooper'] | 49 prev: Optional['ChunkLooper'] |
50 workingSet: 'ChunkedGraph' | 50 workingSet: 'ChunkedGraph' |
51 parent: 'Lhs' # just for lhs.graph, really | |
52 | 51 |
53 def __repr__(self): | 52 def __repr__(self): |
54 return f'{self.__class__.__name__}{self._shortId}{"<pastEnd>" if self.pastEnd() else ""}' | 53 return f'{self.__class__.__name__}{self._shortId}{"<pastEnd>" if self.pastEnd() else ""}' |
55 | 54 |
56 def __post_init__(self): | 55 def __post_init__(self): |
59 | 58 |
60 self._current = CandidateBinding({}) | 59 self._current = CandidateBinding({}) |
61 self._pastEnd = False | 60 self._pastEnd = False |
62 self._seenBindings: List[CandidateBinding] = [] | 61 self._seenBindings: List[CandidateBinding] = [] |
63 | 62 |
64 log.debug(f'{INDENT*6} introducing {self!r}({self.lhsChunk}, {self._myWorkingSetMatches=})') | 63 if log.isEnabledFor(logging.DEBUG): |
64 log.debug(f'{INDENT*6} introducing {self!r}({self.lhsChunk}, {self._myWorkingSetMatches=})') | |
65 | 65 |
66 self.restart() | 66 self.restart() |
67 | 67 |
68 def _prevBindings(self) -> CandidateBinding: | 68 def _prevBindings(self) -> CandidateBinding: |
69 if not self.prev or self.prev.pastEnd(): | 69 if not self.prev or self.prev.pastEnd(): |
122 raise NotImplementedError | 122 raise NotImplementedError |
123 | 123 |
124 log.debug(f'{INDENT*6} advanceWithFunctions {pred}') | 124 log.debug(f'{INDENT*6} advanceWithFunctions {pred}') |
125 | 125 |
126 for functionType in functionsFor(pred): | 126 for functionType in functionsFor(pred): |
127 fn = functionType(self.lhsChunk, self.parent.graph) | 127 fn = functionType(self.lhsChunk) |
128 log.debug(f'{INDENT*7} ChunkLooper{self._shortId} advanceWithFunctions, {functionType=}') | 128 log.debug(f'{INDENT*7} ChunkLooper{self._shortId} advanceWithFunctions, {functionType=}') |
129 | 129 |
130 try: | 130 try: |
131 | 131 |
132 out = fn.bind(self._prevBindings()) | 132 out = fn.bind(self._prevBindings()) |
261 if log.isEnabledFor(logging.DEBUG): | 261 if log.isEnabledFor(logging.DEBUG): |
262 log.debug(f'{INDENT*5} [perm {i}] try stmts in this order: {" -> ".join(repr(p) for p in perm)}') | 262 log.debug(f'{INDENT*5} [perm {i}] try stmts in this order: {" -> ".join(repr(p) for p in perm)}') |
263 | 263 |
264 for s in perm: | 264 for s in perm: |
265 try: | 265 try: |
266 elem = ChunkLooper(s, prev, knownTrue, parent=self) | 266 # These are getting rebuilt a lot which takes time. It would |
267 # be nice if they could accept a changing `prev` order | |
268 # (which might already be ok). | |
269 elem = ChunkLooper(s, prev, knownTrue) | |
267 except NoOptions: | 270 except NoOptions: |
268 log.debug(f'{INDENT*6} permutation didnt work, try another') | 271 log.debug(f'{INDENT*6} permutation didnt work, try another') |
269 break | 272 break |
270 stmtStack.append(elem) | 273 stmtStack.append(elem) |
271 prev = stmtStack[-1] | 274 prev = stmtStack[-1] |