Mercurial > code > home > repos > homeauto
annotate service/mqtt_to_rdf/inference.py @ 1602:e3c44ac6d3c5
do findEvals once at setRules time
author | drewp@bigasterisk.com |
---|---|
date | Mon, 06 Sep 2021 00:56:13 -0700 |
parents | 30463df12d89 |
children | 7f8bf68534ed |
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 | |
1602 | 82 for ev in lhs.evaluations: |
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 |
1602 | 156 self.evaluations = list(Evaluation.findEvals(self.graph)) |
157 | |
158 | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
159 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
|
160 """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
|
161 from LHS""" |
1597 | 162 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
|
163 |
1600 | 164 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
|
165 return |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
166 |
1600 | 167 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
|
168 |
1600 | 169 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
|
170 |
1600 | 171 self._logCandidates(orderedVars, orderedValueSets) |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
172 |
1597 | 173 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
|
174 |
1601 | 175 |
1593
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 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
258 class Evaluation: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
259 """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
|
260 (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
|
261 process. It's like they already 'matched' something, so they don't need |
1594 | 262 to match a statement from the known-true working set. |
263 | |
264 One Evaluation instance is for one function call. | |
265 """ | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
266 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
267 @staticmethod |
1598
9e6a593180b6
Evaluation doesn't have to depend on Lhs class
drewp@bigasterisk.com
parents:
1597
diff
changeset
|
268 def findEvals(graph: Graph) -> Iterator['Evaluation']: |
9e6a593180b6
Evaluation doesn't have to depend on Lhs class
drewp@bigasterisk.com
parents:
1597
diff
changeset
|
269 for stmt in graph.triples((None, MATH['sum'], None)): |
1600 | 270 operands, operandsStmts = _parseList(graph, stmt[0]) |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
271 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
|
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, MATH['greaterThan'], None)): |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
274 yield Evaluation([stmt[0], stmt[2]], stmt, []) |
1594 | 275 |
1598
9e6a593180b6
Evaluation doesn't have to depend on Lhs class
drewp@bigasterisk.com
parents:
1597
diff
changeset
|
276 for stmt in graph.triples((None, ROOM['asFarenheit'], None)): |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
277 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
|
278 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
279 # internal, use findEvals |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
280 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
|
281 self.operands = operands |
1601 | 282 self.operandsStmts = Graph(identifier=GRAPH_ID) |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
283 self.operandsStmts += otherStmts # may grow |
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
284 self.operandsStmts.add(mainStmt) |
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
285 self.stmt = mainStmt |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
286 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
287 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
|
288 """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
|
289 pred = self.stmt[1] |
1594 | 290 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
|
291 boundOperands = [] |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
292 for op in self.operands: |
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
293 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
|
294 try: |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
295 op = inputBindings[op] |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
296 except KeyError: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
297 return {}, self.operandsStmts |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
298 |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
299 boundOperands.append(op) |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
300 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
301 if pred == MATH['sum']: |
1594 | 302 obj = Literal(sum(map(numericNode, boundOperands))) |
303 if not isinstance(objVar, Variable): | |
304 raise TypeError(f'expected Variable, got {objVar!r}') | |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
305 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
|
306 elif pred == ROOM['asFarenheit']: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
307 if len(boundOperands) != 1: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
308 raise ValueError(":asFarenheit takes 1 subject operand") |
1594 | 309 f = Literal(Decimal(numericNode(boundOperands[0])) * 9 / 5 + 32) |
310 if not isinstance(objVar, Variable): | |
311 raise TypeError(f'expected Variable, got {objVar!r}') | |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
312 res: Dict[BindableTerm, Node] = {objVar: f} |
1594 | 313 elif pred == MATH['greaterThan']: |
314 if not (numericNode(boundOperands[0]) > numericNode(boundOperands[1])): | |
315 raise EvaluationFailed() | |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
316 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
|
317 else: |
1594 | 318 raise NotImplementedError(repr(pred)) |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
319 |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
320 return res, self.operandsStmts |
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
321 |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
322 |
1594 | 323 def numericNode(n: Node): |
324 if not isinstance(n, Literal): | |
325 raise TypeError(f'expected Literal, got {n=}') | |
326 val = n.toPython() | |
327 if not isinstance(val, (int, float, Decimal)): | |
328 raise TypeError(f'expected number, got {val=}') | |
329 return val | |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
330 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
331 |
1587 | 332 class Inference: |
333 | |
334 def __init__(self) -> None: | |
335 self.rules = ConjunctiveGraph() | |
336 | |
337 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
|
338 self.rules = ConjunctiveGraph() |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
339 for stmt in g: |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
340 if stmt[1] == LOG['implies']: |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
341 self.rules.add(stmt) |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
342 # others should go to a default working set? |
1587 | 343 |
1601 | 344 @INFER_CALLS.time() |
1587 | 345 def infer(self, graph: Graph): |
346 """ | |
347 returns new graph of inferred statements. | |
348 """ | |
1597 | 349 log.info(f'{INDENT*0} Begin inference of graph len={graph.__len__()} with rules len={len(self.rules)}:') |
1601 | 350 startTime = time.time() |
351 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
|
352 # 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
|
353 workingSet = Graph() |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
354 workingSet += graph |
1587 | 355 |
1594 | 356 # just the statements that came from RHS's of rules that fired. |
1587 | 357 implied = ConjunctiveGraph() |
358 | |
359 bailout_iterations = 100 | |
360 delta = 1 | |
1601 | 361 self.stats['initWorkingSet'] = cast(int, workingSet.__len__()) |
1587 | 362 while delta > 0 and bailout_iterations > 0: |
1597 | 363 log.info(f'{INDENT*1}*iteration ({bailout_iterations} left)') |
1587 | 364 bailout_iterations -= 1 |
365 delta = -len(implied) | |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
366 self._iterateAllRules(workingSet, implied) |
1587 | 367 delta += len(implied) |
1601 | 368 self.stats['iterations'] += 1 |
1597 | 369 log.info(f'{INDENT*2} this inference iteration added {delta} more implied stmts') |
1601 | 370 self.stats['timeSpent'] = round(time.time() - startTime, 3) |
371 self.stats['impliedStmts'] = len(implied) | |
372 log.info(f'{INDENT*0} Inference done {dict(self.stats)}. Implied:') | |
1587 | 373 for st in implied: |
1597 | 374 log.info(f'{INDENT*1} {st}') |
1587 | 375 return implied |
376 | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
377 def _iterateAllRules(self, workingSet: Graph, implied: Graph): |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
378 for i, r in enumerate(self.rules): |
1600 | 379 self._logRuleApplicationHeader(workingSet, i, r) |
1601 | 380 _applyRule(Lhs(r[0], self.stats), r[2], workingSet, implied, self.stats) |
1587 | 381 |
1600 | 382 def _logRuleApplicationHeader(self, workingSet, i, r): |
1594 | 383 if not log.isEnabledFor(logging.DEBUG): |
384 return | |
385 | |
386 log.debug('') | |
387 log.debug(f'{INDENT*2} workingSet:') | |
1597 | 388 for j, stmt in enumerate(sorted(workingSet)): |
389 log.debug(f'{INDENT*3} ({j}) {stmt}') | |
1594 | 390 |
391 log.debug('') | |
392 log.debug(f'{INDENT*2}-applying rule {i}') | |
393 log.debug(f'{INDENT*3} rule def lhs: {graphDump(r[0])}') | |
394 log.debug(f'{INDENT*3} rule def rhs: {graphDump(r[2])}') | |
395 | |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
396 |
1601 | 397 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
|
398 for binding in lhs.findCandidateBindings(ReadOnlyGraphAggregate([workingSet])): |
1597 | 399 log.debug(f'{INDENT*3} rule has a working binding:') |
400 | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
401 for lhsBoundStmt in binding.apply(lhs.graphWithoutEvals(binding)): |
1597 | 402 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
|
403 workingSet.add(lhsBoundStmt) |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
404 for newStmt in binding.apply(rhs): |
1597 | 405 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
|
406 workingSet.add(newStmt) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
407 implied.add(newStmt) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
408 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
409 |
1600 | 410 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
|
411 """"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
|
412 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
|
413 out = [] |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
414 used = set() |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
415 cur = subj |
1594 | 416 while cur != RDF.nil: |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
417 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
|
418 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
|
419 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
420 next = graph.value(cur, RDF.rest) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
421 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
|
422 |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
423 cur = next |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
424 return out, used |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
425 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
426 |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
427 def graphDump(g: Union[Graph, List[Triple]]): |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
428 if not isinstance(g, Graph): |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
429 g2 = Graph() |
1594 | 430 g2 += g |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
431 g = g2 |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
432 g.bind('', ROOM) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
433 g.bind('ex', Namespace('http://example.com/')) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
434 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
|
435 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
|
436 return ' '.join(lines) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
437 |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
438 |
1600 | 439 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
|
440 items = list(candidateTermMatches.items()) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
441 items.sort() |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
442 orderedVars: List[BindableTerm] = [] |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
443 orderedValueSets: List[List[Node]] = [] |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
444 for v, vals in items: |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
445 orderedVars.append(v) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
446 orderedValues: List[Node] = list(vals) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
447 orderedValues.sort(key=str) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
448 orderedValueSets.append(orderedValues) |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
449 |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
450 return orderedVars, orderedValueSets |