def test_with_source(self, mock_cfg, src1):
     standard_cfg(mock_cfg)
     theo = theory.Z3Theory(pp("p(X) :- q(a=X)."))
     theo.build_theory()
     self.assertEqual(
         (['X'], [z3r.Cube({0: 421}, 1), z3r.Cube({0: 567}, 1)]),
         theo.query(parser.parse_atom("p(X)")))
예제 #2
0
def main():
    """Octant entry point"""
    logging.basicConfig(stream=sys.stderr, level=logging.WARNING)
    args = sys.argv[1:]
    options.init(args)
    if cfg.CONF.ipsize != 32:
        primitives.TYPES['ip_address'] = (primitives.IpAddressType(
            size=cfg.CONF.ipsize))
    time_required = cfg.CONF.time
    csv_out = cfg.CONF.csv
    pretty = cfg.CONF.pretty
    debug = cfg.CONF.debug
    if debug:
        logging.getLogger().setLevel(logging.DEBUG)
    if csv_out and (time_required or pretty):
        print("Cannot use option --csv with --time or --pretty.")
        sys.exit(1)
    rules = []
    start = time.clock()
    try:
        for rule_file in cfg.CONF.theory:
            rules += parser.parse_file(rule_file)
    except base.Z3ParseError as exc:
        print(exc.args[1])
        sys.exit(1)
    if time_required:
        print("Parsing time: {}".format(time.clock() - start))
    start = time.clock()
    try:
        theory = datalog_theory.Z3Theory(rules)
        theory.build_theory()
        if time_required:
            print("Data retrieval: {}".format(time.clock() - start))
        for query in cfg.CONF.query:
            start = time.clock()
            atom = parser.parse_atom(query)
            variables, answers = theory.query(atom)
            if csv_out:
                printer.print_csv(variables, answers)
            else:
                print_result(query, variables, answers,
                             time.clock() - start if time_required else None,
                             cfg.CONF.pretty)
        if not csv_out:
            print("*" * 80)
    except base.Z3NotWellFormed as exc:
        print("Badly formed program: {}".format(exc.args[1]))
        sys.exit(1)
    except base.Z3TypeError as exc:
        print("Type error: {}".format(exc.args[1]))
        sys.exit(1)
    except base.Z3SourceError as exc:
        print("Error in datasource: {}".format(exc.args[1]))
        sys.exit(1)
    except base.Z3ParseError:
        print("Parser error in query.")
        sys.exit(1)
 def test_build_theory_simplify(self, mock_cfg):
     standard_cfg(mock_cfg)
     theo = theory.Z3Theory(pp(PROG2))
     theo.build_theory()
     rules = theo.context.get_rules()
     # Just one rule and just one atom in the rule body.
     self.assertEqual(1, len(rules))
     expected = '(forall ((X (_ BitVec 4))) (=> (bvsle X #x3) (p X)))'
     self.assertEqual(expected, rules[0].sexpr())
 def test_build_theory_simple(self, mock_cfg, src1, src2):
     standard_cfg(mock_cfg)
     theo = theory.Z3Theory(pp(PROG1))
     theo.build_theory()
     r = theo.query(parser.parse_atom("p(X)"))
     self.assertEqual((['X'], [z3r.Cube({0: 3}, 1)]), r)
     r = theo.query(parser.parse_atom("q(X)"))
     self.assertEqual(
         (['X'], [z3r.Cube({0: 2}, 1), z3r.Cube({0: 3}, 1)]),
         r)
 def test_query_bad(self, mock_cfg, src1, src2):
     standard_cfg(mock_cfg)
     theo = theory.Z3Theory(pp(PROG1))
     theo.build_theory()
     self.assertRaises(
         obase.Z3NotWellFormed,
         lambda: theo.query(parser.parse_atom("h(X)")))
     self.assertRaises(
         obase.Z3NotWellFormed,
         lambda: theo.query(parser.parse_atom("p(X,Y)")))
 def test_simple_result(self, mock_cfg, src1, src2):
     standard_cfg(mock_cfg)
     theo = theory.Z3Theory(pp("p(). q() :- !p()."))
     theo.build_theory()
     self.assertEqual(([], True), theo.query(parser.parse_atom("p()")))
     self.assertEqual(([], False), theo.query(parser.parse_atom("q()")))
 def test_build_bad(self, mock_cfg, src1, src2):
     standard_cfg(mock_cfg)
     theo = theory.Z3Theory(pp("p(X:ukw_type)."))
     self.assertRaises(obase.Z3TypeError, theo.build_theory)