Mercurial > code > home > repos > homeauto
comparison service/mqtt_to_rdf/inference_test.py @ 1588:0757fafbfdab
WIP inferencer - partial var and function support
author | drewp@bigasterisk.com |
---|---|
date | Thu, 02 Sep 2021 01:58:31 -0700 |
parents | 9a3a18c494f9 |
children | 5c1055be3c36 |
comparison
equal
deleted
inserted
replaced
1587:9a3a18c494f9 | 1588:0757fafbfdab |
---|---|
1 """ | |
2 also see https://github.com/w3c/N3/tree/master/tests/N3Tests | |
3 """ | |
1 import unittest | 4 import unittest |
2 | 5 |
3 from rdflib import ConjunctiveGraph, Namespace, Graph | 6 from rdflib import ConjunctiveGraph, Namespace, Graph |
4 from rdflib.parser import StringInputSource | 7 from rdflib.parser import StringInputSource |
5 | 8 |
8 ROOM = Namespace('http://projects.bigasterisk.com/room/') | 11 ROOM = Namespace('http://projects.bigasterisk.com/room/') |
9 | 12 |
10 | 13 |
11 def N3(txt: str): | 14 def N3(txt: str): |
12 g = ConjunctiveGraph() | 15 g = ConjunctiveGraph() |
13 prefix = "@prefix : <http://example.com/> .\n" | 16 prefix = """ |
17 @prefix : <http://example.com/> . | |
18 @prefix room: <http://projects.bigasterisk.com/room/> . | |
19 @prefix math: <http://www.w3.org/2000/10/swap/math#> . | |
20 """ | |
14 g.parse(StringInputSource((prefix + txt).encode('utf8')), format='n3') | 21 g.parse(StringInputSource((prefix + txt).encode('utf8')), format='n3') |
15 return g | 22 return g |
16 | 23 |
17 | 24 |
18 def makeInferenceWithRules(n3): | 25 def makeInferenceWithRules(n3): |
66 def testVarMatchesTwice(self): | 73 def testVarMatchesTwice(self): |
67 inf = makeInferenceWithRules("{ :a :b ?x . } => { :new :stmt ?x } .") | 74 inf = makeInferenceWithRules("{ :a :b ?x . } => { :new :stmt ?x } .") |
68 implied = inf.infer(N3(":a :b :c, :d .")) | 75 implied = inf.infer(N3(":a :b :c, :d .")) |
69 self.assertGraphEqual(implied, N3(":new :stmt :c, :d .")) | 76 self.assertGraphEqual(implied, N3(":new :stmt :c, :d .")) |
70 | 77 |
71 def testTwoRulesWithVars(self): | 78 def testTwoRulesApplyIndependently(self): |
72 inf = makeInferenceWithRules(""" | 79 inf = makeInferenceWithRules(""" |
73 { :a :b ?x . } => { :new :stmt ?x } . | 80 { :a :b ?x . } => { :new :stmt ?x . } . |
74 { ?y :stmt ?z . } => { :new :stmt2 ?z } | 81 { :d :e ?y . } => { :new :stmt2 ?y . } . |
75 """) | 82 """) |
76 implied = inf.infer(N3(":a :b :c .")) | 83 implied = inf.infer(N3(":a :b :c .")) |
77 self.assertGraphEqual(implied, N3(":new :stmt :c; :stmt2 :new .")) | 84 self.assertGraphEqual(implied, N3(""" |
85 :new :stmt :c . | |
86 """)) | |
87 implied = inf.infer(N3(":a :b :c . :d :e :f .")) | |
88 self.assertGraphEqual(implied, N3(""" | |
89 :new :stmt :c . | |
90 :new :stmt2 :f . | |
91 """)) | |
92 | |
93 def testOneRuleActivatesAnother(self): | |
94 inf = makeInferenceWithRules(""" | |
95 { :a :b ?x . } => { :new :stmt ?x . } . | |
96 { ?y :stmt ?z . } => { :new :stmt2 ?y . } . | |
97 """) | |
98 implied = inf.infer(N3(":a :b :c .")) | |
99 self.assertGraphEqual(implied, N3(""" | |
100 :new :stmt :c . | |
101 :new :stmt2 :new . | |
102 """)) | |
103 | |
104 def testVarLinksTwoStatements(self): | |
105 inf = makeInferenceWithRules("{ :a :b ?x . :d :e ?x } => { :new :stmt ?x } .") | |
106 implied = inf.infer(N3(":a :b :c .")) | |
107 self.assertGraphEqual(implied, N3("")) | |
108 implied = inf.infer(N3(":a :b :c . :d :e :f .")) | |
109 self.assertGraphEqual(implied, N3("")) | |
110 implied = inf.infer(N3(":a :b :c . :d :e :c .")) | |
111 self.assertGraphEqual(implied, N3(":new :stmt :c .")) | |
112 | |
113 def testRuleMatchesStaticStatement(self): | |
114 inf = makeInferenceWithRules("{ :a :b ?x . :a :b :c . } => { :new :stmt ?x } .") | |
115 implied = inf.infer(N3(":a :b :c .")) | |
116 self.assertGraphEqual(implied, N3(":new :stmt :c .")) | |
78 | 117 |
79 | 118 |
80 class TestInferenceWithMathFunctions(WithGraphEqual): | 119 class TestInferenceWithMathFunctions(WithGraphEqual): |
81 | 120 |
82 def test1(self): | 121 def testBoolFilter(self): |
83 inf = makeInferenceWithRules("{ :a :b ?x . ?x math:greaterThan 5 } => { :new :stmt ?x } .") | 122 inf = makeInferenceWithRules("{ :a :b ?x . ?x math:greaterThan 5 } => { :new :stmt ?x } .") |
84 self.assertGraphEqual(inf.infer(N3(":a :b 3 .")), N3("")) | 123 self.assertGraphEqual(inf.infer(N3(":a :b 3 .")), N3("")) |
85 self.assertGraphEqual(inf.infer(N3(":a :b 5 .")), N3("")) | 124 self.assertGraphEqual(inf.infer(N3(":a :b 5 .")), N3("")) |
86 self.assertGraphEqual(inf.infer(N3(":a :b 6 .")), N3(":new :stmt :a .")) | 125 self.assertGraphEqual(inf.infer(N3(":a :b 6 .")), N3(":new :stmt 6 .")) |
126 | |
127 def testStatementGeneratingRule(self): | |
128 inf = makeInferenceWithRules("{ :a :b ?x . (?x 1) math:sum ?y } => { :new :stmt ?y } .") | |
129 self.assertGraphEqual(inf.infer(N3(":a :b 3 .")), N3(":new :stmt 4 .")) | |
130 | |
87 | 131 |
88 class TestInferenceWithCustomFunctions(WithGraphEqual): | 132 class TestInferenceWithCustomFunctions(WithGraphEqual): |
89 | 133 |
90 def testAsFarenheit(self): | 134 def testAsFarenheit(self): |
91 inf = makeInferenceWithRules("{ :a :b ?x . ?x :asFarenheit ?f } => { :new :stmt ?f } .") | 135 inf = makeInferenceWithRules("{ :a :b ?x . ?x room:asFarenheit ?f } => { :new :stmt ?f } .") |
92 self.assertGraphEqual(inf.infer(N3(":a :b 0 .")), N3(":new :stmt -32 .")) | 136 self.assertGraphEqual(inf.infer(N3(":a :b 12 .")), N3(":new :stmt 53.6 .")) |