Mercurial > code > home > repos > homeauto
annotate service/mqtt_to_rdf/inference.py @ 1601:30463df12d89
infer() dumps stats
author | drewp@bigasterisk.com |
---|---|
date | Sun, 05 Sep 2021 23:27:49 -0700 |
parents | 89a50242cb5e |
children | e3c44ac6d3c5 |
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 | |
18 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
|
19 INDENT = ' ' |
1587 | 20 |
21 Triple = Tuple[Node, Node, Node] | |
22 Rule = Tuple[Graph, Node, Graph] | |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
23 BindableTerm = Union[Variable, BNode] |
1594 | 24 ReadOnlyWorkingSet = ReadOnlyGraphAggregate |
1587 | 25 |
1601 | 26 INFER_CALLS = Summary('read_rules_calls', 'calls') |
1587 | 27 |
28 ROOM = Namespace("http://projects.bigasterisk.com/room/") | |
29 LOG = Namespace('http://www.w3.org/2000/10/swap/log#') | |
30 MATH = Namespace('http://www.w3.org/2000/10/swap/math#') | |
31 | |
1601 | 32 # Graph() makes a BNode if you don't pass |
33 # identifier, which can be a bottleneck. | |
34 GRAPH_ID = URIRef('dont/care') | |
1587 | 35 |
1594 | 36 class EvaluationFailed(ValueError): |
37 """e.g. we were given (5 math:greaterThan 6)""" | |
1587 | 38 |
39 | |
1599
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
40 class BindingUnknown(ValueError): |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
41 """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
|
42 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
|
43 """ |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
44 |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
45 |
1594 | 46 @dataclass |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
47 class CandidateBinding: |
1594 | 48 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
|
49 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
50 def __repr__(self): |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
51 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
|
52 return f'CandidateBinding({b})' |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
53 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
54 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
|
55 for stmt in g: |
1599
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
56 try: |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
57 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
|
58 except BindingUnknown: |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
59 continue |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
60 yield bound |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
61 |
1594 | 62 def _applyTerm(self, term: Node): |
63 if isinstance(term, (Variable, BNode)): | |
64 if term in self.binding: | |
65 return self.binding[term] | |
1599
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
66 else: |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
67 raise BindingUnknown() |
1594 | 68 return term |
69 | |
70 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
|
71 """may grow the binding with some results""" |
1601 | 72 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
|
73 while True: |
1594 | 74 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
|
75 if delta == 0: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
76 break |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
77 return usedByFuncs |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
78 |
1594 | 79 def _applyFunctionsIteration(self, lhs, usedByFuncs: Graph): |
80 before = len(self.binding) | |
81 delta = 0 | |
1598
9e6a593180b6
Evaluation doesn't have to depend on Lhs class
drewp@bigasterisk.com
parents:
1597
diff
changeset
|
82 for ev in Evaluation.findEvals(lhs.graph): |
1594 | 83 log.debug(f'{INDENT*3} found Evaluation') |
84 | |
85 newBindings, usedGraph = ev.resultBindings(self.binding) | |
86 usedByFuncs += usedGraph | |
87 self._addNewBindings(newBindings) | |
88 delta = len(self.binding) - before | |
89 dump = "(...)" | |
90 if log.isEnabledFor(logging.DEBUG) and cast(int, usedGraph.__len__()) < 20: | |
91 dump = graphDump(usedGraph) | |
92 log.debug(f'{INDENT*4} rule {dump} made {delta} new bindings') | |
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 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
156 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
|
157 """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
|
158 from LHS""" |
1597 | 159 log.debug(f'{INDENT*3} nodesToBind: {self.lhsBindables}') |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
160 |
1600 | 161 if not self._allStaticStatementsMatch(workingSet): |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
162 return |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
163 |
1600 | 164 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
|
165 |
1600 | 166 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
|
167 |
1600 | 168 self._logCandidates(orderedVars, orderedValueSets) |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
169 |
1597 | 170 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
|
171 |
1601 | 172 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
173 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
|
174 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
|
175 log.debug('') |
1597 | 176 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
|
177 |
1594 | 178 try: |
179 usedByFuncs = binding.applyFunctions(self) | |
180 except EvaluationFailed: | |
1601 | 181 self.stats['permCountFailingEval'] += 1 |
1594 | 182 continue |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
183 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
184 if not binding.verify(self, workingSet, usedByFuncs): |
1597 | 185 log.debug(f'{INDENT*4} this binding did not verify') |
1601 | 186 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
|
187 continue |
1601 | 188 |
189 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
|
190 yield binding |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
191 |
1600 | 192 def _allStaticStatementsMatch(self, workingSet: ReadOnlyWorkingSet) -> bool: |
1594 | 193 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
|
194 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
|
195 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
|
196 return False |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
197 return True |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
198 |
1600 | 199 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
|
200 """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
|
201 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
202 candidateTermMatches: Dict[BindableTerm, Set[Node]] = defaultdict(set) |
1594 | 203 for lhsStmt in self.graph: |
1597 | 204 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
|
205 for i, trueStmt in enumerate(sorted(workingSet)): |
1597 | 206 # 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
|
207 |
1594 | 208 for v, vals in self._bindingsFromStatement(lhsStmt, trueStmt): |
209 candidateTermMatches[v].update(vals) | |
210 | |
211 for trueStmt in itertools.chain(workingSet, self.graph): | |
212 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
|
213 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
|
214 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
|
215 candidateTermMatches[b].add(t) |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
216 return candidateTermMatches |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
217 |
1594 | 218 def _bindingsFromStatement(self, stmt1: Triple, stmt2: Triple) -> Iterator[Tuple[Variable, Set[Node]]]: |
219 """if these stmts match otherwise, what BNode or Variable mappings do we learn? | |
220 | |
221 e.g. stmt1=(?x B ?y) and stmt2=(A B C), then we yield (?x, {A}) and (?y, {C}) | |
222 or stmt1=(_:x B C) and stmt2=(A B C), then we yield (_:x, {A}) | |
223 or stmt1=(?x B C) and stmt2=(A B D), then we yield nothing | |
224 """ | |
225 bindingsFromStatement = {} | |
226 for term1, term2 in zip(stmt1, stmt2): | |
227 if isinstance(term1, (BNode, Variable)): | |
228 bindingsFromStatement.setdefault(term1, set()).add(term2) | |
229 elif term1 != term2: | |
230 break | |
231 else: | |
232 for v, vals in bindingsFromStatement.items(): | |
1597 | 233 log.debug(f'{INDENT*5} {v=} {vals=}') |
1594 | 234 yield v, vals |
235 | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
236 def graphWithoutEvals(self, binding: CandidateBinding) -> Graph: |
1601 | 237 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
|
238 usedByFuncs = binding.applyFunctions(self) |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
239 |
1594 | 240 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
|
241 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
|
242 g.add(stmt) |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
243 return g |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
244 |
1600 | 245 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
|
246 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
|
247 return |
1597 | 248 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
|
249 for v, vals in zip(orderedVars, orderedValueSets): |
1597 | 250 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
|
251 for val in vals: |
1597 | 252 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
|
253 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
254 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
255 class Evaluation: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
256 """some lhs statements need to be evaluated with a special function |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
257 (e.g. math) and then not considered for the rest of the rule-firing |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
258 process. It's like they already 'matched' something, so they don't need |
1594 | 259 to match a statement from the known-true working set. |
260 | |
261 One Evaluation instance is for one function call. | |
262 """ | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
263 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
264 @staticmethod |
1598
9e6a593180b6
Evaluation doesn't have to depend on Lhs class
drewp@bigasterisk.com
parents:
1597
diff
changeset
|
265 def findEvals(graph: Graph) -> Iterator['Evaluation']: |
9e6a593180b6
Evaluation doesn't have to depend on Lhs class
drewp@bigasterisk.com
parents:
1597
diff
changeset
|
266 for stmt in graph.triples((None, MATH['sum'], None)): |
1600 | 267 operands, operandsStmts = _parseList(graph, stmt[0]) |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
268 yield Evaluation(operands, stmt, operandsStmts) |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
269 |
1598
9e6a593180b6
Evaluation doesn't have to depend on Lhs class
drewp@bigasterisk.com
parents:
1597
diff
changeset
|
270 for stmt in graph.triples((None, MATH['greaterThan'], None)): |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
271 yield Evaluation([stmt[0], stmt[2]], stmt, []) |
1594 | 272 |
1598
9e6a593180b6
Evaluation doesn't have to depend on Lhs class
drewp@bigasterisk.com
parents:
1597
diff
changeset
|
273 for stmt in graph.triples((None, ROOM['asFarenheit'], None)): |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
274 yield Evaluation([stmt[0]], stmt, []) |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
275 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
276 # internal, use findEvals |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
277 def __init__(self, operands: List[Node], mainStmt: Triple, otherStmts: Iterable[Triple]) -> None: |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
278 self.operands = operands |
1601 | 279 self.operandsStmts = Graph(identifier=GRAPH_ID) |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
280 self.operandsStmts += otherStmts # may grow |
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
281 self.operandsStmts.add(mainStmt) |
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
282 self.stmt = mainStmt |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
283 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
284 def resultBindings(self, inputBindings) -> Tuple[Dict[BindableTerm, Node], Graph]: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
285 """under the bindings so far, what would this evaluation tell us, and which stmts would be consumed from doing so?""" |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
286 pred = self.stmt[1] |
1594 | 287 objVar: Node = self.stmt[2] |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
288 boundOperands = [] |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
289 for op in self.operands: |
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
290 if isinstance(op, Variable): |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
291 try: |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
292 op = inputBindings[op] |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
293 except KeyError: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
294 return {}, self.operandsStmts |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
295 |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
296 boundOperands.append(op) |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
297 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
298 if pred == MATH['sum']: |
1594 | 299 obj = Literal(sum(map(numericNode, boundOperands))) |
300 if not isinstance(objVar, Variable): | |
301 raise TypeError(f'expected Variable, got {objVar!r}') | |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
302 res: Dict[BindableTerm, Node] = {objVar: obj} |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
303 elif pred == ROOM['asFarenheit']: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
304 if len(boundOperands) != 1: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
305 raise ValueError(":asFarenheit takes 1 subject operand") |
1594 | 306 f = Literal(Decimal(numericNode(boundOperands[0])) * 9 / 5 + 32) |
307 if not isinstance(objVar, Variable): | |
308 raise TypeError(f'expected Variable, got {objVar!r}') | |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
309 res: Dict[BindableTerm, Node] = {objVar: f} |
1594 | 310 elif pred == MATH['greaterThan']: |
311 if not (numericNode(boundOperands[0]) > numericNode(boundOperands[1])): | |
312 raise EvaluationFailed() | |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
313 res: Dict[BindableTerm, Node] = {} |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
314 else: |
1594 | 315 raise NotImplementedError(repr(pred)) |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
316 |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
317 return res, self.operandsStmts |
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
318 |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
319 |
1594 | 320 def numericNode(n: Node): |
321 if not isinstance(n, Literal): | |
322 raise TypeError(f'expected Literal, got {n=}') | |
323 val = n.toPython() | |
324 if not isinstance(val, (int, float, Decimal)): | |
325 raise TypeError(f'expected number, got {val=}') | |
326 return val | |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
327 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
328 |
1587 | 329 class Inference: |
330 | |
331 def __init__(self) -> None: | |
332 self.rules = ConjunctiveGraph() | |
333 | |
334 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
|
335 self.rules = ConjunctiveGraph() |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
336 for stmt in g: |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
337 if stmt[1] == LOG['implies']: |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
338 self.rules.add(stmt) |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
339 # others should go to a default working set? |
1587 | 340 |
1601 | 341 @INFER_CALLS.time() |
1587 | 342 def infer(self, graph: Graph): |
343 """ | |
344 returns new graph of inferred statements. | |
345 """ | |
1597 | 346 log.info(f'{INDENT*0} Begin inference of graph len={graph.__len__()} with rules len={len(self.rules)}:') |
1601 | 347 startTime = time.time() |
348 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
|
349 # 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
|
350 workingSet = Graph() |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
351 workingSet += graph |
1587 | 352 |
1594 | 353 # just the statements that came from RHS's of rules that fired. |
1587 | 354 implied = ConjunctiveGraph() |
355 | |
356 bailout_iterations = 100 | |
357 delta = 1 | |
1601 | 358 self.stats['initWorkingSet'] = cast(int, workingSet.__len__()) |
1587 | 359 while delta > 0 and bailout_iterations > 0: |
1597 | 360 log.info(f'{INDENT*1}*iteration ({bailout_iterations} left)') |
1587 | 361 bailout_iterations -= 1 |
362 delta = -len(implied) | |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
363 self._iterateAllRules(workingSet, implied) |
1587 | 364 delta += len(implied) |
1601 | 365 self.stats['iterations'] += 1 |
1597 | 366 log.info(f'{INDENT*2} this inference iteration added {delta} more implied stmts') |
1601 | 367 self.stats['timeSpent'] = round(time.time() - startTime, 3) |
368 self.stats['impliedStmts'] = len(implied) | |
369 log.info(f'{INDENT*0} Inference done {dict(self.stats)}. Implied:') | |
1587 | 370 for st in implied: |
1597 | 371 log.info(f'{INDENT*1} {st}') |
1587 | 372 return implied |
373 | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
374 def _iterateAllRules(self, workingSet: Graph, implied: Graph): |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
375 for i, r in enumerate(self.rules): |
1600 | 376 self._logRuleApplicationHeader(workingSet, i, r) |
1601 | 377 _applyRule(Lhs(r[0], self.stats), r[2], workingSet, implied, self.stats) |
1587 | 378 |
1600 | 379 def _logRuleApplicationHeader(self, workingSet, i, r): |
1594 | 380 if not log.isEnabledFor(logging.DEBUG): |
381 return | |
382 | |
383 log.debug('') | |
384 log.debug(f'{INDENT*2} workingSet:') | |
1597 | 385 for j, stmt in enumerate(sorted(workingSet)): |
386 log.debug(f'{INDENT*3} ({j}) {stmt}') | |
1594 | 387 |
388 log.debug('') | |
389 log.debug(f'{INDENT*2}-applying rule {i}') | |
390 log.debug(f'{INDENT*3} rule def lhs: {graphDump(r[0])}') | |
391 log.debug(f'{INDENT*3} rule def rhs: {graphDump(r[2])}') | |
392 | |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
393 |
1601 | 394 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
|
395 for binding in lhs.findCandidateBindings(ReadOnlyGraphAggregate([workingSet])): |
1597 | 396 log.debug(f'{INDENT*3} rule has a working binding:') |
397 | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
398 for lhsBoundStmt in binding.apply(lhs.graphWithoutEvals(binding)): |
1597 | 399 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
|
400 workingSet.add(lhsBoundStmt) |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
401 for newStmt in binding.apply(rhs): |
1597 | 402 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
|
403 workingSet.add(newStmt) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
404 implied.add(newStmt) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
405 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
406 |
1600 | 407 def _parseList(graph, subj) -> Tuple[List[Node], Set[Triple]]: |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
408 """"Do like Collection(g, subj) but also return all the |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
409 triples that are involved in the list""" |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
410 out = [] |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
411 used = set() |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
412 cur = subj |
1594 | 413 while cur != RDF.nil: |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
414 out.append(graph.value(cur, RDF.first)) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
415 used.add((cur, RDF.first, out[-1])) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
416 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
417 next = graph.value(cur, RDF.rest) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
418 used.add((cur, RDF.rest, next)) |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
419 |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
420 cur = next |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
421 return out, used |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
422 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
423 |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
424 def graphDump(g: Union[Graph, List[Triple]]): |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
425 if not isinstance(g, Graph): |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
426 g2 = Graph() |
1594 | 427 g2 += g |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
428 g = g2 |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
429 g.bind('', ROOM) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
430 g.bind('ex', Namespace('http://example.com/')) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
431 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
|
432 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
|
433 return ' '.join(lines) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
434 |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
435 |
1600 | 436 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
|
437 items = list(candidateTermMatches.items()) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
438 items.sort() |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
439 orderedVars: List[BindableTerm] = [] |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
440 orderedValueSets: List[List[Node]] = [] |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
441 for v, vals in items: |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
442 orderedVars.append(v) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
443 orderedValues: List[Node] = list(vals) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
444 orderedValues.sort(key=str) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
445 orderedValueSets.append(orderedValues) |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
446 |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
447 return orderedVars, orderedValueSets |