Mercurial > code > home > repos > homeauto
comparison service/mqtt_to_rdf/inference/stmt_chunk_test.py @ 1727:23e6154e6c11
file moves
author | drewp@bigasterisk.com |
---|---|
date | Tue, 20 Jun 2023 23:26:24 -0700 |
parents | service/mqtt_to_rdf/stmt_chunk_test.py@e6d28e6d47b2 |
children |
comparison
equal
deleted
inserted
replaced
1726:7d3797ed6681 | 1727:23e6154e6c11 |
---|---|
1 import unittest | |
2 | |
3 from rdflib import Namespace, Variable | |
4 | |
5 from inference.candidate_binding import CandidateBinding | |
6 from inference.inference_test import N3 | |
7 from inference.inference_types import WorkingSetBnode | |
8 from inference.lhs_evaluation import functionsFor | |
9 from inference.stmt_chunk import (AlignedRuleChunk, Chunk, ChunkedGraph, applyChunky) | |
10 | |
11 ROOM = Namespace('http://projects.bigasterisk.com/room/') | |
12 | |
13 | |
14 class TestChunkedGraph(unittest.TestCase): | |
15 | |
16 def testMakesSimpleChunks(self): | |
17 cg = ChunkedGraph(N3(':a :b :c .'), WorkingSetBnode, functionsFor) | |
18 | |
19 self.assertSetEqual(cg.chunksUsedByFuncs, set()) | |
20 self.assertSetEqual(cg.patternChunks, set()) | |
21 self.assertSetEqual(cg.staticChunks, set([Chunk((ROOM.a, ROOM.b, ROOM.c), subjList=None, objList=None)])) | |
22 | |
23 def testSeparatesPatternChunks(self): | |
24 cg = ChunkedGraph(N3('?x :b :c . :a ?y :c . :a :b ?z .'), WorkingSetBnode, functionsFor) | |
25 self.assertEqual(len(cg.patternChunks), 3) | |
26 | |
27 def testBoolMeansEmpty(self): | |
28 self.assertTrue(ChunkedGraph(N3(":a :b :c ."), WorkingSetBnode, functionsFor)) | |
29 self.assertFalse(ChunkedGraph(N3(""), WorkingSetBnode, functionsFor)) | |
30 | |
31 def testContains(self): | |
32 # If I write with assertIn, there's a seemingly bogus pytype error. | |
33 self.assert_(Chunk((ROOM.a, ROOM.b, ROOM.c)) in ChunkedGraph(N3(":a :b :c ."), WorkingSetBnode, functionsFor)) | |
34 self.assert_(Chunk((ROOM.a, ROOM.b, ROOM.zzz)) not in ChunkedGraph(N3(":a :b :c ."), WorkingSetBnode, functionsFor)) | |
35 | |
36 def testNoPredicatesAppear(self): | |
37 cg = ChunkedGraph(N3(":a :b :c ."), WorkingSetBnode, functionsFor) | |
38 self.assertTrue(cg.noPredicatesAppear([ROOM.d, ROOM.e])) | |
39 self.assertFalse(cg.noPredicatesAppear([ROOM.b, ROOM.d])) | |
40 | |
41 | |
42 class TestListCollection(unittest.TestCase): | |
43 | |
44 def testSubjList(self): | |
45 cg = ChunkedGraph(N3('(:u :v) :b :c .'), WorkingSetBnode, functionsFor) | |
46 expected = Chunk((None, ROOM.b, ROOM.c), subjList=[ROOM.u, ROOM.v]) | |
47 self.assertEqual(cg.staticChunks, set([expected])) | |
48 | |
49 def testObjList(self): | |
50 cg = ChunkedGraph(N3(':a :b (:u :v) .'), WorkingSetBnode, functionsFor) | |
51 expected = Chunk((ROOM.a, ROOM.b, None), objList=[ROOM.u, ROOM.v]) | |
52 self.assertSetEqual(cg.staticChunks, set([expected])) | |
53 | |
54 def testVariableInListMakesAPatternChunk(self): | |
55 cg = ChunkedGraph(N3(':a :b (?x :v) .'), WorkingSetBnode, functionsFor) | |
56 expected = Chunk((ROOM.a, ROOM.b, None), objList=[Variable('x'), ROOM.v]) | |
57 self.assertSetEqual(cg.patternChunks, set([expected])) | |
58 | |
59 def testListUsedTwice(self): | |
60 cg = ChunkedGraph(N3('(:u :v) :b :c, :d .'), WorkingSetBnode, functionsFor) | |
61 | |
62 self.assertSetEqual(cg.staticChunks, | |
63 set([Chunk((None, ROOM.b, ROOM.c), subjList=[ROOM.u, ROOM.v]), | |
64 Chunk((None, ROOM.b, ROOM.d), subjList=[ROOM.u, ROOM.v])])) | |
65 | |
66 def testUnusedListFragment(self): | |
67 cg = ChunkedGraph(N3(':a rdf:first :b .'), WorkingSetBnode, functionsFor) | |
68 self.assertFalse(cg) | |
69 | |
70 | |
71 class TestApplyChunky(unittest.TestCase): | |
72 binding = CandidateBinding({Variable('x'): ROOM.xval}) | |
73 | |
74 def testAllStatements(self): | |
75 rule0 = Chunk((ROOM.a, Variable('pred'), Variable('x'))) | |
76 rule1 = Chunk((ROOM.a, Variable('pred'), Variable('x'))) | |
77 ret = list( | |
78 applyChunky(self.binding, | |
79 g=[ | |
80 AlignedRuleChunk(ruleChunk=rule0, workingSetChunk=Chunk((ROOM.a, ROOM.b, ROOM.xval))), | |
81 AlignedRuleChunk(ruleChunk=rule1, workingSetChunk=Chunk((ROOM.a, ROOM.b, ROOM.yval))), | |
82 ])) | |
83 self.assertCountEqual(ret, | |
84 [AlignedRuleChunk(ruleChunk=Chunk((ROOM.a, Variable('pred'), ROOM.xval)), workingSetChunk=Chunk((ROOM.a, ROOM.b, ROOM.xval)))]) |