Mercurial > code > home > repos > homeauto
annotate service/mqtt_to_rdf/inference_test.py @ 1595:413a280828bf
extract module of rdflib output patches
author | drewp@bigasterisk.com |
---|---|
date | Sun, 05 Sep 2021 01:51:49 -0700 |
parents | e58bcfa66093 |
children | abbf0eb0e640 |
rev | line source |
---|---|
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
1 """ |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
2 also see https://github.com/w3c/N3/tree/master/tests/N3Tests |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
3 """ |
1594 | 4 import itertools |
1587 | 5 import unittest |
1594 | 6 |
7 from rdflib import RDF, BNode, ConjunctiveGraph, Graph, Literal, Namespace | |
1587 | 8 from rdflib.parser import StringInputSource |
9 | |
1594 | 10 from inference import Inference, parseList |
1595
413a280828bf
extract module of rdflib output patches
drewp@bigasterisk.com
parents:
1594
diff
changeset
|
11 from rdflib_debug_patches import patchSlimReprs, patchBnodeCounter |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
12 |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
13 patchSlimReprs() |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
14 patchBnodeCounter() |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
15 |
1594 | 16 EX = Namespace('http://example.com/') |
1587 | 17 ROOM = Namespace('http://projects.bigasterisk.com/room/') |
18 | |
19 | |
20 def N3(txt: str): | |
21 g = ConjunctiveGraph() | |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
22 prefix = """ |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
23 @prefix : <http://example.com/> . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
24 @prefix room: <http://projects.bigasterisk.com/room/> . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
25 @prefix math: <http://www.w3.org/2000/10/swap/math#> . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
26 """ |
1587 | 27 g.parse(StringInputSource((prefix + txt).encode('utf8')), format='n3') |
28 return g | |
29 | |
30 | |
31 def makeInferenceWithRules(n3): | |
32 inf = Inference() | |
33 inf.setRules(N3(n3)) | |
34 return inf | |
35 | |
36 | |
37 class WithGraphEqual(unittest.TestCase): | |
38 | |
39 def assertGraphEqual(self, g: Graph, expected: Graph): | |
40 stmts1 = list(g.triples((None, None, None))) | |
41 stmts2 = list(expected.triples((None, None, None))) | |
42 self.assertCountEqual(stmts1, stmts2) | |
43 | |
44 | |
45 class TestInferenceWithoutVars(WithGraphEqual): | |
46 | |
47 def testEmitNothing(self): | |
48 inf = makeInferenceWithRules("") | |
49 implied = inf.infer(N3(":a :b :c .")) | |
50 self.assertEqual(len(implied), 0) | |
51 | |
52 def testSimple(self): | |
53 inf = makeInferenceWithRules("{ :a :b :c . } => { :a :b :new . } .") | |
54 implied = inf.infer(N3(":a :b :c .")) | |
55 self.assertGraphEqual(implied, N3(":a :b :new .")) | |
56 | |
57 def testTwoRounds(self): | |
58 inf = makeInferenceWithRules(""" | |
59 { :a :b :c . } => { :a :b :new1 . } . | |
60 { :a :b :new1 . } => { :a :b :new2 . } . | |
61 """) | |
62 | |
63 implied = inf.infer(N3(":a :b :c .")) | |
64 self.assertGraphEqual(implied, N3(":a :b :new1, :new2 .")) | |
65 | |
66 | |
67 class TestInferenceWithVars(WithGraphEqual): | |
68 | |
69 def testVarInSubject(self): | |
70 inf = makeInferenceWithRules("{ ?x :b :c . } => { :new :stmt ?x } .") | |
71 implied = inf.infer(N3(":a :b :c .")) | |
72 self.assertGraphEqual(implied, N3(":new :stmt :a .")) | |
73 | |
74 def testVarInObject(self): | |
75 inf = makeInferenceWithRules("{ :a :b ?x . } => { :new :stmt ?x } .") | |
76 implied = inf.infer(N3(":a :b :c .")) | |
77 self.assertGraphEqual(implied, N3(":new :stmt :c .")) | |
78 | |
79 def testVarMatchesTwice(self): | |
80 inf = makeInferenceWithRules("{ :a :b ?x . } => { :new :stmt ?x } .") | |
81 implied = inf.infer(N3(":a :b :c, :d .")) | |
82 self.assertGraphEqual(implied, N3(":new :stmt :c, :d .")) | |
83 | |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
84 def testTwoRulesApplyIndependently(self): |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
85 inf = makeInferenceWithRules(""" |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
86 { :a :b ?x . } => { :new :stmt ?x . } . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
87 { :d :e ?y . } => { :new :stmt2 ?y . } . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
88 """) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
89 implied = inf.infer(N3(":a :b :c .")) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
90 self.assertGraphEqual(implied, N3(""" |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
91 :new :stmt :c . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
92 """)) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
93 implied = inf.infer(N3(":a :b :c . :d :e :f .")) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
94 self.assertGraphEqual(implied, N3(""" |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
95 :new :stmt :c . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
96 :new :stmt2 :f . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
97 """)) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
98 |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
99 def testOneRuleActivatesAnother(self): |
1587 | 100 inf = makeInferenceWithRules(""" |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
101 { :a :b ?x . } => { :new :stmt ?x . } . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
102 { ?y :stmt ?z . } => { :new :stmt2 ?y . } . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
103 """) |
1587 | 104 implied = inf.infer(N3(":a :b :c .")) |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
105 self.assertGraphEqual(implied, N3(""" |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
106 :new :stmt :c . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
107 :new :stmt2 :new . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
108 """)) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
109 |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
110 def testVarLinksTwoStatements(self): |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
111 inf = makeInferenceWithRules("{ :a :b ?x . :d :e ?x } => { :new :stmt ?x } .") |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
112 implied = inf.infer(N3(":a :b :c .")) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
113 self.assertGraphEqual(implied, N3("")) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
114 implied = inf.infer(N3(":a :b :c . :d :e :f .")) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
115 self.assertGraphEqual(implied, N3("")) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
116 implied = inf.infer(N3(":a :b :c . :d :e :c .")) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
117 self.assertGraphEqual(implied, N3(":new :stmt :c .")) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
118 |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
119 def testRuleMatchesStaticStatement(self): |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
120 inf = makeInferenceWithRules("{ :a :b ?x . :a :b :c . } => { :new :stmt ?x } .") |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
121 implied = inf.infer(N3(":a :b :c .")) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
122 self.assertGraphEqual(implied, N3(":new :stmt :c .")) |
1587 | 123 |
124 | |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
125 class TestBnodeMatching(WithGraphEqual): |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
126 |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
127 def testRuleBnodeBindsToInputBnode(self): |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
128 inf = makeInferenceWithRules("{ [ :a :b ] . } => { :new :stmt :here } .") |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
129 implied = inf.infer(N3("[ :a :b ] .")) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
130 self.assertGraphEqual(implied, N3(":new :stmt :here .")) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
131 |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
132 def testRuleVarBindsToInputBNode(self): |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
133 inf = makeInferenceWithRules("{ ?z :a :b . } => { :new :stmt :here } .") |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
134 implied = inf.infer(N3("[] :a :b .")) |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
135 self.assertGraphEqual(implied, N3(":new :stmt :here .")) |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
136 |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
137 |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
138 class TestSelfFulfillingRule(WithGraphEqual): |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
139 |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
140 def test1(self): |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
141 inf = makeInferenceWithRules("{ } => { :new :stmt :x } .") |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
142 self.assertGraphEqual(inf.infer(N3("")), N3(":new :stmt :x .")) |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
143 self.assertGraphEqual(inf.infer(N3(":any :any :any .")), N3(":new :stmt :x .")) |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
144 |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
145 def test2(self): |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
146 inf = makeInferenceWithRules("{ (2) math:sum ?x } => { :new :stmt ?x } .") |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
147 self.assertGraphEqual(inf.infer(N3("")), N3(":new :stmt 2 .")) |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
148 |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
149 |
1587 | 150 class TestInferenceWithMathFunctions(WithGraphEqual): |
151 | |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
152 def testBoolFilter(self): |
1587 | 153 inf = makeInferenceWithRules("{ :a :b ?x . ?x math:greaterThan 5 } => { :new :stmt ?x } .") |
154 self.assertGraphEqual(inf.infer(N3(":a :b 3 .")), N3("")) | |
155 self.assertGraphEqual(inf.infer(N3(":a :b 5 .")), N3("")) | |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
156 self.assertGraphEqual(inf.infer(N3(":a :b 6 .")), N3(":new :stmt 6 .")) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
157 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1590
diff
changeset
|
158 def testNonFiringMathRule(self): |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1590
diff
changeset
|
159 inf = makeInferenceWithRules("{ :a :b ?x . (?x 1) math:sum ?y } => { :new :stmt ?y } .") |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1590
diff
changeset
|
160 self.assertGraphEqual(inf.infer(N3("")), N3("")) |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1590
diff
changeset
|
161 |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
162 def testStatementGeneratingRule(self): |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
163 inf = makeInferenceWithRules("{ :a :b ?x . (?x 1) math:sum ?y } => { :new :stmt ?y } .") |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
164 self.assertGraphEqual(inf.infer(N3(":a :b 3 .")), N3(":new :stmt 4 .")) |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
165 |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
166 def test3Operands(self): |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
167 inf = makeInferenceWithRules("{ :a :b ?x . (2 ?x 2) math:sum ?y } => { :new :stmt ?y } .") |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
168 self.assertGraphEqual(inf.infer(N3(":a :b 2 .")), N3(":new :stmt 6 .")) |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
169 |
1594 | 170 def test0Operands(self): |
171 inf = makeInferenceWithRules("{ :a :b ?x . () math:sum ?y } => { :new :stmt ?y } .") | |
172 self.assertGraphEqual(inf.infer(N3(":a :b 2 .")), N3(":new :stmt 0 .")) | |
173 | |
1587 | 174 |
175 class TestInferenceWithCustomFunctions(WithGraphEqual): | |
176 | |
177 def testAsFarenheit(self): | |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
178 inf = makeInferenceWithRules("{ :a :b ?x . ?x room:asFarenheit ?f } => { :new :stmt ?f } .") |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
179 self.assertGraphEqual(inf.infer(N3(":a :b 12 .")), N3(":new :stmt 53.6 .")) |
1594 | 180 |
181 | |
182 class TestParseList(unittest.TestCase): | |
183 | |
184 def test0Elements(self): | |
185 g = N3(":a :b () .") | |
186 bn = g.value(EX['a'], EX['b']) | |
187 elems, used = parseList(g, bn) | |
188 self.assertEqual(elems, []) | |
189 self.assertFalse(used) | |
190 | |
191 def test1Element(self): | |
192 g = N3(":a :b (0) .") | |
193 bn = g.value(EX['a'], EX['b']) | |
194 elems, used = parseList(g, bn) | |
195 self.assertEqual(elems, [Literal(0)]) | |
196 used = sorted(used) | |
197 self.assertEqual(used, [ | |
198 (bn, RDF.first, Literal(0)), | |
199 (bn, RDF.rest, RDF.nil), | |
200 ]) |