Mercurial > code > home > repos > homeauto
annotate service/mqtt_to_rdf/inference_test.py @ 1624:91e9d4eb612a
slow test is ok now (but other slow tests are now broken)
author | drewp@bigasterisk.com |
---|---|
date | Wed, 08 Sep 2021 18:59:21 -0700 |
parents | 272f78d4671a |
children | 64f4fb8c233f |
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 """ |
1609
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
4 from collections import defaultdict |
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 |
1587 | 7 import unittest |
1594 | 8 |
9 from rdflib import RDF, BNode, ConjunctiveGraph, Graph, Literal, Namespace | |
1609
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
10 from rdflib.graph import ReadOnlyGraphAggregate |
1587 | 11 from rdflib.parser import StringInputSource |
12 | |
1609
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
13 from inference import Inference, Lhs |
1600 | 14 from rdflib_debug_patches import patchBnodeCounter, patchSlimReprs |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
15 |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
16 patchSlimReprs() |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
17 patchBnodeCounter() |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
18 |
1594 | 19 EX = Namespace('http://example.com/') |
1587 | 20 ROOM = Namespace('http://projects.bigasterisk.com/room/') |
21 | |
22 | |
23 def N3(txt: str): | |
24 g = ConjunctiveGraph() | |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
25 prefix = """ |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
26 @prefix : <http://example.com/> . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
27 @prefix room: <http://projects.bigasterisk.com/room/> . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
28 @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
|
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 | |
70 class TestInferenceWithVars(WithGraphEqual): | |
71 | |
72 def testVarInSubject(self): | |
73 inf = makeInferenceWithRules("{ ?x :b :c . } => { :new :stmt ?x } .") | |
74 implied = inf.infer(N3(":a :b :c .")) | |
75 self.assertGraphEqual(implied, N3(":new :stmt :a .")) | |
76 | |
77 def testVarInObject(self): | |
78 inf = makeInferenceWithRules("{ :a :b ?x . } => { :new :stmt ?x } .") | |
79 implied = inf.infer(N3(":a :b :c .")) | |
80 self.assertGraphEqual(implied, N3(":new :stmt :c .")) | |
81 | |
82 def testVarMatchesTwice(self): | |
83 inf = makeInferenceWithRules("{ :a :b ?x . } => { :new :stmt ?x } .") | |
84 implied = inf.infer(N3(":a :b :c, :d .")) | |
85 self.assertGraphEqual(implied, N3(":new :stmt :c, :d .")) | |
86 | |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
87 def testTwoRulesApplyIndependently(self): |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
88 inf = makeInferenceWithRules(""" |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
89 { :a :b ?x . } => { :new :stmt ?x . } . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
90 { :d :e ?y . } => { :new :stmt2 ?y . } . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
91 """) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
92 implied = inf.infer(N3(":a :b :c .")) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
93 self.assertGraphEqual(implied, N3(""" |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
94 :new :stmt :c . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
95 """)) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
96 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
|
97 self.assertGraphEqual(implied, N3(""" |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
98 :new :stmt :c . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
99 :new :stmt2 :f . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
100 """)) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
101 |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
102 def testOneRuleActivatesAnother(self): |
1587 | 103 inf = makeInferenceWithRules(""" |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
104 { :a :b ?x . } => { :new :stmt ?x . } . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
105 { ?y :stmt ?z . } => { :new :stmt2 ?y . } . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
106 """) |
1587 | 107 implied = inf.infer(N3(":a :b :c .")) |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
108 self.assertGraphEqual(implied, N3(""" |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
109 :new :stmt :c . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
110 :new :stmt2 :new . |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
111 """)) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
112 |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
113 def testVarLinksTwoStatements(self): |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
114 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
|
115 implied = inf.infer(N3(":a :b :c .")) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
116 self.assertGraphEqual(implied, N3("")) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
117 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
|
118 self.assertGraphEqual(implied, N3("")) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
119 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
|
120 self.assertGraphEqual(implied, N3(":new :stmt :c .")) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
121 |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
122 def testRuleMatchesStaticStatement(self): |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
123 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
|
124 implied = inf.infer(N3(":a :b :c .")) |
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
125 self.assertGraphEqual(implied, N3(":new :stmt :c .")) |
1587 | 126 |
127 | |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
128 class TestBnodeMatching(WithGraphEqual): |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
129 |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
130 def testRuleBnodeBindsToInputBnode(self): |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
131 inf = makeInferenceWithRules("{ [ :a :b ] . } => { :new :stmt :here } .") |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
132 implied = inf.infer(N3("[ :a :b ] .")) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
133 self.assertGraphEqual(implied, N3(":new :stmt :here .")) |
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
134 |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
135 def testRuleVarBindsToInputBNode(self): |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
136 inf = makeInferenceWithRules("{ ?z :a :b . } => { :new :stmt :here } .") |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
137 implied = inf.infer(N3("[] :a :b .")) |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
138 self.assertGraphEqual(implied, N3(":new :stmt :here .")) |
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 |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
141 class TestSelfFulfillingRule(WithGraphEqual): |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
142 |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
143 def test1(self): |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
144 inf = makeInferenceWithRules("{ } => { :new :stmt :x } .") |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
145 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
|
146 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
|
147 |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
148 def test2(self): |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
149 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
|
150 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
|
151 |
1612
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
152 @unittest.skip("too hard for now") |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
153 def test3(self): |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
154 inf = makeInferenceWithRules("{ :a :b :c . :a :b ?x . } => { :new :stmt ?x } .") |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
155 self.assertGraphEqual(inf.infer(N3("")), N3(":new :stmt :c .")) |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
156 |
1589
5c1055be3c36
WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents:
1588
diff
changeset
|
157 |
1587 | 158 class TestInferenceWithMathFunctions(WithGraphEqual): |
159 | |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
160 def testBoolFilter(self): |
1587 | 161 inf = makeInferenceWithRules("{ :a :b ?x . ?x math:greaterThan 5 } => { :new :stmt ?x } .") |
162 self.assertGraphEqual(inf.infer(N3(":a :b 3 .")), N3("")) | |
163 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
|
164 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
|
165 |
1593
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1590
diff
changeset
|
166 def testNonFiringMathRule(self): |
b0df43d5494c
big rewrite- more classes, smaller methods, more typesafe, all current tests passing
drewp@bigasterisk.com
parents:
1590
diff
changeset
|
167 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
|
168 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
|
169 |
1590
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
170 def testStatementGeneratingRule(self): |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
171 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
|
172 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
|
173 |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
174 def test3Operands(self): |
327202020892
WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents:
1589
diff
changeset
|
175 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
|
176 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
|
177 |
1594 | 178 def test0Operands(self): |
179 inf = makeInferenceWithRules("{ :a :b ?x . () math:sum ?y } => { :new :stmt ?y } .") | |
180 self.assertGraphEqual(inf.infer(N3(":a :b 2 .")), N3(":new :stmt 0 .")) | |
181 | |
1587 | 182 |
183 class TestInferenceWithCustomFunctions(WithGraphEqual): | |
184 | |
185 def testAsFarenheit(self): | |
1588
0757fafbfdab
WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents:
1587
diff
changeset
|
186 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
|
187 self.assertGraphEqual(inf.infer(N3(":a :b 12 .")), N3(":new :stmt 53.6 .")) |
1594 | 188 |
189 | |
1599
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1595
diff
changeset
|
190 class TestUseCases(WithGraphEqual): |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1595
diff
changeset
|
191 |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1595
diff
changeset
|
192 def testSimpleTopic(self): |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1595
diff
changeset
|
193 inf = makeInferenceWithRules(''' |
1606
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
194 { ?msg :body "online" . } => { ?msg :onlineTerm :Online . } . |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
195 { ?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
|
196 |
1606
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
197 { |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
198 ?msg a :MqttMessage ; |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
199 :topic :foo; |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
200 :onlineTerm ?onlineness . } => { |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
201 :frontDoorLockStatus :connectedStatus ?onlineness . |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
202 } . |
1599
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1595
diff
changeset
|
203 ''') |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1595
diff
changeset
|
204 |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1595
diff
changeset
|
205 out = inf.infer(N3('[] a :MqttMessage ; :body "online" ; :topic :foo .')) |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1595
diff
changeset
|
206 self.assertIn((EX['frontDoorLockStatus'], EX['connectedStatus'], EX['Online']), out) |
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1595
diff
changeset
|
207 |
1612
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
208 def testTopicIsList(self): |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
209 inf = makeInferenceWithRules(''' |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
210 { ?msg :body "online" . } => { ?msg :onlineTerm :Online . } . |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
211 { ?msg :body "offline" . } => { ?msg :onlineTerm :Offline . } . |
1606
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
212 |
1612
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
213 { |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
214 ?msg a :MqttMessage ; |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
215 :topic ( "frontdoorlock" "status" ); |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
216 :onlineTerm ?onlineness . } => { |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
217 :frontDoorLockStatus :connectedStatus ?onlineness . |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
218 } . |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
219 ''') |
1606
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
220 |
1612
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
221 out = inf.infer(N3('[] a :MqttMessage ; :body "online" ; :topic ( "frontdoorlock" "status" ) .')) |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
222 self.assertIn((EX['frontDoorLockStatus'], EX['connectedStatus'], EX['Online']), out) |
1606
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
223 |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
224 def testPerformance0(self): |
1599
abbf0eb0e640
fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents:
1595
diff
changeset
|
225 inf = makeInferenceWithRules(''' |
1606
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
226 { |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
227 ?msg a :MqttMessage; |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
228 :topic :topic1; |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
229 :bodyFloat ?valueC . |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
230 ?valueC math:greaterThan -999 . |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
231 ?valueC room:asFarenheit ?valueF . |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
232 } => { |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
233 :airQualityIndoorTemperature :temperatureF ?valueF . |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
234 } . |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
235 ''') |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
236 out = inf.infer( |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
237 N3(''' |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
238 <urn:uuid:c6e1d92c-0ee1-11ec-bdbd-2a42c4691e9a> a :MqttMessage ; |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
239 :body "23.9" ; |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
240 :bodyFloat 2.39e+01 ; |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
241 :topic :topic1 . |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
242 ''')) |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
243 |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
244 vlit = cast(Literal, out.value(EX['airQualityIndoorTemperature'], EX['temperatureF'])) |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
245 valueF = cast(Decimal, vlit.toPython()) |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
246 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
|
247 |
1612
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
248 @unittest.skip("still too slow") |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
249 def testPerformance1(self): |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
250 inf = makeInferenceWithRules(''' |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
251 { |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
252 ?msg a :MqttMessage; |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
253 :topic ( "air_quality_indoor" "sensor" "bme280_temperature" "state" ); |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
254 :bodyFloat ?valueC . |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
255 ?valueC math:greaterThan -999 . |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
256 ?valueC :asFarenheit ?valueF . |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
257 } => { |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
258 :airQualityIndoorTemperature :temperatureF ?valueF . |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
259 } . |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
260 ''') |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
261 out = inf.infer( |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
262 N3(''' |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
263 <urn:uuid:c6e1d92c-0ee1-11ec-bdbd-2a42c4691e9a> a :MqttMessage ; |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
264 :body "23.9" ; |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
265 :bodyFloat 2.39e+01 ; |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
266 :topic ( "air_quality_indoor" "sensor" "bme280_temperature" "state" ) . |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
267 ''')) |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
268 vlit = cast(Literal, out.value(EX['airQualityIndoorTemperature'], EX['temperatureF'])) |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
269 valueF = cast(Decimal, vlit.toPython()) |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
270 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
|
271 |
1606
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
272 |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
273 class TestListPerformance(WithGraphEqual): |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
274 |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
275 def testList1(self): |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
276 inf = makeInferenceWithRules("{ :a :b (:e0) . } => { :new :stmt :here } .") |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
277 implied = inf.infer(N3(":a :b (:e0) .")) |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
278 self.assertGraphEqual(implied, N3(":new :stmt :here .")) |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
279 |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
280 def testList2(self): |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
281 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
|
282 implied = inf.infer(N3(":a :b (:e0 :e1) .")) |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
283 self.assertGraphEqual(implied, N3(":new :stmt :here .")) |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
284 |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
285 def testList3(self): |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
286 inf = makeInferenceWithRules("{ :a :b (:e0 :e1 :e2) . } => { :new :stmt :here } .") |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
287 implied = inf.infer(N3(":a :b (:e0 :e1 :e2) .")) |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
288 self.assertGraphEqual(implied, N3(":new :stmt :here .")) |
6cf39d43fd40
realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
289 |
1612
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
290 @unittest.skip("still too slow") |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
291 def testList4(self): |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
292 inf = makeInferenceWithRules("{ :a :b (:e0 :e1 :e2 :e3) . } => { :new :stmt :here } .") |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
293 implied = inf.infer(N3(":a :b (:e0 :e1 :e2 :e3) .")) |
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
294 self.assertGraphEqual(implied, N3(":new :stmt :here .")) |
1609
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
295 |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
296 |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
297 def fakeStats(): |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
298 return defaultdict(lambda: 0) |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
299 |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
300 class TestLhsFindCandidateBindings(WithGraphEqual): |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
301 |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
302 def testBnodeMatchesStmt(self): |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
303 l = Lhs(N3("[] :a :b .")) |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
304 ws = ReadOnlyGraphAggregate([N3("[] :a :b .")]) |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
305 cands = list(l.findCandidateBindings(ws, fakeStats())) |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
306 self.assertEqual(len(cands), 1) |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
307 |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
308 def testVarMatchesStmt(self): |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
309 l = Lhs(N3("?x :a :b .")) |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
310 ws = ReadOnlyGraphAggregate([N3("[] :a :b .")]) |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
311 cands = list(l.findCandidateBindings(ws, fakeStats())) |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
312 self.assertEqual(len(cands), 1) |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
313 |
1612
272f78d4671a
mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents:
1609
diff
changeset
|
314 @unittest.skip("still too slow") |
1609
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
315 def testListsOnlyMatchEachOther(self): |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
316 l = Lhs(N3(":a :b (:e0 :e1) .")) |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
317 ws = ReadOnlyGraphAggregate([N3(":a :b (:e0 :e1) .")]) |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
318 stats = fakeStats() |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
319 cands = list(l.findCandidateBindings(ws, stats)) |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
320 self.assertLess(stats['permCountFailingVerify'], 20) |
34f2817320cc
new tests for a smaller part of the inner loop
drewp@bigasterisk.com
parents:
1606
diff
changeset
|
321 self.fail(str(cands)) |