Mercurial > code > home > repos > homeauto
annotate service/mqtt_to_rdf/inference.py @ 1626:7b3656867185
metrics on input graph sizes
author | drewp@bigasterisk.com |
---|---|
date | Sat, 11 Sep 2021 23:21:11 -0700 |
parents | cf901d219007 |
children | ea559a846714 |
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 |
1626 | 9 from dataclasses import dataclass |
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 |
1626 | 12 from prometheus_client import Summary, Histogram |
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 |
1626 | 24 INFER_CALLS = Summary('inference_infer_calls', 'calls') |
25 INFER_GRAPH_SIZE = Histogram('inference_graph_size', 'statements', buckets=[2**x for x in range(2, 20, 2)]) | |
1587 | 26 |
27 ROOM = Namespace("http://projects.bigasterisk.com/room/") | |
28 LOG = Namespace('http://www.w3.org/2000/10/swap/log#') | |
29 MATH = Namespace('http://www.w3.org/2000/10/swap/math#') | |
30 | |
31 | |
1594 | 32 @dataclass |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
33 class Lhs: |
1594 | 34 graph: Graph |
35 | |
36 def __post_init__(self): | |
1608 | 37 # do precomputation in here that's not specific to the workingSet |
38 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
|
39 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
|
40 |
1608 | 41 self.lhsBindables: Set[BindableTerm] = set() |
42 self.lhsBnodes: Set[BNode] = set() | |
1594 | 43 for ruleStmt in self.graph: |
44 varsAndBnodesInStmt = [term for term in ruleStmt if isinstance(term, (Variable, BNode))] | |
45 self.lhsBindables.update(varsAndBnodesInStmt) | |
46 self.lhsBnodes.update(x for x in varsAndBnodesInStmt if isinstance(x, BNode)) | |
47 if not varsAndBnodesInStmt: | |
48 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
|
49 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
|
50 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
|
51 |
1615
bcfa368e5498
change a Graph.__sub__ to Set.difference in verify() for a big speedup
drewp@bigasterisk.com
parents:
1614
diff
changeset
|
52 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
|
53 |
1602 | 54 self.evaluations = list(Evaluation.findEvals(self.graph)) |
55 | |
1609
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1608
diff
changeset
|
56 def __repr__(self): |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1608
diff
changeset
|
57 return f"Lhs({graphDump(self.graph)})" |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1608
diff
changeset
|
58 |
1621 | 59 def findCandidateBindings(self, knownTrue: 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
|
60 """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
|
61 from LHS""" |
1597 | 62 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
|
63 stats['findCandidateBindingsCalls'] += 1 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
64 |
1621 | 65 if not self._allStaticStatementsMatch(knownTrue): |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
66 stats['findCandidateBindingEarlyExits'] += 1 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
67 return |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
68 |
1621 | 69 for binding in self._possibleBindings(knownTrue, stats): |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
70 log.debug('') |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
71 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
|
72 |
1621 | 73 if not binding.verify(knownTrue): |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
74 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
|
75 stats['permCountFailingVerify'] += 1 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
76 continue |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
77 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
78 stats['permCountSucceeding'] += 1 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
79 yield binding |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
80 |
1621 | 81 def _allStaticStatementsMatch(self, knownTrue: ReadOnlyWorkingSet) -> bool: |
1613
03ed8c9abd5b
forget GRAPH_ID optimization in this case
drewp@bigasterisk.com
parents:
1612
diff
changeset
|
82 # 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
|
83 # static stmt is matched by a non-static stmt in the rule itself |
1608 | 84 for ruleStmt in self.staticRuleStmts: |
1621 | 85 if ruleStmt not in knownTrue: |
1608 | 86 log.debug(f'{INDENT*3} {ruleStmt} not in working set- skip rule') |
87 return False | |
88 return True | |
89 | |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
90 def _possibleBindings(self, workingSet, stats) -> Iterator['BoundLhs']: |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
91 """this yields at least the working bindings, and possibly others""" |
1600 | 92 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
|
93 for bindRow in self._product(candidateTermMatches): |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
94 try: |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
95 yield BoundLhs(self, bindRow) |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
96 except EvaluationFailed: |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
97 stats['permCountFailingEval'] += 1 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
98 |
1614
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
99 def _product(self, candidateTermMatches: Dict[BindableTerm, Set[Node]]) -> Iterator[CandidateBinding]: |
1600 | 100 orderedVars, orderedValueSets = _organize(candidateTermMatches) |
101 self._logCandidates(orderedVars, orderedValueSets) | |
1597 | 102 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
|
103 if not orderedValueSets: |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
104 yield CandidateBinding({}) |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
105 return |
1620 | 106 |
1614
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
107 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
|
108 # 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
|
109 return |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
110 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
|
111 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
|
112 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
|
113 while True: |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
114 for col, curr in enumerate(currentSet): |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
115 currentSet[col] = next(rings[col]) |
1620 | 116 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
|
117 yield CandidateBinding(dict(zip(orderedVars, currentSet))) |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
118 if curr is not starts[col]: |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
119 break |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
120 if col == len(orderedValueSets) - 1: |
97b2c3cfdb83
refactor: inline an odometer algorithm in place of itertools.product
drewp@bigasterisk.com
parents:
1613
diff
changeset
|
121 return |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
122 |
1600 | 123 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
|
124 """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
|
125 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
126 candidateTermMatches: Dict[BindableTerm, Set[Node]] = defaultdict(set) |
1594 | 127 for lhsStmt in self.graph: |
1597 | 128 log.debug(f'{INDENT*4} possibles for this lhs stmt: {lhsStmt}') |
1608 | 129 for i, trueStmt in enumerate(workingSet): |
1597 | 130 # 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
|
131 |
1594 | 132 for v, vals in self._bindingsFromStatement(lhsStmt, trueStmt): |
133 candidateTermMatches[v].update(vals) | |
134 | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
135 return candidateTermMatches |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
136 |
1594 | 137 def _bindingsFromStatement(self, stmt1: Triple, stmt2: Triple) -> Iterator[Tuple[Variable, Set[Node]]]: |
138 """if these stmts match otherwise, what BNode or Variable mappings do we learn? | |
139 | |
140 e.g. stmt1=(?x B ?y) and stmt2=(A B C), then we yield (?x, {A}) and (?y, {C}) | |
141 or stmt1=(_:x B C) and stmt2=(A B C), then we yield (_:x, {A}) | |
142 or stmt1=(?x B C) and stmt2=(A B D), then we yield nothing | |
143 """ | |
144 bindingsFromStatement = {} | |
145 for term1, term2 in zip(stmt1, stmt2): | |
146 if isinstance(term1, (BNode, Variable)): | |
147 bindingsFromStatement.setdefault(term1, set()).add(term2) | |
148 elif term1 != term2: | |
149 break | |
150 else: | |
151 for v, vals in bindingsFromStatement.items(): | |
1597 | 152 log.debug(f'{INDENT*5} {v=} {vals=}') |
1594 | 153 yield v, vals |
154 | |
1600 | 155 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
|
156 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
|
157 return |
1597 | 158 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
|
159 for v, vals in zip(orderedVars, orderedValueSets): |
1597 | 160 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
|
161 for val in vals: |
1597 | 162 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
|
163 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
164 |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
165 @dataclass |
1622
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
166 class CandidateTermMatches: |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
167 """lazily find the possible matches for this term""" |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
168 term: BindableTerm |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
169 lhs: Lhs |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
170 workingSet: Graph |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
171 boundSoFar: CandidateBinding |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
172 |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
173 def __post_init__(self): |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
174 self.results: List[Node] = [] # we have to be able to repeat the results |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
175 |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
176 res: Set[Node] = set() |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
177 for trueStmt in self.workingSet: # all bound |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
178 lStmts = list(self.lhsStmtsContainingTerm()) |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
179 log.debug(f'{INDENT*4} {trueStmt=} {len(lStmts)}') |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
180 for pat in self.boundSoFar.apply(lStmts, returnBoundStatementsOnly=False): |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
181 log.debug(f'{INDENT*4} {pat=}') |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
182 implied = self._stmtImplies(pat, trueStmt) |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
183 if implied is not None: |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
184 res.add(implied) |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
185 self.results = list(res) |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
186 # self.results.sort() |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
187 |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
188 log.debug(f'{INDENT*3} CandTermMatches: {self.term} {graphDump(self.lhs.graph)} {self.boundSoFar=} ===> {self.results=}') |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
189 |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
190 def _stmtImplies(self, pat: Triple, trueStmt: Triple) -> Optional[Node]: |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
191 """what value, if any, do we learn for our term from this LHS pattern statement and this known-true stmt""" |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
192 r = None |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
193 for p, t in zip(pat, trueStmt): |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
194 if isinstance(p, (Variable, BNode)): |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
195 if p != self.term: |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
196 # stmt is unbound in more than just our term |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
197 continue # unsure what to do - err on the side of too many bindings, since they get rechecked later |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
198 if r is None: |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
199 r = t |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
200 log.debug(f'{INDENT*4} implied term value {p=} {t=}') |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
201 elif r != t: |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
202 # (?x c ?x) matched with (a b c) doesn't work |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
203 return None |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
204 return r |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
205 |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
206 def lhsStmtsContainingTerm(self): |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
207 # lhs could precompute this |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
208 for lhsStmt in self.lhs.graph: |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
209 if self.term in lhsStmt: |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
210 yield lhsStmt |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
211 |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
212 def __iter__(self): |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
213 return iter(self.results) |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
214 |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
215 |
38bd8ef9ef67
add CandidateTermMatches, unused so far
drewp@bigasterisk.com
parents:
1621
diff
changeset
|
216 @dataclass |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
217 class BoundLhs: |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
218 lhs: Lhs |
1610 | 219 binding: CandidateBinding |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
220 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
221 def __post_init__(self): |
1613
03ed8c9abd5b
forget GRAPH_ID optimization in this case
drewp@bigasterisk.com
parents:
1612
diff
changeset
|
222 self.usedByFuncs = Graph() |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
223 self._applyFunctions() |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
224 |
1616
3a6ed545357f
optimization: stream stmts instead of building a Graph
drewp@bigasterisk.com
parents:
1615
diff
changeset
|
225 def lhsStmtsWithoutEvals(self): |
3a6ed545357f
optimization: stream stmts instead of building a Graph
drewp@bigasterisk.com
parents:
1615
diff
changeset
|
226 for stmt in self.lhs.graph: |
3a6ed545357f
optimization: stream stmts instead of building a Graph
drewp@bigasterisk.com
parents:
1615
diff
changeset
|
227 if stmt in self.usedByFuncs: |
3a6ed545357f
optimization: stream stmts instead of building a Graph
drewp@bigasterisk.com
parents:
1615
diff
changeset
|
228 continue |
3a6ed545357f
optimization: stream stmts instead of building a Graph
drewp@bigasterisk.com
parents:
1615
diff
changeset
|
229 yield stmt |
1607
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 def _applyFunctions(self): |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
232 """may grow the binding with some results""" |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
233 while True: |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
234 delta = self._applyFunctionsIteration() |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
235 if delta == 0: |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
236 break |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
237 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
238 def _applyFunctionsIteration(self): |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
239 before = len(self.binding.binding) |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
240 delta = 0 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
241 for ev in self.lhs.evaluations: |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
242 newBindings, usedGraph = ev.resultBindings(self.binding) |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
243 self.usedByFuncs += usedGraph |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
244 self.binding.addNewBindings(newBindings) |
1608 | 245 delta = len(self.binding.binding) - before |
246 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
|
247 return delta |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
248 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
249 def verify(self, workingSet: ReadOnlyWorkingSet) -> bool: |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
250 """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
|
251 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
|
252 boundLhs = self.binding.apply(rem) |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
253 |
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
|
254 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
|
255 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
|
256 self._logVerifyBanner(boundLhs, workingSet) |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
257 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
258 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
|
259 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
|
260 |
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
|
261 if stmt not in workingSet: |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
262 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
|
263 return False |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
264 return True |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
265 |
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
|
266 def _logVerifyBanner(self, boundLhs, workingSet: ReadOnlyWorkingSet): |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
267 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
|
268 for stmt in sorted(boundLhs): |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
269 log.debug(f'{INDENT*4}|{INDENT} {stmt}') |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
270 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
271 # 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
|
272 # for stmt in sorted(workingSet): |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
273 # log.debug(f'{INDENT*4}|{INDENT} {stmt}') |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
274 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
275 log.debug(f'{INDENT*4}\\') |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
276 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
277 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
278 @dataclass |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
279 class Rule: |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
280 lhsGraph: Graph |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
281 rhsGraph: Graph |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
282 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
283 def __post_init__(self): |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
284 self.lhs = Lhs(self.lhsGraph) |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
285 |
1612
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1611
diff
changeset
|
286 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
|
287 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
|
288 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
|
289 |
1616
3a6ed545357f
optimization: stream stmts instead of building a Graph
drewp@bigasterisk.com
parents:
1615
diff
changeset
|
290 for lhsBoundStmt in bound.binding.apply(bound.lhsStmtsWithoutEvals()): |
1620 | 291 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
|
292 workingSet.add(lhsBoundStmt) |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1611
diff
changeset
|
293 for newStmt in bound.binding.apply(self.rhsGraph): |
1620 | 294 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
|
295 workingSet.add(newStmt) |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1611
diff
changeset
|
296 implied.add(newStmt) |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1611
diff
changeset
|
297 |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
298 |
1587 | 299 class Inference: |
300 | |
301 def __init__(self) -> None: | |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
302 self.rules = [] |
1587 | 303 |
304 def setRules(self, g: ConjunctiveGraph): | |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
305 self.rules: List[Rule] = [] |
1599
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
306 for stmt in g: |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
307 if stmt[1] == LOG['implies']: |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
308 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
|
309 # other stmts should go to a default working set? |
1587 | 310 |
1601 | 311 @INFER_CALLS.time() |
1587 | 312 def infer(self, graph: Graph): |
313 """ | |
314 returns new graph of inferred statements. | |
315 """ | |
1626 | 316 n = graph.__len__() |
317 INFER_GRAPH_SIZE.observe(n) | |
318 log.info(f'{INDENT*0} Begin inference of graph len={n} with rules len={len(self.rules)}:') | |
1601 | 319 startTime = time.time() |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
320 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
|
321 # 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
|
322 workingSet = Graph() |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
323 workingSet += graph |
1587 | 324 |
1594 | 325 # just the statements that came from RHS's of rules that fired. |
1587 | 326 implied = ConjunctiveGraph() |
327 | |
328 bailout_iterations = 100 | |
329 delta = 1 | |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
330 stats['initWorkingSet'] = cast(int, workingSet.__len__()) |
1587 | 331 while delta > 0 and bailout_iterations > 0: |
1620 | 332 log.debug('') |
1597 | 333 log.info(f'{INDENT*1}*iteration ({bailout_iterations} left)') |
1587 | 334 bailout_iterations -= 1 |
335 delta = -len(implied) | |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
336 self._iterateAllRules(workingSet, implied, stats) |
1587 | 337 delta += len(implied) |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
338 stats['iterations'] += 1 |
1597 | 339 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
|
340 stats['timeSpent'] = round(time.time() - startTime, 3) |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
341 stats['impliedStmts'] = len(implied) |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
342 log.info(f'{INDENT*0} Inference done {dict(stats)}. Implied:') |
1587 | 343 for st in implied: |
1597 | 344 log.info(f'{INDENT*1} {st}') |
1587 | 345 return implied |
346 | |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
347 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
|
348 for i, rule in enumerate(self.rules): |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1611
diff
changeset
|
349 self._logRuleApplicationHeader(workingSet, i, rule) |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1611
diff
changeset
|
350 rule.applyRule(workingSet, implied, stats) |
1587 | 351 |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
352 def _logRuleApplicationHeader(self, workingSet, i, r: Rule): |
1594 | 353 if not log.isEnabledFor(logging.DEBUG): |
354 return | |
355 | |
356 log.debug('') | |
357 log.debug(f'{INDENT*2} workingSet:') | |
1597 | 358 for j, stmt in enumerate(sorted(workingSet)): |
359 log.debug(f'{INDENT*3} ({j}) {stmt}') | |
1594 | 360 |
361 log.debug('') | |
362 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
|
363 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
|
364 log.debug(f'{INDENT*3} rule def rhs: {graphDump(r.rhsGraph)}') |
1594 | 365 |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
366 |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
367 def graphDump(g: Union[Graph, List[Triple]]): |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
368 if not isinstance(g, Graph): |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
369 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
|
370 g2 = Graph() |
1594 | 371 g2 += g |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
372 g = g2 |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
373 g.bind('', ROOM) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
374 g.bind('ex', Namespace('http://example.com/')) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
375 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
|
376 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
|
377 return ' '.join(lines) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
378 |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
379 |
1600 | 380 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
|
381 items = list(candidateTermMatches.items()) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
382 items.sort() |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
383 orderedVars: List[BindableTerm] = [] |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
384 orderedValueSets: List[List[Node]] = [] |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
385 for v, vals in items: |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
386 orderedVars.append(v) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
387 orderedValues: List[Node] = list(vals) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
388 orderedValues.sort(key=str) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
389 orderedValueSets.append(orderedValues) |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
390 |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
391 return orderedVars, orderedValueSets |