1655
|
1 import unittest
|
|
2
|
|
3 from rdflib.term import Variable
|
|
4
|
|
5 from inference_test import N3
|
|
6 from rdflib import ConjunctiveGraph, Graph, Literal, Namespace, Variable
|
|
7
|
|
8 from stmt_chunk import ChunkedGraph, Chunk, applyChunky
|
|
9
|
|
10 ROOM = Namespace('http://projects.bigasterisk.com/room/')
|
|
11
|
|
12 from lhs_evaluation import functionsFor
|
|
13 from candidate_binding import CandidateBinding
|
|
14
|
|
15
|
|
16 class TestChunkedGraph(unittest.TestCase):
|
|
17
|
|
18 def testMakesSimpleChunks(self):
|
|
19 cg = ChunkedGraph(N3(':a :b :c .'), functionsFor)
|
|
20
|
|
21 self.assertSetEqual(cg.chunksUsedByFuncs, set())
|
|
22 self.assertSetEqual(cg.patternChunks, set())
|
|
23 self.assertSetEqual(cg.staticChunks, set([Chunk((ROOM.a, ROOM.b, ROOM.c), subjList=None, objList=None)]))
|
|
24
|
|
25 def testSeparatesPatternChunks(self):
|
|
26 cg = ChunkedGraph(N3('?x :b :c . :a ?y :c . :a :b ?z .'), functionsFor)
|
|
27 self.assertEqual(len(cg.patternChunks), 3)
|
|
28
|
|
29 def testBoolMeansEmpty(self):
|
|
30 self.assertTrue(ChunkedGraph(N3(":a :b :c ."), functionsFor))
|
|
31 self.assertFalse(ChunkedGraph(N3(""), functionsFor))
|
|
32
|
|
33 def testContains(self):
|
|
34 # If I write with assertIn, there's a seemingly bogus pytype error.
|
|
35 self.assert_(Chunk((ROOM.a, ROOM.b, ROOM.c)) in ChunkedGraph(N3(":a :b :c ."), functionsFor))
|
|
36 self.assert_(Chunk((ROOM.a, ROOM.b, ROOM.zzz)) not in ChunkedGraph(N3(":a :b :c ."), functionsFor))
|
|
37
|
|
38 def testNoPredicatesAppear(self):
|
|
39 cg = ChunkedGraph(N3(":a :b :c ."), functionsFor)
|
|
40 self.assertTrue(cg.noPredicatesAppear([ROOM.d, ROOM.e]))
|
|
41 self.assertFalse(cg.noPredicatesAppear([ROOM.b, ROOM.d]))
|
|
42
|
|
43
|
|
44 class TestApplyChunky(unittest.TestCase):
|
|
45 binding = CandidateBinding({Variable('x'): ROOM.xval})
|
|
46
|
|
47 def testBoundStatementsOnly(self):
|
|
48 ret = list(
|
|
49 applyChunky(self.binding,
|
|
50 g=[Chunk((ROOM.a, ROOM.b, Variable('x'))),
|
|
51 Chunk((ROOM.ay, ROOM.by, Variable('y')))],
|
|
52 returnBoundStatementsOnly=True))
|
|
53 self.assertEqual(ret, [Chunk((ROOM.a, ROOM.b, ROOM.xval))])
|
|
54
|
|
55 def testAllStatements(self):
|
|
56 ret = list(
|
|
57 applyChunky(self.binding,
|
|
58 g=[Chunk((ROOM.a, ROOM.b, Variable('x'))),
|
|
59 Chunk((ROOM.ay, ROOM.by, Variable('y')))],
|
|
60 returnBoundStatementsOnly=False))
|
|
61 self.assertCountEqual(
|
|
62 ret,
|
|
63 [
|
|
64 Chunk((ROOM.a, ROOM.b, ROOM.xval)), #
|
|
65 Chunk((ROOM.ay, ROOM.by, Variable('y')))
|
|
66 ])
|