Mercurial > code > home > repos > homeauto
comparison service/mqtt_to_rdf/inference_test.py @ 1587:9a3a18c494f9
WIP new inferencer. no vars yet.
author | drewp@bigasterisk.com |
---|---|
date | Sun, 29 Aug 2021 23:59:09 -0700 |
parents | |
children | 0757fafbfdab |
comparison
equal
deleted
inserted
replaced
1586:b459b5c3c33a | 1587:9a3a18c494f9 |
---|---|
1 import unittest | |
2 | |
3 from rdflib import ConjunctiveGraph, Namespace, Graph | |
4 from rdflib.parser import StringInputSource | |
5 | |
6 from inference import Inference | |
7 | |
8 ROOM = Namespace('http://projects.bigasterisk.com/room/') | |
9 | |
10 | |
11 def N3(txt: str): | |
12 g = ConjunctiveGraph() | |
13 prefix = "@prefix : <http://example.com/> .\n" | |
14 g.parse(StringInputSource((prefix + txt).encode('utf8')), format='n3') | |
15 return g | |
16 | |
17 | |
18 def makeInferenceWithRules(n3): | |
19 inf = Inference() | |
20 inf.setRules(N3(n3)) | |
21 return inf | |
22 | |
23 | |
24 class WithGraphEqual(unittest.TestCase): | |
25 | |
26 def assertGraphEqual(self, g: Graph, expected: Graph): | |
27 stmts1 = list(g.triples((None, None, None))) | |
28 stmts2 = list(expected.triples((None, None, None))) | |
29 self.assertCountEqual(stmts1, stmts2) | |
30 | |
31 | |
32 class TestInferenceWithoutVars(WithGraphEqual): | |
33 | |
34 def testEmitNothing(self): | |
35 inf = makeInferenceWithRules("") | |
36 implied = inf.infer(N3(":a :b :c .")) | |
37 self.assertEqual(len(implied), 0) | |
38 | |
39 def testSimple(self): | |
40 inf = makeInferenceWithRules("{ :a :b :c . } => { :a :b :new . } .") | |
41 implied = inf.infer(N3(":a :b :c .")) | |
42 self.assertGraphEqual(implied, N3(":a :b :new .")) | |
43 | |
44 def testTwoRounds(self): | |
45 inf = makeInferenceWithRules(""" | |
46 { :a :b :c . } => { :a :b :new1 . } . | |
47 { :a :b :new1 . } => { :a :b :new2 . } . | |
48 """) | |
49 | |
50 implied = inf.infer(N3(":a :b :c .")) | |
51 self.assertGraphEqual(implied, N3(":a :b :new1, :new2 .")) | |
52 | |
53 | |
54 class TestInferenceWithVars(WithGraphEqual): | |
55 | |
56 def testVarInSubject(self): | |
57 inf = makeInferenceWithRules("{ ?x :b :c . } => { :new :stmt ?x } .") | |
58 implied = inf.infer(N3(":a :b :c .")) | |
59 self.assertGraphEqual(implied, N3(":new :stmt :a .")) | |
60 | |
61 def testVarInObject(self): | |
62 inf = makeInferenceWithRules("{ :a :b ?x . } => { :new :stmt ?x } .") | |
63 implied = inf.infer(N3(":a :b :c .")) | |
64 self.assertGraphEqual(implied, N3(":new :stmt :c .")) | |
65 | |
66 def testVarMatchesTwice(self): | |
67 inf = makeInferenceWithRules("{ :a :b ?x . } => { :new :stmt ?x } .") | |
68 implied = inf.infer(N3(":a :b :c, :d .")) | |
69 self.assertGraphEqual(implied, N3(":new :stmt :c, :d .")) | |
70 | |
71 def testTwoRulesWithVars(self): | |
72 inf = makeInferenceWithRules(""" | |
73 { :a :b ?x . } => { :new :stmt ?x } . | |
74 { ?y :stmt ?z . } => { :new :stmt2 ?z } | |
75 """) | |
76 implied = inf.infer(N3(":a :b :c .")) | |
77 self.assertGraphEqual(implied, N3(":new :stmt :c; :stmt2 :new .")) | |
78 | |
79 | |
80 class TestInferenceWithMathFunctions(WithGraphEqual): | |
81 | |
82 def test1(self): | |
83 inf = makeInferenceWithRules("{ :a :b ?x . ?x math:greaterThan 5 } => { :new :stmt ?x } .") | |
84 self.assertGraphEqual(inf.infer(N3(":a :b 3 .")), N3("")) | |
85 self.assertGraphEqual(inf.infer(N3(":a :b 5 .")), N3("")) | |
86 self.assertGraphEqual(inf.infer(N3(":a :b 6 .")), N3(":new :stmt :a .")) | |
87 | |
88 class TestInferenceWithCustomFunctions(WithGraphEqual): | |
89 | |
90 def testAsFarenheit(self): | |
91 inf = makeInferenceWithRules("{ :a :b ?x . ?x :asFarenheit ?f } => { :new :stmt ?f } .") | |
92 self.assertGraphEqual(inf.infer(N3(":a :b 0 .")), N3(":new :stmt -32 .")) |