Mercurial > code > home > repos > homeauto
annotate service/mqtt_to_rdf/inference_test.py @ 1696:cdf706cf5f82 master
fix a test (odometer rollover direction)
author | drewp@bigasterisk.com |
---|---|
date | Mon, 27 Sep 2021 22:56:25 -0700 |
parents | 73abfd4cf5d0 |
children |
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 """ |
1664
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
4 import unittest |
1606
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
5 from decimal import Decimal |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
6 from typing import cast |
1694
73abfd4cf5d0
new html log and other refactoring as i work on the advanceTheStack problems
drewp@bigasterisk.com
parents:
1692
diff
changeset
|
7 from pathlib import Path |
1625
64f4fb8c233f
mostly cleanup; scraps of the next rewrite; unskip tests that are ok now
drewp@bigasterisk.com
parents:
1624
diff
changeset
|
8 from rdflib import ConjunctiveGraph, Graph, Literal, Namespace |
1587 | 9 from rdflib.parser import StringInputSource |
10 | |
1664
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
11 from inference import Inference |
1600 | 12 from rdflib_debug_patches import patchBnodeCounter, patchSlimReprs |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
13 |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
14 patchSlimReprs() |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
15 patchBnodeCounter() |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
16 |
1594 | 17 EX = Namespace('http://example.com/') |
1587 | 18 ROOM = Namespace('http://projects.bigasterisk.com/room/') |
19 | |
20 | |
21 def N3(txt: str): | |
22 g = ConjunctiveGraph() | |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
23 prefix = """ |
1646
af136cf6dd26
make namespaces in tests less confusing
drewp@bigasterisk.com
parents:
1640
diff
changeset
|
24 @prefix : <http://projects.bigasterisk.com/room/> . |
af136cf6dd26
make namespaces in tests less confusing
drewp@bigasterisk.com
parents:
1640
diff
changeset
|
25 @prefix ex: <http://example.com/> . |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
26 @prefix room: <http://projects.bigasterisk.com/room/> . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
27 @prefix math: <http://www.w3.org/2000/10/swap/math#> . |
1659
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1657
diff
changeset
|
28 @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
29 """ |
1587 | 30 g.parse(StringInputSource((prefix + txt).encode('utf8')), format='n3') |
31 return g | |
32 | |
33 | |
34 def makeInferenceWithRules(n3): | |
35 inf = Inference() | |
36 inf.setRules(N3(n3)) | |
37 return inf | |
38 | |
39 | |
40 class WithGraphEqual(unittest.TestCase): | |
41 | |
42 def assertGraphEqual(self, g: Graph, expected: Graph): | |
43 stmts1 = list(g.triples((None, None, None))) | |
44 stmts2 = list(expected.triples((None, None, None))) | |
45 self.assertCountEqual(stmts1, stmts2) | |
46 | |
47 | |
48 class TestInferenceWithoutVars(WithGraphEqual): | |
49 | |
50 def testEmitNothing(self): | |
51 inf = makeInferenceWithRules("") | |
52 implied = inf.infer(N3(":a :b :c .")) | |
53 self.assertEqual(len(implied), 0) | |
54 | |
55 def testSimple(self): | |
56 inf = makeInferenceWithRules("{ :a :b :c . } => { :a :b :new . } .") | |
57 implied = inf.infer(N3(":a :b :c .")) | |
58 self.assertGraphEqual(implied, N3(":a :b :new .")) | |
59 | |
60 def testTwoRounds(self): | |
61 inf = makeInferenceWithRules(""" | |
62 { :a :b :c . } => { :a :b :new1 . } . | |
63 { :a :b :new1 . } => { :a :b :new2 . } . | |
64 """) | |
65 | |
66 implied = inf.infer(N3(":a :b :c .")) | |
67 self.assertGraphEqual(implied, N3(":a :b :new1, :new2 .")) | |
68 | |
69 | |
1646
af136cf6dd26
make namespaces in tests less confusing
drewp@bigasterisk.com
parents:
1640
diff
changeset
|
70 class TestNonRuleStatements(WithGraphEqual): |
af136cf6dd26
make namespaces in tests less confusing
drewp@bigasterisk.com
parents:
1640
diff
changeset
|
71 |
af136cf6dd26
make namespaces in tests less confusing
drewp@bigasterisk.com
parents:
1640
diff
changeset
|
72 def test(self): |
af136cf6dd26
make namespaces in tests less confusing
drewp@bigasterisk.com
parents:
1640
diff
changeset
|
73 inf = makeInferenceWithRules(":d :e :f . { :a :b :c . } => { :a :b :new . } .") |
af136cf6dd26
make namespaces in tests less confusing
drewp@bigasterisk.com
parents:
1640
diff
changeset
|
74 self.assertCountEqual(inf.nonRuleStatements(), [(ROOM.d, ROOM.e, ROOM.f)]) |
af136cf6dd26
make namespaces in tests less confusing
drewp@bigasterisk.com
parents:
1640
diff
changeset
|
75 |
af136cf6dd26
make namespaces in tests less confusing
drewp@bigasterisk.com
parents:
1640
diff
changeset
|
76 |
1587 | 77 class TestInferenceWithVars(WithGraphEqual): |
78 | |
79 def testVarInSubject(self): | |
80 inf = makeInferenceWithRules("{ ?x :b :c . } => { :new :stmt ?x } .") | |
81 implied = inf.infer(N3(":a :b :c .")) | |
82 self.assertGraphEqual(implied, N3(":new :stmt :a .")) | |
83 | |
84 def testVarInObject(self): | |
85 inf = makeInferenceWithRules("{ :a :b ?x . } => { :new :stmt ?x } .") | |
86 implied = inf.infer(N3(":a :b :c .")) | |
87 self.assertGraphEqual(implied, N3(":new :stmt :c .")) | |
88 | |
89 def testVarMatchesTwice(self): | |
90 inf = makeInferenceWithRules("{ :a :b ?x . } => { :new :stmt ?x } .") | |
91 implied = inf.infer(N3(":a :b :c, :d .")) | |
92 self.assertGraphEqual(implied, N3(":new :stmt :c, :d .")) | |
93 | |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
94 def testTwoRulesApplyIndependently(self): |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
95 inf = makeInferenceWithRules(""" |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
96 { :a :b ?x . } => { :new :stmt ?x . } . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
97 { :d :e ?y . } => { :new :stmt2 ?y . } . |
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 implied = inf.infer(N3(":a :b :c .")) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
100 self.assertGraphEqual(implied, N3(""" |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
101 :new :stmt :c . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
102 """)) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
103 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
|
104 self.assertGraphEqual(implied, N3(""" |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
105 :new :stmt :c . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
106 :new :stmt2 :f . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
107 """)) |
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 def testOneRuleActivatesAnother(self): |
1587 | 110 inf = makeInferenceWithRules(""" |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
111 { :a :b ?x . } => { :new :stmt ?x . } . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
112 { ?y :stmt ?z . } => { :new :stmt2 ?y . } . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
113 """) |
1587 | 114 implied = inf.infer(N3(":a :b :c .")) |
1588
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 :new :stmt :c . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
117 :new :stmt2 :new . |
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 |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
120 def testRuleMatchesStaticStatement(self): |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
121 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
|
122 implied = inf.infer(N3(":a :b :c .")) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
123 self.assertGraphEqual(implied, N3(":new :stmt :c .")) |
1587 | 124 |
125 | |
1672 | 126 class TestVarLinksTwoStatements(WithGraphEqual): |
127 | |
128 def setUp(self): | |
129 self.inf = makeInferenceWithRules("{ :a :b ?x . :d :e ?x } => { :new :stmt ?x } .") | |
130 | |
131 def testOnlyOneStatementPresent(self): | |
132 implied = self.inf.infer(N3(":a :b :c .")) | |
133 self.assertGraphEqual(implied, N3("")) | |
134 | |
135 def testObjectsConflict(self): | |
136 implied = self.inf.infer(N3(":a :b :c . :d :e :f .")) | |
137 self.assertGraphEqual(implied, N3("")) | |
138 | |
139 def testObjectsAgree(self): | |
140 implied = self.inf.infer(N3(":a :b :c . :d :e :c .")) | |
141 self.assertGraphEqual(implied, N3(":new :stmt :c .")) | |
142 | |
143 | |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
144 class TestBnodeMatching(WithGraphEqual): |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
145 |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
146 def testRuleBnodeBindsToInputBnode(self): |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
147 inf = makeInferenceWithRules("{ [ :a :b ] . } => { :new :stmt :here } .") |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
148 implied = inf.infer(N3("[ :a :b ] .")) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
149 self.assertGraphEqual(implied, N3(":new :stmt :here .")) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
150 |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
151 def testRuleVarBindsToInputBNode(self): |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
152 inf = makeInferenceWithRules("{ ?z :a :b . } => { :new :stmt :here } .") |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
153 implied = inf.infer(N3("[] :a :b .")) |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
154 self.assertGraphEqual(implied, N3(":new :stmt :here .")) |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
155 |
1646
af136cf6dd26
make namespaces in tests less confusing
drewp@bigasterisk.com
parents:
1640
diff
changeset
|
156 |
1664
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
157 class TestBnodeAliasingSetup(WithGraphEqual): |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
158 |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
159 def setUp(self): |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
160 self.inf = makeInferenceWithRules(""" |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
161 { |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
162 ?var0 :a ?x; :b ?y . |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
163 } => { |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
164 :xVar :value ?x . |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
165 :yVar :value ?y . |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
166 } . |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
167 """) |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
168 |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
169 def assertResult(self, actual): |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
170 self.assertGraphEqual(actual, N3(""" |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
171 :xVar :value :x0, :x1 . |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
172 :yVar :value :y0, :y1 . |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
173 """)) |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
174 |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
175 def testMatchesDistinctStatements(self): |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
176 implied = self.inf.infer(N3(""" |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
177 :stmt0 :a :x0; :b :y0 . |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
178 :stmt1 :a :x1; :b :y1 . |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
179 """)) |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
180 self.assertResult(implied) |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
181 |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
182 def testMatchesDistinctBnodes(self): |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
183 implied = self.inf.infer(N3(""" |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
184 [ :a :x0; :b :y0 ] . |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
185 [ :a :x1; :b :y1 ] . |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
186 """)) |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
187 self.assertResult(implied) |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
188 |
1673 | 189 def testProdCase(self): |
190 inf = makeInferenceWithRules(''' | |
191 { | |
1694
73abfd4cf5d0
new html log and other refactoring as i work on the advanceTheStack problems
drewp@bigasterisk.com
parents:
1692
diff
changeset
|
192 :AirQualitySensor :nameRemap [ |
73abfd4cf5d0
new html log and other refactoring as i work on the advanceTheStack problems
drewp@bigasterisk.com
parents:
1692
diff
changeset
|
193 :sensorName ?sensorName; |
73abfd4cf5d0
new html log and other refactoring as i work on the advanceTheStack problems
drewp@bigasterisk.com
parents:
1692
diff
changeset
|
194 :measurementName ?measurement |
73abfd4cf5d0
new html log and other refactoring as i work on the advanceTheStack problems
drewp@bigasterisk.com
parents:
1692
diff
changeset
|
195 ] . |
1673 | 196 } => { |
1694
73abfd4cf5d0
new html log and other refactoring as i work on the advanceTheStack problems
drewp@bigasterisk.com
parents:
1692
diff
changeset
|
197 :a :b ?sensorName. |
73abfd4cf5d0
new html log and other refactoring as i work on the advanceTheStack problems
drewp@bigasterisk.com
parents:
1692
diff
changeset
|
198 :d :e ?measurement. |
1673 | 199 } . |
200 ''') | |
201 implied = inf.infer( | |
202 N3(''' | |
203 :AirQualitySensor :nameRemap | |
204 [:sensorName "bme280_pressure"; :measurementName "pressure"], | |
205 [:sensorName "bme280_temperature"; :measurementName "temperature"] . | |
206 ''')) | |
207 | |
208 self.assertGraphEqual(implied, N3(''' | |
209 :a :b "bme280_pressure", "bme280_temperature" . | |
210 :d :e "pressure", "temperature" . | |
211 ''')) | |
212 | |
1664
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1662
diff
changeset
|
213 |
1631
2c85a4f5dd9c
big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents:
1627
diff
changeset
|
214 class TestBnodeGenerating(WithGraphEqual): |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
215 |
1631
2c85a4f5dd9c
big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents:
1627
diff
changeset
|
216 def testRuleBnodeMakesNewBnode(self): |
2c85a4f5dd9c
big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents:
1627
diff
changeset
|
217 inf = makeInferenceWithRules("{ [ :a :b ] . } => { [ :c :d ] } .") |
2c85a4f5dd9c
big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents:
1627
diff
changeset
|
218 implied = inf.infer(N3("[ :a :b ] .")) |
1646
af136cf6dd26
make namespaces in tests less confusing
drewp@bigasterisk.com
parents:
1640
diff
changeset
|
219 ruleNode = list(inf.rules[0].rhsGraph)[0] |
1631
2c85a4f5dd9c
big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents:
1627
diff
changeset
|
220 stmt0Node = list(implied)[0][0] |
2c85a4f5dd9c
big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents:
1627
diff
changeset
|
221 self.assertNotEqual(ruleNode, stmt0Node) |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
222 |
1631
2c85a4f5dd9c
big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents:
1627
diff
changeset
|
223 def testRuleBnodeMakesNewBnodesEachTime(self): |
2c85a4f5dd9c
big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents:
1627
diff
changeset
|
224 inf = makeInferenceWithRules("{ [ :a ?x ] . } => { [ :c :d ] } .") |
2c85a4f5dd9c
big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents:
1627
diff
changeset
|
225 implied = inf.infer(N3("[ :a :b, :e ] .")) |
1646
af136cf6dd26
make namespaces in tests less confusing
drewp@bigasterisk.com
parents:
1640
diff
changeset
|
226 ruleNode = list(inf.rules[0].rhsGraph)[0] |
1631
2c85a4f5dd9c
big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents:
1627
diff
changeset
|
227 stmt0Node = list(implied)[0][0] |
2c85a4f5dd9c
big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents:
1627
diff
changeset
|
228 stmt1Node = list(implied)[1][0] |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
229 |
1631
2c85a4f5dd9c
big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents:
1627
diff
changeset
|
230 self.assertNotEqual(ruleNode, stmt0Node) |
2c85a4f5dd9c
big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents:
1627
diff
changeset
|
231 self.assertNotEqual(ruleNode, stmt1Node) |
2c85a4f5dd9c
big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents:
1627
diff
changeset
|
232 self.assertNotEqual(stmt0Node, stmt1Node) |
1612
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
233 |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
234 |
1660
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
235 class TestSelfFulfillingRule(WithGraphEqual): |
1587 | 236 |
1660
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
237 def test1(self): |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
238 inf = makeInferenceWithRules("{ } => { :new :stmt :x } .") |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
239 self.assertGraphEqual(inf.infer(N3("")), N3(":new :stmt :x .")) |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
240 self.assertGraphEqual(inf.infer(N3(":any :any :any .")), N3(":new :stmt :x .")) |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1590
diff
changeset
|
241 |
1651
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1646
diff
changeset
|
242 # def test2(self): |
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1646
diff
changeset
|
243 # inf = makeInferenceWithRules("{ (2) math:sum ?x } => { :new :stmt ?x } .") |
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1646
diff
changeset
|
244 # self.assertGraphEqual(inf.infer(N3("")), N3(":new :stmt 2 .")) |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
245 |
1660
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
246 # @unittest.skip("too hard for now") |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
247 # def test3(self): |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
248 # inf = makeInferenceWithRules("{ :a :b :c . :a :b ?x . } => { :new :stmt ?x } .") |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
249 # self.assertGraphEqual(inf.infer(N3("")), N3(":new :stmt :c .")) |
1594 | 250 |
1587 | 251 |
1657 | 252 class TestInferenceWithMathFunctions(WithGraphEqual): |
1631
2c85a4f5dd9c
big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents:
1627
diff
changeset
|
253 |
1657 | 254 def testBoolFilter(self): |
255 inf = makeInferenceWithRules("{ :a :b ?x . ?x math:greaterThan 5 } => { :new :stmt ?x } .") | |
256 self.assertGraphEqual(inf.infer(N3(":a :b 3 .")), N3("")) | |
257 self.assertGraphEqual(inf.infer(N3(":a :b 5 .")), N3("")) | |
258 self.assertGraphEqual(inf.infer(N3(":a :b 6 .")), N3(":new :stmt 6 .")) | |
1631
2c85a4f5dd9c
big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents:
1627
diff
changeset
|
259 |
1660
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
260 def testNonFiringMathRule(self): |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
261 inf = makeInferenceWithRules("{ :a :b ?x . (?x 1) math:sum ?y } => { :new :stmt ?y } .") |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
262 self.assertGraphEqual(inf.infer(N3("")), N3("")) |
1587 | 263 |
1660
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
264 def testStatementGeneratingRule(self): |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
265 inf = makeInferenceWithRules("{ :a :b ?x . (?x) math:sum ?y } => { :new :stmt ?y } .") |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
266 self.assertGraphEqual(inf.infer(N3(":a :b 3 .")), N3(":new :stmt 3 .")) |
1634
ba59cfc3c747
hack math:sum in there. Test suite is passing except some slow performers
drewp@bigasterisk.com
parents:
1633
diff
changeset
|
267 |
1660
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
268 def test2Operands(self): |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
269 inf = makeInferenceWithRules("{ :a :b ?x . (?x 1) math:sum ?y } => { :new :stmt ?y } .") |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
270 self.assertGraphEqual(inf.infer(N3(":a :b 3 .")), N3(":new :stmt 4 .")) |
1631
2c85a4f5dd9c
big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents:
1627
diff
changeset
|
271 |
1660
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
272 def test3Operands(self): |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
273 inf = makeInferenceWithRules("{ :a :b ?x . (2 ?x 2) math:sum ?y } => { :new :stmt ?y } .") |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
274 self.assertGraphEqual(inf.infer(N3(":a :b 2 .")), N3(":new :stmt 6 .")) |
1631
2c85a4f5dd9c
big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents:
1627
diff
changeset
|
275 |
1660
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
276 # def test0Operands(self): |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
277 # inf = makeInferenceWithRules("{ :a :b ?x . () math:sum ?y } => { :new :stmt ?y } .") |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
278 # self.assertGraphEqual(inf.infer(N3(":a :b 2 .")), N3(":new :stmt 0 .")) |
1631
2c85a4f5dd9c
big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents:
1627
diff
changeset
|
279 |
2c85a4f5dd9c
big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents:
1627
diff
changeset
|
280 |
1657 | 281 class TestInferenceWithCustomFunctions(WithGraphEqual): |
1631
2c85a4f5dd9c
big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents:
1627
diff
changeset
|
282 |
1657 | 283 def testAsFarenheit(self): |
284 inf = makeInferenceWithRules("{ :a :b ?x . ?x room:asFarenheit ?f } => { :new :stmt ?f } .") | |
285 self.assertGraphEqual(inf.infer(N3(":a :b 12 .")), N3(":new :stmt 53.6 .")) | |
1594 | 286 |
1662
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1660
diff
changeset
|
287 def testChildResource(self): |
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1660
diff
changeset
|
288 inf = makeInferenceWithRules("{ :a :b ?x . (:c ?x) room:childResource ?y .} => { :new :stmt ?y } .") |
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1660
diff
changeset
|
289 self.assertGraphEqual(inf.infer(N3(':a :b "foo" .')), N3(":new :stmt <http://projects.bigasterisk.com/room/c/foo> .")) |
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1660
diff
changeset
|
290 |
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1660
diff
changeset
|
291 def testChildResourceSegmentQuoting(self): |
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1660
diff
changeset
|
292 inf = makeInferenceWithRules("{ :a :b ?x . (:c ?x) room:childResource ?y .} => { :new :stmt ?y } .") |
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1660
diff
changeset
|
293 self.assertGraphEqual(inf.infer(N3(':a :b "b / w -> #." .')), |
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1660
diff
changeset
|
294 N3(":new :stmt <http://projects.bigasterisk.com/room/c/b%20%2F%20w%20-%3E%20%23.> .")) |
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1660
diff
changeset
|
295 |
1594 | 296 |
1657 | 297 class TestUseCases(WithGraphEqual): |
1599
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1595
diff
changeset
|
298 |
1657 | 299 def testSimpleTopic(self): |
300 inf = makeInferenceWithRules(''' | |
301 { ?msg :body "online" . } => { ?msg :onlineTerm :Online . } . | |
302 { ?msg :body "offline" . } => { ?msg :onlineTerm :Offline . } . | |
1599
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1595
diff
changeset
|
303 |
1657 | 304 { |
305 ?msg a :MqttMessage ; | |
306 :topic :foo; | |
307 :onlineTerm ?onlineness . } => { | |
308 :frontDoorLockStatus :connectedStatus ?onlineness . | |
309 } . | |
310 ''') | |
1599
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1595
diff
changeset
|
311 |
1657 | 312 out = inf.infer(N3('[] a :MqttMessage ; :body "online" ; :topic :foo .')) |
313 self.assertIn((ROOM['frontDoorLockStatus'], ROOM['connectedStatus'], ROOM['Online']), out) | |
1599
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1595
diff
changeset
|
314 |
1660
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
315 def testTopicIsList(self): |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
316 inf = makeInferenceWithRules(''' |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
317 { ?msg :body "online" . } => { ?msg :onlineTerm :Online . } . |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
318 { ?msg :body "offline" . } => { ?msg :onlineTerm :Offline . } . |
1606
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
319 |
1660
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
320 { |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
321 ?msg a :MqttMessage ; |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
322 :topic ( "frontdoorlock" "status" ); |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
323 :onlineTerm ?onlineness . } => { |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
324 :frontDoorLockStatus :connectedStatus ?onlineness . |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
325 } . |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
326 ''') |
1606
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
327 |
1660
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
328 out = inf.infer(N3('[] a :MqttMessage ; :body "online" ; :topic ( "frontdoorlock" "status" ) .')) |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
329 self.assertIn((ROOM['frontDoorLockStatus'], ROOM['connectedStatus'], ROOM['Online']), out) |
1606
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
330 |
1660
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
331 def testPerformance0(self): |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
332 inf = makeInferenceWithRules(''' |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
333 { |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
334 ?msg a :MqttMessage; |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
335 :topic :topic1; |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
336 :bodyFloat ?valueC . |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
337 ?valueC math:greaterThan -999 . |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
338 ?valueC room:asFarenheit ?valueF . |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
339 } => { |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
340 :airQualityIndoorTemperature :temperatureF ?valueF . |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
341 } . |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
342 ''') |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
343 out = inf.infer( |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
344 N3(''' |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
345 <urn:uuid:c6e1d92c-0ee1-11ec-bdbd-2a42c4691e9a> a :MqttMessage ; |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
346 :body "23.9" ; |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
347 :bodyFloat 2.39e+01 ; |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
348 :topic :topic1 . |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
349 ''')) |
1634
ba59cfc3c747
hack math:sum in there. Test suite is passing except some slow performers
drewp@bigasterisk.com
parents:
1633
diff
changeset
|
350 |
1660
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
351 vlit = cast(Literal, out.value(ROOM['airQualityIndoorTemperature'], ROOM['temperatureF'])) |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
352 valueF = cast(Decimal, vlit.toPython()) |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
353 self.assertAlmostEqual(float(valueF), 75.02) |
1634
ba59cfc3c747
hack math:sum in there. Test suite is passing except some slow performers
drewp@bigasterisk.com
parents:
1633
diff
changeset
|
354 |
1660
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
355 def testPerformance1(self): |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
356 inf = makeInferenceWithRules(''' |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
357 { |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
358 ?msg a :MqttMessage; |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
359 :topic ( "air_quality_indoor" "sensor" "bme280_temperature" "state" ); |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
360 :bodyFloat ?valueC . |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
361 ?valueC math:greaterThan -999 . |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
362 ?valueC room:asFarenheit ?valueF . |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
363 } => { |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
364 :airQualityIndoorTemperature :temperatureF ?valueF . |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
365 } . |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
366 ''') |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
367 out = inf.infer( |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
368 N3(''' |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
369 <urn:uuid:c6e1d92c-0ee1-11ec-bdbd-2a42c4691e9a> a :MqttMessage ; |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
370 :body "23.9" ; |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
371 :bodyFloat 2.39e+01 ; |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
372 :topic ( "air_quality_indoor" "sensor" "bme280_temperature" "state" ) . |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
373 ''')) |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
374 vlit = cast(Literal, out.value(ROOM['airQualityIndoorTemperature'], ROOM['temperatureF'])) |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
375 valueF = cast(Decimal, vlit.toPython()) |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
376 self.assertAlmostEqual(float(valueF), 75.02) |
1599
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1595
diff
changeset
|
377 |
1660
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
378 def testEmitBnodes(self): |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
379 inf = makeInferenceWithRules(''' |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
380 { ?s a :AirQualitySensor; :label ?name . } => { |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
381 [ a :MqttStatementSource; |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
382 :mqttTopic (?name "sensor" "bme280_temperature" "state") ] . |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
383 } . |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
384 ''') |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
385 out = inf.infer(N3(''' |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
386 :airQualityOutdoor a :AirQualitySensor; :label "air_quality_outdoor" . |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
387 ''')) |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
388 out.bind('', ROOM) |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
389 out.bind('ex', EX) |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
390 self.assertEqual( |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
391 out.serialize(format='n3'), b'''\ |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
392 @prefix : <http://projects.bigasterisk.com/room/> . |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
393 @prefix ex: <http://example.com/> . |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
394 @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
395 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
396 @prefix xml: <http://www.w3.org/XML/1998/namespace> . |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
397 @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . |
1627
ea559a846714
some shuffling, i don't know- i'm about to rewrite again
drewp@bigasterisk.com
parents:
1625
diff
changeset
|
398 |
1660
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
399 [] a :MqttStatementSource ; |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
400 :mqttTopic ( "air_quality_outdoor" "sensor" "bme280_temperature" "state" ) . |
1631
2c85a4f5dd9c
big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents:
1627
diff
changeset
|
401 |
1660
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1659
diff
changeset
|
402 ''') |
1627
ea559a846714
some shuffling, i don't know- i'm about to rewrite again
drewp@bigasterisk.com
parents:
1625
diff
changeset
|
403 |
1677 | 404 def testRemap(self): |
405 inf = makeInferenceWithRules(''' | |
406 { | |
1692
2883da14847c
debugging and cleanup, as i looked for a bug
drewp@bigasterisk.com
parents:
1677
diff
changeset
|
407 ?sensor a :AirQualitySensor; :label ?name . |
2883da14847c
debugging and cleanup, as i looked for a bug
drewp@bigasterisk.com
parents:
1677
diff
changeset
|
408 (:mqttSource ?name) :childResource ?base . |
1677 | 409 } => { |
1692
2883da14847c
debugging and cleanup, as i looked for a bug
drewp@bigasterisk.com
parents:
1677
diff
changeset
|
410 ?sensor :statementSourceBase ?base . |
1677 | 411 } . |
412 ''') | |
413 out = inf.infer(N3(''' | |
414 :airQualityIndoor a :AirQualitySensor; :label "air_quality_indoor" . | |
415 :airQualityOutdoor a :AirQualitySensor; :label "air_quality_outdoor" . | |
1694
73abfd4cf5d0
new html log and other refactoring as i work on the advanceTheStack problems
drewp@bigasterisk.com
parents:
1692
diff
changeset
|
416 '''), Path('/tmp/log.html')) |
1677 | 417 self.assertGraphEqual(out, N3(''' |
418 :airQualityIndoor :statementSourceBase <http://projects.bigasterisk.com/room/mqttSource/air_quality_indoor> . | |
419 :airQualityOutdoor :statementSourceBase <http://projects.bigasterisk.com/room/mqttSource/air_quality_outdoor> . | |
420 ''')) | |
421 | |
1606
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
422 |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
423 class TestListPerformance(WithGraphEqual): |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
424 |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
425 def testList1(self): |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
426 inf = makeInferenceWithRules("{ :a :b (:e0) . } => { :new :stmt :here } .") |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
427 implied = inf.infer(N3(":a :b (:e0) .")) |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
428 self.assertGraphEqual(implied, N3(":new :stmt :here .")) |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
429 |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
430 def testList2(self): |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
431 inf = makeInferenceWithRules("{ :a :b (:e0 :e1) . } => { :new :stmt :here } .") |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
432 implied = inf.infer(N3(":a :b (:e0 :e1) .")) |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
433 self.assertGraphEqual(implied, N3(":new :stmt :here .")) |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
434 |
1634
ba59cfc3c747
hack math:sum in there. Test suite is passing except some slow performers
drewp@bigasterisk.com
parents:
1633
diff
changeset
|
435 def testList3(self): |
ba59cfc3c747
hack math:sum in there. Test suite is passing except some slow performers
drewp@bigasterisk.com
parents:
1633
diff
changeset
|
436 inf = makeInferenceWithRules("{ :a :b (:e0 :e1 :e2) . } => { :new :stmt :here } .") |
ba59cfc3c747
hack math:sum in there. Test suite is passing except some slow performers
drewp@bigasterisk.com
parents:
1633
diff
changeset
|
437 implied = inf.infer(N3(":a :b (:e0 :e1 :e2) .")) |
ba59cfc3c747
hack math:sum in there. Test suite is passing except some slow performers
drewp@bigasterisk.com
parents:
1633
diff
changeset
|
438 self.assertGraphEqual(implied, N3(":new :stmt :here .")) |
1606
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
439 |
1657 | 440 # def testList4(self): |
441 # inf = makeInferenceWithRules("{ :a :b (:e0 :e1 :e2 :e3) . } => { :new :stmt :here } .") | |
442 # implied = inf.infer(N3(":a :b (:e0 :e1 :e2 :e3) .")) | |
443 # self.assertGraphEqual(implied, N3(":new :stmt :here .")) |