comparison service/mqtt_to_rdf/inference.py @ 1611:a794a150a89b

a little inner-loop performace. same number of iterations, but less time on logging and some stmt filtering
author drewp@bigasterisk.com
date Mon, 06 Sep 2021 18:18:42 -0700
parents 6fc48ef4c696
children 272f78d4671a
comparison
equal deleted inserted replaced
1610:6fc48ef4c696 1611:a794a150a89b
37 graph: Graph 37 graph: Graph
38 38
39 def __post_init__(self): 39 def __post_init__(self):
40 # do precomputation in here that's not specific to the workingSet 40 # do precomputation in here that's not specific to the workingSet
41 self.staticRuleStmts = Graph() 41 self.staticRuleStmts = Graph()
42 self.nonStaticRuleStmts = Graph()
43
42 self.lhsBindables: Set[BindableTerm] = set() 44 self.lhsBindables: Set[BindableTerm] = set()
43 self.lhsBnodes: Set[BNode] = set() 45 self.lhsBnodes: Set[BNode] = set()
44 for ruleStmt in self.graph: 46 for ruleStmt in self.graph:
45 varsAndBnodesInStmt = [term for term in ruleStmt if isinstance(term, (Variable, BNode))] 47 varsAndBnodesInStmt = [term for term in ruleStmt if isinstance(term, (Variable, BNode))]
46 self.lhsBindables.update(varsAndBnodesInStmt) 48 self.lhsBindables.update(varsAndBnodesInStmt)
47 self.lhsBnodes.update(x for x in varsAndBnodesInStmt if isinstance(x, BNode)) 49 self.lhsBnodes.update(x for x in varsAndBnodesInStmt if isinstance(x, BNode))
48 if not varsAndBnodesInStmt: 50 if not varsAndBnodesInStmt:
49 self.staticRuleStmts.add(ruleStmt) 51 self.staticRuleStmts.add(ruleStmt)
52 else:
53 self.nonStaticRuleStmts.add(ruleStmt)
50 54
51 self.evaluations = list(Evaluation.findEvals(self.graph)) 55 self.evaluations = list(Evaluation.findEvals(self.graph))
52 56
53 def __repr__(self): 57 def __repr__(self):
54 return f"Lhs({graphDump(self.graph)})" 58 return f"Lhs({graphDump(self.graph)})"
172 log.debug(f'{INDENT*4} eval rules made {delta} new bindings') 176 log.debug(f'{INDENT*4} eval rules made {delta} new bindings')
173 return delta 177 return delta
174 178
175 def verify(self, workingSet: ReadOnlyWorkingSet) -> bool: 179 def verify(self, workingSet: ReadOnlyWorkingSet) -> bool:
176 """Can this bound lhs be true all at once in workingSet?""" 180 """Can this bound lhs be true all at once in workingSet?"""
177 boundLhs = list(self.binding.apply(self.lhs.graph)) # leave out statics and evals! 181 boundLhs = self.binding.apply(self.lhs.nonStaticRuleStmts - self.usedByFuncs)
178 boundUsedByFuncs = list(self.binding.apply(self.usedByFuncs)) 182
179 183 if log.isEnabledFor(logging.DEBUG):
180 self._logVerifyBanner(boundLhs, workingSet, boundUsedByFuncs) 184 boundLhs = list(boundLhs)
185 self._logVerifyBanner(boundLhs, workingSet)
181 186
182 for stmt in boundLhs: 187 for stmt in boundLhs:
183 log.debug(f'{INDENT*4} check for {stmt}') 188 log.debug(f'{INDENT*4} check for %s', stmt)
184 189
185 if stmt in boundUsedByFuncs: 190 if stmt not in workingSet:
186 pass
187 elif stmt in workingSet:
188 pass
189 else:
190 log.debug(f'{INDENT*5} stmt not known to be true') 191 log.debug(f'{INDENT*5} stmt not known to be true')
191 return False 192 return False
192 return True 193 return True
193 194
194 def _logVerifyBanner(self, boundLhs, workingSet: ReadOnlyWorkingSet, boundUsedByFuncs): 195 def _logVerifyBanner(self, boundLhs, workingSet: ReadOnlyWorkingSet):
195 if not log.isEnabledFor(logging.DEBUG):
196 return
197
198 log.debug(f'{INDENT*4}/ verify all bindings against this boundLhs:') 196 log.debug(f'{INDENT*4}/ verify all bindings against this boundLhs:')
199 for stmt in sorted(boundLhs): 197 for stmt in sorted(boundLhs):
200 log.debug(f'{INDENT*4}|{INDENT} {stmt}') 198 log.debug(f'{INDENT*4}|{INDENT} {stmt}')
201 199
202 # log.debug(f'{INDENT*4}| and against this workingSet:') 200 # log.debug(f'{INDENT*4}| and against this workingSet:')
203 # for stmt in sorted(workingSet): 201 # for stmt in sorted(workingSet):
204 # log.debug(f'{INDENT*4}|{INDENT} {stmt}') 202 # log.debug(f'{INDENT*4}|{INDENT} {stmt}')
205 203
206 stmts = sorted(boundUsedByFuncs)
207 if stmts:
208 log.debug(f'{INDENT*4}| while ignoring these usedByFuncs:')
209 for stmt in stmts:
210 log.debug(f'{INDENT*4}|{INDENT} {stmt}')
211 log.debug(f'{INDENT*4}\\') 204 log.debug(f'{INDENT*4}\\')
212 205
213 206
214 @dataclass 207 @dataclass
215 class Rule: 208 class Rule: