Mercurial > code > home > repos > homeauto
annotate 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 |
rev | line source |
---|---|
1587 | 1 """ |
2 copied from reasoning 2021-08-29. probably same api. should | |
3 be able to lib/ this out | |
4 """ | |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
5 import itertools |
1587 | 6 import logging |
1601 | 7 import time |
1594 | 8 from collections import defaultdict |
9 from dataclasses import dataclass, field | |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
10 from typing import Dict, Iterator, List, Set, Tuple, Union, cast |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
11 |
1587 | 12 from prometheus_client import Summary |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
13 from rdflib import BNode, Graph, Namespace, URIRef |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
14 from rdflib.graph import ConjunctiveGraph, ReadOnlyGraphAggregate |
1587 | 15 from rdflib.term import Node, Variable |
16 | |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
17 from candidate_binding import CandidateBinding |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
18 from inference_types import (BindableTerm, EvaluationFailed, ReadOnlyWorkingSet, Triple) |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
19 from lhs_evaluation import Evaluation |
1605 | 20 |
1587 | 21 log = logging.getLogger('infer') |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
22 INDENT = ' ' |
1587 | 23 |
1601 | 24 INFER_CALLS = Summary('read_rules_calls', 'calls') |
1587 | 25 |
26 ROOM = Namespace("http://projects.bigasterisk.com/room/") | |
27 LOG = Namespace('http://www.w3.org/2000/10/swap/log#') | |
28 MATH = Namespace('http://www.w3.org/2000/10/swap/math#') | |
29 | |
30 | |
1594 | 31 @dataclass |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
32 class Lhs: |
1594 | 33 graph: Graph |
34 | |
35 def __post_init__(self): | |
1608 | 36 # do precomputation in here that's not specific to the workingSet |
37 self.staticRuleStmts = Graph() | |
1611
a794a150a89b
a little inner-loop performace. same number of iterations, but less time on logging and some stmt filtering
drewp@bigasterisk.com
parents:
1610
diff
changeset
|
38 self.nonStaticRuleStmts = Graph() |
a794a150a89b
a little inner-loop performace. same number of iterations, but less time on logging and some stmt filtering
drewp@bigasterisk.com
parents:
1610
diff
changeset
|
39 |
1608 | 40 self.lhsBindables: Set[BindableTerm] = set() |
41 self.lhsBnodes: Set[BNode] = set() | |
1594 | 42 for ruleStmt in self.graph: |
43 varsAndBnodesInStmt = [term for term in ruleStmt if isinstance(term, (Variable, BNode))] | |
44 self.lhsBindables.update(varsAndBnodesInStmt) | |
45 self.lhsBnodes.update(x for x in varsAndBnodesInStmt if isinstance(x, BNode)) | |
46 if not varsAndBnodesInStmt: | |
47 self.staticRuleStmts.add(ruleStmt) | |
1611
a794a150a89b
a little inner-loop performace. same number of iterations, but less time on logging and some stmt filtering
drewp@bigasterisk.com
parents:
1610
diff
changeset
|
48 else: |
a794a150a89b
a little inner-loop performace. same number of iterations, but less time on logging and some stmt filtering
drewp@bigasterisk.com
parents:
1610
diff
changeset
|
49 self.nonStaticRuleStmts.add(ruleStmt) |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
50 |
1615
bcfa368e5498
change a Graph.__sub__ to Set.difference in verify() for a big speedup
drewp@bigasterisk.com
parents:
1614
diff
changeset
|
51 self.nonStaticRuleStmtsSet = set(self.nonStaticRuleStmts) |
bcfa368e5498
change a Graph.__sub__ to Set.difference in verify() for a big speedup
drewp@bigasterisk.com
parents:
1614
diff
changeset
|
52 |
1602 | 53 self.evaluations = list(Evaluation.findEvals(self.graph)) |
54 | |
1609
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1608
diff
changeset
|
55 def __repr__(self): |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1608
diff
changeset
|
56 return f"Lhs({graphDump(self.graph)})" |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1608
diff
changeset
|
57 |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
58 def findCandidateBindings(self, workingSet: ReadOnlyWorkingSet, stats) -> Iterator['BoundLhs']: |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
59 """bindings that fit the LHS of a rule, using statements from workingSet and functions |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
60 from LHS""" |
1597 | 61 log.debug(f'{INDENT*3} nodesToBind: {self.lhsBindables}') |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
62 stats['findCandidateBindingsCalls'] += 1 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
63 |
1600 | 64 if not self._allStaticStatementsMatch(workingSet): |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
65 stats['findCandidateBindingEarlyExits'] += 1 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
66 return |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
67 |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
68 for binding in self._possibleBindings(workingSet, stats): |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
69 log.debug('') |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
70 log.debug(f'{INDENT*4}*trying {binding.binding}') |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
71 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
72 if not binding.verify(workingSet): |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
73 log.debug(f'{INDENT*4} this binding did not verify') |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
74 stats['permCountFailingVerify'] += 1 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
75 continue |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
76 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
77 stats['permCountSucceeding'] += 1 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
78 yield binding |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
79 |
1608 | 80 def _allStaticStatementsMatch(self, workingSet: ReadOnlyWorkingSet) -> bool: |
1613
03ed8c9abd5b
forget GRAPH_ID optimization in this case
drewp@bigasterisk.com
parents:
1612
diff
changeset
|
81 # bug: see TestSelfFulfillingRule.test3 for a case where this rule's |
03ed8c9abd5b
forget GRAPH_ID optimization in this case
drewp@bigasterisk.com
parents:
1612
diff
changeset
|
82 # static stmt is matched by a non-static stmt in the rule itself |
1608 | 83 for ruleStmt in self.staticRuleStmts: |
84 if ruleStmt not in workingSet: | |
85 log.debug(f'{INDENT*3} {ruleStmt} not in working set- skip rule') | |
86 return False | |
87 return True | |
88 | |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
89 def _possibleBindings(self, workingSet, stats) -> Iterator['BoundLhs']: |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
90 """this yields at least the working bindings, and possibly others""" |
1600 | 91 candidateTermMatches: Dict[BindableTerm, Set[Node]] = self._allCandidateTermMatches(workingSet) |
1614
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
92 for bindRow in self._product(candidateTermMatches): |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
93 try: |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
94 yield BoundLhs(self, bindRow) |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
95 except EvaluationFailed: |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
96 stats['permCountFailingEval'] += 1 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
97 |
1614
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
98 def _product(self, candidateTermMatches: Dict[BindableTerm, Set[Node]]) -> Iterator[CandidateBinding]: |
1600 | 99 orderedVars, orderedValueSets = _organize(candidateTermMatches) |
100 self._logCandidates(orderedVars, orderedValueSets) | |
1597 | 101 log.debug(f'{INDENT*3} trying all permutations:') |
1614
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
102 if not orderedValueSets: |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
103 yield CandidateBinding({}) |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
104 return |
1620 | 105 |
1614
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
106 if not orderedValueSets or not all(orderedValueSets): |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
107 # some var or bnode has no options at all |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
108 return |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
109 rings: List[Iterator[Node]] = [itertools.cycle(valSet) for valSet in orderedValueSets] |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
110 currentSet: List[Node] = [next(ring) for ring in rings] |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
111 starts = [valSet[-1] for valSet in orderedValueSets] |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
112 while True: |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
113 for col, curr in enumerate(currentSet): |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
114 currentSet[col] = next(rings[col]) |
1620 | 115 log.debug(f'{INDENT*4} currentSet: {repr(currentSet)}') |
1614
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
116 yield CandidateBinding(dict(zip(orderedVars, currentSet))) |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
117 if curr is not starts[col]: |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
118 break |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
119 if col == len(orderedValueSets) - 1: |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
120 return |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
121 |
1600 | 122 def _allCandidateTermMatches(self, workingSet: ReadOnlyWorkingSet) -> Dict[BindableTerm, Set[Node]]: |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
123 """the total set of terms each variable could possibly match""" |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
124 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
125 candidateTermMatches: Dict[BindableTerm, Set[Node]] = defaultdict(set) |
1594 | 126 for lhsStmt in self.graph: |
1597 | 127 log.debug(f'{INDENT*4} possibles for this lhs stmt: {lhsStmt}') |
1608 | 128 for i, trueStmt in enumerate(workingSet): |
1597 | 129 # log.debug(f'{INDENT*5} consider this true stmt ({i}): {trueStmt}') |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
130 |
1594 | 131 for v, vals in self._bindingsFromStatement(lhsStmt, trueStmt): |
132 candidateTermMatches[v].update(vals) | |
133 | |
134 for trueStmt in itertools.chain(workingSet, self.graph): | |
135 for b in self.lhsBnodes: | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
136 for t in [trueStmt[0], trueStmt[2]]: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
137 if isinstance(t, (URIRef, BNode)): |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
138 candidateTermMatches[b].add(t) |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
139 return candidateTermMatches |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
140 |
1594 | 141 def _bindingsFromStatement(self, stmt1: Triple, stmt2: Triple) -> Iterator[Tuple[Variable, Set[Node]]]: |
142 """if these stmts match otherwise, what BNode or Variable mappings do we learn? | |
143 | |
144 e.g. stmt1=(?x B ?y) and stmt2=(A B C), then we yield (?x, {A}) and (?y, {C}) | |
145 or stmt1=(_:x B C) and stmt2=(A B C), then we yield (_:x, {A}) | |
146 or stmt1=(?x B C) and stmt2=(A B D), then we yield nothing | |
147 """ | |
148 bindingsFromStatement = {} | |
149 for term1, term2 in zip(stmt1, stmt2): | |
150 if isinstance(term1, (BNode, Variable)): | |
151 bindingsFromStatement.setdefault(term1, set()).add(term2) | |
152 elif term1 != term2: | |
153 break | |
154 else: | |
155 for v, vals in bindingsFromStatement.items(): | |
1597 | 156 log.debug(f'{INDENT*5} {v=} {vals=}') |
1594 | 157 yield v, vals |
158 | |
1600 | 159 def _logCandidates(self, orderedVars, orderedValueSets): |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
160 if not log.isEnabledFor(logging.DEBUG): |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
161 return |
1597 | 162 log.debug(f'{INDENT*3} resulting candidate terms:') |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
163 for v, vals in zip(orderedVars, orderedValueSets): |
1597 | 164 log.debug(f'{INDENT*4} {v!r} could be:') |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
165 for val in vals: |
1597 | 166 log.debug(f'{INDENT*5}{val!r}') |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
167 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
168 |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
169 @dataclass |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
170 class BoundLhs: |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
171 lhs: Lhs |
1610 | 172 binding: CandidateBinding |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
173 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
174 def __post_init__(self): |
1613
03ed8c9abd5b
forget GRAPH_ID optimization in this case
drewp@bigasterisk.com
parents:
1612
diff
changeset
|
175 self.usedByFuncs = Graph() |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
176 self._applyFunctions() |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
177 |
1616
3a6ed545357f
optimization: stream stmts instead of building a Graph
drewp@bigasterisk.com
parents:
1615
diff
changeset
|
178 def lhsStmtsWithoutEvals(self): |
3a6ed545357f
optimization: stream stmts instead of building a Graph
drewp@bigasterisk.com
parents:
1615
diff
changeset
|
179 for stmt in self.lhs.graph: |
3a6ed545357f
optimization: stream stmts instead of building a Graph
drewp@bigasterisk.com
parents:
1615
diff
changeset
|
180 if stmt in self.usedByFuncs: |
3a6ed545357f
optimization: stream stmts instead of building a Graph
drewp@bigasterisk.com
parents:
1615
diff
changeset
|
181 continue |
3a6ed545357f
optimization: stream stmts instead of building a Graph
drewp@bigasterisk.com
parents:
1615
diff
changeset
|
182 yield stmt |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
183 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
184 def _applyFunctions(self): |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
185 """may grow the binding with some results""" |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
186 while True: |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
187 delta = self._applyFunctionsIteration() |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
188 if delta == 0: |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
189 break |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
190 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
191 def _applyFunctionsIteration(self): |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
192 before = len(self.binding.binding) |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
193 delta = 0 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
194 for ev in self.lhs.evaluations: |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
195 newBindings, usedGraph = ev.resultBindings(self.binding) |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
196 self.usedByFuncs += usedGraph |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
197 self.binding.addNewBindings(newBindings) |
1608 | 198 delta = len(self.binding.binding) - before |
199 log.debug(f'{INDENT*4} eval rules made {delta} new bindings') | |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
200 return delta |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
201 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
202 def verify(self, workingSet: ReadOnlyWorkingSet) -> bool: |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
203 """Can this bound lhs be true all at once in workingSet?""" |
1615
bcfa368e5498
change a Graph.__sub__ to Set.difference in verify() for a big speedup
drewp@bigasterisk.com
parents:
1614
diff
changeset
|
204 rem = cast(Set[Triple], self.lhs.nonStaticRuleStmtsSet.difference(self.usedByFuncs)) |
bcfa368e5498
change a Graph.__sub__ to Set.difference in verify() for a big speedup
drewp@bigasterisk.com
parents:
1614
diff
changeset
|
205 boundLhs = self.binding.apply(rem) |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
206 |
1611
a794a150a89b
a little inner-loop performace. same number of iterations, but less time on logging and some stmt filtering
drewp@bigasterisk.com
parents:
1610
diff
changeset
|
207 if log.isEnabledFor(logging.DEBUG): |
a794a150a89b
a little inner-loop performace. same number of iterations, but less time on logging and some stmt filtering
drewp@bigasterisk.com
parents:
1610
diff
changeset
|
208 boundLhs = list(boundLhs) |
a794a150a89b
a little inner-loop performace. same number of iterations, but less time on logging and some stmt filtering
drewp@bigasterisk.com
parents:
1610
diff
changeset
|
209 self._logVerifyBanner(boundLhs, workingSet) |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
210 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
211 for stmt in boundLhs: |
1611
a794a150a89b
a little inner-loop performace. same number of iterations, but less time on logging and some stmt filtering
drewp@bigasterisk.com
parents:
1610
diff
changeset
|
212 log.debug(f'{INDENT*4} check for %s', stmt) |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
213 |
1611
a794a150a89b
a little inner-loop performace. same number of iterations, but less time on logging and some stmt filtering
drewp@bigasterisk.com
parents:
1610
diff
changeset
|
214 if stmt not in workingSet: |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
215 log.debug(f'{INDENT*5} stmt not known to be true') |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
216 return False |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
217 return True |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
218 |
1611
a794a150a89b
a little inner-loop performace. same number of iterations, but less time on logging and some stmt filtering
drewp@bigasterisk.com
parents:
1610
diff
changeset
|
219 def _logVerifyBanner(self, boundLhs, workingSet: ReadOnlyWorkingSet): |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
220 log.debug(f'{INDENT*4}/ verify all bindings against this boundLhs:') |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
221 for stmt in sorted(boundLhs): |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
222 log.debug(f'{INDENT*4}|{INDENT} {stmt}') |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
223 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
224 # log.debug(f'{INDENT*4}| and against this workingSet:') |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
225 # for stmt in sorted(workingSet): |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
226 # log.debug(f'{INDENT*4}|{INDENT} {stmt}') |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
227 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
228 log.debug(f'{INDENT*4}\\') |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
229 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
230 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
231 @dataclass |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
232 class Rule: |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
233 lhsGraph: Graph |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
234 rhsGraph: Graph |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
235 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
236 def __post_init__(self): |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
237 self.lhs = Lhs(self.lhsGraph) |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
238 |
1612
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1611
diff
changeset
|
239 def applyRule(self, workingSet: Graph, implied: Graph, stats: Dict): |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1611
diff
changeset
|
240 for bound in self.lhs.findCandidateBindings(ReadOnlyGraphAggregate([workingSet]), stats): |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1611
diff
changeset
|
241 log.debug(f'{INDENT*3} rule has a working binding:') |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1611
diff
changeset
|
242 |
1616
3a6ed545357f
optimization: stream stmts instead of building a Graph
drewp@bigasterisk.com
parents:
1615
diff
changeset
|
243 for lhsBoundStmt in bound.binding.apply(bound.lhsStmtsWithoutEvals()): |
1620 | 244 log.debug(f'{INDENT*4} adding {lhsBoundStmt=}') |
1612
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1611
diff
changeset
|
245 workingSet.add(lhsBoundStmt) |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1611
diff
changeset
|
246 for newStmt in bound.binding.apply(self.rhsGraph): |
1620 | 247 log.debug(f'{INDENT*4} adding {newStmt=}') |
1612
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1611
diff
changeset
|
248 workingSet.add(newStmt) |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1611
diff
changeset
|
249 implied.add(newStmt) |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1611
diff
changeset
|
250 |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
251 |
1587 | 252 class Inference: |
253 | |
254 def __init__(self) -> None: | |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
255 self.rules = [] |
1587 | 256 |
257 def setRules(self, g: ConjunctiveGraph): | |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
258 self.rules: List[Rule] = [] |
1599
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
259 for stmt in g: |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
260 if stmt[1] == LOG['implies']: |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
261 self.rules.append(Rule(stmt[0], stmt[2])) |
1612
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1611
diff
changeset
|
262 # other stmts should go to a default working set? |
1587 | 263 |
1601 | 264 @INFER_CALLS.time() |
1587 | 265 def infer(self, graph: Graph): |
266 """ | |
267 returns new graph of inferred statements. | |
268 """ | |
1597 | 269 log.info(f'{INDENT*0} Begin inference of graph len={graph.__len__()} with rules len={len(self.rules)}:') |
1601 | 270 startTime = time.time() |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
271 stats: Dict[str, Union[int, float]] = defaultdict(lambda: 0) |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
272 # everything that is true: the input graph, plus every rule conclusion we can make |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
273 workingSet = Graph() |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
274 workingSet += graph |
1587 | 275 |
1594 | 276 # just the statements that came from RHS's of rules that fired. |
1587 | 277 implied = ConjunctiveGraph() |
278 | |
279 bailout_iterations = 100 | |
280 delta = 1 | |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
281 stats['initWorkingSet'] = cast(int, workingSet.__len__()) |
1587 | 282 while delta > 0 and bailout_iterations > 0: |
1620 | 283 log.debug('') |
1597 | 284 log.info(f'{INDENT*1}*iteration ({bailout_iterations} left)') |
1587 | 285 bailout_iterations -= 1 |
286 delta = -len(implied) | |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
287 self._iterateAllRules(workingSet, implied, stats) |
1587 | 288 delta += len(implied) |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
289 stats['iterations'] += 1 |
1597 | 290 log.info(f'{INDENT*2} this inference iteration added {delta} more implied stmts') |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
291 stats['timeSpent'] = round(time.time() - startTime, 3) |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
292 stats['impliedStmts'] = len(implied) |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
293 log.info(f'{INDENT*0} Inference done {dict(stats)}. Implied:') |
1587 | 294 for st in implied: |
1597 | 295 log.info(f'{INDENT*1} {st}') |
1587 | 296 return implied |
297 | |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
298 def _iterateAllRules(self, workingSet: Graph, implied: Graph, stats): |
1612
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1611
diff
changeset
|
299 for i, rule in enumerate(self.rules): |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1611
diff
changeset
|
300 self._logRuleApplicationHeader(workingSet, i, rule) |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1611
diff
changeset
|
301 rule.applyRule(workingSet, implied, stats) |
1587 | 302 |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
303 def _logRuleApplicationHeader(self, workingSet, i, r: Rule): |
1594 | 304 if not log.isEnabledFor(logging.DEBUG): |
305 return | |
306 | |
307 log.debug('') | |
308 log.debug(f'{INDENT*2} workingSet:') | |
1597 | 309 for j, stmt in enumerate(sorted(workingSet)): |
310 log.debug(f'{INDENT*3} ({j}) {stmt}') | |
1594 | 311 |
312 log.debug('') | |
313 log.debug(f'{INDENT*2}-applying rule {i}') | |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
314 log.debug(f'{INDENT*3} rule def lhs: {graphDump(r.lhsGraph)}') |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
315 log.debug(f'{INDENT*3} rule def rhs: {graphDump(r.rhsGraph)}') |
1594 | 316 |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
317 |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
318 def graphDump(g: Union[Graph, List[Triple]]): |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
319 if not isinstance(g, Graph): |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
320 log.warning(f"it's a {type(g)}") |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
321 g2 = Graph() |
1594 | 322 g2 += g |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
323 g = g2 |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
324 g.bind('', ROOM) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
325 g.bind('ex', Namespace('http://example.com/')) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
326 lines = cast(bytes, g.serialize(format='n3')).decode('utf8').splitlines() |
1612
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1611
diff
changeset
|
327 lines = [line.strip() for line in lines if not line.startswith('@prefix')] |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
328 return ' '.join(lines) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
329 |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
330 |
1600 | 331 def _organize(candidateTermMatches: Dict[BindableTerm, Set[Node]]) -> Tuple[List[BindableTerm], List[List[Node]]]: |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
332 items = list(candidateTermMatches.items()) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
333 items.sort() |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
334 orderedVars: List[BindableTerm] = [] |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
335 orderedValueSets: List[List[Node]] = [] |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
336 for v, vals in items: |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
337 orderedVars.append(v) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
338 orderedValues: List[Node] = list(vals) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
339 orderedValues.sort(key=str) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
340 orderedValueSets.append(orderedValues) |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
341 |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
342 return orderedVars, orderedValueSets |