Exemplo n.º 1
0
 def test_inject(self):
     theory = mock.MagicMock(spec=topdown.TopDownTheory)
     world = {'t': theory}
     theory.name = 't'
     theory.schema = ast.Schema()
     theory.schema.map['l'] = [mkc('Int'), mkc('Int')]
     theory.select.return_value = ast.parse('l(1,2). l(3,4). l(5,6)')
     context = z3theory.Z3Context()
     # An external theory world
     context.theories = world
     # inject the declaration of external relation without rules
     param_types = [
         context.type_registry.get_type(typ)
         for typ in ['Int', 'Int', 'Bool']
     ]
     relation = z3.Function('t:l', *param_types)
     context.context.register_relation(relation)
     context.relations['t:l'] = relation
     # the test
     context.inject('t', 'l')
     rules = context.context._rules  # pylint: disable=E1101
     self.assertIs(True, all(r.decl().name() == 't:l' for r in rules))
     self.assertEqual([[1, 2], [3, 4], [5, 6]],
                      sorted([[c.as_long() for c in r.children()]
                              for r in rules]))
Exemplo n.º 2
0
 def init_one_theory(self, prog):
     context = z3theory.Z3Context()
     world = {}
     t1 = z3theory.Z3Theory('t1', theories=world)
     world['t1'] = t1
     context.register(t1)
     for rule in ast.parse(prog):
         t1.insert(rule)
     return (context, t1)
Exemplo n.º 3
0
 def init_one_builtin(self, body, typ, arity):
     context = z3theory.Z3Context()
     world = {}
     t1 = z3theory.Z3Theory('t1', theories=world)
     world['t1'] = t1
     context.register(t1)
     rule = ast.parse('p(x) :- ' + body + '.')[0]
     t1.schema.map['p'] = [mkc(typ)]
     context.declare_tables()
     return (context, t1, rule, {0: [mkc(typ)] * arity})
Exemplo n.º 4
0
 def init_one_rule(self):
     context = z3theory.Z3Context()
     world = {}
     t1 = z3theory.Z3Theory('t1', theories=world)
     world['t1'] = t1
     context.register(t1)
     rule = ast.parse('p(x) :- l(x,y), l(3,x).')[0]
     t1.schema.map['l'] = [mkc('Int'), mkc('Int')]
     t1.schema.map['p'] = [mkc('Int')]
     context.declare_tables()
     return (context, t1, rule)
Exemplo n.º 5
0
 def test_registration(self):
     context = z3theory.Z3Context()
     theory = mock.MagicMock(z3theory.Z3Theory)
     name = 'test'
     world = {}
     theory.name = name
     theory.theories = world
     world['foo'] = theory
     context.register(theory)
     self.assertIn(name, context.z3theories)
     self.assertEqual(theory, context.z3theories[name])
     self.assertEqual(world, context.theories)
     context.drop(theory)
     self.assertNotIn(name, context.z3theories)
Exemplo n.º 6
0
 def init_two_theories(prog):
     context = z3theory.Z3Context()
     world = {}
     for name in ['t1', 't2']:
         world[name] = z3theory.Z3Theory(name, theories=world)
     t1, t2 = world['t1'], world['t2']
     context.register(t1)
     # t2 is kept external
     # Declare rules
     for rule in ast.parse(prog):
         t1.insert(rule)
     # typechecker
     t2.schema.map['p'] = [mkc('Small')]
     t2.schema.map['q'] = [mkc('Str')]
     return context
Exemplo n.º 7
0
 def test_compile_facts(self):
     context = z3theory.Z3Context()
     world = {}
     t1 = z3theory.Z3Theory('t1', theories=world)
     world['t1'] = t1
     context.register(t1)
     for rule in ast.parse('l(1,2). l(3,4). l(5,6).'):
         t1.insert(rule)
     t1.schema.map['l'] = [mkc('Int'), mkc('Int')]
     context.declare_tables()
     context.compile_facts(t1)
     rules = context.context.get_rules()
     self.assertEqual(3, len(rules))
     self.assertIs(True, all(r.decl().name() == 't1:l' for r in rules))
     self.assertEqual([[1, 2], [3, 4], [5, 6]],
                      [[c.as_long() for c in r.children()] for r in rules])
Exemplo n.º 8
0
    def test_declare_table(self):
        """Test single table declaration

        Declare table declares the relation of a single table from its type
        """
        context = z3theory.Z3Context()
        name = 'test'
        tbname = 'table'
        world = {}
        theory = z3theory.Z3Theory(name, theories=world)
        theory.schema.map[tbname] = [mkc('Int'), mkc('Int')]
        world[name] = theory
        context.declare_table(theory, tbname)
        # Only the mock as a _relations element and pylint is confused
        rels = context.context._relations  # pylint: disable=E1101
        self.assertEqual(1, len(rels))
        self.assertEqual(name + ':' + tbname, rels[0].name())
        self.assertEqual(3, len(rels[0]._typs))
Exemplo n.º 9
0
 def init_three_theories(self):
     context = z3theory.Z3Context()
     world = {}
     for name in ['t1', 't2', 't3']:
         world[name] = z3theory.Z3Theory(name, theories=world)
     t1, t2, t3 = world['t1'], world['t2'], world['t3']
     context.register(t1)
     context.register(t2)
     # t3 is kept external
     # Declare rules
     for rule in ast.parse('p(x) :- t2:r(x), t3:s(x). q(x) :- p(x). p(4).'):
         t1.insert(rule)
     for rule in ast.parse('r(x) :- t1:p(x).'):
         t2.insert(rule)
     # typechecker
     t1.schema.map['p'] = [mkc('Int')]
     t1.schema.map['q'] = [mkc('Int')]
     t2.schema.map['r'] = [mkc('Int')]
     t3.schema.map['s'] = [mkc('Int')]
     t3.schema.map['t'] = [mkc('Int')]
     return context
Exemplo n.º 10
0
    def test_declare_tables(self):
        """Test declaration of internal z3theories tables

        Declare tables must iterate over all schemas and create relations
        with the right arity and types
        """
        context = z3theory.Z3Context()
        world = {}
        t1 = z3theory.Z3Theory('t1', theories=world)
        t2 = z3theory.Z3Theory('t2', theories=world)
        world['t1'] = t1
        world['t2'] = t2
        t1.schema.map['p'] = [mkc('Int')]
        t1.schema.map['q'] = [mkc('Str'), mkc('Str')]
        t2.schema.map['k'] = [mkc('Bool')]
        context.register(t1)
        context.register(t2)
        context.declare_tables()
        # Only the mock as a _relations element and pylint is confused
        rels = context.context._relations  # pylint: disable=E1101
        self.assertEqual(3, len(rels))
        self.assertIn('t1:q', context.relations)
        self.assertEqual(2, len(context.relations['t1:p']._typs))