Mercurial > code > home > repos > homeauto
annotate service/mqtt_to_rdf/inference.py @ 1597:387a9cb66517
logging adjustments
author | drewp@bigasterisk.com |
---|---|
date | Sun, 05 Sep 2021 22:39:38 -0700 |
parents | 4e795ed3a693 |
children | 9e6a593180b6 |
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 |
1594 | 7 from collections import defaultdict |
8 from dataclasses import dataclass, field | |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
9 from decimal import Decimal |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
10 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
|
11 |
1587 | 12 from prometheus_client import Summary |
1594 | 13 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
|
14 from rdflib.graph import ConjunctiveGraph, ReadOnlyGraphAggregate |
1587 | 15 from rdflib.term import Node, Variable |
16 | |
17 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
|
18 INDENT = ' ' |
1587 | 19 |
20 Triple = Tuple[Node, Node, Node] | |
21 Rule = Tuple[Graph, Node, Graph] | |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
22 BindableTerm = Union[Variable, BNode] |
1594 | 23 ReadOnlyWorkingSet = ReadOnlyGraphAggregate |
1587 | 24 |
25 READ_RULES_CALLS = Summary('read_rules_calls', 'calls') | |
26 | |
27 ROOM = Namespace("http://projects.bigasterisk.com/room/") | |
28 LOG = Namespace('http://www.w3.org/2000/10/swap/log#') | |
29 MATH = Namespace('http://www.w3.org/2000/10/swap/math#') | |
30 | |
31 | |
1594 | 32 class EvaluationFailed(ValueError): |
33 """e.g. we were given (5 math:greaterThan 6)""" | |
1587 | 34 |
35 | |
1594 | 36 @dataclass |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
37 class CandidateBinding: |
1594 | 38 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
|
39 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
40 def __repr__(self): |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
41 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
|
42 return f'CandidateBinding({b})' |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
43 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
44 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
|
45 for stmt in g: |
1594 | 46 yield (self._applyTerm(stmt[0]), self._applyTerm(stmt[1]), self._applyTerm(stmt[2])) |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
47 |
1594 | 48 def _applyTerm(self, term: Node): |
49 if isinstance(term, (Variable, BNode)): | |
50 if term in self.binding: | |
51 return self.binding[term] | |
52 return term | |
53 | |
54 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
|
55 """may grow the binding with some results""" |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
56 usedByFuncs = Graph() |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
57 while True: |
1594 | 58 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
|
59 if delta == 0: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
60 break |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
61 return usedByFuncs |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
62 |
1594 | 63 def _applyFunctionsIteration(self, lhs, usedByFuncs: Graph): |
64 before = len(self.binding) | |
65 delta = 0 | |
66 for ev in Evaluation.findEvals(lhs): | |
67 log.debug(f'{INDENT*3} found Evaluation') | |
68 | |
69 newBindings, usedGraph = ev.resultBindings(self.binding) | |
70 usedByFuncs += usedGraph | |
71 self._addNewBindings(newBindings) | |
72 delta = len(self.binding) - before | |
73 dump = "(...)" | |
74 if log.isEnabledFor(logging.DEBUG) and cast(int, usedGraph.__len__()) < 20: | |
75 dump = graphDump(usedGraph) | |
76 log.debug(f'{INDENT*4} rule {dump} made {delta} new bindings') | |
77 return delta | |
78 | |
79 def _addNewBindings(self, newBindings): | |
80 for k, v in newBindings.items(): | |
81 if k in self.binding and self.binding[k] != v: | |
82 raise ValueError(f'conflict- thought {k} would be {self.binding[k]} but another Evaluation said it should be {v}') | |
83 self.binding[k] = v | |
84 | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
85 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
|
86 """Can this lhs be true all at once in workingSet? Does it match with these bindings?""" |
1594 | 87 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
|
88 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
|
89 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
90 self.logVerifyBanner(boundLhs, workingSet, boundUsedByFuncs) |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
91 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
92 for stmt in boundLhs: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
93 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
|
94 |
1594 | 95 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
|
96 pass |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
97 elif stmt in workingSet: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
98 pass |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
99 else: |
1597 | 100 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
|
101 return False |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
102 return True |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
103 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
104 def logVerifyBanner(self, boundLhs, workingSet: ReadOnlyWorkingSet, boundUsedByFuncs): |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
105 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
|
106 return |
1597 | 107 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
|
108 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
|
109 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
|
110 |
1597 | 111 # log.debug(f'{INDENT*4}| and against this workingSet:') |
112 # for stmt in sorted(workingSet): | |
113 # 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
|
114 |
1597 | 115 stmts = sorted(boundUsedByFuncs) |
116 if stmts: | |
117 log.debug(f'{INDENT*4}| while ignoring these usedByFuncs:') | |
118 for stmt in stmts: | |
119 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
|
120 log.debug(f'{INDENT*4}\\') |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
121 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
122 |
1594 | 123 @dataclass |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
124 class Lhs: |
1594 | 125 graph: Graph |
126 | |
127 staticRuleStmts: Graph = field(default_factory=Graph) | |
128 lhsBindables: Set[BindableTerm] = field(default_factory=set) | |
129 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
|
130 |
1594 | 131 def __post_init__(self): |
132 for ruleStmt in self.graph: | |
133 varsAndBnodesInStmt = [term for term in ruleStmt if isinstance(term, (Variable, BNode))] | |
134 self.lhsBindables.update(varsAndBnodesInStmt) | |
135 self.lhsBnodes.update(x for x in varsAndBnodesInStmt if isinstance(x, BNode)) | |
136 if not varsAndBnodesInStmt: | |
137 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
|
138 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
139 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
|
140 """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
|
141 from LHS""" |
1597 | 142 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
|
143 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
144 if not self.allStaticStatementsMatch(workingSet): |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
145 return |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
146 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
147 candidateTermMatches: Dict[BindableTerm, Set[Node]] = self.allCandidateTermMatches(workingSet) |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
148 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
149 orderedVars, orderedValueSets = organize(candidateTermMatches) |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
150 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
151 self.logCandidates(orderedVars, orderedValueSets) |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
152 |
1597 | 153 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
|
154 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
155 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
|
156 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
|
157 log.debug('') |
1597 | 158 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
|
159 |
1594 | 160 try: |
161 usedByFuncs = binding.applyFunctions(self) | |
162 except EvaluationFailed: | |
163 continue | |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
164 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
165 if not binding.verify(self, workingSet, usedByFuncs): |
1597 | 166 log.debug(f'{INDENT*4} this binding did not verify') |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
167 continue |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
168 yield binding |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
169 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
170 def allStaticStatementsMatch(self, workingSet: ReadOnlyWorkingSet) -> bool: |
1594 | 171 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
|
172 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
|
173 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
|
174 return False |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
175 return True |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
176 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
177 def allCandidateTermMatches(self, workingSet: ReadOnlyWorkingSet) -> Dict[BindableTerm, Set[Node]]: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
178 """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
|
179 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
180 candidateTermMatches: Dict[BindableTerm, Set[Node]] = defaultdict(set) |
1594 | 181 for lhsStmt in self.graph: |
1597 | 182 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
|
183 for i, trueStmt in enumerate(sorted(workingSet)): |
1597 | 184 # 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
|
185 |
1594 | 186 for v, vals in self._bindingsFromStatement(lhsStmt, trueStmt): |
187 candidateTermMatches[v].update(vals) | |
188 | |
189 for trueStmt in itertools.chain(workingSet, self.graph): | |
190 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
|
191 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
|
192 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
|
193 candidateTermMatches[b].add(t) |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
194 return candidateTermMatches |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
195 |
1594 | 196 def _bindingsFromStatement(self, stmt1: Triple, stmt2: Triple) -> Iterator[Tuple[Variable, Set[Node]]]: |
197 """if these stmts match otherwise, what BNode or Variable mappings do we learn? | |
198 | |
199 e.g. stmt1=(?x B ?y) and stmt2=(A B C), then we yield (?x, {A}) and (?y, {C}) | |
200 or stmt1=(_:x B C) and stmt2=(A B C), then we yield (_:x, {A}) | |
201 or stmt1=(?x B C) and stmt2=(A B D), then we yield nothing | |
202 """ | |
203 bindingsFromStatement = {} | |
204 for term1, term2 in zip(stmt1, stmt2): | |
205 if isinstance(term1, (BNode, Variable)): | |
206 bindingsFromStatement.setdefault(term1, set()).add(term2) | |
207 elif term1 != term2: | |
208 break | |
209 else: | |
210 for v, vals in bindingsFromStatement.items(): | |
1597 | 211 log.debug(f'{INDENT*5} {v=} {vals=}') |
1594 | 212 yield v, vals |
213 | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
214 def graphWithoutEvals(self, binding: CandidateBinding) -> Graph: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
215 g = Graph() |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
216 usedByFuncs = binding.applyFunctions(self) |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
217 |
1594 | 218 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
|
219 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
|
220 g.add(stmt) |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
221 return g |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
222 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
223 def logCandidates(self, orderedVars, orderedValueSets): |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
224 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
|
225 return |
1597 | 226 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
|
227 for v, vals in zip(orderedVars, orderedValueSets): |
1597 | 228 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
|
229 for val in vals: |
1597 | 230 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
|
231 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
232 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
233 class Evaluation: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
234 """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
|
235 (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
|
236 process. It's like they already 'matched' something, so they don't need |
1594 | 237 to match a statement from the known-true working set. |
238 | |
239 One Evaluation instance is for one function call. | |
240 """ | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
241 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
242 @staticmethod |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
243 def findEvals(lhs: Lhs) -> Iterator['Evaluation']: |
1594 | 244 for stmt in lhs.graph.triples((None, MATH['sum'], None)): |
245 operands, operandsStmts = parseList(lhs.graph, stmt[0]) | |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
246 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
|
247 |
1594 | 248 for stmt in lhs.graph.triples((None, MATH['greaterThan'], None)): |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
249 yield Evaluation([stmt[0], stmt[2]], stmt, []) |
1594 | 250 |
251 for stmt in lhs.graph.triples((None, ROOM['asFarenheit'], None)): | |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
252 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
|
253 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
254 # internal, use findEvals |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
255 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
|
256 self.operands = operands |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
257 self.operandsStmts = Graph() |
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
258 self.operandsStmts += otherStmts # may grow |
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
259 self.operandsStmts.add(mainStmt) |
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
260 self.stmt = mainStmt |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
261 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
262 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
|
263 """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
|
264 pred = self.stmt[1] |
1594 | 265 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
|
266 boundOperands = [] |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
267 for op in self.operands: |
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
268 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
|
269 try: |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
270 op = inputBindings[op] |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
271 except KeyError: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
272 return {}, self.operandsStmts |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
273 |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
274 boundOperands.append(op) |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
275 |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
276 if pred == MATH['sum']: |
1594 | 277 obj = Literal(sum(map(numericNode, boundOperands))) |
278 if not isinstance(objVar, Variable): | |
279 raise TypeError(f'expected Variable, got {objVar!r}') | |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
280 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
|
281 elif pred == ROOM['asFarenheit']: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
282 if len(boundOperands) != 1: |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
283 raise ValueError(":asFarenheit takes 1 subject operand") |
1594 | 284 f = Literal(Decimal(numericNode(boundOperands[0])) * 9 / 5 + 32) |
285 if not isinstance(objVar, Variable): | |
286 raise TypeError(f'expected Variable, got {objVar!r}') | |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
287 res: Dict[BindableTerm, Node] = {objVar: f} |
1594 | 288 elif pred == MATH['greaterThan']: |
289 if not (numericNode(boundOperands[0]) > numericNode(boundOperands[1])): | |
290 raise EvaluationFailed() | |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
291 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
|
292 else: |
1594 | 293 raise NotImplementedError(repr(pred)) |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
294 |
1596
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
295 return res, self.operandsStmts |
4e795ed3a693
more cleanup, especially around Evaluation
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
296 |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
297 |
1594 | 298 def numericNode(n: Node): |
299 if not isinstance(n, Literal): | |
300 raise TypeError(f'expected Literal, got {n=}') | |
301 val = n.toPython() | |
302 if not isinstance(val, (int, float, Decimal)): | |
303 raise TypeError(f'expected number, got {val=}') | |
304 return val | |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
305 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
306 |
1587 | 307 class Inference: |
308 | |
309 def __init__(self) -> None: | |
310 self.rules = ConjunctiveGraph() | |
311 | |
312 def setRules(self, g: ConjunctiveGraph): | |
313 self.rules = g | |
314 | |
315 def infer(self, graph: Graph): | |
316 """ | |
317 returns new graph of inferred statements. | |
318 """ | |
1597 | 319 log.info(f'{INDENT*0} Begin inference of graph len={graph.__len__()} with rules len={len(self.rules)}:') |
1587 | 320 |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
321 # everything that is true: the input graph, plus every rule conclusion we can make |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
322 workingSet = Graph() |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
323 workingSet += graph |
1587 | 324 |
1594 | 325 # just the statements that came from RHS's of rules that fired. |
1587 | 326 implied = ConjunctiveGraph() |
327 | |
328 bailout_iterations = 100 | |
329 delta = 1 | |
330 while delta > 0 and bailout_iterations > 0: | |
1597 | 331 log.info(f'{INDENT*1}*iteration ({bailout_iterations} left)') |
1587 | 332 bailout_iterations -= 1 |
333 delta = -len(implied) | |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
334 self._iterateAllRules(workingSet, implied) |
1587 | 335 delta += len(implied) |
1597 | 336 log.info(f'{INDENT*2} this inference iteration added {delta} more implied stmts') |
337 log.info(f'{INDENT*0} Inference done; {len(implied)} stmts implied:') | |
1587 | 338 for st in implied: |
1597 | 339 log.info(f'{INDENT*1} {st}') |
1587 | 340 return implied |
341 | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
342 def _iterateAllRules(self, workingSet: Graph, implied: Graph): |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
343 for i, r in enumerate(self.rules): |
1594 | 344 self.logRuleApplicationHeader(workingSet, i, r) |
1587 | 345 if r[1] == LOG['implies']: |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
346 applyRule(Lhs(r[0]), r[2], workingSet, implied) |
1587 | 347 else: |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
348 log.info(f'{INDENT*2} {r} not a rule?') |
1587 | 349 |
1594 | 350 def logRuleApplicationHeader(self, workingSet, i, r): |
351 if not log.isEnabledFor(logging.DEBUG): | |
352 return | |
353 | |
354 log.debug('') | |
355 log.debug(f'{INDENT*2} workingSet:') | |
1597 | 356 for j, stmt in enumerate(sorted(workingSet)): |
357 log.debug(f'{INDENT*3} ({j}) {stmt}') | |
1594 | 358 |
359 log.debug('') | |
360 log.debug(f'{INDENT*2}-applying rule {i}') | |
361 log.debug(f'{INDENT*3} rule def lhs: {graphDump(r[0])}') | |
362 log.debug(f'{INDENT*3} rule def rhs: {graphDump(r[2])}') | |
363 | |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
364 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
365 def applyRule(lhs: Lhs, rhs: Graph, workingSet: Graph, implied: Graph): |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
366 for binding in lhs.findCandidateBindings(ReadOnlyGraphAggregate([workingSet])): |
1597 | 367 log.debug(f'{INDENT*3} rule has a working binding:') |
368 | |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
369 for lhsBoundStmt in binding.apply(lhs.graphWithoutEvals(binding)): |
1597 | 370 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
|
371 workingSet.add(lhsBoundStmt) |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1592
diff
changeset
|
372 for newStmt in binding.apply(rhs): |
1597 | 373 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
|
374 workingSet.add(newStmt) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
375 implied.add(newStmt) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
376 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
377 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
378 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
|
379 """"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
|
380 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
|
381 out = [] |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
382 used = set() |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
383 cur = subj |
1594 | 384 while cur != RDF.nil: |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
385 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
|
386 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
|
387 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
388 next = graph.value(cur, RDF.rest) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
389 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
|
390 |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
391 cur = next |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
392 return out, used |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
393 |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
394 |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
395 def graphDump(g: Union[Graph, List[Triple]]): |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
396 if not isinstance(g, Graph): |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
397 g2 = Graph() |
1594 | 398 g2 += g |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
399 g = g2 |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
400 g.bind('', ROOM) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
401 g.bind('ex', Namespace('http://example.com/')) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
402 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
|
403 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
|
404 return ' '.join(lines) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
405 |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
406 |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
407 def organize(candidateTermMatches: Dict[BindableTerm, Set[Node]]) -> Tuple[List[BindableTerm], List[List[Node]]]: |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
408 items = list(candidateTermMatches.items()) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
409 items.sort() |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
410 orderedVars: List[BindableTerm] = [] |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
411 orderedValueSets: List[List[Node]] = [] |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
412 for v, vals in items: |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
413 orderedVars.append(v) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
414 orderedValues: List[Node] = list(vals) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
415 orderedValues.sort(key=str) |
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
416 orderedValueSets.append(orderedValues) |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
417 |
1592
d7b66234064b
pure reordering of funcs to make the next diffs smaller
drewp@bigasterisk.com
parents:
1591
diff
changeset
|
418 return orderedVars, orderedValueSets |