Mercurial > code > home > repos > homeauto
comparison service/mqtt_to_rdf/inference_test.py @ 1590:327202020892
WIP inference- getting into more degenerate test cases
author | drewp@bigasterisk.com |
---|---|
date | Thu, 02 Sep 2021 23:20:55 -0700 |
parents | 5c1055be3c36 |
children | b0df43d5494c |
comparison
equal
deleted
inserted
replaced
1589:5c1055be3c36 | 1590:327202020892 |
---|---|
1 """ | 1 """ |
2 also see https://github.com/w3c/N3/tree/master/tests/N3Tests | 2 also see https://github.com/w3c/N3/tree/master/tests/N3Tests |
3 """ | 3 """ |
4 import unittest | 4 import unittest |
5 | 5 import itertools |
6 from rdflib import ConjunctiveGraph, Namespace, Graph | 6 from rdflib import ConjunctiveGraph, Namespace, Graph, BNode |
7 from rdflib.parser import StringInputSource | 7 from rdflib.parser import StringInputSource |
8 | 8 |
9 from inference import Inference | 9 from inference import Inference |
10 | |
11 | |
12 def patchSlimReprs(): | |
13 import rdflib.term | |
14 | |
15 def ur(self): | |
16 clsName = "U" if self.__class__ is rdflib.term.URIRef else self.__class__.__name__ | |
17 return """%s(%s)""" % (clsName, super(rdflib.term.URIRef, self).__repr__()) | |
18 | |
19 rdflib.term.URIRef.__repr__ = ur | |
20 | |
21 def br(self): | |
22 clsName = "BNode" if self.__class__ is rdflib.term.BNode else self.__class__.__name__ | |
23 return """%s(%s)""" % (clsName, super(rdflib.term.BNode, self).__repr__()) | |
24 | |
25 rdflib.term.BNode.__repr__ = br | |
26 | |
27 def vr(self): | |
28 clsName = "V" if self.__class__ is rdflib.term.Variable else self.__class__.__name__ | |
29 return """%s(%s)""" % (clsName, super(rdflib.term.Variable, self).__repr__()) | |
30 | |
31 rdflib.term.Variable.__repr__ = vr | |
32 | |
33 | |
34 patchSlimReprs() | |
35 | |
36 | |
37 def patchBnodeCounter(): | |
38 import rdflib.term | |
39 serial = itertools.count() | |
40 | |
41 def n(cls, value=None, _sn_gen='', _prefix='') -> BNode: | |
42 if value is None: | |
43 value = 'N-%s' % next(serial) | |
44 return rdflib.term.Identifier.__new__(cls, value) | |
45 | |
46 rdflib.term.BNode.__new__ = n | |
47 | |
48 import rdflib.plugins.parsers.notation3 | |
49 | |
50 def newBlankNode(self, uri=None, why=None): | |
51 if uri is None: | |
52 self.counter += 1 | |
53 bn = BNode('f-%s-%s' % (self.number, self.counter)) | |
54 else: | |
55 bn = BNode(uri.split('#').pop().replace('_', 'b')) | |
56 return bn | |
57 | |
58 rdflib.plugins.parsers.notation3.Formula.newBlankNode = newBlankNode | |
59 | |
60 | |
61 patchBnodeCounter() | |
10 | 62 |
11 ROOM = Namespace('http://projects.bigasterisk.com/room/') | 63 ROOM = Namespace('http://projects.bigasterisk.com/room/') |
12 | 64 |
13 | 65 |
14 def N3(txt: str): | 66 def N3(txt: str): |
115 implied = inf.infer(N3(":a :b :c .")) | 167 implied = inf.infer(N3(":a :b :c .")) |
116 self.assertGraphEqual(implied, N3(":new :stmt :c .")) | 168 self.assertGraphEqual(implied, N3(":new :stmt :c .")) |
117 | 169 |
118 | 170 |
119 class TestBnodeMatching(WithGraphEqual): | 171 class TestBnodeMatching(WithGraphEqual): |
120 def test1(self): | 172 |
173 def testRuleBnodeBindsToInputBnode(self): | |
121 inf = makeInferenceWithRules("{ [ :a :b ] . } => { :new :stmt :here } .") | 174 inf = makeInferenceWithRules("{ [ :a :b ] . } => { :new :stmt :here } .") |
122 implied = inf.infer(N3("[ :a :b ] .")) | 175 implied = inf.infer(N3("[ :a :b ] .")) |
123 self.assertGraphEqual(implied, N3(":new :stmt :here .")) | 176 self.assertGraphEqual(implied, N3(":new :stmt :here .")) |
177 | |
178 def testRuleVarBindsToInputBNode(self): | |
179 inf = makeInferenceWithRules("{ ?z :a :b . } => { :new :stmt :here } .") | |
180 implied = inf.infer(N3("[] :a :b .")) | |
181 self.assertGraphEqual(implied, N3(":new :stmt :here .")) | |
182 | |
183 | |
184 class TestSelfFulfillingRule(WithGraphEqual): | |
185 | |
186 def test1(self): | |
187 inf = makeInferenceWithRules("{ } => { :new :stmt :x } .") | |
188 self.assertGraphEqual(inf.infer(N3("")), N3(":new :stmt :x .")) | |
189 self.assertGraphEqual(inf.infer(N3(":any :any :any .")), N3(":new :stmt :x .")) | |
190 | |
191 def test2(self): | |
192 inf = makeInferenceWithRules("{ (2) math:sum ?x } => { :new :stmt ?x } .") | |
193 self.assertGraphEqual(inf.infer(N3("")), N3(":new :stmt 2 .")) | |
124 | 194 |
125 | 195 |
126 class TestInferenceWithMathFunctions(WithGraphEqual): | 196 class TestInferenceWithMathFunctions(WithGraphEqual): |
127 | 197 |
128 def testBoolFilter(self): | 198 def testBoolFilter(self): |
129 inf = makeInferenceWithRules("{ :a :b ?x . ?x math:greaterThan 5 } => { :new :stmt ?x } .") | 199 inf = makeInferenceWithRules("{ :a :b ?x . ?x math:greaterThan 5 } => { :new :stmt ?x } .") |
130 self.assertGraphEqual(inf.infer(N3(":a :b 3 .")), N3("")) | 200 self.assertGraphEqual(inf.infer(N3(":a :b 3 .")), N3("")) |
131 self.assertGraphEqual(inf.infer(N3(":a :b 5 .")), N3("")) | 201 self.assertGraphEqual(inf.infer(N3(":a :b 5 .")), N3("")) |
132 self.assertGraphEqual(inf.infer(N3(":a :b 6 .")), N3(":new :stmt 6 .")) | 202 self.assertGraphEqual(inf.infer(N3(":a :b 6 .")), N3(":new :stmt 6 .")) |
133 | 203 |
134 # def testStatementGeneratingRule(self): | 204 def testStatementGeneratingRule(self): |
135 # inf = makeInferenceWithRules("{ :a :b ?x . (?x 1) math:sum ?y } => { :new :stmt ?y } .") | 205 inf = makeInferenceWithRules("{ :a :b ?x . (?x 1) math:sum ?y } => { :new :stmt ?y } .") |
136 # self.assertGraphEqual(inf.infer(N3(":a :b 3 .")), N3(":new :stmt 4 .")) | 206 self.assertGraphEqual(inf.infer(N3(":a :b 3 .")), N3(":new :stmt 4 .")) |
207 | |
208 def test3Operands(self): | |
209 inf = makeInferenceWithRules("{ :a :b ?x . (2 ?x 2) math:sum ?y } => { :new :stmt ?y } .") | |
210 self.assertGraphEqual(inf.infer(N3(":a :b 2 .")), N3(":new :stmt 6 .")) | |
137 | 211 |
138 | 212 |
139 class TestInferenceWithCustomFunctions(WithGraphEqual): | 213 class TestInferenceWithCustomFunctions(WithGraphEqual): |
140 | 214 |
141 def testAsFarenheit(self): | 215 def testAsFarenheit(self): |