Mercurial > code > home > repos > homeauto
annotate service/mqtt_to_rdf/inference.py @ 1606:6cf39d43fd40
realign tests, turn off slow ones for now
author | drewp@bigasterisk.com |
---|---|
date | Mon, 06 Sep 2021 01:15:14 -0700 |
parents | 449746d1598f |
children | b21885181e35 |
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 | |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
10 from decimal import Decimal |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
11 from typing import Dict, Iterable, Iterator, List, Set, Tuple, Union, cast |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
12 |
1587 | 13 from prometheus_client import Summary |
1594 | 14 from rdflib import RDF, BNode, Graph, Literal, Namespace, URIRef |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
15 from rdflib.graph import ConjunctiveGraph, ReadOnlyGraphAggregate |
1587 | 16 from rdflib.term import Node, Variable |
17 | |
1605 | 18 from lhs_evaluation import EvaluationFailed, Evaluation |
19 | |
1587 | 20 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
|
21 INDENT = ' ' |
1587 | 22 |
23 Triple = Tuple[Node, Node, Node] | |
24 Rule = Tuple[Graph, Node, Graph] | |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
25 BindableTerm = Union[Variable, BNode] |
1594 | 26 ReadOnlyWorkingSet = ReadOnlyGraphAggregate |
1587 | 27 |
1601 | 28 INFER_CALLS = Summary('read_rules_calls', 'calls') |
1587 | 29 |
30 ROOM = Namespace("http://projects.bigasterisk.com/room/") | |
31 LOG = Namespace('http://www.w3.org/2000/10/swap/log#') | |
32 MATH = Namespace('http://www.w3.org/2000/10/swap/math#') | |
33 | |
1601 | 34 # Graph() makes a BNode if you don't pass |
35 # identifier, which can be a bottleneck. | |
36 GRAPH_ID = URIRef('dont/care') | |
1587 | 37 |
38 | |
1599
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
39 class BindingUnknown(ValueError): |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
40 """e.g. we were asked to make the bound version |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
41 of (A B ?c) and we don't have a binding for ?c |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
42 """ |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
43 |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
44 |
1594 | 45 @dataclass |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
46 class CandidateBinding: |
1594 | 47 binding: Dict[BindableTerm, Node] |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
48 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
49 def __repr__(self): |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
50 b = " ".join("%s=%s" % (k, v) for k, v in sorted(self.binding.items())) |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
51 return f'CandidateBinding({b})' |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
52 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
53 def apply(self, g: Graph) -> Iterator[Triple]: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
54 for stmt in g: |
1599
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
55 try: |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
56 bound = (self._applyTerm(stmt[0]), self._applyTerm(stmt[1]), self._applyTerm(stmt[2])) |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
57 except BindingUnknown: |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
58 continue |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
59 yield bound |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
60 |
1594 | 61 def _applyTerm(self, term: Node): |
62 if isinstance(term, (Variable, BNode)): | |
63 if term in self.binding: | |
64 return self.binding[term] | |
1599
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
65 else: |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
66 raise BindingUnknown() |
1594 | 67 return term |
68 | |
69 def applyFunctions(self, lhs) -> Graph: | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
70 """may grow the binding with some results""" |
1601 | 71 usedByFuncs = Graph(identifier=GRAPH_ID) |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
72 while True: |
1594 | 73 delta = self._applyFunctionsIteration(lhs, usedByFuncs) |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
74 if delta == 0: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
75 break |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
76 return usedByFuncs |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
77 |
1594 | 78 def _applyFunctionsIteration(self, lhs, usedByFuncs: Graph): |
79 before = len(self.binding) | |
80 delta = 0 | |
1602 | 81 for ev in lhs.evaluations: |
1594 | 82 log.debug(f'{INDENT*3} found Evaluation') |
83 | |
84 newBindings, usedGraph = ev.resultBindings(self.binding) | |
85 usedByFuncs += usedGraph | |
86 self._addNewBindings(newBindings) | |
87 delta = len(self.binding) - before | |
1603 | 88 if log.isEnabledFor(logging.DEBUG): |
89 dump = "(...)" | |
90 if cast(int, usedGraph.__len__()) < 20: | |
91 dump = graphDump(usedGraph) | |
92 log.debug(f'{INDENT*4} rule {dump} made {delta} new bindings') | |
1594 | 93 return delta |
94 | |
95 def _addNewBindings(self, newBindings): | |
96 for k, v in newBindings.items(): | |
97 if k in self.binding and self.binding[k] != v: | |
98 raise ValueError(f'conflict- thought {k} would be {self.binding[k]} but another Evaluation said it should be {v}') | |
99 self.binding[k] = v | |
100 | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
101 def verify(self, lhs: 'Lhs', workingSet: ReadOnlyWorkingSet, usedByFuncs: Graph) -> bool: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
102 """Can this lhs be true all at once in workingSet? Does it match with these bindings?""" |
1594 | 103 boundLhs = list(self.apply(lhs.graph)) |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
104 boundUsedByFuncs = list(self.apply(usedByFuncs)) |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
105 |
1600 | 106 self._logVerifyBanner(boundLhs, workingSet, boundUsedByFuncs) |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
107 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
108 for stmt in boundLhs: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
109 log.debug(f'{INDENT*4} check for {stmt}') |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
110 |
1594 | 111 if stmt in boundUsedByFuncs: |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
112 pass |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
113 elif stmt in workingSet: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
114 pass |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
115 else: |
1597 | 116 log.debug(f'{INDENT*5} stmt not known to be true') |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
117 return False |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
118 return True |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
119 |
1600 | 120 def _logVerifyBanner(self, boundLhs, workingSet: ReadOnlyWorkingSet, boundUsedByFuncs): |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
121 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
|
122 return |
1597 | 123 log.debug(f'{INDENT*4}/ verify all bindings against this boundLhs:') |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
124 for stmt in sorted(boundLhs): |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
125 log.debug(f'{INDENT*4}|{INDENT} {stmt}') |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
126 |
1597 | 127 # log.debug(f'{INDENT*4}| and against this workingSet:') |
128 # for stmt in sorted(workingSet): | |
129 # log.debug(f'{INDENT*4}|{INDENT} {stmt}') | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
130 |
1597 | 131 stmts = sorted(boundUsedByFuncs) |
132 if stmts: | |
133 log.debug(f'{INDENT*4}| while ignoring these usedByFuncs:') | |
134 for stmt in stmts: | |
135 log.debug(f'{INDENT*4}|{INDENT} {stmt}') | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
136 log.debug(f'{INDENT*4}\\') |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
137 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
138 |
1594 | 139 @dataclass |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
140 class Lhs: |
1594 | 141 graph: Graph |
1601 | 142 stats: Dict |
1594 | 143 |
144 staticRuleStmts: Graph = field(default_factory=Graph) | |
145 lhsBindables: Set[BindableTerm] = field(default_factory=set) | |
146 lhsBnodes: Set[BNode] = field(default_factory=set) | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
147 |
1594 | 148 def __post_init__(self): |
149 for ruleStmt in self.graph: | |
150 varsAndBnodesInStmt = [term for term in ruleStmt if isinstance(term, (Variable, BNode))] | |
151 self.lhsBindables.update(varsAndBnodesInStmt) | |
152 self.lhsBnodes.update(x for x in varsAndBnodesInStmt if isinstance(x, BNode)) | |
153 if not varsAndBnodesInStmt: | |
154 self.staticRuleStmts.add(ruleStmt) | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
155 |
1602 | 156 self.evaluations = list(Evaluation.findEvals(self.graph)) |
157 | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
158 def findCandidateBindings(self, workingSet: ReadOnlyWorkingSet) -> Iterator[CandidateBinding]: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
159 """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
|
160 from LHS""" |
1597 | 161 log.debug(f'{INDENT*3} nodesToBind: {self.lhsBindables}') |
1603 | 162 self.stats['findCandidateBindingsCalls'] += 1 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
163 |
1600 | 164 if not self._allStaticStatementsMatch(workingSet): |
1603 | 165 self.stats['findCandidateBindingEarlyExits'] += 1 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
166 return |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
167 |
1600 | 168 candidateTermMatches: Dict[BindableTerm, Set[Node]] = self._allCandidateTermMatches(workingSet) |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
169 |
1600 | 170 orderedVars, orderedValueSets = _organize(candidateTermMatches) |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
171 |
1600 | 172 self._logCandidates(orderedVars, orderedValueSets) |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
173 |
1597 | 174 log.debug(f'{INDENT*3} trying all permutations:') |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
175 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
176 for perm in itertools.product(*orderedValueSets): |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
177 binding = CandidateBinding(dict(zip(orderedVars, perm))) |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
178 log.debug('') |
1597 | 179 log.debug(f'{INDENT*4}*trying {binding}') |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
180 |
1594 | 181 try: |
182 usedByFuncs = binding.applyFunctions(self) | |
183 except EvaluationFailed: | |
1601 | 184 self.stats['permCountFailingEval'] += 1 |
1594 | 185 continue |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
186 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
187 if not binding.verify(self, workingSet, usedByFuncs): |
1597 | 188 log.debug(f'{INDENT*4} this binding did not verify') |
1601 | 189 self.stats['permCountFailingVerify'] += 1 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
190 continue |
1601 | 191 |
192 self.stats['permCountSucceeding'] += 1 | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
193 yield binding |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
194 |
1600 | 195 def _allStaticStatementsMatch(self, workingSet: ReadOnlyWorkingSet) -> bool: |
1594 | 196 for ruleStmt in self.staticRuleStmts: |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
197 if ruleStmt not in workingSet: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
198 log.debug(f'{INDENT*3} {ruleStmt} not in working set- skip rule') |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
199 return False |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
200 return True |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
201 |
1600 | 202 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
|
203 """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
|
204 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
205 candidateTermMatches: Dict[BindableTerm, Set[Node]] = defaultdict(set) |
1594 | 206 for lhsStmt in self.graph: |
1597 | 207 log.debug(f'{INDENT*4} possibles for this lhs stmt: {lhsStmt}') |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
208 for i, trueStmt in enumerate(sorted(workingSet)): |
1597 | 209 # 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
|
210 |
1594 | 211 for v, vals in self._bindingsFromStatement(lhsStmt, trueStmt): |
212 candidateTermMatches[v].update(vals) | |
213 | |
214 for trueStmt in itertools.chain(workingSet, self.graph): | |
215 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
|
216 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
|
217 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
|
218 candidateTermMatches[b].add(t) |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
219 return candidateTermMatches |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
220 |
1594 | 221 def _bindingsFromStatement(self, stmt1: Triple, stmt2: Triple) -> Iterator[Tuple[Variable, Set[Node]]]: |
222 """if these stmts match otherwise, what BNode or Variable mappings do we learn? | |
223 | |
224 e.g. stmt1=(?x B ?y) and stmt2=(A B C), then we yield (?x, {A}) and (?y, {C}) | |
225 or stmt1=(_:x B C) and stmt2=(A B C), then we yield (_:x, {A}) | |
226 or stmt1=(?x B C) and stmt2=(A B D), then we yield nothing | |
227 """ | |
228 bindingsFromStatement = {} | |
229 for term1, term2 in zip(stmt1, stmt2): | |
230 if isinstance(term1, (BNode, Variable)): | |
231 bindingsFromStatement.setdefault(term1, set()).add(term2) | |
232 elif term1 != term2: | |
233 break | |
234 else: | |
235 for v, vals in bindingsFromStatement.items(): | |
1597 | 236 log.debug(f'{INDENT*5} {v=} {vals=}') |
1594 | 237 yield v, vals |
238 | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
239 def graphWithoutEvals(self, binding: CandidateBinding) -> Graph: |
1601 | 240 g = Graph(identifier=GRAPH_ID) |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
241 usedByFuncs = binding.applyFunctions(self) |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
242 |
1594 | 243 for stmt in self.graph: |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
244 if stmt not in usedByFuncs: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
245 g.add(stmt) |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
246 return g |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
247 |
1600 | 248 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
|
249 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
|
250 return |
1597 | 251 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
|
252 for v, vals in zip(orderedVars, orderedValueSets): |
1597 | 253 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
|
254 for val in vals: |
1597 | 255 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
|
256 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
257 |
1587 | 258 class Inference: |
259 | |
260 def __init__(self) -> None: | |
261 self.rules = ConjunctiveGraph() | |
262 | |
263 def setRules(self, g: ConjunctiveGraph): | |
1599
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
264 self.rules = ConjunctiveGraph() |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
265 for stmt in g: |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
266 if stmt[1] == LOG['implies']: |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
267 self.rules.add(stmt) |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
268 # others should go to a default working set? |
1587 | 269 |
1601 | 270 @INFER_CALLS.time() |
1587 | 271 def infer(self, graph: Graph): |
272 """ | |
273 returns new graph of inferred statements. | |
274 """ | |
1597 | 275 log.info(f'{INDENT*0} Begin inference of graph len={graph.__len__()} with rules len={len(self.rules)}:') |
1601 | 276 startTime = time.time() |
1605 | 277 self.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
|
278 # 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
|
279 workingSet = Graph() |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
280 workingSet += graph |
1587 | 281 |
1594 | 282 # just the statements that came from RHS's of rules that fired. |
1587 | 283 implied = ConjunctiveGraph() |
284 | |
285 bailout_iterations = 100 | |
286 delta = 1 | |
1601 | 287 self.stats['initWorkingSet'] = cast(int, workingSet.__len__()) |
1587 | 288 while delta > 0 and bailout_iterations > 0: |
1597 | 289 log.info(f'{INDENT*1}*iteration ({bailout_iterations} left)') |
1587 | 290 bailout_iterations -= 1 |
291 delta = -len(implied) | |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
292 self._iterateAllRules(workingSet, implied) |
1587 | 293 delta += len(implied) |
1601 | 294 self.stats['iterations'] += 1 |
1597 | 295 log.info(f'{INDENT*2} this inference iteration added {delta} more implied stmts') |
1601 | 296 self.stats['timeSpent'] = round(time.time() - startTime, 3) |
297 self.stats['impliedStmts'] = len(implied) | |
298 log.info(f'{INDENT*0} Inference done {dict(self.stats)}. Implied:') | |
1587 | 299 for st in implied: |
1597 | 300 log.info(f'{INDENT*1} {st}') |
1587 | 301 return implied |
302 | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
303 def _iterateAllRules(self, workingSet: Graph, implied: Graph): |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
304 for i, r in enumerate(self.rules): |
1600 | 305 self._logRuleApplicationHeader(workingSet, i, r) |
1601 | 306 _applyRule(Lhs(r[0], self.stats), r[2], workingSet, implied, self.stats) |
1587 | 307 |
1600 | 308 def _logRuleApplicationHeader(self, workingSet, i, r): |
1594 | 309 if not log.isEnabledFor(logging.DEBUG): |
310 return | |
311 | |
312 log.debug('') | |
313 log.debug(f'{INDENT*2} workingSet:') | |
1597 | 314 for j, stmt in enumerate(sorted(workingSet)): |
315 log.debug(f'{INDENT*3} ({j}) {stmt}') | |
1594 | 316 |
317 log.debug('') | |
318 log.debug(f'{INDENT*2}-applying rule {i}') | |
319 log.debug(f'{INDENT*3} rule def lhs: {graphDump(r[0])}') | |
320 log.debug(f'{INDENT*3} rule def rhs: {graphDump(r[2])}') | |
321 | |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
322 |
1601 | 323 def _applyRule(lhs: Lhs, rhs: Graph, workingSet: Graph, implied: Graph, stats: Dict): |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
324 for binding in lhs.findCandidateBindings(ReadOnlyGraphAggregate([workingSet])): |
1597 | 325 log.debug(f'{INDENT*3} rule has a working binding:') |
326 | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
327 for lhsBoundStmt in binding.apply(lhs.graphWithoutEvals(binding)): |
1597 | 328 log.debug(f'{INDENT*5} adding {lhsBoundStmt=}') |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
329 workingSet.add(lhsBoundStmt) |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
330 for newStmt in binding.apply(rhs): |
1597 | 331 log.debug(f'{INDENT*5} adding {newStmt=}') |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
332 workingSet.add(newStmt) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
333 implied.add(newStmt) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
334 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
335 |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
336 def graphDump(g: Union[Graph, List[Triple]]): |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
337 if not isinstance(g, Graph): |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
338 g2 = Graph() |
1594 | 339 g2 += g |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
340 g = g2 |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
341 g.bind('', ROOM) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
342 g.bind('ex', Namespace('http://example.com/')) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
343 lines = cast(bytes, g.serialize(format='n3')).decode('utf8').splitlines() |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
344 lines = [line for line in lines if not line.startswith('@prefix')] |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
345 return ' '.join(lines) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
346 |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
347 |
1600 | 348 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
|
349 items = list(candidateTermMatches.items()) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
350 items.sort() |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
351 orderedVars: List[BindableTerm] = [] |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
352 orderedValueSets: List[List[Node]] = [] |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
353 for v, vals in items: |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
354 orderedVars.append(v) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
355 orderedValues: List[Node] = list(vals) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
356 orderedValues.sort(key=str) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
357 orderedValueSets.append(orderedValues) |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
358 |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
359 return orderedVars, orderedValueSets |