annotate service/mqtt_to_rdf/inference_test.py @ 1671:2b905c07e82b

try a bnode test mode improvement, but it's not so useful if you parse graphs with reused bnodes in them
author drewp@bigasterisk.com
date Tue, 21 Sep 2021 23:21:38 -0700
parents 4fd9fdfcf16a
children 23beadd3e83a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
1594
e58bcfa66093 cleanups and a few fixed cases
drewp@bigasterisk.com
parents: 1593
diff changeset
7
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
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
9 from rdflib.parser import StringInputSource
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
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
89a50242cb5e cleanup internal names, imports
drewp@bigasterisk.com
parents: 1599
diff changeset
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
e58bcfa66093 cleanups and a few fixed cases
drewp@bigasterisk.com
parents: 1593
diff changeset
17 EX = Namespace('http://example.com/')
1587
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
18 ROOM = Namespace('http://projects.bigasterisk.com/room/')
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
19
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
20
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
21 def N3(txt: str):
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
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
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
30 g.parse(StringInputSource((prefix + txt).encode('utf8')), format='n3')
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
31 return g
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
32
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
33
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
34 def makeInferenceWithRules(n3):
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
35 inf = Inference()
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
36 inf.setRules(N3(n3))
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
37 return inf
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
38
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
39
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
40 class WithGraphEqual(unittest.TestCase):
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
41
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
42 def assertGraphEqual(self, g: Graph, expected: Graph):
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
43 stmts1 = list(g.triples((None, None, None)))
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
44 stmts2 = list(expected.triples((None, None, None)))
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
45 self.assertCountEqual(stmts1, stmts2)
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
46
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
47
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
48 class TestInferenceWithoutVars(WithGraphEqual):
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
49
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
50 def testEmitNothing(self):
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
51 inf = makeInferenceWithRules("")
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
52 implied = inf.infer(N3(":a :b :c ."))
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
53 self.assertEqual(len(implied), 0)
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
54
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
55 def testSimple(self):
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
56 inf = makeInferenceWithRules("{ :a :b :c . } => { :a :b :new . } .")
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
57 implied = inf.infer(N3(":a :b :c ."))
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
58 self.assertGraphEqual(implied, N3(":a :b :new ."))
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
59
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
60 def testTwoRounds(self):
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
61 inf = makeInferenceWithRules("""
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
62 { :a :b :c . } => { :a :b :new1 . } .
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
63 { :a :b :new1 . } => { :a :b :new2 . } .
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
64 """)
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
65
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
66 implied = inf.infer(N3(":a :b :c ."))
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
67 self.assertGraphEqual(implied, N3(":a :b :new1, :new2 ."))
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
68
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
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
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
77 class TestInferenceWithVars(WithGraphEqual):
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
78
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
79 def testVarInSubject(self):
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
80 inf = makeInferenceWithRules("{ ?x :b :c . } => { :new :stmt ?x } .")
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
81 implied = inf.infer(N3(":a :b :c ."))
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
82 self.assertGraphEqual(implied, N3(":new :stmt :a ."))
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
83
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
84 def testVarInObject(self):
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
85 inf = makeInferenceWithRules("{ :a :b ?x . } => { :new :stmt ?x } .")
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
86 implied = inf.infer(N3(":a :b :c ."))
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
87 self.assertGraphEqual(implied, N3(":new :stmt :c ."))
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
88
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
89 def testVarMatchesTwice(self):
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
90 inf = makeInferenceWithRules("{ :a :b ?x . } => { :new :stmt ?x } .")
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
91 implied = inf.infer(N3(":a :b :c, :d ."))
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
92 self.assertGraphEqual(implied, N3(":new :stmt :c, :d ."))
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
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
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
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
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
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 testVarLinksTwoStatements(self):
0757fafbfdab WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents: 1587
diff changeset
121 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
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(""))
0757fafbfdab WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents: 1587
diff changeset
124 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
125 self.assertGraphEqual(implied, N3(""))
0757fafbfdab WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents: 1587
diff changeset
126 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
127 self.assertGraphEqual(implied, N3(":new :stmt :c ."))
0757fafbfdab WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents: 1587
diff changeset
128
0757fafbfdab WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents: 1587
diff changeset
129 def testRuleMatchesStaticStatement(self):
0757fafbfdab WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents: 1587
diff changeset
130 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
131 implied = inf.infer(N3(":a :b :c ."))
0757fafbfdab WIP inferencer - partial var and function support
drewp@bigasterisk.com
parents: 1587
diff changeset
132 self.assertGraphEqual(implied, N3(":new :stmt :c ."))
1587
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
133
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
134
1589
5c1055be3c36 WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents: 1588
diff changeset
135 class TestBnodeMatching(WithGraphEqual):
1590
327202020892 WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents: 1589
diff changeset
136
327202020892 WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents: 1589
diff changeset
137 def testRuleBnodeBindsToInputBnode(self):
1589
5c1055be3c36 WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents: 1588
diff changeset
138 inf = makeInferenceWithRules("{ [ :a :b ] . } => { :new :stmt :here } .")
5c1055be3c36 WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents: 1588
diff changeset
139 implied = inf.infer(N3("[ :a :b ] ."))
5c1055be3c36 WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents: 1588
diff changeset
140 self.assertGraphEqual(implied, N3(":new :stmt :here ."))
5c1055be3c36 WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents: 1588
diff changeset
141
1590
327202020892 WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents: 1589
diff changeset
142 def testRuleVarBindsToInputBNode(self):
327202020892 WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents: 1589
diff changeset
143 inf = makeInferenceWithRules("{ ?z :a :b . } => { :new :stmt :here } .")
327202020892 WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents: 1589
diff changeset
144 implied = inf.infer(N3("[] :a :b ."))
327202020892 WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents: 1589
diff changeset
145 self.assertGraphEqual(implied, N3(":new :stmt :here ."))
327202020892 WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents: 1589
diff changeset
146
1646
af136cf6dd26 make namespaces in tests less confusing
drewp@bigasterisk.com
parents: 1640
diff changeset
147
1664
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
148 class TestBnodeAliasingSetup(WithGraphEqual):
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
149
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
150 def setUp(self):
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
151 self.inf = makeInferenceWithRules("""
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
152 {
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
153 ?var0 :a ?x; :b ?y .
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
154 } => {
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
155 :xVar :value ?x .
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
156 :yVar :value ?y .
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
157 } .
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
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
160 def assertResult(self, actual):
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
161 self.assertGraphEqual(actual, N3("""
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
162 :xVar :value :x0, :x1 .
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
163 :yVar :value :y0, :y1 .
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
164 """))
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
165
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
166 def testMatchesDistinctStatements(self):
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
167 implied = self.inf.infer(N3("""
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
168 :stmt0 :a :x0; :b :y0 .
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
169 :stmt1 :a :x1; :b :y1 .
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
170 """))
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
171 self.assertResult(implied)
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
172
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
173 def testMatchesDistinctBnodes(self):
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
174 implied = self.inf.infer(N3("""
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
175 [ :a :x0; :b :y0 ] .
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
176 [ :a :x1; :b :y1 ] .
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
177 """))
1a7c1261302c logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents: 1662
diff changeset
178 self.assertResult(implied)
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
1631
2c85a4f5dd9c big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents: 1627
diff changeset
181 class TestBnodeGenerating(WithGraphEqual):
1590
327202020892 WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents: 1589
diff changeset
182
1631
2c85a4f5dd9c big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents: 1627
diff changeset
183 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
184 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
185 implied = inf.infer(N3("[ :a :b ] ."))
1646
af136cf6dd26 make namespaces in tests less confusing
drewp@bigasterisk.com
parents: 1640
diff changeset
186 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
187 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
188 self.assertNotEqual(ruleNode, stmt0Node)
1590
327202020892 WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents: 1589
diff changeset
189
1631
2c85a4f5dd9c big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents: 1627
diff changeset
190 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
191 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
192 implied = inf.infer(N3("[ :a :b, :e ] ."))
1646
af136cf6dd26 make namespaces in tests less confusing
drewp@bigasterisk.com
parents: 1640
diff changeset
193 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
194 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
195 stmt1Node = list(implied)[1][0]
1590
327202020892 WIP inference- getting into more degenerate test cases
drewp@bigasterisk.com
parents: 1589
diff changeset
196
1631
2c85a4f5dd9c big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents: 1627
diff changeset
197 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
198 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
199 self.assertNotEqual(stmt0Node, stmt1Node)
1612
272f78d4671a mark skipped tests. move applyRule into Rule. minor cleanups.
drewp@bigasterisk.com
parents: 1609
diff changeset
200
1589
5c1055be3c36 WIP more debugging, working towards bnode-matching support
drewp@bigasterisk.com
parents: 1588
diff changeset
201
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
202 class TestSelfFulfillingRule(WithGraphEqual):
1587
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
203
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
204 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
205 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
206 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
207 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
208
1651
20474ad4968e WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents: 1646
diff changeset
209 # 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
210 # 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
211 # 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
212
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
213 # @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
214 # 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
215 # 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
216 # self.assertGraphEqual(inf.infer(N3("")), N3(":new :stmt :c ."))
1594
e58bcfa66093 cleanups and a few fixed cases
drewp@bigasterisk.com
parents: 1593
diff changeset
217
1587
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
218
1657
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
219 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
220
1657
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
221 def testBoolFilter(self):
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
222 inf = makeInferenceWithRules("{ :a :b ?x . ?x math:greaterThan 5 } => { :new :stmt ?x } .")
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
223 self.assertGraphEqual(inf.infer(N3(":a :b 3 .")), N3(""))
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
224 self.assertGraphEqual(inf.infer(N3(":a :b 5 .")), N3(""))
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
225 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
226
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
227 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
228 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
229 self.assertGraphEqual(inf.infer(N3("")), N3(""))
1587
9a3a18c494f9 WIP new inferencer. no vars yet.
drewp@bigasterisk.com
parents:
diff changeset
230
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
231 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
232 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
233 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
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 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
236 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
237 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
238
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
239 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
240 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
241 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
242
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
243 # 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
244 # 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
245 # 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
246
2c85a4f5dd9c big rewrite of infer() using statements not variables as the things to iterate over
drewp@bigasterisk.com
parents: 1627
diff changeset
247
1657
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
248 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
249
1657
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
250 def testAsFarenheit(self):
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
251 inf = makeInferenceWithRules("{ :a :b ?x . ?x room:asFarenheit ?f } => { :new :stmt ?f } .")
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
252 self.assertGraphEqual(inf.infer(N3(":a :b 12 .")), N3(":new :stmt 53.6 ."))
1594
e58bcfa66093 cleanups and a few fixed cases
drewp@bigasterisk.com
parents: 1593
diff changeset
253
1662
7a61113fd17d new childResource function for making new URIs
drewp@bigasterisk.com
parents: 1660
diff changeset
254 def testChildResource(self):
7a61113fd17d new childResource function for making new URIs
drewp@bigasterisk.com
parents: 1660
diff changeset
255 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
256 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
257
7a61113fd17d new childResource function for making new URIs
drewp@bigasterisk.com
parents: 1660
diff changeset
258 def testChildResourceSegmentQuoting(self):
7a61113fd17d new childResource function for making new URIs
drewp@bigasterisk.com
parents: 1660
diff changeset
259 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
260 self.assertGraphEqual(inf.infer(N3(':a :b "b / w -> #." .')),
7a61113fd17d new childResource function for making new URIs
drewp@bigasterisk.com
parents: 1660
diff changeset
261 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
262
1594
e58bcfa66093 cleanups and a few fixed cases
drewp@bigasterisk.com
parents: 1593
diff changeset
263
1657
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
264 class TestUseCases(WithGraphEqual):
1599
abbf0eb0e640 fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents: 1595
diff changeset
265
1657
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
266 def testSimpleTopic(self):
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
267 inf = makeInferenceWithRules('''
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
268 { ?msg :body "online" . } => { ?msg :onlineTerm :Online . } .
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
269 { ?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
270
1657
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
271 {
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
272 ?msg a :MqttMessage ;
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
273 :topic :foo;
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
274 :onlineTerm ?onlineness . } => {
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
275 :frontDoorLockStatus :connectedStatus ?onlineness .
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
276 } .
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
277 ''')
1599
abbf0eb0e640 fix a bug with a slightly moer complicated set of rules
drewp@bigasterisk.com
parents: 1595
diff changeset
278
1657
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
279 out = inf.infer(N3('[] a :MqttMessage ; :body "online" ; :topic :foo .'))
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
280 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
281
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
282 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
283 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
284 { ?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
285 { ?msg :body "offline" . } => { ?msg :onlineTerm :Offline . } .
1606
6cf39d43fd40 realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents: 1605
diff changeset
286
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
287 {
31f7dab6a60b function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents: 1659
diff changeset
288 ?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
289 :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
290 :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
291 :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
292 } .
31f7dab6a60b function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents: 1659
diff changeset
293 ''')
1606
6cf39d43fd40 realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents: 1605
diff changeset
294
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
295 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
296 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
297
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
298 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
299 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
300 {
31f7dab6a60b function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents: 1659
diff changeset
301 ?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
302 :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
303 :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
304 ?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
305 ?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
306 } => {
31f7dab6a60b function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents: 1659
diff changeset
307 :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
308 } .
31f7dab6a60b function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents: 1659
diff changeset
309 ''')
31f7dab6a60b function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents: 1659
diff changeset
310 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
311 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
312 <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
313 :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
314 :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
315 :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
316 '''))
1634
ba59cfc3c747 hack math:sum in there. Test suite is passing except some slow performers
drewp@bigasterisk.com
parents: 1633
diff changeset
317
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
318 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
319 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
320 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
321
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
322 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
323 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
324 {
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 ?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
326 :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
327 :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
328 ?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
329 ?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
330 } => {
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 :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
332 } .
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 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
335 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
336 <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
337 :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
338 :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
339 :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
340 '''))
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 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
342 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
343 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
344
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
345 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
346 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
347 { ?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
348 [ 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
349 :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
350 } .
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 ''')
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 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
353 :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
354 '''))
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 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
356 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
357 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
358 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
359 @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
360 @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
361 @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
362 @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
363 @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
364 @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
365
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
366 [] 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
367 :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
368
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
369 ''')
1627
ea559a846714 some shuffling, i don't know- i'm about to rewrite again
drewp@bigasterisk.com
parents: 1625
diff changeset
370
1606
6cf39d43fd40 realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents: 1605
diff changeset
371
6cf39d43fd40 realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents: 1605
diff changeset
372 class TestListPerformance(WithGraphEqual):
6cf39d43fd40 realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents: 1605
diff changeset
373
6cf39d43fd40 realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents: 1605
diff changeset
374 def testList1(self):
6cf39d43fd40 realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents: 1605
diff changeset
375 inf = makeInferenceWithRules("{ :a :b (:e0) . } => { :new :stmt :here } .")
6cf39d43fd40 realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents: 1605
diff changeset
376 implied = inf.infer(N3(":a :b (:e0) ."))
6cf39d43fd40 realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents: 1605
diff changeset
377 self.assertGraphEqual(implied, N3(":new :stmt :here ."))
6cf39d43fd40 realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents: 1605
diff changeset
378
6cf39d43fd40 realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents: 1605
diff changeset
379 def testList2(self):
6cf39d43fd40 realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents: 1605
diff changeset
380 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
381 implied = inf.infer(N3(":a :b (:e0 :e1) ."))
6cf39d43fd40 realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents: 1605
diff changeset
382 self.assertGraphEqual(implied, N3(":new :stmt :here ."))
6cf39d43fd40 realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents: 1605
diff changeset
383
1634
ba59cfc3c747 hack math:sum in there. Test suite is passing except some slow performers
drewp@bigasterisk.com
parents: 1633
diff changeset
384 def testList3(self):
ba59cfc3c747 hack math:sum in there. Test suite is passing except some slow performers
drewp@bigasterisk.com
parents: 1633
diff changeset
385 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
386 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
387 self.assertGraphEqual(implied, N3(":new :stmt :here ."))
1606
6cf39d43fd40 realign tests, turn off slow ones for now
drewp@bigasterisk.com
parents: 1605
diff changeset
388
1657
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
389 # def testList4(self):
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
390 # inf = makeInferenceWithRules("{ :a :b (:e0 :e1 :e2 :e3) . } => { :new :stmt :here } .")
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
391 # implied = inf.infer(N3(":a :b (:e0 :e1 :e2 :e3) ."))
274bb6c04627 update currently-working tests
drewp@bigasterisk.com
parents: 1651
diff changeset
392 # self.assertGraphEqual(implied, N3(":new :stmt :here ."))