def p_temp_formula(self, p): """temp_formula : temp_formula EQUIVALENCE temp_formula | temp_formula IMPLIES temp_formula | temp_formula OR temp_formula | temp_formula AND temp_formula | BOXLSEPARATOR path BOXRSEPARATOR temp_formula | DIAMONDLSEPARATOR path DIAMONDRSEPARATOR temp_formula | NOT temp_formula | TT | FF | END | LAST""" if len(p) == 2: if p[1] == Symbols.LOGICAL_TRUE.value: p[0] = LDLfLogicalTrue() elif p[1] == Symbols.LOGICAL_FALSE.value: p[0] = LDLfLogicalFalse() elif p[1] == Symbols.END.value: p[0] = LDLfEnd() elif p[1] == Symbols.LAST.value: p[0] = LDLfLast() else: p[0] = LDLfDiamond(RegExpPropositional(p[1]), LDLfLogicalTrue()) elif len(p) == 3: p[0] = LDLfNot(p[2]) elif len(p) == 4: l, o, r = p[1:] if o == Symbols.EQUIVALENCE.value: p[0] = LDLfEquivalence([l, r]) elif o == Symbols.IMPLIES.value: p[0] = LDLfImplies([l, r]) elif o == Symbols.OR.value: p[0] = LDLfOr([l, r]) elif o == Symbols.AND.value: p[0] = LDLfAnd([l, r]) else: raise ValueError elif len(p) == 5: if p[1] == Symbols.ALWAYS_BRACKET_LEFT.value: p[0] = LDLfBox(p[2], p[4]) elif p[1] == Symbols.EVENTUALLY_BRACKET_LEFT.value: p[0] = LDLfDiamond(p[2], p[4]) else: raise ValueError else: raise ValueError
def to_LDLf(self): f1 = self.formulas[0].to_LDLf() f2 = LTLfUntil(self.formulas[1:]).to_LDLf() if len( self.formulas) > 2 else self.formulas[1].to_LDLf() return LDLfDiamond( RegExpStar( RegExpSequence([RegExpTest(f1), RegExpPropositional(PLTrue())])), LDLfAnd([f2, LDLfNot(LDLfEnd())]))
def test_1(self): sa, sb = "A", "B" a, b = PLAtomic(sa), PLAtomic(sb) i_ = {} i_a = {"A": True} i_b = {"B": True} i_ab = {"A": True, "B": True} tr_false_a_b_ab = [i_, i_a, i_b, i_ab, i_] tt = LDLfLogicalTrue() ff = LDLfLogicalFalse() assert tt.truth(tr_false_a_b_ab, 0) assert not ff.truth(tr_false_a_b_ab, 0) assert not LDLfNot(tt).truth(tr_false_a_b_ab, 0) assert LDLfNot(ff).truth(tr_false_a_b_ab, 0) # assert LDLfAnd([LDLfPropositional(a), LDLfPropositional(b)]).truth( # tr_false_a_b_ab, 3 # ) assert not LDLfDiamond(RegExpPropositional(PLAnd([a, b])), tt).truth( tr_false_a_b_ab, 0) trace = self.trace parser = self.parser formula = "<true*;A&B>tt" parsed_formula = parser(formula) assert parsed_formula.truth(trace, 0) formula = "[(A+!B)*]<C>tt" parsed_formula = parser(formula) assert not parsed_formula.truth(trace, 1) formula = "<?(<!C>tt)><A>tt" parsed_formula = parser(formula) assert parsed_formula.truth(trace, 1) formula = "<!C+A>tt" parsed_formula = parser(formula) assert parsed_formula.truth(trace, 1) formula = "<!C+A>tt" parsed_formula = parser(formula) assert parsed_formula.truth(trace, 1)
def test_truth(): sa, sb = "a", "b" a, b = PLAtomic(sa), PLAtomic(sb) i_ = PLFalseInterpretation() i_a = PLInterpretation({sa}) i_b = PLInterpretation({sb}) i_ab = PLInterpretation({sa, sb}) tr_false_a_b_ab = FiniteTrace([i_, i_a, i_b, i_ab, i_]) tt = LDLfLogicalTrue() ff = LDLfLogicalFalse() assert tt.truth(tr_false_a_b_ab, 0) assert not ff.truth(tr_false_a_b_ab, 0) assert not LDLfNot(tt).truth(tr_false_a_b_ab, 0) assert LDLfNot(ff).truth(tr_false_a_b_ab, 0) assert LDLfAnd([LDLfPropositional(a), LDLfPropositional(b)]).truth(tr_false_a_b_ab, 3) assert not LDLfDiamond(RegExpPropositional(PLAnd([a, b])), tt).truth( tr_false_a_b_ab, 0) parser = LDLfParser() trace = FiniteTrace.from_symbol_sets([{}, {"A"}, {"A"}, {"A", "B"}, {}]) formula = "<true*;A&B>tt" parsed_formula = parser(formula) assert parsed_formula.truth(trace, 0) formula = "[(A+!B)*]<C>tt" parsed_formula = parser(formula) assert not parsed_formula.truth(trace, 1) formula = "<(<!C>tt)?><A>tt" parsed_formula = parser(formula) assert parsed_formula.truth(trace, 1) formula = "<!C+A>tt" parsed_formula = parser(formula) assert parsed_formula.truth(trace, 1)
def to_LDLf(self): return LDLfDiamond(RegExpStar(RegExpPropositional(PLTrue())), LDLfAnd([self.f.to_LDLf(), LDLfNot(LDLfEnd())]))
def test_parser(): parser = LDLfParser() a, b = PLAtomic("A"), PLAtomic("B") tt = LDLfLogicalTrue() ff = LDLfLogicalFalse() true = PLTrue() false = PLFalse() r_true = RegExpPropositional(true) r_false = RegExpPropositional(false) assert tt == parser("tt") assert ff == parser("ff") assert LDLfDiamond(r_true, tt) == parser("<true>tt") assert LDLfDiamond(r_false, tt) == parser("<false>tt") assert parser("!tt & <!A&B>tt") == LDLfAnd([ LDLfNot(tt), LDLfDiamond(RegExpPropositional(PLAnd([PLNot(a), b])), tt) ]) assert parser("[true*]([true]ff | <!A>tt | <(true)*><B>tt)") == LDLfBox( RegExpStar(r_true), LDLfOr([ LDLfBox(r_true, ff), LDLfDiamond(RegExpPropositional(PLNot(a)), tt), LDLfDiamond(RegExpStar(r_true), (LDLfDiamond(RegExpPropositional(b), tt))), ]), ) assert parser("[A&B&A]ff <-> <A&B&A>tt") == LDLfEquivalence([ LDLfBox(RegExpPropositional(PLAnd([a, b, a])), ff), LDLfDiamond(RegExpPropositional(PLAnd([a, b, a])), tt), ]) assert parser("<A+B>tt") == LDLfDiamond( RegExpUnion([RegExpPropositional(a), RegExpPropositional(b)]), tt) assert parser("<A;B>tt") == LDLfDiamond( RegExpSequence([RegExpPropositional(a), RegExpPropositional(b)]), tt) assert parser("<A+(B;A)>end") == LDLfDiamond( RegExpUnion([ RegExpPropositional(a), RegExpSequence([RegExpPropositional(b), RegExpPropositional(a)]), ]), LDLfEnd(), ) assert parser("!(<(!(A<->D))+((B;C)*)+(?!last)>[(true)*]end)") == LDLfNot( LDLfDiamond( RegExpUnion([ RegExpPropositional(PLNot(PLEquivalence([a, PLAtomic("D")]))), RegExpStar( RegExpSequence([ RegExpPropositional(PLAtomic("B")), RegExpPropositional(PLAtomic("C")), ])), RegExpTest(LDLfNot(LDLfLast())), ]), LDLfBox(RegExpStar(RegExpPropositional(PLTrue())), LDLfEnd()), ))
def ldlf_diamond(self, args): assert len(args) == 4 _, regex, _, formula = args return LDLfDiamond(regex, formula)