Mercurial > code > home > repos > homeauto
annotate service/mqtt_to_rdf/stmt_chunk_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 | 00a5624d1d14 |
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 |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
65 self.assertSetEqual(cg.staticChunks, set([ |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
66 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
|
67 Chunk((None, ROOM.b, ROOM.d), subjList=[ROOM.u, ROOM.v]) |
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 |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
70 def testUnusedListFragment(self): |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
71 cg = ChunkedGraph(N3(':a rdf:first :b .'), functionsFor) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
72 self.assertFalse(cg) |
15e84c71beee
parse lists from graph into the Chunks
drewp@bigasterisk.com
parents:
1655
diff
changeset
|
73 |
1655 | 74 class TestApplyChunky(unittest.TestCase): |
75 binding = CandidateBinding({Variable('x'): ROOM.xval}) | |
76 | |
77 def testBoundStatementsOnly(self): | |
78 ret = list( | |
79 applyChunky(self.binding, | |
80 g=[Chunk((ROOM.a, ROOM.b, Variable('x'))), | |
81 Chunk((ROOM.ay, ROOM.by, Variable('y')))], | |
82 returnBoundStatementsOnly=True)) | |
83 self.assertEqual(ret, [Chunk((ROOM.a, ROOM.b, ROOM.xval))]) | |
84 | |
85 def testAllStatements(self): | |
86 ret = list( | |
87 applyChunky(self.binding, | |
88 g=[Chunk((ROOM.a, ROOM.b, Variable('x'))), | |
89 Chunk((ROOM.ay, ROOM.by, Variable('y')))], | |
90 returnBoundStatementsOnly=False)) | |
91 self.assertCountEqual( | |
92 ret, | |
93 [ | |
94 Chunk((ROOM.a, ROOM.b, ROOM.xval)), # | |
95 Chunk((ROOM.ay, ROOM.by, Variable('y'))) | |
96 ]) |