Mercurial > code > home > repos > homeauto
annotate service/mqtt_to_rdf/stmt_chunk_test.py @ 1661:00a5624d1d14
cleanups and optimizations
author | drewp@bigasterisk.com |
---|---|
date | Sun, 19 Sep 2021 16:51:51 -0700 |
parents | 15e84c71beee |
children | 6fa7ddee9ba8 |
rev | line source |
---|---|
1659
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
1 from time import clock_gettime |
1655 | 2 import unittest |
3 | |
4 from rdflib.term import Variable | |
5 | |
6 from inference_test import N3 | |
7 from rdflib import ConjunctiveGraph, Graph, Literal, Namespace, Variable | |
8 | |
9 from stmt_chunk import ChunkedGraph, Chunk, applyChunky | |
10 | |
11 ROOM = Namespace('http://projects.bigasterisk.com/room/') | |
12 | |
13 from lhs_evaluation import functionsFor | |
14 from candidate_binding import CandidateBinding | |
15 | |
16 | |
17 class TestChunkedGraph(unittest.TestCase): | |
18 | |
19 def testMakesSimpleChunks(self): | |
20 cg = ChunkedGraph(N3(':a :b :c .'), functionsFor) | |
21 | |
22 self.assertSetEqual(cg.chunksUsedByFuncs, set()) | |
23 self.assertSetEqual(cg.patternChunks, set()) | |
24 self.assertSetEqual(cg.staticChunks, set([Chunk((ROOM.a, ROOM.b, ROOM.c), subjList=None, objList=None)])) | |
25 | |
26 def testSeparatesPatternChunks(self): | |
27 cg = ChunkedGraph(N3('?x :b :c . :a ?y :c . :a :b ?z .'), functionsFor) | |
28 self.assertEqual(len(cg.patternChunks), 3) | |
29 | |
30 def testBoolMeansEmpty(self): | |
31 self.assertTrue(ChunkedGraph(N3(":a :b :c ."), functionsFor)) | |
32 self.assertFalse(ChunkedGraph(N3(""), functionsFor)) | |
33 | |
34 def testContains(self): | |
35 # If I write with assertIn, there's a seemingly bogus pytype error. | |
36 self.assert_(Chunk((ROOM.a, ROOM.b, ROOM.c)) in ChunkedGraph(N3(":a :b :c ."), functionsFor)) | |
37 self.assert_(Chunk((ROOM.a, ROOM.b, ROOM.zzz)) not in ChunkedGraph(N3(":a :b :c ."), functionsFor)) | |
38 | |
39 def testNoPredicatesAppear(self): | |
40 cg = ChunkedGraph(N3(":a :b :c ."), functionsFor) | |
41 self.assertTrue(cg.noPredicatesAppear([ROOM.d, ROOM.e])) | |
42 self.assertFalse(cg.noPredicatesAppear([ROOM.b, ROOM.d])) | |
43 | |
44 | |
1659
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
45 class TestListCollection(unittest.TestCase): |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
46 |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
47 def testSubjList(self): |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
48 cg = ChunkedGraph(N3('(:u :v) :b :c .'), functionsFor) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
49 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
|
50 self.assertEqual(cg.staticChunks, set([expected])) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
51 |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
52 def testObjList(self): |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
53 cg = ChunkedGraph(N3(':a :b (:u :v) .'), functionsFor) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
54 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
|
55 self.assertSetEqual(cg.staticChunks, set([expected])) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
56 |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
57 def testVariableInListMakesAPatternChunk(self): |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
58 cg = ChunkedGraph(N3(':a :b (?x :v) .'), functionsFor) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
59 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
|
60 self.assertSetEqual(cg.patternChunks, set([expected])) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
61 |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
62 def testListUsedTwice(self): |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
63 cg = ChunkedGraph(N3('(:u :v) :b :c, :d .'), functionsFor) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
64 |
1661 | 65 self.assertSetEqual( |
66 cg.staticChunks, | |
67 set([ | |
68 Chunk((None, ROOM.b, ROOM.c), subjList=[ROOM.u, ROOM.v]), | |
69 Chunk((None, ROOM.b, ROOM.d), subjList=[ROOM.u, ROOM.v]) | |
70 ])) | |
1659
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
71 |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
72 def testUnusedListFragment(self): |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
73 cg = ChunkedGraph(N3(':a rdf:first :b .'), functionsFor) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
74 self.assertFalse(cg) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
75 |
1661 | 76 |
1655 | 77 class TestApplyChunky(unittest.TestCase): |
78 binding = CandidateBinding({Variable('x'): ROOM.xval}) | |
79 | |
80 def testBoundStatementsOnly(self): | |
81 ret = list( | |
82 applyChunky(self.binding, | |
83 g=[Chunk((ROOM.a, ROOM.b, Variable('x'))), | |
84 Chunk((ROOM.ay, ROOM.by, Variable('y')))], | |
85 returnBoundStatementsOnly=True)) | |
86 self.assertEqual(ret, [Chunk((ROOM.a, ROOM.b, ROOM.xval))]) | |
87 | |
88 def testAllStatements(self): | |
89 ret = list( | |
90 applyChunky(self.binding, | |
91 g=[Chunk((ROOM.a, ROOM.b, Variable('x'))), | |
92 Chunk((ROOM.ay, ROOM.by, Variable('y')))], | |
93 returnBoundStatementsOnly=False)) | |
94 self.assertCountEqual( | |
95 ret, | |
96 [ | |
97 Chunk((ROOM.a, ROOM.b, ROOM.xval)), # | |
98 Chunk((ROOM.ay, ROOM.by, Variable('y'))) | |
99 ]) |