Mercurial > code > home > repos > homeauto
comparison service/mqtt_to_rdf/inference_test.py @ 1646:af136cf6dd26
make namespaces in tests less confusing
author | drewp@bigasterisk.com |
---|---|
date | Fri, 17 Sep 2021 11:06:52 -0700 |
parents | 4bb6f593ebf3 |
children | 20474ad4968e |
comparison
equal
deleted
inserted
replaced
1645:d3b295c28a26 | 1646:af136cf6dd26 |
---|---|
21 | 21 |
22 | 22 |
23 def N3(txt: str): | 23 def N3(txt: str): |
24 g = ConjunctiveGraph() | 24 g = ConjunctiveGraph() |
25 prefix = """ | 25 prefix = """ |
26 @prefix : <http://example.com/> . | 26 @prefix : <http://projects.bigasterisk.com/room/> . |
27 @prefix ex: <http://example.com/> . | |
27 @prefix room: <http://projects.bigasterisk.com/room/> . | 28 @prefix room: <http://projects.bigasterisk.com/room/> . |
28 @prefix math: <http://www.w3.org/2000/10/swap/math#> . | 29 @prefix math: <http://www.w3.org/2000/10/swap/math#> . |
29 """ | 30 """ |
30 g.parse(StringInputSource((prefix + txt).encode('utf8')), format='n3') | 31 g.parse(StringInputSource((prefix + txt).encode('utf8')), format='n3') |
31 return g | 32 return g |
63 { :a :b :new1 . } => { :a :b :new2 . } . | 64 { :a :b :new1 . } => { :a :b :new2 . } . |
64 """) | 65 """) |
65 | 66 |
66 implied = inf.infer(N3(":a :b :c .")) | 67 implied = inf.infer(N3(":a :b :c .")) |
67 self.assertGraphEqual(implied, N3(":a :b :new1, :new2 .")) | 68 self.assertGraphEqual(implied, N3(":a :b :new1, :new2 .")) |
69 | |
70 | |
71 class TestNonRuleStatements(WithGraphEqual): | |
72 | |
73 def test(self): | |
74 inf = makeInferenceWithRules(":d :e :f . { :a :b :c . } => { :a :b :new . } .") | |
75 self.assertCountEqual(inf.nonRuleStatements(), [(ROOM.d, ROOM.e, ROOM.f)]) | |
68 | 76 |
69 | 77 |
70 class TestInferenceWithVars(WithGraphEqual): | 78 class TestInferenceWithVars(WithGraphEqual): |
71 | 79 |
72 def testVarInSubject(self): | 80 def testVarInSubject(self): |
135 def testRuleVarBindsToInputBNode(self): | 143 def testRuleVarBindsToInputBNode(self): |
136 inf = makeInferenceWithRules("{ ?z :a :b . } => { :new :stmt :here } .") | 144 inf = makeInferenceWithRules("{ ?z :a :b . } => { :new :stmt :here } .") |
137 implied = inf.infer(N3("[] :a :b .")) | 145 implied = inf.infer(N3("[] :a :b .")) |
138 self.assertGraphEqual(implied, N3(":new :stmt :here .")) | 146 self.assertGraphEqual(implied, N3(":new :stmt :here .")) |
139 | 147 |
148 | |
140 class TestBnodeGenerating(WithGraphEqual): | 149 class TestBnodeGenerating(WithGraphEqual): |
141 | 150 |
142 def testRuleBnodeMakesNewBnode(self): | 151 def testRuleBnodeMakesNewBnode(self): |
143 inf = makeInferenceWithRules("{ [ :a :b ] . } => { [ :c :d ] } .") | 152 inf = makeInferenceWithRules("{ [ :a :b ] . } => { [ :c :d ] } .") |
144 implied = inf.infer(N3("[ :a :b ] .")) | 153 implied = inf.infer(N3("[ :a :b ] .")) |
145 ruleNode = list(inf.rules[0].rhsGraph)[0] | 154 ruleNode = list(inf.rules[0].rhsGraph)[0] |
146 stmt0Node = list(implied)[0][0] | 155 stmt0Node = list(implied)[0][0] |
147 self.assertNotEqual(ruleNode, stmt0Node) | 156 self.assertNotEqual(ruleNode, stmt0Node) |
148 | 157 |
149 def testRuleBnodeMakesNewBnodesEachTime(self): | 158 def testRuleBnodeMakesNewBnodesEachTime(self): |
150 inf = makeInferenceWithRules("{ [ :a ?x ] . } => { [ :c :d ] } .") | 159 inf = makeInferenceWithRules("{ [ :a ?x ] . } => { [ :c :d ] } .") |
151 implied = inf.infer(N3("[ :a :b, :e ] .")) | 160 implied = inf.infer(N3("[ :a :b, :e ] .")) |
152 ruleNode = list(inf.rules[0].rhsGraph)[0] | 161 ruleNode = list(inf.rules[0].rhsGraph)[0] |
153 stmt0Node = list(implied)[0][0] | 162 stmt0Node = list(implied)[0][0] |
154 stmt1Node = list(implied)[1][0] | 163 stmt1Node = list(implied)[1][0] |
155 | 164 |
156 self.assertNotEqual(ruleNode, stmt0Node) | 165 self.assertNotEqual(ruleNode, stmt0Node) |
157 self.assertNotEqual(ruleNode, stmt1Node) | 166 self.assertNotEqual(ruleNode, stmt1Node) |
167 | 176 |
168 def test2(self): | 177 def test2(self): |
169 inf = makeInferenceWithRules("{ (2) math:sum ?x } => { :new :stmt ?x } .") | 178 inf = makeInferenceWithRules("{ (2) math:sum ?x } => { :new :stmt ?x } .") |
170 self.assertGraphEqual(inf.infer(N3("")), N3(":new :stmt 2 .")) | 179 self.assertGraphEqual(inf.infer(N3("")), N3(":new :stmt 2 .")) |
171 | 180 |
181 | |
172 # @unittest.skip("too hard for now") | 182 # @unittest.skip("too hard for now") |
173 # def test3(self): | 183 # def test3(self): |
174 # inf = makeInferenceWithRules("{ :a :b :c . :a :b ?x . } => { :new :stmt ?x } .") | 184 # inf = makeInferenceWithRules("{ :a :b :c . :a :b ?x . } => { :new :stmt ?x } .") |
175 # self.assertGraphEqual(inf.infer(N3("")), N3(":new :stmt :c .")) | 185 # self.assertGraphEqual(inf.infer(N3("")), N3(":new :stmt :c .")) |
176 | 186 |
177 | 187 |
178 class TestInferenceWithMathFunctions(WithGraphEqual): | 188 class TestInferenceWithMathFunctions(WithGraphEqual): |
179 | 189 |
180 def testBoolFilter(self): | 190 def testBoolFilter(self): |
225 :frontDoorLockStatus :connectedStatus ?onlineness . | 235 :frontDoorLockStatus :connectedStatus ?onlineness . |
226 } . | 236 } . |
227 ''') | 237 ''') |
228 | 238 |
229 out = inf.infer(N3('[] a :MqttMessage ; :body "online" ; :topic :foo .')) | 239 out = inf.infer(N3('[] a :MqttMessage ; :body "online" ; :topic :foo .')) |
230 self.assertIn((EX['frontDoorLockStatus'], EX['connectedStatus'], EX['Online']), out) | 240 self.assertIn((ROOM['frontDoorLockStatus'], ROOM['connectedStatus'], ROOM['Online']), out) |
231 | 241 |
232 def testTopicIsList(self): | 242 def testTopicIsList(self): |
233 inf = makeInferenceWithRules(''' | 243 inf = makeInferenceWithRules(''' |
234 { ?msg :body "online" . } => { ?msg :onlineTerm :Online . } . | 244 { ?msg :body "online" . } => { ?msg :onlineTerm :Online . } . |
235 { ?msg :body "offline" . } => { ?msg :onlineTerm :Offline . } . | 245 { ?msg :body "offline" . } => { ?msg :onlineTerm :Offline . } . |
241 :frontDoorLockStatus :connectedStatus ?onlineness . | 251 :frontDoorLockStatus :connectedStatus ?onlineness . |
242 } . | 252 } . |
243 ''') | 253 ''') |
244 | 254 |
245 out = inf.infer(N3('[] a :MqttMessage ; :body "online" ; :topic ( "frontdoorlock" "status" ) .')) | 255 out = inf.infer(N3('[] a :MqttMessage ; :body "online" ; :topic ( "frontdoorlock" "status" ) .')) |
246 self.assertIn((EX['frontDoorLockStatus'], EX['connectedStatus'], EX['Online']), out) | 256 self.assertIn((ROOM['frontDoorLockStatus'], ROOM['connectedStatus'], ROOM['Online']), out) |
247 | 257 |
248 def testPerformance0(self): | 258 def testPerformance0(self): |
249 inf = makeInferenceWithRules(''' | 259 inf = makeInferenceWithRules(''' |
250 { | 260 { |
251 ?msg a :MqttMessage; | 261 ?msg a :MqttMessage; |
263 :body "23.9" ; | 273 :body "23.9" ; |
264 :bodyFloat 2.39e+01 ; | 274 :bodyFloat 2.39e+01 ; |
265 :topic :topic1 . | 275 :topic :topic1 . |
266 ''')) | 276 ''')) |
267 | 277 |
268 vlit = cast(Literal, out.value(EX['airQualityIndoorTemperature'], EX['temperatureF'])) | 278 vlit = cast(Literal, out.value(ROOM['airQualityIndoorTemperature'], ROOM['temperatureF'])) |
269 valueF = cast(Decimal, vlit.toPython()) | 279 valueF = cast(Decimal, vlit.toPython()) |
270 self.assertAlmostEqual(float(valueF), 75.02) | 280 self.assertAlmostEqual(float(valueF), 75.02) |
271 | 281 |
272 def testPerformance1(self): | 282 def testPerformance1(self): |
273 inf = makeInferenceWithRules(''' | 283 inf = makeInferenceWithRules(''' |
286 <urn:uuid:c6e1d92c-0ee1-11ec-bdbd-2a42c4691e9a> a :MqttMessage ; | 296 <urn:uuid:c6e1d92c-0ee1-11ec-bdbd-2a42c4691e9a> a :MqttMessage ; |
287 :body "23.9" ; | 297 :body "23.9" ; |
288 :bodyFloat 2.39e+01 ; | 298 :bodyFloat 2.39e+01 ; |
289 :topic ( "air_quality_indoor" "sensor" "bme280_temperature" "state" ) . | 299 :topic ( "air_quality_indoor" "sensor" "bme280_temperature" "state" ) . |
290 ''')) | 300 ''')) |
291 vlit = cast(Literal, out.value(EX['airQualityIndoorTemperature'], EX['temperatureF'])) | 301 vlit = cast(Literal, out.value(ROOM['airQualityIndoorTemperature'], ROOM['temperatureF'])) |
292 valueF = cast(Decimal, vlit.toPython()) | 302 valueF = cast(Decimal, vlit.toPython()) |
293 self.assertAlmostEqual(float(valueF), 75.02) | 303 self.assertAlmostEqual(float(valueF), 75.02) |
294 | 304 |
295 def testEmitBnodes(self): | 305 def testEmitBnodes(self): |
296 inf = makeInferenceWithRules(''' | 306 inf = makeInferenceWithRules(''' |
302 out = inf.infer(N3(''' | 312 out = inf.infer(N3(''' |
303 :airQualityOutdoor a :AirQualitySensor; :label "air_quality_outdoor" . | 313 :airQualityOutdoor a :AirQualitySensor; :label "air_quality_outdoor" . |
304 ''')) | 314 ''')) |
305 out.bind('', ROOM) | 315 out.bind('', ROOM) |
306 out.bind('ex', EX) | 316 out.bind('ex', EX) |
307 self.assertEqual(out.serialize(format='n3'), b'''@prefix : <http://projects.bigasterisk.com/room/> . | 317 self.assertEqual( |
318 out.serialize(format='n3'), b'''\ | |
319 @prefix : <http://projects.bigasterisk.com/room/> . | |
308 @prefix ex: <http://example.com/> . | 320 @prefix ex: <http://example.com/> . |
309 @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . | 321 @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . |
310 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . | 322 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . |
311 @prefix xml: <http://www.w3.org/XML/1998/namespace> . | 323 @prefix xml: <http://www.w3.org/XML/1998/namespace> . |
312 @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . | 324 @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . |
313 | 325 |
314 [] a ex:MqttStatementSource ; | 326 [] a :MqttStatementSource ; |
315 ex:mqttTopic ( "air_quality_outdoor" "sensor" "bme280_temperature" "state" ) . | 327 :mqttTopic ( "air_quality_outdoor" "sensor" "bme280_temperature" "state" ) . |
316 | 328 |
317 ''') | 329 ''') |
318 | 330 |
319 | 331 |
320 class TestListPerformance(WithGraphEqual): | 332 class TestListPerformance(WithGraphEqual): |
340 self.assertGraphEqual(implied, N3(":new :stmt :here .")) | 352 self.assertGraphEqual(implied, N3(":new :stmt :here .")) |
341 | 353 |
342 | 354 |
343 # def fakeStats(): | 355 # def fakeStats(): |
344 # return defaultdict(lambda: 0) | 356 # return defaultdict(lambda: 0) |
345 | |
346 | 357 |
347 # class TestLhsFindCandidateBindings(WithGraphEqual): | 358 # class TestLhsFindCandidateBindings(WithGraphEqual): |
348 | 359 |
349 # def testBnodeMatchesStmt(self): | 360 # def testBnodeMatchesStmt(self): |
350 # l = Lhs(N3("[] :a :b .")) | 361 # l = Lhs(N3("[] :a :b .")) |