def test_empty(self): # full empty th = nonrecursive.NonrecursiveRuleTheory() th.insert(compile.parse1('p(x) :- q(x)')) th.insert(compile.parse1('p(1)')) th.insert(compile.parse1('q(2)')) th.empty() self.assertEqual(len(th.content()), 0) # empty with tablenames th = nonrecursive.NonrecursiveRuleTheory() th.insert(compile.parse1('p(x) :- q(x)')) th.insert(compile.parse1('p(1)')) th.insert(compile.parse1('q(2)')) th.empty(['p']) e = helper.datalog_equal(th.content_string(), 'q(2)') self.assertTrue(e) # empty with invert th = nonrecursive.NonrecursiveRuleTheory() th.insert(compile.parse1('p(x) :- q(x)')) th.insert(compile.parse1('p(1)')) th.insert(compile.parse1('q(2)')) th.empty(['p'], invert=True) correct = ('p(x) :- q(x) p(1)') e = helper.datalog_equal(th.content_string(), correct) self.assertTrue(e)
def test_empty(self): # full empty th = nonrecursive.NonrecursiveRuleTheory() th.insert(compile.parse1("p(x) :- q(x)")) th.insert(compile.parse1("p(1)")) th.insert(compile.parse1("q(2)")) th.empty() self.assertEqual(len(th.content()), 0) # empty with tablenames th = nonrecursive.NonrecursiveRuleTheory() th.insert(compile.parse1("p(x) :- q(x)")) th.insert(compile.parse1("p(1)")) th.insert(compile.parse1("q(2)")) th.empty(["p"]) e = helper.datalog_equal(th.content_string(), "q(2)") self.assertTrue(e) # empty with invert th = nonrecursive.NonrecursiveRuleTheory() th.insert(compile.parse1("p(x) :- q(x)")) th.insert(compile.parse1("p(1)")) th.insert(compile.parse1("q(2)")) th.empty(["p"], invert=True) correct = "p(x) :- q(x) p(1)" e = helper.datalog_equal(th.content_string(), correct) self.assertTrue(e)
def test_empty(self): # full empty th = NonrecursiveRuleTheory() th.insert(compile.parse1('p(x) :- q(x)')) th.insert(compile.parse1('p(1)')) th.insert(compile.parse1('q(2)')) th.empty() self.assertEqual(len(th.content()), 0) # empty with tablenames th = NonrecursiveRuleTheory() th.insert(compile.parse1('p(x) :- q(x)')) th.insert(compile.parse1('p(1)')) th.insert(compile.parse1('q(2)')) th.empty(['p']) e = helper.datalog_equal(th.content_string(), 'q(2)') self.assertTrue(e) # empty with invert th = NonrecursiveRuleTheory() th.insert(compile.parse1('p(x) :- q(x)')) th.insert(compile.parse1('p(1)')) th.insert(compile.parse1('q(2)')) th.empty(['p'], invert=True) correct = ('p(x) :- q(x) p(1)') e = helper.datalog_equal(th.content_string(), correct) self.assertTrue(e)
def test_policy_rule_api_model(self): """Test the policy model with rules.""" api = self.api # create 2 policies, add rules to each (aliceid, _) = api['policy'].add_item({'name': 'alice'}, {}) api['policy'].add_item({'name': 'bob'}, {}) (_, rule1) = api['rule'].add_item( {'rule': 'p(x) :- q(x)'}, {}, context={'policy_id': 'alice'}) (_, rule2) = api['rule'].add_item( {'rule': 'r(x) :- s(x)'}, {}, context={'policy_id': 'bob'}) # check we got the same thing back that we inserted alice_rules = api['rule'].get_items( {}, context={'policy_id': 'alice'})['results'] bob_rules = api['rule'].get_items( {}, context={'policy_id': 'bob'})['results'] self.assertEqual(len(alice_rules), 1) self.assertEqual(len(bob_rules), 1) e = helper.datalog_equal(alice_rules[0]['rule'], rule1['rule']) self.assertTrue(e) e = helper.datalog_equal(bob_rules[0]['rule'], rule2['rule']) self.assertTrue(e) # check that deleting the policy also deletes the rules api['policy'].delete_item(aliceid, {}) alice_rules = api['rule'].get_items( {}, context={'policy_id': 'alice'})['results'] self.assertEqual(len(alice_rules), 0)
def test_select(self): engine = vm_placement.ComputePlacementEngine() engine.debug_mode() engine.insert('p(x) :- q(x)') engine.insert('q(1)') ans = engine.select('p(x)') self.assertTrue(helper.datalog_equal(ans, 'p(1)'))
def test_select(self): engine = ComputePlacementEngine() engine.debug_mode() engine.insert('p(x) :- q(x)') engine.insert('q(1)') ans = engine.select('p(x)') self.assertTrue(helper.datalog_equal(ans, 'p(1)'))
def test_initialize(self): """Test initialize() functionality of Runtime.""" run = runtime.Runtime() run.insert('p(1) p(2)') run.initialize(['p'], ['p(3)', 'p(4)']) e = helper.datalog_equal(run.select('p(x)'), 'p(3) p(4)') self.assertTrue(e)
def test_initialize_tables(self): """Test initialize_tables() functionality of Runtime.""" run = runtime.Runtime() run.create_policy('test') run.insert('p(1) p(2)') run.initialize_tables(['p'], ['p(3)', 'p(4)']) e = helper.datalog_equal(run.select('p(x)'), 'p(3) p(4)') self.assertTrue(e)
def test_theory_in_head(self): engine = ComputePlacementEngine() engine.debug_mode() engine.policy.insert(engine.parse1('p(x) :- nova:q(x)')) engine.policy.insert(engine.parse1('nova:q(1)')) ans = engine.policy.select(engine.parse1('p(x)')) ans = " ".join(str(x) for x in ans) self.assertTrue(helper.datalog_equal(ans, 'p(1)'))
def test_theory_in_head(self): engine = vm_placement.ComputePlacementEngine() engine.debug_mode() engine.policy.insert(engine.parse1('p(x) :- nova:q(x)')) engine.policy.insert(engine.parse1('nova:q(1)')) ans = engine.policy.select(engine.parse1('p(x)')) ans = " ".join(str(x) for x in ans) self.assertTrue(helper.datalog_equal(ans, 'p(1)'))
def test_modal_with_theory(self): """Test that the modal operators work properly with a theory.""" run = agnostic.Runtime() run.debug_mode() run.create_policy("test") run.insert("execute[nova:p(x)] :- q(x)", "test") run.insert("q(1)", "test") self.assertTrue(helper.datalog_equal(run.select("execute[nova:p(x)]", "test"), "execute[nova:p(1)]"))
def test_initialize_tables(self): """Test initialize_tables() functionality of agnostic.""" run = agnostic.Runtime() run.create_policy('test') run.insert('p(1) p(2)') facts = [Fact('p', (3, )), Fact('p', (4, ))] run.initialize_tables(['p'], facts) e = helper.datalog_equal(run.select('p(x)'), 'p(3) p(4)') self.assertTrue(e)
def test_initialize_tables(self): """Test initialize_tables() functionality of agnostic.""" run = agnostic.Runtime() run.create_policy('test') run.insert('p(1) p(2)') facts = [Fact('p', (3,)), Fact('p', (4,))] run.initialize_tables(['p'], facts) e = helper.datalog_equal(run.select('p(x)'), 'p(3) p(4)') self.assertTrue(e)
def test_modals(self): """Test that the modal operators work properly.""" run = agnostic.Runtime() run.debug_mode() run.create_policy("test") run.insert('execute[p(x)] :- q(x)', 'test') run.insert('q(1)', 'test') self.assertTrue(helper.datalog_equal( run.select('execute[p(x)]', 'test'), 'execute[p(1)]'))
def check(self, run, action_sequence, query, correct, msg, delta=False): original_db = str(run.theory[self.DEFAULT_THEORY]) actual = run.simulate( query, self.DEFAULT_THEORY, action_sequence, self.ACTION_THEORY, delta=delta) e = helper.datalog_equal(actual, correct) self.assertTrue(e, msg + " (Query results not correct)") e = helper.db_equal( str(run.theory[self.DEFAULT_THEORY]), original_db) self.assertTrue(e, msg + " (Rollback failed)")
def test_modal_with_theory(self): """Test that the modal operators work properly with a theory.""" run = agnostic.Runtime() run.debug_mode() run.create_policy("test") run.insert('execute[nova:p(x)] :- q(x)', 'test') run.insert('q(1)', 'test') self.assertTrue( helper.datalog_equal(run.select('execute[nova:p(x)]', 'test'), 'execute[nova:p(1)]'))
def check(self, run, action_sequence, query, correct, msg, delta=False): original_db = str(run.theory[self.DEFAULT_THEORY]) actual = run.simulate(query, self.DEFAULT_THEORY, action_sequence, self.ACTION_THEORY, delta=delta) e = helper.datalog_equal(actual, correct) self.assertTrue(e, msg + " (Query results not correct)") e = helper.db_equal(str(run.theory[self.DEFAULT_THEORY]), original_db) self.assertTrue(e, msg + " (Rollback failed)")
def test_wrong_arity_index(self): run = agnostic.Runtime() run.create_policy('test1') run.insert('p(x) :- r(x), q(y, x)') run.insert('r(1)') run.insert('q(1,1)') # run query first to build index self.assertTrue(helper.datalog_equal(run.select('p(x)'), 'p(1)')) # next insert causes an exceptionsince the thing we indexed on # doesn't exist self.assertRaises(IndexError, run.insert, 'q(5)') # double-check that the error didn't result in an inconsistent state self.assertEqual(run.select('q(5)'), '')
def test_indexing(self): MAX = 100 th = NREC_THEORY for table in ("a", "b", "c"): updates = [self._create_event(table, (i,), True, th) for i in range(MAX)] self._agnostic.update(updates) # With indexing, this query should take O(n) time where n is MAX. # Without indexing, this query will take O(n^3). self._agnostic.insert("d(x) :- a(x), b(x), c(x)", th) ans = " ".join(["d(%d)" % i for i in range(MAX)]) self.assertTrue(helper.datalog_equal(self._agnostic.select("d(x)", th), ans))
def test_dump_load(self): """Test if dumping/loading theories works properly.""" run = runtime.Runtime() run.debug_mode() policy = ('p(4,"a","bcdef ghi", 17.1) ' 'p(5,"a","bcdef ghi", 17.1) ' 'p(6,"a","bcdef ghi", 17.1)') run.insert(policy) full_path = os.path.realpath(__file__) path = os.path.dirname(full_path) path = os.path.join(path, "snapshot") run.dump_dir(path) run = runtime.Runtime() run.load_dir(path) e = helper.datalog_equal(str(run.theory[run.DEFAULT_THEORY]), policy, 'Service theory dump/load') self.assertTrue(e)
def test_dump_load(self): """Test if dumping/loading theories works properly.""" run = agnostic.Runtime() run.create_policy('test') run.debug_mode() policy = ('p(4,"a","bcdef ghi", 17.1) ' 'p(5,"a","bcdef ghi", 17.1) ' 'p(6,"a","bcdef ghi", 17.1)') run.insert(policy) full_path = os.path.realpath(__file__) path = os.path.dirname(full_path) path = os.path.join(path, "snapshot") run.dump_dir(path) run = agnostic.Runtime() run.load_dir(path) e = helper.datalog_equal(run.theory['test'].content_string(), policy, 'Service theory dump/load') self.assertTrue(e)
def test_dump_load(self): """Test if dumping/loading theories works properly.""" run = runtime.Runtime() run.create_policy('test') run.debug_mode() policy = ('p(4,"a","bcdef ghi", 17.1) ' 'p(5,"a","bcdef ghi", 17.1) ' 'p(6,"a","bcdef ghi", 17.1)') run.insert(policy) full_path = os.path.realpath(__file__) path = os.path.dirname(full_path) path = os.path.join(path, "snapshot") run.dump_dir(path) run = runtime.Runtime() run.load_dir(path) e = helper.datalog_equal(run.theory['test'].content_string(), policy, 'Service theory dump/load') self.assertTrue(e)
def check(self, rule, data, correct, possibilities=None): rule = compile.parse1(rule, use_modules=False) data = compile.parse(data, use_modules=False) possibilities = possibilities or '' possibilities = compile.parse(possibilities, use_modules=False) possibilities = [compile.Rule(x, []) for x in possibilities] poss = {} for rule_lit in possibilities: if rule_lit.head.tablename() not in poss: poss[rule_lit.head.tablename()] = set([rule_lit]) else: poss[rule_lit.head.tablename()].add(rule_lit) th = MultiModuleNonrecursiveRuleTheory() th.debug_mode() for lit in data: th.insert(lit) result = th.instances(rule, poss) actual = " ".join(str(x) for x in result) e = helper.datalog_equal(actual, correct) self.assertTrue(e)
def check(self, rule, data, correct, possibilities=None): rule = compile.parse1(rule, use_modules=False) data = compile.parse(data, use_modules=False) possibilities = possibilities or '' possibilities = compile.parse(possibilities, use_modules=False) possibilities = [compile.Rule(x, []) for x in possibilities] poss = {} for rule_lit in possibilities: if rule_lit.head.tablename() not in poss: poss[rule_lit.head.tablename()] = set([rule_lit]) else: poss[rule_lit.head.tablename()].add(rule_lit) th = nonrecursive.MultiModuleNonrecursiveRuleTheory() th.debug_mode() for lit in data: th.insert(lit) result = th.instances(rule, poss) actual = " ".join(str(x) for x in result) e = helper.datalog_equal(actual, correct) self.assertTrue(e)
def check_equal(self, actual_string, correct_string, msg): self.assertTrue(helper.datalog_equal( actual_string, correct_string, msg))
def check(self, run, query_string, correct_string, msg): actual_string = run.select(query_string) self.assertTrue(helper.datalog_equal( actual_string, correct_string, msg))
def check_equal(actual, correct): e = helper.datalog_equal(actual, correct) self.assertTrue(e)
def check(self, run, query_string, correct_string, msg): actual_string = run.select(query_string) self.assertTrue( helper.datalog_equal(actual_string, correct_string, msg))