Mercurial > code > home > repos > homeauto
annotate service/mqtt_to_rdf/stmt_chunk_test.py @ 1710:f4009f41f15d
patchablegraph to its own repo
author | drewp@bigasterisk.com |
---|---|
date | Wed, 24 Nov 2021 10:16:03 -0800 |
parents | e6d28e6d47b2 |
children |
rev | line source |
---|---|
1676
e6d28e6d47b2
update stmt_chunk_test for new bnode subtypes
drewp@bigasterisk.com
parents:
1670
diff
changeset
|
1 from inference_types import WorkingSetBnode |
1655 | 2 import unittest |
3 | |
1670 | 4 from rdflib import Namespace, Variable |
1655 | 5 |
1670 | 6 from candidate_binding import CandidateBinding |
1655 | 7 from inference_test import N3 |
1670 | 8 from lhs_evaluation import functionsFor |
9 from stmt_chunk import AlignedRuleChunk, Chunk, ChunkedGraph, applyChunky | |
1655 | 10 |
11 ROOM = Namespace('http://projects.bigasterisk.com/room/') | |
12 | |
13 | |
14 class TestChunkedGraph(unittest.TestCase): | |
15 | |
16 def testMakesSimpleChunks(self): | |
1676
e6d28e6d47b2
update stmt_chunk_test for new bnode subtypes
drewp@bigasterisk.com
parents:
1670
diff
changeset
|
17 cg = ChunkedGraph(N3(':a :b :c .'), WorkingSetBnode, functionsFor) |
1655 | 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): | |
1676
e6d28e6d47b2
update stmt_chunk_test for new bnode subtypes
drewp@bigasterisk.com
parents:
1670
diff
changeset
|
24 cg = ChunkedGraph(N3('?x :b :c . :a ?y :c . :a :b ?z .'), WorkingSetBnode, functionsFor) |
1655 | 25 self.assertEqual(len(cg.patternChunks), 3) |
26 | |
27 def testBoolMeansEmpty(self): | |
1676
e6d28e6d47b2
update stmt_chunk_test for new bnode subtypes
drewp@bigasterisk.com
parents:
1670
diff
changeset
|
28 self.assertTrue(ChunkedGraph(N3(":a :b :c ."), WorkingSetBnode, functionsFor)) |
e6d28e6d47b2
update stmt_chunk_test for new bnode subtypes
drewp@bigasterisk.com
parents:
1670
diff
changeset
|
29 self.assertFalse(ChunkedGraph(N3(""), WorkingSetBnode, functionsFor)) |
1655 | 30 |
31 def testContains(self): | |
32 # If I write with assertIn, there's a seemingly bogus pytype error. | |
1676
e6d28e6d47b2
update stmt_chunk_test for new bnode subtypes
drewp@bigasterisk.com
parents:
1670
diff
changeset
|
33 self.assert_(Chunk((ROOM.a, ROOM.b, ROOM.c)) in ChunkedGraph(N3(":a :b :c ."), WorkingSetBnode, functionsFor)) |
e6d28e6d47b2
update stmt_chunk_test for new bnode subtypes
drewp@bigasterisk.com
parents:
1670
diff
changeset
|
34 self.assert_(Chunk((ROOM.a, ROOM.b, ROOM.zzz)) not in ChunkedGraph(N3(":a :b :c ."), WorkingSetBnode, functionsFor)) |
1655 | 35 |
36 def testNoPredicatesAppear(self): | |
1676
e6d28e6d47b2
update stmt_chunk_test for new bnode subtypes
drewp@bigasterisk.com
parents:
1670
diff
changeset
|
37 cg = ChunkedGraph(N3(":a :b :c ."), WorkingSetBnode, functionsFor) |
1655 | 38 self.assertTrue(cg.noPredicatesAppear([ROOM.d, ROOM.e])) |
39 self.assertFalse(cg.noPredicatesAppear([ROOM.b, ROOM.d])) | |
40 | |
41 | |
1659
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
42 class TestListCollection(unittest.TestCase): |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
43 |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
44 def testSubjList(self): |
1676
e6d28e6d47b2
update stmt_chunk_test for new bnode subtypes
drewp@bigasterisk.com
parents:
1670
diff
changeset
|
45 cg = ChunkedGraph(N3('(:u :v) :b :c .'), WorkingSetBnode, functionsFor) |
1659
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
46 expected = Chunk((None, ROOM.b, ROOM.c), subjList=[ROOM.u, ROOM.v]) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
47 self.assertEqual(cg.staticChunks, set([expected])) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
48 |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
49 def testObjList(self): |
1676
e6d28e6d47b2
update stmt_chunk_test for new bnode subtypes
drewp@bigasterisk.com
parents:
1670
diff
changeset
|
50 cg = ChunkedGraph(N3(':a :b (:u :v) .'), WorkingSetBnode, functionsFor) |
1659
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
51 expected = Chunk((ROOM.a, ROOM.b, None), objList=[ROOM.u, ROOM.v]) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
52 self.assertSetEqual(cg.staticChunks, set([expected])) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
53 |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
54 def testVariableInListMakesAPatternChunk(self): |
1676
e6d28e6d47b2
update stmt_chunk_test for new bnode subtypes
drewp@bigasterisk.com
parents:
1670
diff
changeset
|
55 cg = ChunkedGraph(N3(':a :b (?x :v) .'), WorkingSetBnode, functionsFor) |
1659
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
56 expected = Chunk((ROOM.a, ROOM.b, None), objList=[Variable('x'), ROOM.v]) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
57 self.assertSetEqual(cg.patternChunks, set([expected])) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
58 |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
59 def testListUsedTwice(self): |
1676
e6d28e6d47b2
update stmt_chunk_test for new bnode subtypes
drewp@bigasterisk.com
parents:
1670
diff
changeset
|
60 cg = ChunkedGraph(N3('(:u :v) :b :c, :d .'), WorkingSetBnode, functionsFor) |
1659
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
61 |
1661 | 62 self.assertSetEqual( |
63 cg.staticChunks, | |
64 set([ | |
65 Chunk((None, ROOM.b, ROOM.c), subjList=[ROOM.u, ROOM.v]), | |
66 Chunk((None, ROOM.b, ROOM.d), subjList=[ROOM.u, ROOM.v]) | |
67 ])) | |
1659
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
68 |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
69 def testUnusedListFragment(self): |
1676
e6d28e6d47b2
update stmt_chunk_test for new bnode subtypes
drewp@bigasterisk.com
parents:
1670
diff
changeset
|
70 cg = ChunkedGraph(N3(':a rdf:first :b .'), WorkingSetBnode, functionsFor) |
1659
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
71 self.assertFalse(cg) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
72 |
1661 | 73 |
1655 | 74 class TestApplyChunky(unittest.TestCase): |
75 binding = CandidateBinding({Variable('x'): ROOM.xval}) | |
76 | |
1670 | 77 def testAllStatements(self): |
78 rule0 = Chunk((ROOM.a, Variable('pred'), Variable('x'))) | |
79 rule1 = Chunk((ROOM.a, Variable('pred'), Variable('x'))) | |
1655 | 80 ret = list( |
81 applyChunky(self.binding, | |
1670 | 82 g=[ |
1676
e6d28e6d47b2
update stmt_chunk_test for new bnode subtypes
drewp@bigasterisk.com
parents:
1670
diff
changeset
|
83 AlignedRuleChunk(ruleChunk=rule0, workingSetChunk=Chunk((ROOM.a, ROOM.b, ROOM.xval))), |
e6d28e6d47b2
update stmt_chunk_test for new bnode subtypes
drewp@bigasterisk.com
parents:
1670
diff
changeset
|
84 AlignedRuleChunk(ruleChunk=rule1, workingSetChunk=Chunk((ROOM.a, ROOM.b, ROOM.yval))), |
1670 | 85 ])) |
1676
e6d28e6d47b2
update stmt_chunk_test for new bnode subtypes
drewp@bigasterisk.com
parents:
1670
diff
changeset
|
86 self.assertCountEqual(ret, [ |
e6d28e6d47b2
update stmt_chunk_test for new bnode subtypes
drewp@bigasterisk.com
parents:
1670
diff
changeset
|
87 AlignedRuleChunk(ruleChunk=Chunk((ROOM.a, Variable('pred'), ROOM.xval)), |
e6d28e6d47b2
update stmt_chunk_test for new bnode subtypes
drewp@bigasterisk.com
parents:
1670
diff
changeset
|
88 workingSetChunk=Chunk((ROOM.a, ROOM.b, ROOM.xval))) |
e6d28e6d47b2
update stmt_chunk_test for new bnode subtypes
drewp@bigasterisk.com
parents:
1670
diff
changeset
|
89 ]) |