示例#1
0
    def test_determinism(self):
        def get_set(env):
            mgr = env.formula_manager
            r = set(mgr.Symbol("x%d" % i) for i in xrange(1000))
            for (f, _, _, _) in get_example_formulae(env):
                r |= set([f])
            return r

        # As first thing on the environment we build the set of formulae
        l1 = list(get_set(get_env()))

        # We try this ten times...
        for _ in xrange(10):
            # Do something to screw up memory layout...
            for y in (Symbol("y%d" % i) for i in xrange(1000)):
                self.assertIsNotNone(y)

            with Environment() as new_env:
                # As first thing on the environment we build the set of formulae
                l_test = list(get_set(new_env))

                # The ordering of the sets should be the same...
                for i,f in enumerate(l1):
                    nf = new_env.formula_manager.normalize(f)
                    self.assertEqual(nf, l_test[i])
示例#2
0
    def test_normalization(self):
        from pysmt.environment import Environment

        env2 = Environment()
        mgr2 = env2.formula_manager

        ty = ArrayType(BOOL, REAL)
        x = FreshSymbol(ty)
        fty = FunctionType(BOOL, (ty, ))
        f = FreshSymbol(fty)
        g = Function(f, (x, ))
        self.assertIsNotNone(g)
        self.assertNotIn(g, mgr2)
        g2 = mgr2.normalize(g)
        self.assertIn(g2, mgr2)
        # Since the types are from two different environments, they
        # should be different.
        x2 = g2.arg(0)
        ty2 = x2.symbol_type()
        self.assertFalse(ty2 is ty, ty)
        fname = g2.function_name()
        fty2 = fname.symbol_type()
        self.assertFalse(fty2 is fty, fty)

        # Test ArrayValue
        h = Array(BVType(4), BV(0, 4))
        h2 = mgr2.normalize(h)
        self.assertEqual(h.array_value_index_type(),
                         h2.array_value_index_type())
        self.assertFalse(h.array_value_index_type() is \
                         h2.array_value_index_type())
示例#3
0
    def test_pickling(self):
        import pickle

        src_env = Environment()
        dst_env = Environment()
        src_mgr = src_env.formula_manager
        dst_mgr = dst_env.formula_manager

        a = src_mgr.Symbol("A")
        b = src_mgr.Symbol("B")
        f = src_mgr.And(a, src_mgr.Not(b))

        self.assertEqual(str(f), "(A & (! B))", str(f))

        # NOTE: We cannot use the textual format for pickle, because
        # we are using slots. We can, however, use more advanced
        # versions of pickle. Therefore, we specify here to use the
        # latest protocol.
        serialized = pickle.dumps(f, pickle.HIGHEST_PROTOCOL)

        f_new = pickle.loads(serialized)
        f_new = dst_mgr.normalize(f)

        args = f_new.args()
        self.assertEqual(
            str(args[0]), "A",
            "Expecting symbol A, " + "symbol %s found instead" % str(args[0]))

        a = dst_mgr.Symbol("A")
        b = dst_mgr.Symbol("B")
        g = dst_mgr.And(a, dst_mgr.Not(b))

        # Contextualized formula is memoized
        self.assertEqual(f_new, g, "%s != %s" % (id(f_new), id(g)))
        # But it differs from the one in the other formula manager
        self.assertNotEqual(f_new, f)

        # Normalizing a formula in the same manager should not
        # be a problem
        f_new = src_mgr.normalize(f)
        self.assertEqual(f_new, f, "%s != %s" % (id(a), id(b)))

        # Verify that new types do not lead to errors in pickling
        from pysmt.test.examples import get_example_formulae
        for (f, _, _, _) in get_example_formulae():
            pickle.dumps(f, pickle.HIGHEST_PROTOCOL)
示例#4
0
    def test_with_env(self):
        env1 = get_env()
        a1 = Symbol("A", REAL)
        with Environment():
            env2 = get_env()
            self.assertIsNotNone(env2, "Context should create an environment")
            self.assertNotEqual(env1, env2, "Context should create a new environment")
            a2 = Symbol("A", REAL)
            self.assertNotEqual(a1, a2, "Symbols in different context should differ")

        a3 = Symbol("A", REAL)
        self.assertEqual(a1, a3, "Exiting a context should restore the previous environment")
