Пример #1
0
 def test_eq(self):
     a1 = ast.Atom('p', [])
     a2 = ast.Atom('q', [])
     a3 = ast.Atom('r', [])
     self.assertIs(True, ast.Rule(a1, [a2, a3]) == ast.Rule(a1, [a2, a3]))
     self.assertIs(False, ast.Rule(a2, [a2, a3]) == ast.Rule(a1, [a2, a3]))
     self.assertIs(False, ast.Rule(a1, [a1, a3]) == ast.Rule(a1, [a2, a3]))
 def test_translate(self):
     fp = z3.Fixedpoint()
     atom1 = ast.Atom('p', [
         ast.NumConstant(2, 'int4'),
         ast.NumConstant(3, 'int4'),
         ast.Variable('X', 'int4')
     ])
     atom2 = ast.Atom('p', [
         ast.NumConstant(2, 'int4'),
         ast.NumConstant(3, 'int4'),
         ast.Variable('Y', 'int4')
     ])
     int4 = z3.BitVecSort(4)
     p = z3.Function('p', int4, int4, int4, z3.BoolSort())
     a0 = z3.BitVecVal(2, int4)
     a1 = z3.BitVecVal(3, int4)
     a2 = z3.Const('X', int4)
     a3 = z3.Const('Y', int4)
     args1 = [a0, a1, a2]
     args2 = [a0, a1, a3]
     project = projection.Projection([], None)
     project.grounded = {'p': (0, 1)}
     project.items = {'p': {}}
     project.relations = {'p': p}
     result = project.translate(fp, atom1, args1)
     self.assertIn((0, 1), project.items['p'])
     self.assertIn((a0, a1), project.items['p'][(0, 1)])
     p0 = project.items['p'][(0, 1)][(a0, a1)]
     self.assertIs(True, z3.is_true(z3.simplify(p0(a2) == result)))
     result2 = project.translate(fp, atom2, args2)
     self.assertIs(True, z3.is_true(z3.simplify(p0(a3) == result2)))
Пример #3
0
def mk_r():
    v1 = ast.Variable('V1')
    v2 = ast.Variable('V2')
    v3 = ast.Variable('V3')
    a1 = ast.Atom('p', [v1, v2])
    a2 = ast.Atom('q', [v1, v3])
    a3 = ast.Atom('r', [v3, v2])
    r = ast.Rule(a1, [a2, a3])
    return r, v1, v2, v3
Пример #4
0
 def test_atom(self):
     v = ast.Variable('V')
     n = ast.NumConstant(3)
     a = ast.Atom('p', [v, n])
     self.assertEqual('p', a.table)
     self.assertEqual([v, n], a.args)
     self.assertIs(False, a.negated)
     self.assertEqual('p', str(a)[0])
     a = ast.Atom('p', [v, n], negated=True)
     self.assertEqual('p', a.table)
     self.assertIs(True, a.negated)
     self.assertEqual('~p', str(a)[0:2])
Пример #5
0
def p_positive_eq(t):
    '''positive : expr EQUAL eexpr
                | expr LT eexpr
                | expr LE eexpr
                | expr GT eexpr
                | expr GE eexpr
    '''   # noqa: H405
    t[0] = ast.Atom(table=t[2], args=[t[1], t[3]])
Пример #6
0
 def find_base_relations(self):
     """Extracts base relations of the theory"""
     aux_counter = 0
     new_rules = []
     for rule in self.rules:
         if self.datasource.is_extensible(rule.head):
             raise base.Z3NotWellFormed(
                 "No base predicate allowed in head: " + rule.head.table)
         for i, atom in enumerate(rule.body):
             if not self.datasource.is_extensible(atom):
                 continue
             fields = self.extensible_tables.setdefault(atom.table, [])
             if atom.labels is None:
                 raise base.Z3NotWellFormed(
                     "No labels for extensible atom {}".format(atom))
             for label in atom.labels:
                 if label not in fields:
                     fields.append(label)
             if atom.negated:
                 new_table = "_negated_%d" % aux_counter
                 aux_counter += 1
                 new_atom = ast.Atom(new_table, atom.args, negated=True)
                 var_row = [
                     ast.Variable("V%d" % i) for i in range(len(atom.args))
                 ]
                 atom_head = ast.Atom(new_table, var_row)
                 atom_body = ast.Atom(atom.table,
                                      var_row,
                                      labels=atom.labels)
                 new_rule = ast.Rule(atom_head, [atom_body])
                 new_rules.append(new_rule)
                 rule.body[i] = new_atom
     self.rules.extend(new_rules)
     for fields in self.extensible_tables.values():
         fields.sort()
     for rule in self.rules:
         for atom in rule.body:
             if self.datasource.is_extensible(atom):
                 self.flatten(atom, self.extensible_tables[atom.table])
Пример #7
0
 def test_eq(self):
     v = ast.Variable('V')
     n = ast.NumConstant(3)
     self.assertIs(True, ast.Atom('p', [v]) == ast.Atom('p', [v]))
     self.assertIs(
         False, ast.Atom('p', [v], negated=True) == ast.Atom('p', [v]))
     self.assertIs(
         False, ast.Atom('p', [v]) == ast.Atom('p', [n]))
Пример #8
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))
Пример #9
0
def p_positive_empty(t):
    'positive : IDENT OPAR CPAR'
    t[0] = ast.Atom(table=t[1], args=[])
Пример #10
0
def p_positive_named(t):
    'positive : IDENT OPAR expr_list_named CPAR'
    t[0] = ast.Atom(table=t[1], args=t[3][1], labels=t[3][0])
Пример #11
0
def p_positive(t):
    'positive : IDENT OPAR expr_list CPAR'
    t[0] = ast.Atom(table=t[1], args=t[3])
Пример #12
0
 def test_primitive(self):
     """Registration"""
     self.assertIs(True, self.src.is_extensible(ast.Atom("T1", [])))
     self.assertIs(False, self.src.is_extensible(ast.Atom("G3", [])))
Пример #13
0
def mk_a():
    o, v1, v2, v3 = mk_o()
    a = ast.Atom('p', [o, v1])
    return a, v1, v2, v3