Mercurial > code > home > repos > homeauto
comparison service/mqtt_to_rdf/inference.py @ 1616:3a6ed545357f
optimization: stream stmts instead of building a Graph
author | drewp@bigasterisk.com |
---|---|
date | Mon, 06 Sep 2021 23:23:43 -0700 |
parents | bcfa368e5498 |
children | 48bf62008c82 92f8deb59735 |
comparison
equal
deleted
inserted
replaced
1615:bcfa368e5498 | 1616:3a6ed545357f |
---|---|
172 | 172 |
173 def __post_init__(self): | 173 def __post_init__(self): |
174 self.usedByFuncs = Graph() | 174 self.usedByFuncs = Graph() |
175 self._applyFunctions() | 175 self._applyFunctions() |
176 | 176 |
177 self.graphWithoutEvals = self.lhs.graph - self.usedByFuncs | 177 def lhsStmtsWithoutEvals(self): |
178 for stmt in self.lhs.graph: | |
179 if stmt in self.usedByFuncs: | |
180 continue | |
181 yield stmt | |
178 | 182 |
179 def _applyFunctions(self): | 183 def _applyFunctions(self): |
180 """may grow the binding with some results""" | 184 """may grow the binding with some results""" |
181 while True: | 185 while True: |
182 delta = self._applyFunctionsIteration() | 186 delta = self._applyFunctionsIteration() |
233 | 237 |
234 def applyRule(self, workingSet: Graph, implied: Graph, stats: Dict): | 238 def applyRule(self, workingSet: Graph, implied: Graph, stats: Dict): |
235 for bound in self.lhs.findCandidateBindings(ReadOnlyGraphAggregate([workingSet]), stats): | 239 for bound in self.lhs.findCandidateBindings(ReadOnlyGraphAggregate([workingSet]), stats): |
236 log.debug(f'{INDENT*3} rule has a working binding:') | 240 log.debug(f'{INDENT*3} rule has a working binding:') |
237 | 241 |
238 for lhsBoundStmt in bound.binding.apply(bound.graphWithoutEvals): | 242 for lhsBoundStmt in bound.binding.apply(bound.lhsStmtsWithoutEvals()): |
239 log.debug(f'{INDENT*5} adding {lhsBoundStmt=}') | 243 log.debug(f'{INDENT*5} adding {lhsBoundStmt=}') |
240 workingSet.add(lhsBoundStmt) | 244 workingSet.add(lhsBoundStmt) |
241 for newStmt in bound.binding.apply(self.rhsGraph): | 245 for newStmt in bound.binding.apply(self.rhsGraph): |
242 log.debug(f'{INDENT*5} adding {newStmt=}') | 246 log.debug(f'{INDENT*5} adding {newStmt=}') |
243 workingSet.add(newStmt) | 247 workingSet.add(newStmt) |