Mercurial > code > home > repos > homeauto
annotate service/mqtt_to_rdf/inference.py @ 1603:7f8bf68534ed
logging and stats
author | drewp@bigasterisk.com |
---|---|
date | Mon, 06 Sep 2021 00:56:47 -0700 |
parents | e3c44ac6d3c5 |
children | 449746d1598f |
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 | |
1603 | 89 if log.isEnabledFor(logging.DEBUG): |
90 dump = "(...)" | |
91 if cast(int, usedGraph.__len__()) < 20: | |
92 dump = graphDump(usedGraph) | |
93 log.debug(f'{INDENT*4} rule {dump} made {delta} new bindings') | |
1594 | 94 return delta |
95 | |
96 def _addNewBindings(self, newBindings): | |
97 for k, v in newBindings.items(): | |
98 if k in self.binding and self.binding[k] != v: | |
99 raise ValueError(f'conflict- thought {k} would be {self.binding[k]} but another Evaluation said it should be {v}') | |
100 self.binding[k] = v | |
101 | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
102 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
|
103 """Can this lhs be true all at once in workingSet? Does it match with these bindings?""" |
1594 | 104 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
|
105 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
|
106 |
1600 | 107 self._logVerifyBanner(boundLhs, workingSet, boundUsedByFuncs) |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
108 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
109 for stmt in boundLhs: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
110 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
|
111 |
1594 | 112 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
|
113 pass |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
114 elif stmt in workingSet: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
115 pass |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
116 else: |
1597 | 117 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
|
118 return False |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
119 return True |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
120 |
1600 | 121 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
|
122 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
|
123 return |
1597 | 124 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
|
125 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
|
126 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
|
127 |
1597 | 128 # log.debug(f'{INDENT*4}| and against this workingSet:') |
129 # for stmt in sorted(workingSet): | |
130 # 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
|
131 |
1597 | 132 stmts = sorted(boundUsedByFuncs) |
133 if stmts: | |
134 log.debug(f'{INDENT*4}| while ignoring these usedByFuncs:') | |
135 for stmt in stmts: | |
136 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
|
137 log.debug(f'{INDENT*4}\\') |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
138 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
139 |
1594 | 140 @dataclass |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
141 class Lhs: |
1594 | 142 graph: Graph |
1601 | 143 stats: Dict |
1594 | 144 |
145 staticRuleStmts: Graph = field(default_factory=Graph) | |
146 lhsBindables: Set[BindableTerm] = field(default_factory=set) | |
147 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
|
148 |
1594 | 149 def __post_init__(self): |
150 for ruleStmt in self.graph: | |
151 varsAndBnodesInStmt = [term for term in ruleStmt if isinstance(term, (Variable, BNode))] | |
152 self.lhsBindables.update(varsAndBnodesInStmt) | |
153 self.lhsBnodes.update(x for x in varsAndBnodesInStmt if isinstance(x, BNode)) | |
154 if not varsAndBnodesInStmt: | |
155 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
|
156 |
1602 | 157 self.evaluations = list(Evaluation.findEvals(self.graph)) |
158 | |
159 | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
160 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
|
161 """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
|
162 from LHS""" |
1597 | 163 log.debug(f'{INDENT*3} nodesToBind: {self.lhsBindables}') |
1603 | 164 self.stats['findCandidateBindingsCalls'] += 1 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
165 |
1600 | 166 if not self._allStaticStatementsMatch(workingSet): |
1603 | 167 self.stats['findCandidateBindingEarlyExits'] += 1 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
168 return |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
169 |
1600 | 170 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
|
171 |
1600 | 172 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
|
173 |
1600 | 174 self._logCandidates(orderedVars, orderedValueSets) |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
175 |
1597 | 176 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
|
177 |
1601 | 178 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
179 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
|
180 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
|
181 log.debug('') |
1597 | 182 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
|
183 |
1594 | 184 try: |
185 usedByFuncs = binding.applyFunctions(self) | |
186 except EvaluationFailed: | |
1601 | 187 self.stats['permCountFailingEval'] += 1 |
1594 | 188 continue |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
189 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
190 if not binding.verify(self, workingSet, usedByFuncs): |
1597 | 191 log.debug(f'{INDENT*4} this binding did not verify') |
1601 | 192 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
|
193 continue |
1601 | 194 |
195 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
|
196 yield binding |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
197 |
1600 | 198 def _allStaticStatementsMatch(self, workingSet: ReadOnlyWorkingSet) -> bool: |
1594 | 199 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
|
200 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
|
201 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
|
202 return False |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
203 return True |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
204 |
1600 | 205 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
|
206 """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
|
207 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
208 candidateTermMatches: Dict[BindableTerm, Set[Node]] = defaultdict(set) |
1594 | 209 for lhsStmt in self.graph: |
1597 | 210 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
|
211 for i, trueStmt in enumerate(sorted(workingSet)): |
1597 | 212 # 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
|
213 |
1594 | 214 for v, vals in self._bindingsFromStatement(lhsStmt, trueStmt): |
215 candidateTermMatches[v].update(vals) | |
216 | |
217 for trueStmt in itertools.chain(workingSet, self.graph): | |
218 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
|
219 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
|
220 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
|
221 candidateTermMatches[b].add(t) |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
222 return candidateTermMatches |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
223 |
1594 | 224 def _bindingsFromStatement(self, stmt1: Triple, stmt2: Triple) -> Iterator[Tuple[Variable, Set[Node]]]: |
225 """if these stmts match otherwise, what BNode or Variable mappings do we learn? | |
226 | |
227 e.g. stmt1=(?x B ?y) and stmt2=(A B C), then we yield (?x, {A}) and (?y, {C}) | |
228 or stmt1=(_:x B C) and stmt2=(A B C), then we yield (_:x, {A}) | |
229 or stmt1=(?x B C) and stmt2=(A B D), then we yield nothing | |
230 """ | |
231 bindingsFromStatement = {} | |
232 for term1, term2 in zip(stmt1, stmt2): | |
233 if isinstance(term1, (BNode, Variable)): | |
234 bindingsFromStatement.setdefault(term1, set()).add(term2) | |
235 elif term1 != term2: | |
236 break | |
237 else: | |
238 for v, vals in bindingsFromStatement.items(): | |
1597 | 239 log.debug(f'{INDENT*5} {v=} {vals=}') |
1594 | 240 yield v, vals |
241 | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
242 def graphWithoutEvals(self, binding: CandidateBinding) -> Graph: |
1601 | 243 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
|
244 usedByFuncs = binding.applyFunctions(self) |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
245 |
1594 | 246 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
|
247 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
|
248 g.add(stmt) |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
249 return g |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
250 |
1600 | 251 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
|
252 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
|
253 return |
1597 | 254 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
|
255 for v, vals in zip(orderedVars, orderedValueSets): |
1597 | 256 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
|
257 for val in vals: |
1597 | 258 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
|
259 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
260 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
261 class Evaluation: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
262 """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
|
263 (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
|
264 process. It's like they already 'matched' something, so they don't need |
1594 | 265 to match a statement from the known-true working set. |
266 | |
267 One Evaluation instance is for one function call. | |
268 """ | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
269 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
270 @staticmethod |
1598
9e6a593180b6
Evaluation doesn't have to depend on Lhs class
drewp@bigasterisk.com
parents:
1597
diff
changeset
|
271 def findEvals(graph: Graph) -> Iterator['Evaluation']: |
9e6a593180b6
Evaluation doesn't have to depend on Lhs class
drewp@bigasterisk.com
parents:
1597
diff
changeset
|
272 for stmt in graph.triples((None, MATH['sum'], None)): |
1600 | 273 operands, operandsStmts = _parseList(graph, stmt[0]) |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
274 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
|
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, MATH['greaterThan'], None)): |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
277 yield Evaluation([stmt[0], stmt[2]], stmt, []) |
1594 | 278 |
1598
9e6a593180b6
Evaluation doesn't have to depend on Lhs class
drewp@bigasterisk.com
parents:
1597
diff
changeset
|
279 for stmt in graph.triples((None, ROOM['asFarenheit'], None)): |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
280 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
|
281 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
282 # internal, use findEvals |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
283 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
|
284 self.operands = operands |
1601 | 285 self.operandsStmts = Graph(identifier=GRAPH_ID) |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
286 self.operandsStmts += otherStmts # may grow |
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
287 self.operandsStmts.add(mainStmt) |
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
288 self.stmt = mainStmt |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
289 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
290 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
|
291 """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
|
292 pred = self.stmt[1] |
1594 | 293 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
|
294 boundOperands = [] |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
295 for op in self.operands: |
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
296 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
|
297 try: |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
298 op = inputBindings[op] |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
299 except KeyError: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
300 return {}, self.operandsStmts |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
301 |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
302 boundOperands.append(op) |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
303 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
304 if pred == MATH['sum']: |
1594 | 305 obj = Literal(sum(map(numericNode, boundOperands))) |
306 if not isinstance(objVar, Variable): | |
307 raise TypeError(f'expected Variable, got {objVar!r}') | |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
308 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
|
309 elif pred == ROOM['asFarenheit']: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
310 if len(boundOperands) != 1: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
311 raise ValueError(":asFarenheit takes 1 subject operand") |
1594 | 312 f = Literal(Decimal(numericNode(boundOperands[0])) * 9 / 5 + 32) |
313 if not isinstance(objVar, Variable): | |
314 raise TypeError(f'expected Variable, got {objVar!r}') | |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
315 res: Dict[BindableTerm, Node] = {objVar: f} |
1594 | 316 elif pred == MATH['greaterThan']: |
317 if not (numericNode(boundOperands[0]) > numericNode(boundOperands[1])): | |
318 raise EvaluationFailed() | |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
319 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
|
320 else: |
1594 | 321 raise NotImplementedError(repr(pred)) |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
322 |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
323 return res, self.operandsStmts |
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
324 |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
325 |
1594 | 326 def numericNode(n: Node): |
327 if not isinstance(n, Literal): | |
328 raise TypeError(f'expected Literal, got {n=}') | |
329 val = n.toPython() | |
330 if not isinstance(val, (int, float, Decimal)): | |
331 raise TypeError(f'expected number, got {val=}') | |
332 return val | |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
333 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
334 |
1587 | 335 class Inference: |
336 | |
337 def __init__(self) -> None: | |
338 self.rules = ConjunctiveGraph() | |
339 | |
340 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
|
341 self.rules = ConjunctiveGraph() |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
342 for stmt in g: |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
343 if stmt[1] == LOG['implies']: |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
344 self.rules.add(stmt) |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1598
diff
changeset
|
345 # others should go to a default working set? |
1587 | 346 |
1601 | 347 @INFER_CALLS.time() |
1587 | 348 def infer(self, graph: Graph): |
349 """ | |
350 returns new graph of inferred statements. | |
351 """ | |
1597 | 352 log.info(f'{INDENT*0} Begin inference of graph len={graph.__len__()} with rules len={len(self.rules)}:') |
1601 | 353 startTime = time.time() |
354 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
|
355 # 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
|
356 workingSet = Graph() |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
357 workingSet += graph |
1587 | 358 |
1594 | 359 # just the statements that came from RHS's of rules that fired. |
1587 | 360 implied = ConjunctiveGraph() |
361 | |
362 bailout_iterations = 100 | |
363 delta = 1 | |
1601 | 364 self.stats['initWorkingSet'] = cast(int, workingSet.__len__()) |
1587 | 365 while delta > 0 and bailout_iterations > 0: |
1597 | 366 log.info(f'{INDENT*1}*iteration ({bailout_iterations} left)') |
1587 | 367 bailout_iterations -= 1 |
368 delta = -len(implied) | |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
369 self._iterateAllRules(workingSet, implied) |
1587 | 370 delta += len(implied) |
1601 | 371 self.stats['iterations'] += 1 |
1597 | 372 log.info(f'{INDENT*2} this inference iteration added {delta} more implied stmts') |
1601 | 373 self.stats['timeSpent'] = round(time.time() - startTime, 3) |
374 self.stats['impliedStmts'] = len(implied) | |
375 log.info(f'{INDENT*0} Inference done {dict(self.stats)}. Implied:') | |
1587 | 376 for st in implied: |
1597 | 377 log.info(f'{INDENT*1} {st}') |
1587 | 378 return implied |
379 | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
380 def _iterateAllRules(self, workingSet: Graph, implied: Graph): |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
381 for i, r in enumerate(self.rules): |
1600 | 382 self._logRuleApplicationHeader(workingSet, i, r) |
1601 | 383 _applyRule(Lhs(r[0], self.stats), r[2], workingSet, implied, self.stats) |
1587 | 384 |
1600 | 385 def _logRuleApplicationHeader(self, workingSet, i, r): |
1594 | 386 if not log.isEnabledFor(logging.DEBUG): |
387 return | |
388 | |
389 log.debug('') | |
390 log.debug(f'{INDENT*2} workingSet:') | |
1597 | 391 for j, stmt in enumerate(sorted(workingSet)): |
392 log.debug(f'{INDENT*3} ({j}) {stmt}') | |
1594 | 393 |
394 log.debug('') | |
395 log.debug(f'{INDENT*2}-applying rule {i}') | |
396 log.debug(f'{INDENT*3} rule def lhs: {graphDump(r[0])}') | |
397 log.debug(f'{INDENT*3} rule def rhs: {graphDump(r[2])}') | |
398 | |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
399 |
1601 | 400 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
|
401 for binding in lhs.findCandidateBindings(ReadOnlyGraphAggregate([workingSet])): |
1597 | 402 log.debug(f'{INDENT*3} rule has a working binding:') |
403 | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
404 for lhsBoundStmt in binding.apply(lhs.graphWithoutEvals(binding)): |
1597 | 405 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
|
406 workingSet.add(lhsBoundStmt) |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
407 for newStmt in binding.apply(rhs): |
1597 | 408 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
|
409 workingSet.add(newStmt) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
410 implied.add(newStmt) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
411 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
412 |
1600 | 413 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
|
414 """"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
|
415 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
|
416 out = [] |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
417 used = set() |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
418 cur = subj |
1594 | 419 while cur != RDF.nil: |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
420 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
|
421 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
|
422 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
423 next = graph.value(cur, RDF.rest) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
424 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
|
425 |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
426 cur = next |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
427 return out, used |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
428 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
429 |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
430 def graphDump(g: Union[Graph, List[Triple]]): |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
431 if not isinstance(g, Graph): |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
432 g2 = Graph() |
1594 | 433 g2 += g |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
434 g = g2 |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
435 g.bind('', ROOM) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
436 g.bind('ex', Namespace('http://example.com/')) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
437 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
|
438 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
|
439 return ' '.join(lines) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
440 |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
441 |
1600 | 442 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
|
443 items = list(candidateTermMatches.items()) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
444 items.sort() |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
445 orderedVars: List[BindableTerm] = [] |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
446 orderedValueSets: List[List[Node]] = [] |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
447 for v, vals in items: |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
448 orderedVars.append(v) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
449 orderedValues: List[Node] = list(vals) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
450 orderedValues.sort(key=str) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
451 orderedValueSets.append(orderedValues) |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
452 |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
453 return orderedVars, orderedValueSets |