Mercurial > code > home > repos > homeauto
diff 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 |
line wrap: on
line diff
--- a/service/mqtt_to_rdf/inference_test.py Sun Aug 29 23:59:09 2021 -0700 +++ b/service/mqtt_to_rdf/inference_test.py Thu Sep 02 01:58:31 2021 -0700 @@ -1,3 +1,6 @@ +""" +also see https://github.com/w3c/N3/tree/master/tests/N3Tests +""" import unittest from rdflib import ConjunctiveGraph, Namespace, Graph @@ -10,7 +13,11 @@ def N3(txt: str): g = ConjunctiveGraph() - prefix = "@prefix : <http://example.com/> .\n" + prefix = """ +@prefix : <http://example.com/> . +@prefix room: <http://projects.bigasterisk.com/room/> . +@prefix math: <http://www.w3.org/2000/10/swap/math#> . +""" g.parse(StringInputSource((prefix + txt).encode('utf8')), format='n3') return g @@ -68,25 +75,62 @@ implied = inf.infer(N3(":a :b :c, :d .")) self.assertGraphEqual(implied, N3(":new :stmt :c, :d .")) - def testTwoRulesWithVars(self): + def testTwoRulesApplyIndependently(self): + inf = makeInferenceWithRules(""" + { :a :b ?x . } => { :new :stmt ?x . } . + { :d :e ?y . } => { :new :stmt2 ?y . } . + """) + implied = inf.infer(N3(":a :b :c .")) + self.assertGraphEqual(implied, N3(""" + :new :stmt :c . + """)) + implied = inf.infer(N3(":a :b :c . :d :e :f .")) + self.assertGraphEqual(implied, N3(""" + :new :stmt :c . + :new :stmt2 :f . + """)) + + def testOneRuleActivatesAnother(self): inf = makeInferenceWithRules(""" - { :a :b ?x . } => { :new :stmt ?x } . - { ?y :stmt ?z . } => { :new :stmt2 ?z } - """) + { :a :b ?x . } => { :new :stmt ?x . } . + { ?y :stmt ?z . } => { :new :stmt2 ?y . } . + """) implied = inf.infer(N3(":a :b :c .")) - self.assertGraphEqual(implied, N3(":new :stmt :c; :stmt2 :new .")) + self.assertGraphEqual(implied, N3(""" + :new :stmt :c . + :new :stmt2 :new . + """)) + + def testVarLinksTwoStatements(self): + inf = makeInferenceWithRules("{ :a :b ?x . :d :e ?x } => { :new :stmt ?x } .") + implied = inf.infer(N3(":a :b :c .")) + self.assertGraphEqual(implied, N3("")) + implied = inf.infer(N3(":a :b :c . :d :e :f .")) + self.assertGraphEqual(implied, N3("")) + implied = inf.infer(N3(":a :b :c . :d :e :c .")) + self.assertGraphEqual(implied, N3(":new :stmt :c .")) + + def testRuleMatchesStaticStatement(self): + inf = makeInferenceWithRules("{ :a :b ?x . :a :b :c . } => { :new :stmt ?x } .") + implied = inf.infer(N3(":a :b :c .")) + self.assertGraphEqual(implied, N3(":new :stmt :c .")) class TestInferenceWithMathFunctions(WithGraphEqual): - def test1(self): + def testBoolFilter(self): inf = makeInferenceWithRules("{ :a :b ?x . ?x math:greaterThan 5 } => { :new :stmt ?x } .") self.assertGraphEqual(inf.infer(N3(":a :b 3 .")), N3("")) self.assertGraphEqual(inf.infer(N3(":a :b 5 .")), N3("")) - self.assertGraphEqual(inf.infer(N3(":a :b 6 .")), N3(":new :stmt :a .")) + self.assertGraphEqual(inf.infer(N3(":a :b 6 .")), N3(":new :stmt 6 .")) + + def testStatementGeneratingRule(self): + inf = makeInferenceWithRules("{ :a :b ?x . (?x 1) math:sum ?y } => { :new :stmt ?y } .") + self.assertGraphEqual(inf.infer(N3(":a :b 3 .")), N3(":new :stmt 4 .")) + class TestInferenceWithCustomFunctions(WithGraphEqual): def testAsFarenheit(self): - inf = makeInferenceWithRules("{ :a :b ?x . ?x :asFarenheit ?f } => { :new :stmt ?f } .") - self.assertGraphEqual(inf.infer(N3(":a :b 0 .")), N3(":new :stmt -32 .")) + inf = makeInferenceWithRules("{ :a :b ?x . ?x room:asFarenheit ?f } => { :new :stmt ?f } .") + self.assertGraphEqual(inf.infer(N3(":a :b 12 .")), N3(":new :stmt 53.6 ."))