def test1(self): stream = { 0: ["p(a,b)", "q", "z(a,b,c)", "p(a)", "p(a,b,v)"], 1: ["p(c,d)", "q", "z(a,b,c)", "q(a)"], 2: ["q", "z(a,b,c)", "q(a)"], } rules = [ "h(Y,X) :- p(X,Y)", ] s = TestStream(stream, 0, 2) prog = Program(rules, s) res, tuples = prog.evaluate(0) self.assertTrue(res) self.assertEquals(tuples, {0: set(['h(b,a)'])}) res, tuples = prog.evaluate(1) self.assertTrue(res) self.assertEquals(tuples, {1: set(['h(d,c)'])}) res, tuples = prog.evaluate(2) self.assertFalse(res) self.assertEquals(tuples, {})
def test_1(self): stream = { 0: [ "r(a,b)", "a", "z(a,b,c)", ], 1: ["p(c,d)", "b", "z(a,b,c)", "t(a)"], 2: ["b", "e(a,b,c)", "e(a)"], } rules = [ "s(A) :- t(A)", "p(B,C) :- not(s(C)) and r(B,C)", "q(XX,YY) :- p(XX,YY)", "z(Y,X) :- q(X,Y)", ] s = TestStream(stream, 0, 2) prog = Program(rules, s) res, tuples = prog.evaluate(0) self.assertTrue(res) self.assertEquals(tuples, {0: set(['p(a,b)', 'q(a,b)', 'z(b,a)'])}) res, tuples = prog.evaluate(1) self.assertTrue(res) self.assertEquals(tuples, {1: set(['q(c,d)', 's(a)', 'z(d,c)'])})
def evalCoolingSystem(winSize, nTriples): print("******************* Cooling System Evaluation winSize = %d, Triples = %d *********************" % (winSize, nTriples)) rules = [ "@(T, steam(V)) :- time_win(" + str(winSize) + ", 0, 1, @(T, (temp(V)))) and COMP(>=, V, 100)", "@(T, liquid(V)) :- time_win(" + str(winSize) + ", 0, 1, @(T, temp(V))) and COMP(<, V, 100) and COMP(>=, V, 1)", "@(T, isSteam) :- time_win(" + str(winSize) + ", 0, 1, @(T, steam(V)))", "@(T, isLiquid) :- time_win(" + str(winSize) + ", 0, 1, @(T, liquid(V)))", "alarm :- time_win(" + str(winSize) + ", 0, 1, box(isSteam))", "normal :- time_win(" + str(winSize) + ", 0, 1, box(isLiquid))", "veryHot(T) :- time_win(" + str(winSize) + ",0,1, @(T, steam(V))) and COMP(>=, V, 150)", "veryCold(T) :- time_win(" + str(winSize) + ",0,1, @(T, liquid(V))) and COMP(=, V, 1)", "freeze :- not(alarm) and not(normal)", ] s = EvalStream("temp", nTriples, maxRand=200) prog = Program(rules, s) tList = list() for t in range(0, 2000): start = time.clock() res, tuples = prog.evaluate(t) end = time.clock() sys.stdout.write("Time = %d\r" % (t)); sys.stdout.flush() tList.append(end - start) print("Avg = %f seconds for %d triples (%f seconds per triple)!" % (float(sum(tList)) / len(tList), nTriples, (float(sum(tList) / len(tList) / nTriples)))) print("**********************************************************************************************************")
def test_5(self): stream = { 0: ["r(a,b)", 'r(c,d)', "s", "z(g)", "z(h)"], 1: ["r(a,b)", 'r(c,d)', "s", "z(g)", "z(h)"], 2: ["r(a,b)", 'r(c,d)', "a", "z(g)", "z(h)"], 3: ["r(e,f)", 'r(g,h)', "a", "z(g)", "z(h)"], 4: ["r(i,j)", 'r(k,l)', "a", "z(g)", "z(h)"], 5: ["r(m,n)", "s", "z(g)", "z(h)"], 6: ["r(m,n)", "s", "z(g)", "z(h)"], } rules = [ "p(B,C) :- time_win(1,0,1,diamond(not(s))) and time_win(2,0,1, diamond(r(B,C)))", ] s = TestStream(stream, 0, 6) prog = Program(rules, s) res, tuples = prog.evaluate(0) self.assertFalse(res) self.assertEquals(tuples, {}) res, tuples = prog.evaluate(1) self.assertFalse(res) self.assertEquals(tuples, {}) res, tuples = prog.evaluate(2) self.assertTrue(res) self.assertEquals(tuples, {2: set(['p(a,b)', 'p(c,d)'])}) res, tuples = prog.evaluate(3) self.assertTrue(res) self.assertEquals(tuples, {3: set(['p(a,b)', 'p(c,d)', 'p(e,f)', 'p(g,h)'])}) res, tuples = prog.evaluate(4) self.assertTrue(res) self.assertEquals(tuples, { 4: set(['p(a,b)', 'p(c,d)', 'p(e,f)', 'p(g,h)', 'p(i,j)', 'p(k,l)']) }) res, tuples = prog.evaluate(5) self.assertTrue(res) self.assertEquals( tuples, {5: set(['p(e,f)', 'p(g,h)', 'p(i,j)', 'p(k,l)', 'p(m,n)'])}) res, tuples = prog.evaluate(6) self.assertFalse(res) self.assertEquals(tuples, {})
def test35(self): stream = { 0: ['p(c,d)'], 1: ['p(a,b)'], 2: ['p(d,e)'], 3: ['p(e,f)'], 4: ['p(b,c)'], } rules = [ "q(X,Z) :- time_win(0, 0, 1, diamond(p(Y,Z))) and time_win(3,0,1, diamond(p(X,Y))) ", ] s = TestStream(stream, 0, 4) prog = Program(rules, s) res, tuples = prog.evaluate(0) self.assertFalse(res) self.assertEquals(tuples, {}) res, tuples = prog.evaluate(1) self.assertFalse(res) self.assertEquals(tuples, {}) res, tuples = prog.evaluate(2) self.assertTrue(res) self.assertEquals(tuples, {2: set(['q(c,e)'])}) res, tuples = prog.evaluate(3) self.assertTrue(res) self.assertEquals(tuples, {3: set(['q(d,f)'])}) res, tuples = prog.evaluate(4) self.assertTrue(res) self.assertEquals(tuples, {4: set(['q(a,c)'])})
def test27(self): stream = { 0: ["a(12)", "a(20)", "a(10)"], 1: ["a(100)"], 2: ["a(0)"], 3: [], 4: [], 5: [], 6: [], 7: [], 8: [], 9: [], } rules = [ "@(T, high(V)) :- time_win(2,0,1, @(T, a(V))) and COMP(>=,V,18)", "@(T, mid(V)) :- time_win(2,0,1, @(T, a(V))) and COMP(>=,V,12) and COMP(<, V, 18)", "@(T, low(V)) :- time_win(2,0,1, @(T, a(V))) and COMP(<,V,12)", ] s = TestStream(stream, 0, 5) prog = Program(rules, s) res, tuples = prog.evaluate(0) self.assertTrue(res) self.assertEquals(tuples, {0: set(["high(20)", "mid(12)", "low(10)"])}) res, tuples = prog.evaluate(1) self.assertTrue(res) self.assertEquals(tuples, { 0: set(["high(20)", "mid(12)", "low(10)"]), 1: set(["high(100)"]) }) res, tuples = prog.evaluate(2) self.assertTrue(res) self.assertEquals( tuples, { 0: set(["high(20)", "mid(12)", "low(10)"]), 1: set(["high(100)"]), 2: set(["low(0)"]) }) res, tuples = prog.evaluate(3) self.assertTrue(res) self.assertEquals(tuples, {1: set(["high(100)"]), 2: set(["low(0)"])}) res, tuples = prog.evaluate(4) self.assertTrue(res) self.assertEquals(tuples, {2: set(["low(0)"])}) res, tuples = prog.evaluate(5) self.assertFalse(res) self.assertEquals(tuples, {})
def evalBox(winSize, nTriples): print("******************* Box Evaluation winSize = %d, Triples = %d *********************" % (winSize, nTriples)) rules = [ "z(X) :- time_win(" + str(winSize) + ", 0, 1, box(p(X)))", ] s = EvalStream("p", nTriples, maxRand=(nTriples / 3 + 1)) prog = Program(rules, s) tList = list() for t in range(0, 2000): start = time.clock() res, tuples = prog.evaluate(t) end = time.clock() sys.stdout.write("Time = %d\r" % (t)) sys.stdout.flush() tList.append(end - start) print("Avg = %f seconds for %d triples (%f seconds per triple)!" % (float(sum(tList)) / len(tList), nTriples, (float(sum(tList) / len(tList) / nTriples)))) print("********************************************************")
def test_4(self): stream = { 0: ["r(a,b)", 'r(c,d)', "a", "z(g)", "z(h)"], 1: ["r(e,f)", 's(c,d)', "a", "z(g)", "z(h)"], 2: ["r(g,h)", 's(e,f)', "a", "z(g)", "z(h)"], 3: ["z(g,h)", 'z(e,f)', "a", "z(g)", "z(h)"], } rules = [ "p(B,C) :- time_win(1,0,1,diamond(not(s(C, B)))) and time_win(2,0,1, diamond(r(B,C)))", ] s = TestStream(stream, 0, 3) prog = Program(rules, s) res, tuples = prog.evaluate(0) self.assertTrue(res) self.assertEquals(tuples, {0: set(['p(a,b)', 'p(c,d)'])}) res, tuples = prog.evaluate(1) self.assertTrue(res) self.assertEquals(tuples, {1: set(['p(a,b)', 'p(c,d)'])}) res, tuples = prog.evaluate(2) self.assertFalse(res) self.assertEquals(tuples, {}) res, tuples = prog.evaluate(3) self.assertTrue(res) self.assertEquals(tuples, {3: set(['p(e,f)', 'p(g,h)'])})
def test4(self): stream = { 0: ["p", "q", "z(a,b,c)", "p(a)", "p(a,b,v)"], 1: ["q", "z(a,b,c)", "q(a)"], 2: ["q", "z(a,b,c)", "q(a)"], 3: ["p", "z(a,b,c)", "q(a)"], } rules = [ "d :- not(s)", "s :- not(h)", "b :- not(s) and not(h)", "h :- not(p)", ] s = TestStream(stream, 0, 2) prog = Program(rules, s) res, tuples = prog.evaluate(0) self.assertTrue(res) self.assertEquals(tuples, {0: set(['d', 's'])}) res, tuples = prog.evaluate(1) self.assertTrue(res) self.assertEquals(tuples, {1: set(['d', 'h'])}) res, tuples = prog.evaluate(2) self.assertTrue(res) self.assertEquals(tuples, {2: set(['d', 'h'])}) res, tuples = prog.evaluate(3) self.assertFalse(res) self.assertEquals(tuples, {})
def evalSingleJoin(winSize, nTriples): print("******************* Join Evaluation winSize = %d, Triples = %d *********************" % (winSize, nTriples)) # ************************************************ # Equivalent query in CSPARQL: # # REGISTER STREAM abc AS # SELECT ?s <http://example.org/stream/predicate#p1> ?o # FROM STREAM <http://ex.org/stream> [RANGE X s STEP 1s] # WHERE { # ?s <http://example.org/stream/predicate#p> ?t . # ?t <http://example.org/stream/predicate#p> ?o . # } # ************************************************ # Equivalent query in CQELS: # # SELECT ?s <http://example.org/stream/predicate#p1> ?o # WHERE { # STREAM <http://ex.org/stream>[RANGE X s] { # ?s <http://example.org/stream/predicate#p> ?t . # ?t <http://example.org/stream/predicate#p> ?o . # } # } # ************************************************ rules = [ "'http://example.org/stream/predicate#p1'(X,Z) :- time_win(" + str(winSize) + ", 0, 1, diamond('http://example.org/stream/predicate#p'(X,Y))) and time_win(" + str(winSize) + " ,0,1,diamond('http://example.org/stream/predicate#p'(Y,Z)))", ] s = RDFStream2(nTriples) prog = Program(rules, s) tList = list() for t in range(0, 2000): start = time.clock() res, tuples = prog.evaluate(t) end = time.clock() sys.stdout.write("Time = %d\r" % (t)); sys.stdout.flush() tList.append(end - start) print("Avg = %f seconds for %d triples (%f seconds per triple)!" % (float(sum(tList)) / len(tList), nTriples, (float(sum(tList) / len(tList) / nTriples)))) print("********************************************************")
def evalMultipleRules(nRules, winSize, nTriples): print("******************* Multirule Join Evaluation Rules = %d, winSize = %d, Triples = %d *********************" % (nRules, winSize, nTriples)) def generate_rules(nRules, winSize): rules = [ "'http://example.org/stream/predicate#p1'(X,Z) :- time_win(" + str(winSize) + ", 0, 1, diamond('http://example.org/stream/predicate#p'(X,Y))) and time_win(" + str(winSize) + ",0,1,diamond('http://example.org/stream/predicate#p'(Y,Z)))", ] return rules * nRules rules = generate_rules(nRules, winSize) s = RDFStream2(nTriples) prog = Program(rules, s) tList = list() for t in range(0, 2000): start = time.clock() res, tuples = prog.evaluate(t) end = time.clock() sys.stdout.write("Time = %d\r" % (t)); sys.stdout.flush() tList.append(end - start) print("Avg = %f seconds for %d triples (%f seconds per triple)!" % (float(sum(tList)) / len(tList), nTriples, (float(sum(tList) / len(tList) / nTriples)))) print("**********************************************************************************************************")
def test24(self): stream = { 0: ["p(a,b)", "z(a,b,c)", "p(a)", "p(a,b,v)"], 1: ["p(c,d)", "q", "z(a,b,c)", "q(a)"], 2: ["q", "z(a,b,c)", "q(a)"], 3: ["p(e,f)", "z(e,b,c)", "q(a)"], 4: ["p(g,h)", "z(a,b,c)", "q(a)"], 5: ["p(i,j)", "z(g,b,c)", "q(a)"], 6: [""], 7: [""], 8: [""], 9: [""], } rules = [ "h1(T1,X) :- time_win(1,0,1, @(T, p(X,Y))) and MATH(+,T1,T,2)", "h2(Y,X) :- time_win(2,0,1, diamond(h1(X,Y)))", ] s = TestStream(stream, 0, 5) prog = Program(rules, s) res, tuples = prog.evaluate(0) self.assertTrue(res) self.assertEquals(tuples, {0: set(['h1(2,a)', 'h2(a,2)'])}) res, tuples = prog.evaluate(1) self.assertTrue(res) self.assertEquals( tuples, {1: set(['h1(2,a)', 'h2(a,2)', 'h1(3,c)', 'h2(c,3)'])}) res, tuples = prog.evaluate(2) self.assertTrue(res) self.assertEquals(tuples, {2: set(['h1(3,c)', 'h2(c,3)'])}) res, tuples = prog.evaluate(3) self.assertTrue(res) self.assertEquals(tuples, {3: set(['h1(5,e)', 'h2(e,5)'])}) res, tuples = prog.evaluate(4) self.assertTrue(res) self.assertEquals( tuples, {4: set(['h1(5,e)', 'h2(e,5)', 'h1(6,g)', 'h2(g,6)'])}) res, tuples = prog.evaluate(5) self.assertTrue(res) self.assertEquals( tuples, {5: set(['h1(6,g)', 'h2(g,6)', 'h1(7,i)', 'h2(i,7)'])})
def test15(self): stream = { 0: ["p(a,b)", "q", "z(a,b,c)", "p(a)", "p(a,b,v)"], 1: ["p(c,d)", "q", "z(a,b,c)", "q(a)"], 2: ["q", "z(a,b,c)", "q(a)"], 3: ["p(e,f)", "z(a,b,c)", "q(a)"], 4: ["p(g,h)", "z(a,b,c)", "q(a)"], 5: ["p(i,j)", "z(a,b,c)", "q(a)"], 6: [""], 7: [""], 8: [""], 9: [""], } rules = [ "h(T,X) :- @(T, p(X,Y))", ] s = TestStream(stream, 0, 5) prog = Program(rules, s) res, tuples = prog.evaluate(0) self.assertTrue(res) self.assertEquals(tuples, {0: set(['h(0,a)'])}) res, tuples = prog.evaluate(1) self.assertTrue(res) self.assertEquals(tuples, {1: set(['h(0,a)', 'h(1,c)'])}) res, tuples = prog.evaluate(2) self.assertTrue(res) self.assertEquals(tuples, {2: set(['h(0,a)', 'h(1,c)'])}) res, tuples = prog.evaluate(3) self.assertTrue(res) self.assertEquals(tuples, {3: set(['h(0,a)', 'h(1,c)', 'h(3,e)'])}) res, tuples = prog.evaluate(4) self.assertTrue(res) self.assertEquals(tuples, {4: set(['h(0,a)', 'h(1,c)', 'h(3,e)', 'h(4,g)'])}) res, tuples = prog.evaluate(5) self.assertTrue(res) self.assertEquals( tuples, {5: set(['h(0,a)', 'h(1,c)', 'h(3,e)', 'h(4,g)', 'h(5,i)'])})
def test32(self): stream = { 0: ["http://ex.org/(a,b)", "qq(aa)", "z(a,b,c)", "p(a)", "p(a,b,v)"], 1: ["http://ex.org/(c,d)", "q", "z(a,b,c)", "q(a)"], 2: ["http://ex.org/(e,f)", "z(a,b,c)", "q(a)"], 3: ["http://ex.org/(g,h)", "z(a,b,c)", "q(a)"], 4: ["pp(a,d)", "z(a,b,c)", "q(a)"], 5: ["http://ex.org/(i,j)", "z(a,b,c)", "q(a)"], 6: [""], 7: [""], 8: [""], 9: [""], } rules = [ "h(Y,X) :- time_win(2,0,1, diamond('http://ex.org/'(X,Y)))", ] s = TestStream(stream, 0, 5) prog = Program(rules, s) res, tuples = prog.evaluate(0) self.assertTrue(res) self.assertEquals(tuples, {0: set(['h(b,a)'])}) res, tuples = prog.evaluate(1) self.assertTrue(res) self.assertEquals(tuples, {1: set(['h(d,c)', 'h(b,a)'])}) res, tuples = prog.evaluate(2) self.assertTrue(res) self.assertEquals(tuples, {2: set(['h(f,e)', 'h(d,c)', 'h(b,a)'])}) res, tuples = prog.evaluate(3) self.assertTrue(res) self.assertEquals(tuples, {3: set(['h(h,g)', 'h(f,e)', 'h(d,c)'])}) res, tuples = prog.evaluate(4) self.assertTrue(res) self.assertEquals(tuples, {4: set(['h(h,g)', 'h(f,e)'])}) res, tuples = prog.evaluate(5) self.assertTrue(res) self.assertEquals(tuples, {5: set(['h(h,g)', 'h(j,i)'])})
def test20(self): stream = { 0: ["p(a,b)", "z(a,b,c)", "p(a)", "p(a,b,v)"], 1: ["p(c,d)", "q", "z(a,b,c)", "q(a)"], 2: ["q", "z(a,b,c)", "q(a)"], 3: ["p(e,f)", "z(e,b,c)", "q(a)"], 4: ["p(g,h)", "z(a,b,c)", "q(a)"], 5: ["p(i,j)", "z(g,b,c)", "q(a)"], 6: [""], 7: [""], 8: [""], 9: [""], } rules = [ "h1(T,X) :- tuple_win(3,0,1, @(T, p(X,Y))) and z(X,W,Z)", ] s = TestStream(stream, 0, 5) prog = Program(rules, s) res, tuples = prog.evaluate(0) self.assertTrue(res) self.assertEquals(tuples, {0: set(['h1(0,a)'])}) res, tuples = prog.evaluate(1) self.assertFalse(res) self.assertEquals(tuples, {}) res, tuples = prog.evaluate(2) self.assertFalse(res) self.assertEquals(tuples, {}) res, tuples = prog.evaluate(3) self.assertTrue(res) self.assertEquals(tuples, {3: set(['h1(3,e)'])}) res, tuples = prog.evaluate(4) self.assertFalse(res) self.assertEquals(tuples, {}) res, tuples = prog.evaluate(5) self.assertTrue(res) self.assertEquals(tuples, {5: set(['h1(4,g)'])})
def test13(self): stream = { 0: ["p(a,b)", "qq(aa)", "z(a,b,c)", "p(a)", "p(a,b,v)"], 1: ["p(a,b)", "q", "z(a,b,c)", "q(a)"], 2: ["p(a,b)", "z(a,b,c)", "q(a)"], 3: ["p(g,h)", "p(a,b)", "z(a,b,c)", "q(a)"], 4: ["p(g,h)", "z(a,b,c)", "q(a)"], 5: ["p(i,j)", "z(a,b,c)", "q(a)"], 6: [""], 7: [""], 8: [""], 9: [""], } rules = [ "h(Y,X) :- tuple_win(4, diamond(p(X,Y)))", ] s = TestStream(stream, 0, 5) prog = Program(rules, s) res, tuples = prog.evaluate(0) self.assertTrue(res) self.assertEquals(tuples, {0: set(['h(b,a)'])}) res, tuples = prog.evaluate(1) self.assertTrue(res) self.assertEquals(tuples, {1: set(['h(b,a)'])}) res, tuples = prog.evaluate(2) self.assertTrue(res) self.assertEquals(tuples, {2: set(['h(b,a)'])}) res, tuples = prog.evaluate(3) self.assertTrue(res) self.assertEquals(tuples, {3: set(['h(b,a)', 'h(h,g)'])}) res, tuples = prog.evaluate(4) self.assertTrue(res) self.assertEquals(tuples, {4: set(['h(b,a)', 'h(h,g)'])}) res, tuples = prog.evaluate(5) self.assertTrue(res) self.assertEquals(tuples, {5: set(['h(j,i)', 'h(h,g)'])})
def test9(self): stream = { 0: ["p(a,b)", "qq(aa)", "z(a,b,c)", "p(a)", "p(a,b,v)"], 1: ["p(a,b)", "q", "z(a,b,c)", "q(a)"], 2: ["p(a,b)", "z(a,b,c)", "q(a)"], 3: ["p(c,d)", "z(a,b,c)", "q(a)"], 4: ["p(c,d)", "z(a,b,c)", "q(a)"], 5: ["p(c,d)", "z(a,b,c)", "q(a)"], 6: [""], 7: [""], 8: [""], 9: [""], } rules = [ "h(Y,X) :- time_win(2,0,1, box(p(X,Y)))", ] s = TestStream(stream, 0, 5) prog = Program(rules, s) res, tuples = prog.evaluate(0) self.assertTrue(res) self.assertEquals(tuples, {0: set(['h(b,a)'])}) res, tuples = prog.evaluate(1) self.assertTrue(res) self.assertEquals(tuples, {1: set(['h(b,a)'])}) res, tuples = prog.evaluate(2) self.assertTrue(res) self.assertEquals(tuples, {2: set(['h(b,a)'])}) res, tuples = prog.evaluate(3) self.assertFalse(res) self.assertEquals(tuples, {}) res, tuples = prog.evaluate(4) self.assertFalse(res) self.assertEquals(tuples, {}) res, tuples = prog.evaluate(5) self.assertTrue(res) self.assertEquals(tuples, {5: set(['h(d,c)'])})
def test6(self): stream = { 0: ["p(a,b)", "q", "z(a,b,c)", "p(a)", "p(a,b,v)"], 1: ["p(c,d)", "q", "z(a,b,c)", "q(a)"], 2: ["q", "z(a,b,c)", "q(a)"], 3: ["p(e,f)", "z(a,b,c)", "q(a)"], 4: ["p(g,h)", "z(a,b,c)", "q(a)"], 5: [""], 6: [""], 7: [""], 8: [""], 9: [""], } rules = [ "h(Y,X) :- diamond(p(X,Y))", ] s = TestStream(stream, 0, 4) prog = Program(rules, s) res, tuples = prog.evaluate(0) self.assertTrue(res) self.assertEquals(tuples, {0: set(['h(b,a)'])}) res, tuples = prog.evaluate(1) self.assertTrue(res) self.assertEquals(tuples, {1: set(['h(b,a)', 'h(d,c)'])}) res, tuples = prog.evaluate(2) self.assertTrue(res) self.assertEquals(tuples, {2: set(['h(b,a)', 'h(d,c)'])}) res, tuples = prog.evaluate(3) self.assertTrue(res) self.assertEquals(tuples, {3: set(['h(b,a)', 'h(d,c)', 'h(f,e)'])}) res, tuples = prog.evaluate(4) self.assertTrue(res) self.assertEquals(tuples, {4: set(['h(b,a)', 'h(d,c)', 'h(f,e)', 'h(h,g)'])})
def test5(self): stream = { 0: ["p(a,b)", "q", "z(a,b,c)", "p(a)", "p(a,b,v)"], 1: ["p(a,b)", "q", "z(a,b,c)", "q(a)"], 2: ["q", "z(a,b,c)", "q(a)"], 3: ["p(e,f)", "z(a,b,c)", "q(a)"], 4: ["p(g,h)", "z(a,b,c)", "q(a)"], 5: [""], 6: [""] } rules = [ "h(Y,X) :- box(p(X,Y))", ] s = TestStream(stream, 0, 4) prog = Program(rules, s) res, tuples = prog.evaluate(0) self.assertTrue(res) self.assertEquals(tuples, {0: set(['h(b,a)'])}) res, tuples = prog.evaluate(1) self.assertTrue(res) self.assertEquals(tuples, {1: set(['h(b,a)'])}) res, tuples = prog.evaluate(2) self.assertFalse(res) self.assertEquals(tuples, {}) res, tuples = prog.evaluate(3) self.assertFalse(res) self.assertEquals(tuples, {}) res, tuples = prog.evaluate(4) self.assertFalse(res) self.assertEquals(tuples, {})
def test_3(self): stream = { 0: [ "r(a,b)", "a", "z(a,b,c)", ], 1: ["r(c,d)", "b", "z(a,b,c)", "t(a)"], 2: ["b", "e(a,b,c)", "e(a)"], 3: ["b", "e(a,b,c)", "e(a)"], } rules = [ "s(A) :- time_win(1,0,1,diamond(t(A)))", "p(B,C) :- time_win(1,0,1,diamond(not(s(C)))) and time_win(2,0,1, diamond(r(B,C)))", "q(XX,YY) :- p(XX,YY)", "z(Y,X) :- q(X,Y)", ] s = TestStream(stream, 0, 3) prog = Program(rules, s) res, tuples = prog.evaluate(0) self.assertTrue(res) self.assertEquals(tuples, {0: set(['p(a,b)', 'q(a,b)', 'z(b,a)'])}) res, tuples = prog.evaluate(1) self.assertTrue(res) self.assertEquals(tuples, {1: set(['p(a,b)', 'q(a,b)', 'z(b,a)', 's(a)'])}) res, tuples = prog.evaluate(2) self.assertTrue(res) self.assertEquals(tuples, {2: set(['s(a)'])}) res, tuples = prog.evaluate(3) self.assertTrue(res) self.assertEquals(tuples, {3: set(['p(c,d)', 'q(c,d)', 'z(d,c)'])})
def evalGroundWeightedQuery(dStream, rules, queryFormula, t): s = TestStream(dStream, 0, len(dStream) - 1) prog = Program(rules, s) aStream = None for i in range(t + 1): res, aStream = prog.evaluate(i) down = True formStack = [queryFormula] valStack = [None] timeStack = [t] idxStack = [] minMaxStack = [(0, len(dStream) - 1)] while (0 < len(formStack)): if (down): if (formStack[-1].operator == Formula.BOX): print("BOX") valStack[-1] = one valStack.append(one) idxStack.append(0) timeStack.append(-1) down = False elif (formStack[-1].operator == Formula.DIAMOND): print("DIAMOND") valStack[-1] = zero valStack.append(zero) idxStack.append(0) timeStack.append(-1) down = False elif (formStack[-1].operator == Formula.ATT): print("ATT") timeStack.append(formStack[-1].arguments[0]) formStack.append(formStack[-1].arguments[1]) elif (formStack[-1].operator == Formula.TRIANGLE): print("TRIANGLE") formStack.append(formStack[-1].arguments[0]) minMaxStack.append(minMaxStack[0]) elif (formStack[-1].operator == Formula.AND): print("AND") formStack.append(formStack[-1].arguments[0]) idxStack.append(0) elif (formStack[-1].operator == Formula.OR): print("OR") formStack.append(formStack[-1].arguments[0]) idxStack.append(0) elif (formStack[-1].operator == Formula.NEG): print("NEG") formStack.append(formStack[-1].arguments[0]) elif (formStack[-1].operator == Formula.WINDOW): print("WINDOW") cur = minMaxStack[-1] newMin = max(cur[0], timeStack[-1] - formStack[-1].arguments[0]) newMax = min(cur[1], timeStack[-1] + formStack[-1].arguments[1]) minMaxStack.append((newMin, newMax)) formStack.append(formStack[-1].arguments[2]) elif (formStack[-1].operator == Formula.NONE): if (formStack[-1].basicType == Formula.PROP): print("PROP") if (timeStack[-1] in aStream and timeStack[-1] >= minMaxStack[-1][0] and timeStack[-1] <= minMaxStack[-1][1]): stringRep = formStack[-1].arguments[0] if (len(formStack[-1].arguments) > 1): stringRep += '(' args = formStack[-1].arguments[1:] for i in range(len(args) - 1): stringRep += args[i] stringRep += ',' stringRep += args[-1] stringRep += ')' print(stringRep) valStack[-1] = one if stringRep in aStream[ timeStack[-1]] else zero else: valStack[-1] = 0 down = False elif (formStack[-1].basicType == Formula.NUM): print("NUM") valStack[-1] = formStack[-1].arguments[0] down = False else: if (formStack[-1].operator == Formula.BOX): print("BOX") lastVal = valStack.pop() timeStack.pop() valStack[-1] = mult(valStack[-1], lastVal) minMax = minMaxStack[-1] if (idxStack[-1] <= minMax[1] - minMax[0]): down = True formStack.append(formStack[-1].arguments[0]) valStack.append(None) timeStack.append(minMax[0] + idxStack[-1]) idxStack[-1] += 1 else: formStack.pop() idxStack.pop() elif (formStack[-1].operator == Formula.DIAMOND): print("DIAMOND") lastVal = valStack.pop() timeStack.pop() valStack[-1] = add(valStack[-1], lastVal) minMax = minMaxStack[-1] if (idxStack[-1] <= minMax[1] - minMax[0]): down = True formStack.append(formStack[-1].arguments[0]) valStack.append(None) timeStack.append(minMax[0] + idxStack[-1]) idxStack[-1] += 1 else: formStack.pop() idxStack.pop() elif (formStack[-1].operator == Formula.ATT): print("ATT") formStack.pop() timeStack.pop() elif (formStack[-1].operator == Formula.TRIANGLE): print("TRIANGLE") formStack.pop() minMaxStack.pop() elif (formStack[-1].operator == Formula.AND): print("AND") if (idxStack[-1] == 0): down = True formStack.append(formStack[-1].arguments[1]) valStack.append(None) idxStack[-1] = 1 else: formStack.pop() cVal = valStack.pop() valStack[-1] = mult(valStack[-1], cVal) idxStack.pop() elif (formStack[-1].operator == Formula.OR): print("OR") if (idxStack[-1] == 0): down = True formStack.append(formStack[-1].arguments[1]) valStack.append(None) idxStack[-1] = 1 else: formStack.pop() cVal = valStack.pop() valStack[-1] = add(valStack[-1], cVal) idxStack.pop() elif (formStack[-1].operator == Formula.NEG): print("NEG") valStack[-1] = one if valStack[-1] == zero else zero formStack.pop() elif (formStack[-1].operator == Formula.WINDOW): print("WINDOW") formStack.pop() minMaxStack.pop() elif (formStack[-1].operator == Formula.NONE): if (formStack[-1].basicType == Formula.PROP): print("PROP") formStack.pop() elif (formStack[-1].basicType == Formula.NUM): print("NUM") formStack.pop() return valStack.pop()
"r(a,b)", "a", "z(a,b,c)", ], 1: ["p(c,d)", "b", "z(a,b,c)", "t(a)"], 2: ["b", "e(a,b,c)", "e(a)"], } rules = [ "s(A) :- t(A)", "p(B,C) :- r(B,C) and not(s(C))", "q(XX,YY) :- p(XX,YY)", "z(Y,X) :- q(X,Y)", ] s = TestStream(stream, 0, 2) prog = Program(rules, s) tuples = {} for i in range(len(stream) - 1): res, tupl = prog.evaluate(i) tuples.update(tupl) add = lambda x, y: x + y mult = lambda x, y: x * y one = 1 zero = 0 print(add(one, one))
def test19(self): stream = { 0: ["p(a,b)", "z(a,b,c)", "p(a)", "p(a,b,v)"], 1: [], 2: [], 3: [], 4: ["p(c,d)", "q", "z(a,b,c)", "q(a)"], 5: ["q", "z(a,b,c)", "q(a)"], 6: ["p(e,f)", "z(a,b,c)", "q(a)"], 7: [], 8: [], 9: [], } rules = [ "h1(T,X) :- tuple_win(4, @(T, p(X,Y)))", "h2 :- time_win(2,0,1, diamond(h1(X,Y)))", "h3 :- not(h2)", ] s = TestStream(stream, 0, 9) prog = Program(rules, s) res, tuples = prog.evaluate(0) self.assertTrue(res) self.assertEquals(tuples, {0: set(['h1(0,a)', "h2"])}) res, tuples = prog.evaluate(1) self.assertTrue(res) self.assertEquals(tuples, {1: set(['h1(0,a)', "h2"])}) res, tuples = prog.evaluate(2) self.assertTrue(res) self.assertEquals(tuples, {2: set(['h1(0,a)', "h2"])}) res, tuples = prog.evaluate(3) self.assertTrue(res) self.assertEquals(tuples, {3: set(['h1(0,a)', "h3"])}) res, tuples = prog.evaluate(4) self.assertTrue(res) self.assertEquals(tuples, {4: set(['h1(0,a)', 'h1(4,c)', 'h2'])}) res, tuples = prog.evaluate(5) self.assertTrue(res) self.assertEquals(tuples, {5: set(['h1(4,c)', "h2"])}) res, tuples = prog.evaluate(6) self.assertTrue(res) self.assertEquals(tuples, {6: set(['h1(6,e)', "h2"])}) res, tuples = prog.evaluate(7) self.assertTrue(res) self.assertEquals(tuples, {7: set(['h1(6,e)', "h2"])}) res, tuples = prog.evaluate(8) self.assertTrue(res) self.assertEquals(tuples, {8: set(['h1(6,e)', "h2"])}) res, tuples = prog.evaluate(9) self.assertTrue(res) self.assertEquals(tuples, {9: set(['h1(6,e)', "h3"])})
def test29(self): stream = { 0: ["a(12)", "a(20)", "a(10)"], 1: ["a(100)"], 2: ["a(30)"], 3: ["a(25)"], 4: [], 5: [], 6: [], 7: [], 8: [], 9: [], } rules = [ "@(T, high) :- time_win(2,0,1, @(T, a(V))) and COMP(>=,V,18)", "@(T, mid) :- time_win(2,0,1, @(T, a(V))) and COMP(>=,V,12) and COMP(<, V, 18)", "@(T, low) :- time_win(2,0,1, @(T, a(V))) and COMP(<,V,12)", "lfu :- time_win(2,0,1, box(high))", "lru :- time_win(2,0,1, box(mid))", "fifo :- time_win(2,0,1, box(low)) and time_win(2,0,1, diamond(realtm))", "done :- lfu", "done :- lru", "done :- fifo", "random :- not(done)", ] s = TestStream(stream, 0, 6) prog = Program(rules, s) res, tuples = prog.evaluate(0) self.assertTrue(res) self.assertEquals( tuples, {0: set(["high", "mid", "low", "lfu", "lru", "done"])}) res, tuples = prog.evaluate(1) self.assertTrue(res) self.assertEquals(tuples, { 0: set(["high", "mid", "low"]), 1: set(["high", "lfu", "done"]) }) res, tuples = prog.evaluate(2) self.assertTrue(res) self.assertEquals( tuples, { 0: set(["high", "mid", "low"]), 1: set(["high"]), 2: set(["high", "lfu", "done"]) }) res, tuples = prog.evaluate(3) self.assertTrue(res) self.assertEquals(tuples, { 1: set(["high"]), 2: set(["high"]), 3: set(["high", "lfu", "done"]) }) res, tuples = prog.evaluate(4) self.assertTrue(res) self.assertEquals(tuples, { 2: set(["high"]), 3: set(["high"]), 4: set(["random"]) }) res, tuples = prog.evaluate(5) self.assertTrue(res) self.assertEquals(tuples, {3: set(["high"]), 5: set(["random"])}) res, tuples = prog.evaluate(6) self.assertTrue(res) self.assertEquals(tuples, {6: set(['random'])})