Mercurial > code > home > repos > homeauto
comparison service/mqtt_to_rdf/inference_test.py @ 1660:31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
author | drewp@bigasterisk.com |
---|---|
date | Sun, 19 Sep 2021 15:39:37 -0700 |
parents | 15e84c71beee |
children | 7a61113fd17d |
comparison
equal
deleted
inserted
replaced
1659:15e84c71beee | 1660:31f7dab6a60b |
---|---|
166 self.assertNotEqual(ruleNode, stmt0Node) | 166 self.assertNotEqual(ruleNode, stmt0Node) |
167 self.assertNotEqual(ruleNode, stmt1Node) | 167 self.assertNotEqual(ruleNode, stmt1Node) |
168 self.assertNotEqual(stmt0Node, stmt1Node) | 168 self.assertNotEqual(stmt0Node, stmt1Node) |
169 | 169 |
170 | 170 |
171 # class TestSelfFulfillingRule(WithGraphEqual): | 171 class TestSelfFulfillingRule(WithGraphEqual): |
172 | 172 |
173 # def test1(self): | 173 def test1(self): |
174 # inf = makeInferenceWithRules("{ } => { :new :stmt :x } .") | 174 inf = makeInferenceWithRules("{ } => { :new :stmt :x } .") |
175 # self.assertGraphEqual(inf.infer(N3("")), N3(":new :stmt :x .")) | 175 self.assertGraphEqual(inf.infer(N3("")), N3(":new :stmt :x .")) |
176 # self.assertGraphEqual(inf.infer(N3(":any :any :any .")), N3(":new :stmt :x .")) | 176 self.assertGraphEqual(inf.infer(N3(":any :any :any .")), N3(":new :stmt :x .")) |
177 | 177 |
178 # def test2(self): | 178 # def test2(self): |
179 # inf = makeInferenceWithRules("{ (2) math:sum ?x } => { :new :stmt ?x } .") | 179 # inf = makeInferenceWithRules("{ (2) math:sum ?x } => { :new :stmt ?x } .") |
180 # self.assertGraphEqual(inf.infer(N3("")), N3(":new :stmt 2 .")) | 180 # self.assertGraphEqual(inf.infer(N3("")), N3(":new :stmt 2 .")) |
181 | 181 |
182 | 182 # @unittest.skip("too hard for now") |
183 # @unittest.skip("too hard for now") | 183 # def test3(self): |
184 # def test3(self): | 184 # inf = makeInferenceWithRules("{ :a :b :c . :a :b ?x . } => { :new :stmt ?x } .") |
185 # inf = makeInferenceWithRules("{ :a :b :c . :a :b ?x . } => { :new :stmt ?x } .") | 185 # self.assertGraphEqual(inf.infer(N3("")), N3(":new :stmt :c .")) |
186 # self.assertGraphEqual(inf.infer(N3("")), N3(":new :stmt :c .")) | |
187 | 186 |
188 | 187 |
189 class TestInferenceWithMathFunctions(WithGraphEqual): | 188 class TestInferenceWithMathFunctions(WithGraphEqual): |
190 | 189 |
191 def testBoolFilter(self): | 190 def testBoolFilter(self): |
192 inf = makeInferenceWithRules("{ :a :b ?x . ?x math:greaterThan 5 } => { :new :stmt ?x } .") | 191 inf = makeInferenceWithRules("{ :a :b ?x . ?x math:greaterThan 5 } => { :new :stmt ?x } .") |
193 self.assertGraphEqual(inf.infer(N3(":a :b 3 .")), N3("")) | 192 self.assertGraphEqual(inf.infer(N3(":a :b 3 .")), N3("")) |
194 self.assertGraphEqual(inf.infer(N3(":a :b 5 .")), N3("")) | 193 self.assertGraphEqual(inf.infer(N3(":a :b 5 .")), N3("")) |
195 self.assertGraphEqual(inf.infer(N3(":a :b 6 .")), N3(":new :stmt 6 .")) | 194 self.assertGraphEqual(inf.infer(N3(":a :b 6 .")), N3(":new :stmt 6 .")) |
196 | 195 |
197 # def testNonFiringMathRule(self): | 196 def testNonFiringMathRule(self): |
198 # inf = makeInferenceWithRules("{ :a :b ?x . (?x 1) math:sum ?y } => { :new :stmt ?y } .") | 197 inf = makeInferenceWithRules("{ :a :b ?x . (?x 1) math:sum ?y } => { :new :stmt ?y } .") |
199 # self.assertGraphEqual(inf.infer(N3("")), N3("")) | 198 self.assertGraphEqual(inf.infer(N3("")), N3("")) |
200 | 199 |
201 # def testStatementGeneratingRule(self): | 200 def testStatementGeneratingRule(self): |
202 # inf = makeInferenceWithRules("{ :a :b ?x . (?x) math:sum ?y } => { :new :stmt ?y } .") | 201 inf = makeInferenceWithRules("{ :a :b ?x . (?x) math:sum ?y } => { :new :stmt ?y } .") |
203 # self.assertGraphEqual(inf.infer(N3(":a :b 3 .")), N3(":new :stmt 3 .")) | 202 self.assertGraphEqual(inf.infer(N3(":a :b 3 .")), N3(":new :stmt 3 .")) |
204 | 203 |
205 # def test2Operands(self): | 204 def test2Operands(self): |
206 # inf = makeInferenceWithRules("{ :a :b ?x . (?x 1) math:sum ?y } => { :new :stmt ?y } .") | 205 inf = makeInferenceWithRules("{ :a :b ?x . (?x 1) math:sum ?y } => { :new :stmt ?y } .") |
207 # self.assertGraphEqual(inf.infer(N3(":a :b 3 .")), N3(":new :stmt 4 .")) | 206 self.assertGraphEqual(inf.infer(N3(":a :b 3 .")), N3(":new :stmt 4 .")) |
208 | 207 |
209 # def test3Operands(self): | 208 def test3Operands(self): |
210 # inf = makeInferenceWithRules("{ :a :b ?x . (2 ?x 2) math:sum ?y } => { :new :stmt ?y } .") | 209 inf = makeInferenceWithRules("{ :a :b ?x . (2 ?x 2) math:sum ?y } => { :new :stmt ?y } .") |
211 # self.assertGraphEqual(inf.infer(N3(":a :b 2 .")), N3(":new :stmt 6 .")) | 210 self.assertGraphEqual(inf.infer(N3(":a :b 2 .")), N3(":new :stmt 6 .")) |
212 | 211 |
213 # def test0Operands(self): | 212 # def test0Operands(self): |
214 # inf = makeInferenceWithRules("{ :a :b ?x . () math:sum ?y } => { :new :stmt ?y } .") | 213 # inf = makeInferenceWithRules("{ :a :b ?x . () math:sum ?y } => { :new :stmt ?y } .") |
215 # self.assertGraphEqual(inf.infer(N3(":a :b 2 .")), N3(":new :stmt 0 .")) | 214 # self.assertGraphEqual(inf.infer(N3(":a :b 2 .")), N3(":new :stmt 0 .")) |
216 | 215 |
217 | 216 |
218 class TestInferenceWithCustomFunctions(WithGraphEqual): | 217 class TestInferenceWithCustomFunctions(WithGraphEqual): |
219 | 218 |
220 def testAsFarenheit(self): | 219 def testAsFarenheit(self): |
238 ''') | 237 ''') |
239 | 238 |
240 out = inf.infer(N3('[] a :MqttMessage ; :body "online" ; :topic :foo .')) | 239 out = inf.infer(N3('[] a :MqttMessage ; :body "online" ; :topic :foo .')) |
241 self.assertIn((ROOM['frontDoorLockStatus'], ROOM['connectedStatus'], ROOM['Online']), out) | 240 self.assertIn((ROOM['frontDoorLockStatus'], ROOM['connectedStatus'], ROOM['Online']), out) |
242 | 241 |
243 # def testTopicIsList(self): | 242 def testTopicIsList(self): |
244 # inf = makeInferenceWithRules(''' | 243 inf = makeInferenceWithRules(''' |
245 # { ?msg :body "online" . } => { ?msg :onlineTerm :Online . } . | 244 { ?msg :body "online" . } => { ?msg :onlineTerm :Online . } . |
246 # { ?msg :body "offline" . } => { ?msg :onlineTerm :Offline . } . | 245 { ?msg :body "offline" . } => { ?msg :onlineTerm :Offline . } . |
247 | 246 |
248 # { | 247 { |
249 # ?msg a :MqttMessage ; | 248 ?msg a :MqttMessage ; |
250 # :topic ( "frontdoorlock" "status" ); | 249 :topic ( "frontdoorlock" "status" ); |
251 # :onlineTerm ?onlineness . } => { | 250 :onlineTerm ?onlineness . } => { |
252 # :frontDoorLockStatus :connectedStatus ?onlineness . | 251 :frontDoorLockStatus :connectedStatus ?onlineness . |
253 # } . | 252 } . |
254 # ''') | 253 ''') |
255 | 254 |
256 # out = inf.infer(N3('[] a :MqttMessage ; :body "online" ; :topic ( "frontdoorlock" "status" ) .')) | 255 out = inf.infer(N3('[] a :MqttMessage ; :body "online" ; :topic ( "frontdoorlock" "status" ) .')) |
257 # self.assertIn((ROOM['frontDoorLockStatus'], ROOM['connectedStatus'], ROOM['Online']), out) | 256 self.assertIn((ROOM['frontDoorLockStatus'], ROOM['connectedStatus'], ROOM['Online']), out) |
258 | 257 |
259 # def testPerformance0(self): | 258 def testPerformance0(self): |
260 # inf = makeInferenceWithRules(''' | 259 inf = makeInferenceWithRules(''' |
261 # { | 260 { |
262 # ?msg a :MqttMessage; | 261 ?msg a :MqttMessage; |
263 # :topic :topic1; | 262 :topic :topic1; |
264 # :bodyFloat ?valueC . | 263 :bodyFloat ?valueC . |
265 # ?valueC math:greaterThan -999 . | 264 ?valueC math:greaterThan -999 . |
266 # ?valueC room:asFarenheit ?valueF . | 265 ?valueC room:asFarenheit ?valueF . |
267 # } => { | 266 } => { |
268 # :airQualityIndoorTemperature :temperatureF ?valueF . | 267 :airQualityIndoorTemperature :temperatureF ?valueF . |
269 # } . | 268 } . |
270 # ''') | 269 ''') |
271 # out = inf.infer( | 270 out = inf.infer( |
272 # N3(''' | 271 N3(''' |
273 # <urn:uuid:c6e1d92c-0ee1-11ec-bdbd-2a42c4691e9a> a :MqttMessage ; | 272 <urn:uuid:c6e1d92c-0ee1-11ec-bdbd-2a42c4691e9a> a :MqttMessage ; |
274 # :body "23.9" ; | 273 :body "23.9" ; |
275 # :bodyFloat 2.39e+01 ; | 274 :bodyFloat 2.39e+01 ; |
276 # :topic :topic1 . | 275 :topic :topic1 . |
277 # ''')) | 276 ''')) |
278 | 277 |
279 # vlit = cast(Literal, out.value(ROOM['airQualityIndoorTemperature'], ROOM['temperatureF'])) | 278 vlit = cast(Literal, out.value(ROOM['airQualityIndoorTemperature'], ROOM['temperatureF'])) |
280 # valueF = cast(Decimal, vlit.toPython()) | 279 valueF = cast(Decimal, vlit.toPython()) |
281 # self.assertAlmostEqual(float(valueF), 75.02) | 280 self.assertAlmostEqual(float(valueF), 75.02) |
282 | 281 |
283 # def testPerformance1(self): | 282 def testPerformance1(self): |
284 # inf = makeInferenceWithRules(''' | 283 inf = makeInferenceWithRules(''' |
285 # { | 284 { |
286 # ?msg a :MqttMessage; | 285 ?msg a :MqttMessage; |
287 # :topic ( "air_quality_indoor" "sensor" "bme280_temperature" "state" ); | 286 :topic ( "air_quality_indoor" "sensor" "bme280_temperature" "state" ); |
288 # :bodyFloat ?valueC . | 287 :bodyFloat ?valueC . |
289 # ?valueC math:greaterThan -999 . | 288 ?valueC math:greaterThan -999 . |
290 # ?valueC room:asFarenheit ?valueF . | 289 ?valueC room:asFarenheit ?valueF . |
291 # } => { | 290 } => { |
292 # :airQualityIndoorTemperature :temperatureF ?valueF . | 291 :airQualityIndoorTemperature :temperatureF ?valueF . |
293 # } . | 292 } . |
294 # ''') | 293 ''') |
295 # out = inf.infer( | 294 out = inf.infer( |
296 # N3(''' | 295 N3(''' |
297 # <urn:uuid:c6e1d92c-0ee1-11ec-bdbd-2a42c4691e9a> a :MqttMessage ; | 296 <urn:uuid:c6e1d92c-0ee1-11ec-bdbd-2a42c4691e9a> a :MqttMessage ; |
298 # :body "23.9" ; | 297 :body "23.9" ; |
299 # :bodyFloat 2.39e+01 ; | 298 :bodyFloat 2.39e+01 ; |
300 # :topic ( "air_quality_indoor" "sensor" "bme280_temperature" "state" ) . | 299 :topic ( "air_quality_indoor" "sensor" "bme280_temperature" "state" ) . |
301 # ''')) | 300 ''')) |
302 # vlit = cast(Literal, out.value(ROOM['airQualityIndoorTemperature'], ROOM['temperatureF'])) | 301 vlit = cast(Literal, out.value(ROOM['airQualityIndoorTemperature'], ROOM['temperatureF'])) |
303 # valueF = cast(Decimal, vlit.toPython()) | 302 valueF = cast(Decimal, vlit.toPython()) |
304 # self.assertAlmostEqual(float(valueF), 75.02) | 303 self.assertAlmostEqual(float(valueF), 75.02) |
305 | 304 |
306 # def testEmitBnodes(self): | 305 def testEmitBnodes(self): |
307 # inf = makeInferenceWithRules(''' | 306 inf = makeInferenceWithRules(''' |
308 # { ?s a :AirQualitySensor; :label ?name . } => { | 307 { ?s a :AirQualitySensor; :label ?name . } => { |
309 # [ a :MqttStatementSource; | 308 [ a :MqttStatementSource; |
310 # :mqttTopic (?name "sensor" "bme280_temperature" "state") ] . | 309 :mqttTopic (?name "sensor" "bme280_temperature" "state") ] . |
311 # } . | 310 } . |
312 # ''') | 311 ''') |
313 # out = inf.infer(N3(''' | 312 out = inf.infer(N3(''' |
314 # :airQualityOutdoor a :AirQualitySensor; :label "air_quality_outdoor" . | 313 :airQualityOutdoor a :AirQualitySensor; :label "air_quality_outdoor" . |
315 # ''')) | 314 ''')) |
316 # out.bind('', ROOM) | 315 out.bind('', ROOM) |
317 # out.bind('ex', EX) | 316 out.bind('ex', EX) |
318 # self.assertEqual( | 317 self.assertEqual( |
319 # out.serialize(format='n3'), b'''\ | 318 out.serialize(format='n3'), b'''\ |
320 # @prefix : <http://projects.bigasterisk.com/room/> . | 319 @prefix : <http://projects.bigasterisk.com/room/> . |
321 # @prefix ex: <http://example.com/> . | 320 @prefix ex: <http://example.com/> . |
322 # @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#> . |
323 # @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . | 322 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . |
324 # @prefix xml: <http://www.w3.org/XML/1998/namespace> . | 323 @prefix xml: <http://www.w3.org/XML/1998/namespace> . |
325 # @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . | 324 @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . |
326 | 325 |
327 # [] a :MqttStatementSource ; | 326 [] a :MqttStatementSource ; |
328 # :mqttTopic ( "air_quality_outdoor" "sensor" "bme280_temperature" "state" ) . | 327 :mqttTopic ( "air_quality_outdoor" "sensor" "bme280_temperature" "state" ) . |
329 | 328 |
330 # ''') | 329 ''') |
331 | 330 |
332 | 331 |
333 class TestListPerformance(WithGraphEqual): | 332 class TestListPerformance(WithGraphEqual): |
334 | 333 |
335 def testList1(self): | 334 def testList1(self): |