def setup_class(cls): """Set the test up.""" cls.automaton = SymbolicAutomaton() state_0 = 0 cls.automaton.set_initial_state(state_0) cls.automaton.set_accepting_state(state_0, True) cls.automaton.add_transition((state_0, BooleanTrue(), state_0))
def test_BooleanTrue_Class(self): assert sympify(True) is true assert S.true is true #as_set() assert(BooleanTrue(True).as_set()) is not set()
def visit_Conditional(self, o): if o.condition is BooleanTrue(): return c.Collection(self._visit(o.then_body)) else: then_body = c.Block(self._visit(o.then_body)) if o.else_body: else_body = c.Block(self._visit(o.else_body)) return c.If(ccode(o.condition), then_body, else_body) else: return c.If(ccode(o.condition), then_body)
def fill_value(self): dtype = self.dtype if isinstance(dtype, NativeInteger): value = LiteralInteger(1) elif isinstance(dtype, NativeReal): value = LiteralFloat(1.) elif isinstance(dtype, NativeComplex): value = LiteralComplex(LiteralFloat(1.), LiteralFloat(0.)) elif isinstance(dtype, NativeBool): value = BooleanTrue() else: raise TypeError('Unknown type') return value
def init_value(self): dtype = self.dtype if isinstance(dtype, NativeInteger): value = 1 elif isinstance(dtype, NativeReal): value = 1.0 elif isinstance(dtype, NativeComplex): value = 1.0 elif isinstance(dtype, NativeBool): value = BooleanTrue() else: raise TypeError('Unknown type') return value
def setup_class(cls): """Set the test up.""" A, B, C = sympy.symbols("A B C") aut = SymbolicAutomaton() aut.create_state() aut.create_state() aut.create_state() aut.set_initial_state(3) aut.set_accepting_state(0, True) aut.set_accepting_state(1, True) aut.set_accepting_state(3, True) trfun = { 3: {0: A | ~B, 2: B & ~A}, 0: {1: BooleanTrue()}, 2: {2: BooleanTrue()}, 1: {1: BooleanTrue()}, } for s in trfun: for d, guard in trfun[s].items(): aut.add_transition((s, guard, d)) cls.automaton = aut cls.determinized = aut.determinize()
def to_sympy( formula: Formula, replace: Optional[Dict[AtomSymbol, sympy.Symbol]] = None) -> Boolean: """ Translate a PLFormula object into a SymPy expression. :param formula: the formula to translate. :param replace: an optional mapping from symbols to replace to other replacement symbols. :return: the SymPy formula object equivalent to the formula. """ if replace is None: replace = {} if isinstance(formula, PLTrue): return BooleanTrue() elif isinstance(formula, PLFalse): return BooleanFalse() elif isinstance(formula, PLAtomic): symbol = replace.get(formula.s, formula.s) return sympy.Symbol(symbol) elif isinstance(formula, PLNot): return sympy.Not(to_sympy(formula.f, replace=replace)) elif isinstance(formula, PLOr): return sympy.simplify( sympy.Or(*[to_sympy(f, replace=replace) for f in formula.formulas])) elif isinstance(formula, PLAnd): return sympy.simplify( sympy.And( *[to_sympy(f, replace=replace) for f in formula.formulas])) elif isinstance(formula, PLImplies): return sympy.simplify( sympy.Implies( *[to_sympy(f, replace=replace) for f in formula.formulas])) elif isinstance(formula, PLEquivalence): return sympy.simplify( sympy.Equivalent( *[to_sympy(f, replace=replace) for f in formula.formulas])) else: raise ValueError("Formula is not valid.")
def evaluate(formula: Boolean, i: PropInt) -> bool: """ Evaluate a SymPy boolean expression against a propositional interpretation. The symbols not present in the propositional interpretation will be considered False. >>> from sympy.parsing.sympy_parser import parse_expr >>> evaluate(parse_expr("a & b"), {"a": True}) False >>> evaluate(parse_expr("a | b"), {"b": True}) True >>> evaluate(parse_expr("a"), {"b": True}) False :param formula: a sympy.logic.boolalg.Boolean. :param i: a propositional interpretation, i.e. a mapping from symbol identifiers to True/False :return: True if the formula is true in the interpretation, False o/w. """ return formula.subs(i).replace(Symbol, BooleanFalse) == BooleanTrue()
def complete(self) -> "SymbolicAutomaton": """Complete the automaton.""" states = set(self.states) initial_state = self.initial_state final_states = self.accepting_states transitions = set() sink_state = None for source in states: transitions_from_source = self._transition_function.get(source, {}) transitions.update( set( map(lambda x: (source, x[1], x[0]), transitions_from_source.items()))) guards = transitions_from_source.values() guards_negation = simplify(Not(Or(*guards))) if satisfiable(guards_negation) is not False: sink_state = len(states) if sink_state is None else sink_state transitions.add((source, guards_negation, sink_state)) if sink_state is not None: states.add(sink_state) transitions.add((sink_state, BooleanTrue(), sink_state)) return SymbolicAutomaton._from_transitions(states, initial_state, final_states, transitions)