def setUp(self): """Set up test fixtures, if any.""" self.a_sym = Symbol("a") self.b_sym = Symbol("b") self.c_sym = Symbol("c") self.alphabet = Alphabet({self.a_sym, self.b_sym, self.c_sym}) # Propositions self.a = AtomicFormula(self.a_sym) self.b = AtomicFormula(self.b_sym) self.c = AtomicFormula(self.c_sym) self.not_a = Not(self.a) self.not_a_and_b = And(self.not_a, self.b) self.not_a_or_c = Or(self.not_a, self.c) self.true = TrueFormula() self.false = FalseFormula() self.symbol2truth = { self.a_sym: True, self.b_sym: False, self.c_sym: True } self.I = PLInterpretation(self.alphabet, self.symbol2truth) self.PL = PL(self.alphabet)
def test_to_nnf_allowed_formulas_not_normalized(self): a_sym = Symbol("a") b_sym = Symbol("b") alphabet = Alphabet({a_sym, b_sym}) a = AtomicFormula(a_sym) b = AtomicFormula(b_sym) pl = PL(alphabet) self.assertEqual(pl.to_nnf(Not(Not(b))), b) self.assertEqual(pl.to_nnf(Not(And(a, Not(b)))), Or(Not(a), b))
def test_to_nnf_derived_formula(self): a_sym = Symbol("a") b_sym = Symbol("b") alphabet = Alphabet({a_sym, b_sym}) a = AtomicFormula(a_sym) b = AtomicFormula(b_sym) pl = PL(alphabet) self.assertEqual(pl.to_nnf(Not(Or(b, Not(a)))), And(Not(b), a)) self.assertEqual(pl.to_nnf(Not(Implies(b, Not(a)))), And(b, a))
def test_expand_formula_allowed_formulas(self): a_sym = Symbol("a") b_sym = Symbol("b") alphabet = Alphabet({a_sym, b_sym}) a = AtomicFormula(a_sym) b = AtomicFormula(b_sym) pl = PL(alphabet) self.assertEqual(pl.expand_formula(a), a) self.assertEqual(pl.expand_formula(Not(b)), Not(b)) self.assertEqual(pl.expand_formula(And(a, b)), And(a, b))
def test_find_atomics(self): a = Symbol("a") b = Symbol("b") c = Symbol("c") atomic_a = AtomicFormula(a) atomic_b = AtomicFormula(b) atomic_c = AtomicFormula(c) self.assertEqual( PL.find_atomics(And(atomic_a, And(atomic_b, atomic_c))), {atomic_a, atomic_b, atomic_c}) self.assertEqual(PL.find_atomics(Or(atomic_a, Or(atomic_b, atomic_c))), {atomic_a, atomic_b, atomic_c}) self.assertEqual( PL.find_atomics(Equivalence(atomic_a, Or(atomic_b, atomic_c))), {atomic_a, atomic_b, atomic_c})
def test_powerset(self): a = Symbol("a") b = Symbol("b") c = Symbol("c") d = Symbol("d") s = [a,b,c,d] true_ps = { (), (a,),(b,),(c,),(d,), (a, b), (a, c), (a, d), (b, c), (b, d), (c, d), (a,b,c), (a,b,d), (a,c,d), (b,c,d), (a, b, c, d) } set_true_ps_frozenset = {frozenset(t) for t in true_ps} self.assertEqual(set(powerset(s)), set_true_ps_frozenset)
def test_expand_formula_error(self): a_sym = Symbol("a") alphabet = Alphabet({a_sym}) a = Next(AtomicFormula(a_sym)) pl = PL(alphabet) with self.assertRaises(ValueError) as ve: pl.expand_formula(a)
def __init__(self, env): super().__init__(env) self.row_symbols = [Symbol(r) for r in ["r0", "r1", "r2"]] self.dfa = self._build_automata() self.goal_reward = 1000 self.transition_reward = 100 self.simulator = Simulator(self.dfa) self.last_status = None
def test_is_formula_composed(self): a_sym = Symbol("a") alphabet = Alphabet({a_sym}) a = AtomicFormula(a_sym) pl = PL(alphabet) self.assertTrue( pl.is_formula( Implies(Not(a), And(TrueFormula(), Not(FalseFormula()))))) self.assertFalse( pl.is_formula( Implies(Not(a), And(TrueFormula(), Next(FalseFormula())))))
def setUp(self): # Symbols self.a_sym = Symbol("a") self.b_sym = Symbol("b") self.c_sym = Symbol("c") # Propositions self.a = AtomicFormula(self.a_sym) self.b = AtomicFormula(self.b_sym) self.c = AtomicFormula(self.c_sym) self.alphabet = Alphabet({self.a_sym, self.b_sym, self.c_sym}) self.ref = REf(self.alphabet) self.trace_1_list = [ {self.a_sym, self.b_sym}, {self.a_sym, self.c_sym}, {self.a_sym, self.b_sym}, {self.a_sym, self.c_sym}, {self.b_sym, self.c_sym}, ] self.trace_1 = FiniteTrace(self.trace_1_list, self.alphabet)
def test_expand_formula_derived_formulas(self): a_sym = Symbol("a") b_sym = Symbol("b") alphabet = Alphabet({a_sym, b_sym}) a = AtomicFormula(a_sym) b = AtomicFormula(b_sym) # T = Not(And(Not(DUMMY_ATOMIC), DUMMY_ATOMIC)) # F = And(Not(DUMMY_ATOMIC), DUMMY_ATOMIC) T = TrueFormula() F = FalseFormula() pl = PL(alphabet) self.assertEqual(pl.expand_formula(TrueFormula()), T) self.assertEqual(pl.expand_formula(FalseFormula()), F) self.assertEqual(pl.expand_formula(Or(a, b)), Not(And(Not(a), Not(b)))) self.assertEqual(pl.expand_formula(Implies(a, b)), Not(And(Not(Not(a)), Not(b)))) self.assertEqual(pl.expand_formula(Implies(b, a)), Not(And(Not(Not(b)), Not(a)))) # A === B = (A AND B) OR (NOT A AND NOT B) = NOT( NOT(A AND B) AND NOT(NOT A AND NOT B) ) self.assertEqual(pl.expand_formula(Equivalence(a, b)), Not(And(Not(And(a, b)), Not(And(Not(a), Not(b))))))
def test_expand_formula_composed(self): a_sym = Symbol("a") alphabet = Alphabet({a_sym}) a = AtomicFormula(a_sym) # T = Not(And(Not(DUMMY_ATOMIC), DUMMY_ATOMIC)) # F = And(Not(DUMMY_ATOMIC), DUMMY_ATOMIC) T = TrueFormula() F = FalseFormula() pl = PL(alphabet) self.assertEqual(pl.expand_formula(And(TrueFormula(), FalseFormula())), And(T, F)) self.assertEqual(pl.expand_formula(Or(TrueFormula(), FalseFormula())), Not(And(Not(T), Not(F)))) self.assertEqual( pl.expand_formula(Implies(TrueFormula(), FalseFormula())), Not(And(Not(Not(T)), Not(F)))) self.assertEqual( pl.expand_formula(Equivalence(TrueFormula(), FalseFormula())), Not(And(Not(And(T, F)), Not(And(Not(T), Not(F))))))
def fromString(cls, name:str): return Variable(Symbol(name))
def test_chain(self): a_sym, b_sym, c_sym = [Symbol(s) for s in ["a", "b", "c"]] a, b, c = [AtomicFormula(s) for s in [a_sym,b_sym,c_sym]] and_chain = And.chain([a, b, c]) self.assertEqual(and_chain, And(a, And(b, And(c, TrueFormula()))))
def test_is_formula_error(self): a_sym = Symbol("a") alphabet = Alphabet({a_sym}) a = Next(AtomicFormula(a_sym)) pl = PL(alphabet) self.assertFalse(pl.is_formula(a))
def fromStrings(symbol_strings:Set[str]): return Alphabet(set(Symbol(s) for s in symbol_strings))
def test_is_formula_atomic(self): a_sym = Symbol("a") alphabet = Alphabet({a_sym}) a = AtomicFormula(a_sym) pl = PL(alphabet) self.assertTrue(pl.is_formula(a))
def test_minimal_models(self): a = Symbol("a") b = Symbol("b") c = Symbol("c") alphabet = Alphabet({a, b, c}) pl = PL(alphabet) atomic_a = AtomicFormula(a) atomic_b = AtomicFormula(b) atomic_c = AtomicFormula(c) self.assertEqual( pl.minimal_models(TrueFormula()), {PLInterpretation(alphabet, { a: False, b: False, c: False })}) self.assertEqual(pl.minimal_models(FalseFormula()), set()) self.assertEqual( pl.minimal_models(atomic_a), {PLInterpretation(alphabet, { a: True, b: False, c: False })}) self.assertEqual( pl.minimal_models(Not(atomic_a)), {PLInterpretation(alphabet, { a: False, b: False, c: False })}) self.assertEqual( pl.minimal_models(And(atomic_a, atomic_b)), {PLInterpretation(alphabet, { a: True, b: True, c: False })}) self.assertEqual(pl.minimal_models(And(atomic_a, Not(atomic_a))), set()) self.assertEqual( pl.minimal_models(Or(atomic_a, atomic_b)), { PLInterpretation(alphabet, { a: False, b: True, c: False }), PLInterpretation(alphabet, { a: True, b: False, c: False }) }) self.assertEqual( pl.minimal_models(And.chain([atomic_a, atomic_b, atomic_c])), {PLInterpretation(alphabet, { a: True, b: True, c: True })})
def setUp(self): """Set up test fixtures, if any.""" # Symbols self.a_sym = Symbol("a") self.b_sym = Symbol("b") self.c_sym = Symbol("c") # Propositions self.a = AtomicFormula(self.a_sym) self.b = AtomicFormula(self.b_sym) self.c = AtomicFormula(self.c_sym) # Propositionals self.not_a = Not(self.a) self.not_b = Not(self.b) self.not_c = Not(self.c) self.a_and_b = And(self.a, self.b) self.a_and_c = And(self.a, self.c) self.b_and_c = And(self.b, self.c) self.abc = And(self.a, And(self.b, self.c)) self.b_or_c = Or(self.b, self.c) self.a_or_b = Or(self.a, self.b) self.not_abc = Not(And(self.a, And(self.b, self.c))) ### Path expression # Tests self.test_a = PathExpressionTest(self.a) self.test_b = PathExpressionTest(self.b) self.test_not_a = PathExpressionTest(self.not_a) self.test_not_b = PathExpressionTest(self.not_b) # Union self.path_a_or_b = PathExpressionUnion(self.a, self.b) self.path_b_or_c = PathExpressionUnion(self.b, self.c) # Sequence self.path_seq_a_and_b__a_and_c = PathExpressionSequence( self.a_and_b, self.a_and_c) self.path_a_or_b__b_or_c = PathExpressionSequence( self.path_a_or_b, self.path_b_or_c) # Stars self.path_b_or_c_star = PathExpressionStar(self.path_b_or_c) self.path_not_abc = PathExpressionStar(self.not_abc) # Modal connective self.eventually_propositional_a_and_b__a_and_c = PathExpressionEventually( self.a_and_b, self.a_and_c) self.eventually_test_a__c = PathExpressionEventually( self.test_a, self.c) self.eventually_test_a__b = PathExpressionEventually( self.test_a, self.b) self.eventually_seq_a_and_b__a_and_c__not_c = PathExpressionEventually( self.path_seq_a_and_b__a_and_c, self.not_c) self.eventually_seq_a_and_b__a_and_c__c = PathExpressionEventually( self.path_seq_a_and_b__a_and_c, self.c) self.eventually_b_or_c_star__b_and_c = PathExpressionEventually( self.path_b_or_c_star, self.b_and_c) self.next_a_and_c = PathExpressionEventually(TrueFormula(), self.a_and_c) self.liveness_b_and_c = PathExpressionEventually( PathExpressionStar(TrueFormula()), self.b_and_c) self.liveness_abc = PathExpressionEventually( PathExpressionStar(TrueFormula()), self.abc) self.always_true__a = PathExpressionAlways( PathExpressionStar(TrueFormula()), self.a) self.always_true__b_or_c = PathExpressionAlways( PathExpressionStar(TrueFormula()), self.b_or_c) self.alphabet = Alphabet({self.a_sym, self.b_sym, self.c_sym}) # Traces self.ldlf = LDLf(self.alphabet) self.trace_1_list = [ {self.a_sym, self.b_sym}, {self.a_sym, self.c_sym}, {self.a_sym, self.b_sym}, {self.a_sym, self.c_sym}, {self.b_sym, self.c_sym}, ] self.trace_1 = FiniteTrace(self.trace_1_list, self.alphabet)
def _members(self): return (Symbol(Symbols.LOGICAL_FALSE.value))
def __str__(self): return str(Symbol(Symbols.LOGICAL_FALSE.value))
def _members(self): return (Symbol(Symbols.END.value))
def __str__(self): return str(Symbol(Symbols.END.value))
def to_nfa(self, f: Formula): # TODO: optimize!!! assert self.is_formula(f) nnf_f = self.to_nnf(f) alphabet = powerset(self.alphabet.symbols) initial_states = {frozenset([nnf_f])} final_states = {frozenset()} delta = set() pl, I = PL._from_set_of_propositionals(set(), Alphabet(set())) d = self.delta(nnf_f, frozenset(), epsilon=True) if pl.truth(d, I): final_states.add(frozenset([nnf_f])) states = {frozenset(), frozenset([nnf_f])} states_changed, delta_changed = True, True while states_changed or delta_changed: states_changed, delta_changed = False, False for actions_set in alphabet: states_list = list(states) for q in states_list: delta_formulas = [ self.delta(subf, actions_set) for subf in q ] atomics = [ s for subf in delta_formulas for s in PL.find_atomics(subf) ] symbol2formula = { Symbol(str(f)): f for f in atomics if f != TrueFormula() and f != FalseFormula() } formula2atomic_formulas = { f: AtomicFormula.fromName(str(f)) if f != TrueFormula() and f != FalseFormula() else f for f in atomics } transformed_delta_formulas = [ self._tranform_delta(f, formula2atomic_formulas) for f in delta_formulas ] conjunctions = And.chain(transformed_delta_formulas) models = frozenset( PL(Alphabet( set(symbol2formula))).minimal_models(conjunctions)) if len(models) == 0: continue for min_model in models: q_prime = frozenset({ symbol2formula[s] for s in min_model.symbol2truth if min_model.symbol2truth[s] }) len_before = len(states) states.add(q_prime) if len(states) == len_before + 1: states_list.append(q_prime) states_changed = True len_before = len(delta) delta.add((q, actions_set, q_prime)) if len(delta) == len_before + 1: delta_changed = True # check if q_prime should be added as final state if len(q_prime) == 0: final_states.add(q_prime) else: q_prime_delta_conjunction = And.chain([ self.delta(subf, frozenset(), epsilon=True) for subf in q_prime ]) pl, I = PL._from_set_of_propositionals( set(), Alphabet(set())) if pl.truth(q_prime_delta_conjunction, I): final_states.add(q_prime) return { "alphabet": alphabet, "states": frozenset(states), "initial_states": frozenset(initial_states), "transitions": delta, "accepting_states": frozenset(final_states) }
def fromName(cls, name: str): return cls(Symbol(name))
def setUp(self): """Set up symbols, terms and formulas. Both legal and illegal, according to some FOL system""" # Symbols self.a_sym = Symbol('a') self.b_sym = Symbol('b') self.const_sym = ConstantSymbol("Const") self.fun_sym = FunctionSymbol("Fun", 3) self.predicate_sym = PredicateSymbol("Predicate", 2) self.A = PredicateSymbol("A", 1) # Terms self.a = Variable(self.a_sym) self.b = Variable(self.b_sym) self.c = Variable.fromString("c") self.const = ConstantTerm(self.const_sym) self.fun_abc = FunctionTerm(self.fun_sym, self.a, self.b, self.c) # Formulas self.predicate_ab = PredicateFormula(self.predicate_sym, self.a, self.b) self.predicate_ac = PredicateFormula(self.predicate_sym, self.a, self.c) self.A_a = PredicateFormula(self.A, self.a) self.a_equal_a = Equal(self.a, self.a) self.b_equal_c = Equal(self.b, self.c) self.neg_a_equal_a = Not(self.a_equal_a) self.neg_Aa = Not(self.A_a) self.Aa_and_b_equal_c = And(self.A_a, self.b_equal_c) self.Aa_or_b_equal_c = Or(self.A_a, self.b_equal_c) self.Aa_implies_b_equal_c = Implies(self.A_a, self.b_equal_c) self.exists_a_predicate_ab = Exists(self.a, self.predicate_ab) self.forall_b_exists_a_predicate_ab = ForAll( self.b, self.exists_a_predicate_ab) # FOL self.vars = {self.a, self.b, self.c} self.functions = {self.const_sym, self.fun_sym} self.predicates = {self.predicate_sym, self.A} self.alphabet = FOLAlphabet(self.functions, self.predicates) self.myFOL = FOL(self.alphabet) # define dummy stuff # does not belong to myFOL. They are used for test membership to myFOL self.dummy_variable = Variable.fromString( "ThisVariableDoesNotBelongToFOLSystem") self.dummy_fun_sym = FunctionSymbol( "ThisFunctionDoesNotBelongToFOLSystem", 3) self.dummy_constant_sym = ConstantSymbol( "ThisConstDoesNotBelongToFOLSystem") self.dummy_predicate_sym = PredicateSymbol( "ThisPredicateDoesNotBelongToFOLSystem", 2) self.dummy_fun = FunctionTerm(self.dummy_fun_sym, self.a, self.b, self.dummy_variable) self.dummy_constant = ConstantTerm(self.dummy_constant_sym) self.dummy_predicate = PredicateFormula(self.dummy_predicate_sym, self.a, self.b) self.dummy_predicate_only_one_symbol_false = PredicateFormula( self.predicate_sym, self.dummy_variable, self.dummy_constant) self.dummy_equal = Equal(self.c, self.dummy_constant) self.dummy_neg = Not(self.dummy_predicate_only_one_symbol_false) self.dummy_and = And(self.dummy_predicate, self.predicate_ab) self.dummy_or = Or(self.dummy_predicate_only_one_symbol_false, self.predicate_ac) self.dummy_exists = Exists(self.dummy_variable, self.dummy_predicate_only_one_symbol_false) self.dummy_forall = ForAll(self.b, self.dummy_predicate)
def test_eq(self): # Symbols self.assertEqual(self.a_sym, self.a_sym) self.assertEqual(self.a_sym, Symbol("a")) self.assertEqual(self.const_sym, ConstantSymbol("Const")) self.assertEqual(self.fun_sym, FunctionSymbol("Fun", 3)) self.assertEqual(self.predicate_sym, PredicateSymbol("Predicate", 2)) self.assertNotEqual(self.a_sym, Symbol("c")) self.assertNotEqual(self.const_sym, ConstantSymbol("Another_Const")) self.assertNotEqual(self.fun_sym, FunctionSymbol("Another_Fun", 3)) self.assertNotEqual(self.fun_sym, FunctionSymbol("Fun", 2)) self.assertNotEqual(self.predicate_sym, PredicateSymbol("Another_Predicate", 2)) self.assertNotEqual(self.predicate_sym, PredicateSymbol("Predicate", 1)) self.assertNotEqual(self.a_sym, self.fun_sym) self.assertNotEqual(self.const_sym, self.fun_sym) # Terms self.assertEqual(self.a, self.a) self.assertEqual(self.a, Variable.fromString("a")) self.assertEqual(self.const, ConstantTerm(ConstantSymbol("Const"))) self.assertEqual( self.fun_abc, FunctionTerm(FunctionSymbol("Fun", 3), self.a, self.b, self.c)) self.assertNotEqual(self.a, self.b) self.assertNotEqual(self.a, Variable.fromString("c")) self.assertNotEqual(self.const, ConstantTerm(ConstantSymbol("Another_Const"))) self.assertNotEqual( self.fun_abc, FunctionTerm(FunctionSymbol("Another_Fun", 3), self.a, self.b, self.c)) self.assertNotEqual( self.fun_abc, FunctionTerm(FunctionSymbol("Fun", 3), self.a, self.b, self.a)) self.assertNotEqual(self.a, self.fun_abc) # Formulas self.assertEqual( self.predicate_ab, PredicateFormula(PredicateSymbol("Predicate", 2), Variable.fromString("a"), Variable.fromString("b"))) self.assertEqual( self.A_a, PredicateFormula(PredicateSymbol("A", 1), Variable.fromString("a"))) self.assertEqual( self.b_equal_c, Equal(Variable.fromString("b"), Variable.fromString("c"))) self.assertEqual( self.neg_a_equal_a, Not(Equal(Variable.fromString("a"), Variable.fromString("a")))) self.assertEqual( self.forall_b_exists_a_predicate_ab, ForAll( Variable.fromString("b"), Exists( Variable.fromString("a"), PredicateFormula(PredicateSymbol("Predicate", 2), Variable.fromString("a"), Variable.fromString("b"))))) self.assertNotEqual( self.predicate_ab, PredicateFormula(PredicateSymbol("Predicate", 2), Variable.fromString("a"), Variable.fromString("c"))) self.assertNotEqual( self.predicate_ab, PredicateFormula(PredicateSymbol("Another_Predicate", 2), Variable.fromString("a"), Variable.fromString("c"))) self.assertNotEqual( self.A_a, PredicateFormula(PredicateSymbol("A", 1), Variable.fromString("b"))) self.assertNotEqual( self.b_equal_c, Equal(Variable.fromString("b"), Variable.fromString("b"))) self.assertNotEqual( self.neg_a_equal_a, Not(Equal(Variable.fromString("b"), Variable.fromString("a")))) self.assertNotEqual( self.forall_b_exists_a_predicate_ab, ForAll( Variable.fromString("b"), Exists( Variable.fromString("a"), PredicateFormula(PredicateSymbol("ANOTHER_PREDICATE", 2), Variable.fromString("a"), Variable.fromString("b")))))