def solve(self, simplify=False): formula = And(self.constraints) if simplify: formula = formula.simplify() model = get_model(formula) assert model is not None # check for unsatisfiability return model
class TransitionSystem: """ Symbolic transition system. All the objects are from PySMT (e.g. Symbols, formulas...) The TS is a tuple < state_var, init, trans > """ def __init__(self, env=None, helper=None): if env is None: self.env = get_env() self.helper = Helper(self.env) else: self.env = env self.helper = helper assert (self.helper.env == env) # internal representation of the transition system self.state_vars = set() self.var_types = {} self.init = TRUE_PYSMT() self.trans = TRUE_PYSMT() self.final = FALSE_PYSMT() def add_var(self, var): self.state_vars.add(var) def product(self, other_ts): """ Computes the synchronous product of self with other_ts, storing the product in self. Given TS1 = <V1, I1, T1> and TS2 = <V2, I2, T2> the product is the transition system TSP = <V1 union V2, I1 and I2, T1 and T2> (V are the state variables, I is the initial condition, T the transition relation) """ self.state_vars.update(other_ts.state_vars) self.init = And(self.init, other_ts.init) self.trans = And(self.trans, other_ts.trans) def __repr__(self): """ Not efficient, need to use a buffer...""" res = "State vars: " for v in self.state_vars: res += ", %s" % v res += "\nINIT: " res += str(self.init.serialize()) res += "\nTRANS: " res += str(self.trans.simplify().serialize()) return res
def test_create_and_solve(self): solver = Solver(logic=QF_BOOL) varA = Symbol("A", BOOL) varB = Symbol("B", BOOL) f = And(varA, Not(varB)) g = f.substitute({varB: varA}) solver.add_assertion(g) res = solver.solve() self.assertFalse(res, "Formula was expected to be UNSAT") h = And(g, Bool(False)) simp_h = h.simplify() self.assertEqual(simp_h, Bool(False))
def test_create_and_solve(self): solver = Solver(logic=QF_BOOL) varA = Symbol("A", BOOL) varB = Symbol("B", BOOL) f = And(varA, Not(varB)) g = f.substitute({varB:varA}) solver.add_assertion(g) res = solver.solve() self.assertFalse(res, "Formula was expected to be UNSAT") h = And(g, Bool(False)) simp_h = h.simplify() self.assertEqual(simp_h, Bool(False))
def test_trivial_false_and(self): x, y, z = (Symbol(name) for name in "xyz") f = And(x, y, z, Not(x)) self.assertEqual(f.simplify(), FALSE())
def test_and_flattening(self): x, y, z = (Symbol(name) for name in "xyz") f1 = And(x, y, z) f2 = And(x, And(y, z)) self.assertEqual(f2.simplify(), f1)
def test_trivial_false_and(self): x,y,z = (Symbol(name) for name in "xyz") f = And(x, y, z, Not(x)) self.assertEqual(f.simplify(), FALSE())
def test_and_flattening(self): x,y,z = (Symbol(name) for name in "xyz") f1 = And(x, y, z) f2 = And(x, And(y, z)) self.assertEqual(f2.simplify(), f1)