示例#5
0
    def test_pickling(self):
        import pickle

        src_env = Environment()
        dst_env = Environment()
        src_mgr = src_env.formula_manager
        dst_mgr = dst_env.formula_manager

        a = src_mgr.Symbol("A")
        b = src_mgr.Symbol("B")
        f = src_mgr.And(a, src_mgr.Not(b))

        self.assertEqual(str(f), "(A & (! B))", str(f))

        serialized = pickle.dumps(f)

        f_new = pickle.loads(serialized)
        f_new = dst_mgr.normalize(f)

        args = f_new.args()
        self.assertEqual(
            str(args[0]), "A",
            "Expecting symbol A, " + "symbol %s found instead" % str(args[0]))

        a = dst_mgr.Symbol("A")
        b = dst_mgr.Symbol("B")
        g = dst_mgr.And(a, dst_mgr.Not(b))

        # Contextualized formula is memoized
        self.assertEqual(f_new, g, "%s != %s" % (id(f_new), id(g)))
        # But it differs from the one in the other formula manager
        self.assertNotEqual(f_new, f)

        # Normalizing a formula in the same manager should not
        # be a problem
        f_new = src_mgr.normalize(f)
        self.assertEqual(f_new, f, "%s != %s" % (id(a), id(b)))
示例#6
0
 def __init__(self,
              regular_strategy,
              thresholds,
              tight_mode=True,
              class_label=True,
              background_knowledge=None):
     self.regular_strategy = regular_strategy  # type: SelectionStrategy
     self.thresholds = thresholds
     self.tight_mode = tight_mode
     assert class_label, "Currently only the positive setting is supported"
     self.class_label = class_label
     self.environment = Environment()
     if background_knowledge is None:
         self.background_knowledge = self.environment.formula_manager.TRUE()
     else:
         self.background_knowledge = self.environment.formula_manager.normalize(
             background_knowledge)
def extract_and_replace_literals(formula: FNode,
                                 cache_size=512
                                 ) -> (Environment, FNode, LiteralInfo):
    abstractions = {}
    booleans = {}

    env = Environment()
    fm = env.formula_manager

    build_logic = {
        AND: fm.And,
        OR: fm.Or,
        NOT: fm.Not,
        IMPLIES: fm.Implies,
        IFF: fm.Iff,
        ITE: fm.Ite
    }

    @functools.lru_cache(maxsize=cache_size)
    def recurse(formula):
        if formula.node_type() == BOOL_CONSTANT:
            return formula
        if formula.node_type() == SYMBOL:
            assert formula.symbol_type() == BOOL
            if formula.symbol_name() not in booleans:
                booleans[formula.symbol_name()] = f"b{len(booleans)}"
            return fm.Symbol(booleans[formula.symbol_name()])
        elif formula.node_type() in LOGIC_TYPES:
            return build_logic[formula.node_type()](
                *(recurse(arg) for arg in formula.args()))
        elif formula.node_type() in TEST_TYPES:
            assert all(is_all_real(arg)
                       for arg in formula.args()), "Wrong test expression"
            canonical_formula = to_canonical(formula)
            if canonical_formula not in abstractions:
                abstractions[canonical_formula] = f"a{len(abstractions)}"
            return fm.Symbol(abstractions[canonical_formula])
        else:
            raise RuntimeError(f"Cannot extract literals from {formula}")

    repl_formula = recurse(formula)
    # TODO: having to return env here is a bit ugly? Might be redundant, but I'd rather be safe
    return env, repl_formula, LiteralInfo(abstractions, booleans)
示例#8
0
    def test_write_shortcut(self):
        read_configuration(self._get_config_path("config1.ini"))

        tmp = self._get_tmp_file()
        fname = tmp.name

        write_configuration(fname)

        new_env = Environment()
        read_configuration(fname, new_env)
        os.remove(fname)

        env = get_env()
        self.assertEqual(new_env.enable_infix_notation,
                         env.enable_infix_notation)
        self.assertEqual(new_env.factory.solver_preference_list,
                         env.factory.solver_preference_list)

        self.assertTrue("mathsat-smt" in new_env.factory.all_solvers())
        self.assertTrue("z3-smt" in new_env.factory.all_solvers())
示例#9
0
    def test_write(self):
        env = get_env()
        configure_environment(self._get_config_path("config1.ini"), env)

        tmp = self._get_tmp_file()
        fname = tmp.name

        write_environment_configuration(fname, env)

        new_env = Environment()
        configure_environment(fname, new_env)
        try:
            os.remove(fname)
        except WindowsError:
            print("Error deleting file...")

        self.assertEqual(new_env.enable_infix_notation,
                         env.enable_infix_notation)
        self.assertEqual(new_env.factory.solver_preference_list,
                         env.factory.solver_preference_list)

        self.assertTrue("mathsat-smt" in new_env.factory.all_solvers())
        self.assertTrue("z3-smt" in new_env.factory.all_solvers())