Mercurial > code > home > repos > homeauto
comparison service/mqtt_to_rdf/inference_test.py @ 1594:e58bcfa66093
cleanups and a few fixed cases
author | drewp@bigasterisk.com |
---|---|
date | Sun, 05 Sep 2021 01:15:55 -0700 |
parents | b0df43d5494c |
children | 413a280828bf |
comparison
equal
deleted
inserted
replaced
1593:b0df43d5494c | 1594:e58bcfa66093 |
---|---|
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 itertools | |
4 import unittest | 5 import unittest |
5 import itertools | 6 |
6 from rdflib import ConjunctiveGraph, Namespace, Graph, BNode | 7 from rdflib import RDF, BNode, ConjunctiveGraph, Graph, Literal, Namespace |
7 from rdflib.parser import StringInputSource | 8 from rdflib.parser import StringInputSource |
8 | 9 |
9 from inference import Inference | 10 from inference import Inference, parseList |
10 | 11 |
11 | 12 |
12 def patchSlimReprs(): | 13 def patchSlimReprs(): |
13 import rdflib.term | 14 import rdflib.term |
14 | 15 |
58 rdflib.plugins.parsers.notation3.Formula.newBlankNode = newBlankNode | 59 rdflib.plugins.parsers.notation3.Formula.newBlankNode = newBlankNode |
59 | 60 |
60 | 61 |
61 patchBnodeCounter() | 62 patchBnodeCounter() |
62 | 63 |
64 EX = Namespace('http://example.com/') | |
63 ROOM = Namespace('http://projects.bigasterisk.com/room/') | 65 ROOM = Namespace('http://projects.bigasterisk.com/room/') |
64 | 66 |
65 | 67 |
66 def N3(txt: str): | 68 def N3(txt: str): |
67 g = ConjunctiveGraph() | 69 g = ConjunctiveGraph() |
211 | 213 |
212 def test3Operands(self): | 214 def test3Operands(self): |
213 inf = makeInferenceWithRules("{ :a :b ?x . (2 ?x 2) math:sum ?y } => { :new :stmt ?y } .") | 215 inf = makeInferenceWithRules("{ :a :b ?x . (2 ?x 2) math:sum ?y } => { :new :stmt ?y } .") |
214 self.assertGraphEqual(inf.infer(N3(":a :b 2 .")), N3(":new :stmt 6 .")) | 216 self.assertGraphEqual(inf.infer(N3(":a :b 2 .")), N3(":new :stmt 6 .")) |
215 | 217 |
218 def test0Operands(self): | |
219 inf = makeInferenceWithRules("{ :a :b ?x . () math:sum ?y } => { :new :stmt ?y } .") | |
220 self.assertGraphEqual(inf.infer(N3(":a :b 2 .")), N3(":new :stmt 0 .")) | |
221 | |
216 | 222 |
217 class TestInferenceWithCustomFunctions(WithGraphEqual): | 223 class TestInferenceWithCustomFunctions(WithGraphEqual): |
218 | 224 |
219 def testAsFarenheit(self): | 225 def testAsFarenheit(self): |
220 inf = makeInferenceWithRules("{ :a :b ?x . ?x room:asFarenheit ?f } => { :new :stmt ?f } .") | 226 inf = makeInferenceWithRules("{ :a :b ?x . ?x room:asFarenheit ?f } => { :new :stmt ?f } .") |
221 self.assertGraphEqual(inf.infer(N3(":a :b 12 .")), N3(":new :stmt 53.6 .")) | 227 self.assertGraphEqual(inf.infer(N3(":a :b 12 .")), N3(":new :stmt 53.6 .")) |
228 | |
229 | |
230 class TestParseList(unittest.TestCase): | |
231 | |
232 def test0Elements(self): | |
233 g = N3(":a :b () .") | |
234 bn = g.value(EX['a'], EX['b']) | |
235 elems, used = parseList(g, bn) | |
236 self.assertEqual(elems, []) | |
237 self.assertFalse(used) | |
238 | |
239 def test1Element(self): | |
240 g = N3(":a :b (0) .") | |
241 bn = g.value(EX['a'], EX['b']) | |
242 elems, used = parseList(g, bn) | |
243 self.assertEqual(elems, [Literal(0)]) | |
244 used = sorted(used) | |
245 self.assertEqual(used, [ | |
246 (bn, RDF.first, Literal(0)), | |
247 (bn, RDF.rest, RDF.nil), | |
248 ]) |