comparison service/mqtt_to_rdf/inference_test.py @ 1606:6cf39d43fd40

realign tests, turn off slow ones for now
author drewp@bigasterisk.com
date Mon, 06 Sep 2021 01:15:14 -0700
parents 449746d1598f
children 34f2817320cc
comparison
equal deleted inserted replaced
1605:449746d1598f 1606:6cf39d43fd40
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 from decimal import Decimal
5 from typing import cast
4 import unittest 6 import unittest
5 7
6 from rdflib import RDF, BNode, ConjunctiveGraph, Graph, Literal, Namespace 8 from rdflib import RDF, BNode, ConjunctiveGraph, Graph, Literal, Namespace
7 from rdflib.parser import StringInputSource 9 from rdflib.parser import StringInputSource
8 10
180 182
181 class TestUseCases(WithGraphEqual): 183 class TestUseCases(WithGraphEqual):
182 184
183 def testSimpleTopic(self): 185 def testSimpleTopic(self):
184 inf = makeInferenceWithRules(''' 186 inf = makeInferenceWithRules('''
185 { ?msg :body "online" . } => { ?msg :onlineTerm :Online . } . 187 { ?msg :body "online" . } => { ?msg :onlineTerm :Online . } .
186 { ?msg :body "offline" . } => { ?msg :onlineTerm :Offline . } . 188 { ?msg :body "offline" . } => { ?msg :onlineTerm :Offline . } .
187 189
188 { 190 {
189 ?msg a :MqttMessage ; 191 ?msg a :MqttMessage ;
190 :topic :foo; 192 :topic :foo;
191 :onlineTerm ?onlineness . } => { 193 :onlineTerm ?onlineness . } => {
192 :frontDoorLockStatus :connectedStatus ?onlineness . 194 :frontDoorLockStatus :connectedStatus ?onlineness .
193 } . 195 } .
194 ''') 196 ''')
195 197
196 out = inf.infer(N3('[] a :MqttMessage ; :body "online" ; :topic :foo .')) 198 out = inf.infer(N3('[] a :MqttMessage ; :body "online" ; :topic :foo .'))
197 self.assertIn((EX['frontDoorLockStatus'], EX['connectedStatus'], EX['Online']), out) 199 self.assertIn((EX['frontDoorLockStatus'], EX['connectedStatus'], EX['Online']), out)
198 200
199 def testTopicIsListhg(self): 201 # def testTopicIsList(self):
202 # inf = makeInferenceWithRules('''
203 # { ?msg :body "online" . } => { ?msg :onlineTerm :Online . } .
204 # { ?msg :body "offline" . } => { ?msg :onlineTerm :Offline . } .
205
206 # {
207 # ?msg a :MqttMessage ;
208 # :topic ( "frontdoorlock" "status" );
209 # :onlineTerm ?onlineness . } => {
210 # :frontDoorLockStatus :connectedStatus ?onlineness .
211 # } .
212 # ''')
213
214 # out = inf.infer(N3('[] a :MqttMessage ; :body "online" ; :topic ( "frontdoorlock" "status" ) .'))
215 # self.assertIn((EX['frontDoorLockStatus'], EX['connectedStatus'], EX['Online']), out)
216
217 def testPerformance0(self):
200 inf = makeInferenceWithRules(''' 218 inf = makeInferenceWithRules('''
201 { ?msg :body "online" . } => { ?msg :onlineTerm :Online . } . 219 {
202 { ?msg :body "offline" . } => { ?msg :onlineTerm :Offline . } . 220 ?msg a :MqttMessage;
203 221 :topic :topic1;
204 { 222 :bodyFloat ?valueC .
205 ?msg a :MqttMessage ; 223 ?valueC math:greaterThan -999 .
206 :topic ( "frontdoorlock" "status" ); 224 ?valueC room:asFarenheit ?valueF .
207 :onlineTerm ?onlineness . } => { 225 } => {
208 :frontDoorLockStatus :connectedStatus ?onlineness . 226 :airQualityIndoorTemperature :temperatureF ?valueF .
209 } . 227 } .
210 ''') 228 ''')
211 229 out = inf.infer(
212 out = inf.infer(N3('[] a :MqttMessage ; :body "online" ; :topic ( "frontdoorlock" "status" ) .')) 230 N3('''
213 self.assertIn((EX['frontDoorLockStatus'], EX['connectedStatus'], EX['Online']), out) 231 <urn:uuid:c6e1d92c-0ee1-11ec-bdbd-2a42c4691e9a> a :MqttMessage ;
232 :body "23.9" ;
233 :bodyFloat 2.39e+01 ;
234 :topic :topic1 .
235 '''))
236
237 vlit = cast(Literal, out.value(EX['airQualityIndoorTemperature'], EX['temperatureF']))
238 valueF = cast(Decimal, vlit.toPython())
239 self.assertAlmostEqual(float(valueF), 75.02)
240
241 # def testPerformance1(self):
242 # inf = makeInferenceWithRules('''
243 # {
244 # ?msg a :MqttMessage;
245 # :topic ( "air_quality_indoor" "sensor" "bme280_temperature" "state" );
246 # :bodyFloat ?valueC .
247 # ?valueC math:greaterThan -999 .
248 # ?valueC :asFarenheit ?valueF .
249 # } => {
250 # :airQualityIndoorTemperature :temperatureF ?valueF .
251 # } .
252 # ''')
253 # out = inf.infer(
254 # N3('''
255 # <urn:uuid:c6e1d92c-0ee1-11ec-bdbd-2a42c4691e9a> a :MqttMessage ;
256 # :body "23.9" ;
257 # :bodyFloat 2.39e+01 ;
258 # :topic ( "air_quality_indoor" "sensor" "bme280_temperature" "state" ) .
259 # '''))
260 # vlit = cast(Literal, out.value(EX['airQualityIndoorTemperature'], EX['temperatureF']))
261 # valueF = cast(Decimal, vlit.toPython())
262 # self.assertAlmostEqual(float(valueF), 75.02)
263
264
265 class TestListPerformance(WithGraphEqual):
266
267 def testList1(self):
268 inf = makeInferenceWithRules("{ :a :b (:e0) . } => { :new :stmt :here } .")
269 implied = inf.infer(N3(":a :b (:e0) ."))
270 self.assertGraphEqual(implied, N3(":new :stmt :here ."))
271
272 def testList2(self):
273 inf = makeInferenceWithRules("{ :a :b (:e0 :e1) . } => { :new :stmt :here } .")
274 implied = inf.infer(N3(":a :b (:e0 :e1) ."))
275 self.assertGraphEqual(implied, N3(":new :stmt :here ."))
276
277 def testList3(self):
278 inf = makeInferenceWithRules("{ :a :b (:e0 :e1 :e2) . } => { :new :stmt :here } .")
279 implied = inf.infer(N3(":a :b (:e0 :e1 :e2) ."))
280 self.assertGraphEqual(implied, N3(":new :stmt :here ."))
281
282 # def testList4(self):
283 # inf = makeInferenceWithRules("{ :a :b (:e0 :e1 :e2 :e3) . } => { :new :stmt :here } .")
284 # implied = inf.infer(N3(":a :b (:e0 :e1 :e2 :e3) ."))
285 # self.assertGraphEqual(implied, N3(":new :stmt :here ."))