comparison service/mqtt_to_rdf/inference_test.py @ 1612:272f78d4671a

mark skipped tests. move applyRule into Rule. minor cleanups.
author drewp@bigasterisk.com
date Mon, 06 Sep 2021 18:39:38 -0700
parents 34f2817320cc
children 91e9d4eb612a
comparison
equal deleted inserted replaced
1611:a794a150a89b 1612:272f78d4671a
147 147
148 def test2(self): 148 def test2(self):
149 inf = makeInferenceWithRules("{ (2) math:sum ?x } => { :new :stmt ?x } .") 149 inf = makeInferenceWithRules("{ (2) math:sum ?x } => { :new :stmt ?x } .")
150 self.assertGraphEqual(inf.infer(N3("")), N3(":new :stmt 2 .")) 150 self.assertGraphEqual(inf.infer(N3("")), N3(":new :stmt 2 ."))
151 151
152 @unittest.skip("too hard for now")
153 def test3(self):
154 inf = makeInferenceWithRules("{ :a :b :c . :a :b ?x . } => { :new :stmt ?x } .")
155 self.assertGraphEqual(inf.infer(N3("")), N3(":new :stmt :c ."))
156
152 157
153 class TestInferenceWithMathFunctions(WithGraphEqual): 158 class TestInferenceWithMathFunctions(WithGraphEqual):
154 159
155 def testBoolFilter(self): 160 def testBoolFilter(self):
156 inf = makeInferenceWithRules("{ :a :b ?x . ?x math:greaterThan 5 } => { :new :stmt ?x } .") 161 inf = makeInferenceWithRules("{ :a :b ?x . ?x math:greaterThan 5 } => { :new :stmt ?x } .")
198 ''') 203 ''')
199 204
200 out = inf.infer(N3('[] a :MqttMessage ; :body "online" ; :topic :foo .')) 205 out = inf.infer(N3('[] a :MqttMessage ; :body "online" ; :topic :foo .'))
201 self.assertIn((EX['frontDoorLockStatus'], EX['connectedStatus'], EX['Online']), out) 206 self.assertIn((EX['frontDoorLockStatus'], EX['connectedStatus'], EX['Online']), out)
202 207
203 # def testTopicIsList(self): 208 @unittest.skip("still too slow")
204 # inf = makeInferenceWithRules(''' 209 def testTopicIsList(self):
205 # { ?msg :body "online" . } => { ?msg :onlineTerm :Online . } . 210 inf = makeInferenceWithRules('''
206 # { ?msg :body "offline" . } => { ?msg :onlineTerm :Offline . } . 211 { ?msg :body "online" . } => { ?msg :onlineTerm :Online . } .
207 212 { ?msg :body "offline" . } => { ?msg :onlineTerm :Offline . } .
208 # { 213
209 # ?msg a :MqttMessage ; 214 {
210 # :topic ( "frontdoorlock" "status" ); 215 ?msg a :MqttMessage ;
211 # :onlineTerm ?onlineness . } => { 216 :topic ( "frontdoorlock" "status" );
212 # :frontDoorLockStatus :connectedStatus ?onlineness . 217 :onlineTerm ?onlineness . } => {
213 # } . 218 :frontDoorLockStatus :connectedStatus ?onlineness .
214 # ''') 219 } .
215 220 ''')
216 # out = inf.infer(N3('[] a :MqttMessage ; :body "online" ; :topic ( "frontdoorlock" "status" ) .')) 221
217 # self.assertIn((EX['frontDoorLockStatus'], EX['connectedStatus'], EX['Online']), out) 222 out = inf.infer(N3('[] a :MqttMessage ; :body "online" ; :topic ( "frontdoorlock" "status" ) .'))
223 self.assertIn((EX['frontDoorLockStatus'], EX['connectedStatus'], EX['Online']), out)
218 224
219 def testPerformance0(self): 225 def testPerformance0(self):
220 inf = makeInferenceWithRules(''' 226 inf = makeInferenceWithRules('''
221 { 227 {
222 ?msg a :MqttMessage; 228 ?msg a :MqttMessage;
238 244
239 vlit = cast(Literal, out.value(EX['airQualityIndoorTemperature'], EX['temperatureF'])) 245 vlit = cast(Literal, out.value(EX['airQualityIndoorTemperature'], EX['temperatureF']))
240 valueF = cast(Decimal, vlit.toPython()) 246 valueF = cast(Decimal, vlit.toPython())
241 self.assertAlmostEqual(float(valueF), 75.02) 247 self.assertAlmostEqual(float(valueF), 75.02)
242 248
243 # def testPerformance1(self): 249 @unittest.skip("still too slow")
244 # inf = makeInferenceWithRules(''' 250 def testPerformance1(self):
245 # { 251 inf = makeInferenceWithRules('''
246 # ?msg a :MqttMessage; 252 {
247 # :topic ( "air_quality_indoor" "sensor" "bme280_temperature" "state" ); 253 ?msg a :MqttMessage;
248 # :bodyFloat ?valueC . 254 :topic ( "air_quality_indoor" "sensor" "bme280_temperature" "state" );
249 # ?valueC math:greaterThan -999 . 255 :bodyFloat ?valueC .
250 # ?valueC :asFarenheit ?valueF . 256 ?valueC math:greaterThan -999 .
251 # } => { 257 ?valueC :asFarenheit ?valueF .
252 # :airQualityIndoorTemperature :temperatureF ?valueF . 258 } => {
253 # } . 259 :airQualityIndoorTemperature :temperatureF ?valueF .
254 # ''') 260 } .
255 # out = inf.infer( 261 ''')
256 # N3(''' 262 out = inf.infer(
257 # <urn:uuid:c6e1d92c-0ee1-11ec-bdbd-2a42c4691e9a> a :MqttMessage ; 263 N3('''
258 # :body "23.9" ; 264 <urn:uuid:c6e1d92c-0ee1-11ec-bdbd-2a42c4691e9a> a :MqttMessage ;
259 # :bodyFloat 2.39e+01 ; 265 :body "23.9" ;
260 # :topic ( "air_quality_indoor" "sensor" "bme280_temperature" "state" ) . 266 :bodyFloat 2.39e+01 ;
261 # ''')) 267 :topic ( "air_quality_indoor" "sensor" "bme280_temperature" "state" ) .
262 # vlit = cast(Literal, out.value(EX['airQualityIndoorTemperature'], EX['temperatureF'])) 268 '''))
263 # valueF = cast(Decimal, vlit.toPython()) 269 vlit = cast(Literal, out.value(EX['airQualityIndoorTemperature'], EX['temperatureF']))
264 # self.assertAlmostEqual(float(valueF), 75.02) 270 valueF = cast(Decimal, vlit.toPython())
271 self.assertAlmostEqual(float(valueF), 75.02)
265 272
266 273
267 class TestListPerformance(WithGraphEqual): 274 class TestListPerformance(WithGraphEqual):
268 275
269 def testList1(self): 276 def testList1(self):
279 def testList3(self): 286 def testList3(self):
280 inf = makeInferenceWithRules("{ :a :b (:e0 :e1 :e2) . } => { :new :stmt :here } .") 287 inf = makeInferenceWithRules("{ :a :b (:e0 :e1 :e2) . } => { :new :stmt :here } .")
281 implied = inf.infer(N3(":a :b (:e0 :e1 :e2) .")) 288 implied = inf.infer(N3(":a :b (:e0 :e1 :e2) ."))
282 self.assertGraphEqual(implied, N3(":new :stmt :here .")) 289 self.assertGraphEqual(implied, N3(":new :stmt :here ."))
283 290
284 # def testList4(self): 291 @unittest.skip("still too slow")
285 # inf = makeInferenceWithRules("{ :a :b (:e0 :e1 :e2 :e3) . } => { :new :stmt :here } .") 292 def testList4(self):
286 # implied = inf.infer(N3(":a :b (:e0 :e1 :e2 :e3) .")) 293 inf = makeInferenceWithRules("{ :a :b (:e0 :e1 :e2 :e3) . } => { :new :stmt :here } .")
287 # self.assertGraphEqual(implied, N3(":new :stmt :here .")) 294 implied = inf.infer(N3(":a :b (:e0 :e1 :e2 :e3) ."))
295 self.assertGraphEqual(implied, N3(":new :stmt :here ."))
288 296
289 297
290 def fakeStats(): 298 def fakeStats():
291 return defaultdict(lambda: 0) 299 return defaultdict(lambda: 0)
292 300
302 l = Lhs(N3("?x :a :b .")) 310 l = Lhs(N3("?x :a :b ."))
303 ws = ReadOnlyGraphAggregate([N3("[] :a :b .")]) 311 ws = ReadOnlyGraphAggregate([N3("[] :a :b .")])
304 cands = list(l.findCandidateBindings(ws, fakeStats())) 312 cands = list(l.findCandidateBindings(ws, fakeStats()))
305 self.assertEqual(len(cands), 1) 313 self.assertEqual(len(cands), 1)
306 314
315 @unittest.skip("still too slow")
307 def testListsOnlyMatchEachOther(self): 316 def testListsOnlyMatchEachOther(self):
308 l = Lhs(N3(":a :b (:e0 :e1) .")) 317 l = Lhs(N3(":a :b (:e0 :e1) ."))
309 ws = ReadOnlyGraphAggregate([N3(":a :b (:e0 :e1) .")]) 318 ws = ReadOnlyGraphAggregate([N3(":a :b (:e0 :e1) .")])
310 stats = fakeStats() 319 stats = fakeStats()
311 cands = list(l.findCandidateBindings(ws, stats)) 320 cands = list(l.findCandidateBindings(ws, stats))