Ejemplo n.º 1
0
 def test_unfolding(self):
     prog = "t(X) :- p(X,Y), X = Y & 1.\ns(X) :- t(X), 2 = X & 2."
     rules = parser.wrapped_parse(prog)
     extensible = {"p": ["int", "int"]}
     unfold = origin.Origin(rules, extensible)
     self.assertEqual((2, True), unfold.tables["p"])
     self.assertEqual((1, False), unfold.tables["t"])
Ejemplo n.º 2
0
 def test_type(self):
     rules = parser.wrapped_parse(prog0)
     external = {'q': ['int', 'int'], 'p': ['int']}
     unfold = origin.Origin(rules, external)
     unfold.type()
     result = unfold.table_types
     typ_s0 = result['s'][0]
     self.assertIsInstance(typ_s0, origin.UFDisj)
Ejemplo n.º 3
0
 def test_strategy_1(self):
     rules = parser.wrapped_parse(prog1)
     external = {'q': ['int', 'int'], 'p': ['int'], 'r': ['int']}
     unfold = unfolding.Unfolding(rules, external, (lambda t: t))
     ogn = origin.Origin(rules, external)
     var_types = ogn.type()
     result = unfold.rule_strategy(var_types, rules[0])
     filtered = [(t, sorted(pos)) for (((t, pos),), _) in result]
     self.assertEqual([('q', [0, 1]), ('p', [0])], filtered)
Ejemplo n.º 4
0
 def test_strategy_2(self):
     rules = parser.wrapped_parse(prog2)
     rule = rules[0]
     external = {'q': ['int'], 'p': ['int']}
     unfold = unfolding.Unfolding(rules, external, (lambda t: t))
     ogn = origin.Origin(rules, external)
     var_types = ogn.type()
     result = unfold.rule_strategy(var_types, rule)
     filtered = [
         sorted([(t, sorted(pos)) for (t, pos) in plan],
                key=lambda p: p[0])
         for (plan, _) in result]
     self.assertEqual([[('p', [0])], [('p', [0]), ('q', [0])]], filtered)
Ejemplo n.º 5
0
 def test_type_variables(self):
     rules = parser.wrapped_parse(prog0)
     vars = [rules[rid].head.args[pos].full_id()
             for (rid, pos) in [(1, 0), (3, 0), (3, 1)]]
     external = {'q': ['int', 'int'], 'p': ['int']}
     unfold = origin.Origin(rules, external)
     unfold.initialize_types()
     unfold.type_variables()
     typs = [unfold.var_types[v] for v in vars]
     self.assertEqual(['p', 'q', 'q'], [t.table for t in typs])
     self.assertEqual([0, 0, 1], [t.pos for t in typs])
     occs = [t.occurrence for t in typs]
     self.assertEqual(occs[1], occs[2])
     self.assertEqual(False, occs[0] == occs[1])
Ejemplo n.º 6
0
 def test_initialize_types_and_get_atom_types(self):
     rules = parser.wrapped_parse(prog0)
     atom_t = rules[0].head
     atom_z = ast.Atom('z', [])
     external = {'q': ['int', 'int'], 'p': ['int']}
     unfold = origin.Origin(rules, external)
     unfold.initialize_types()
     self.assertEqual(
         [origin.UFBot(), origin.UFGround(1, "t", None)],
         unfold.table_types['t'])
     self.assertEqual(
         origin.UFBot(),
         unfold.get_atom_type(atom_t, 0))
     self.assertEqual(None, unfold.get_atom_type(atom_t, 3))
     self.assertEqual(None, unfold.get_atom_type(atom_z, 0))
Ejemplo n.º 7
0
 def proceed(self):
     """The main entry point: type, then compute a strategy"""
     origins = origin.Origin(self.rules, self.extensible_tables)
     var_types = origins.type()
     return self.strategy(var_types)
Ejemplo n.º 8
0
 def test_partially_ground(self):
     rules = parser.wrapped_parse(prog0)
     unfold = origin.Origin(rules, {})
     result = unfold.get_partially_ground_preds()
     self.assertEqual({'s': set(), 't': set([1])}, result)