def _truth(self, f: Formula, trace: FiniteTrace, position: int): assert trace.alphabet == self.alphabet truth = self._truth pl = PL(self.alphabet) if pl.is_formula(f): return self.truth(PathExpressionEventually(f, LogicalTrue()), trace, position) elif isinstance(f, LogicalTrue): return True elif isinstance(f, Not): return not self.truth(f.f, trace, position) elif isinstance(f, And): return self.truth(f.f1, trace, position) and self.truth( f.f2, trace, position) elif isinstance(f, PathExpressionEventually): path = f.p assert self._is_path(path) if isinstance(path, PathExpressionTest): return truth(path.f, trace, position) and truth( f.f, trace, position) elif isinstance(path, PathExpressionUnion): return truth(PathExpressionEventually(path.p1, f.f), trace, position) or truth( PathExpressionEventually(path.p2, f.f), trace, position) elif isinstance(path, PathExpressionSequence): return truth( PathExpressionEventually( path.p1, PathExpressionEventually(path.p2, f.f)), trace, position) elif isinstance(path, PathExpressionStar): return truth(f.f, trace, position) or (position < trace.last() and truth( PathExpressionEventually( path.p, PathExpressionEventually( path, f.f)), trace, position) and not self._is_testonly(path)) # path should be a Propositional Formula else: pl, I = PL._from_set_of_propositionals(trace.get(position), trace.alphabet) return position < trace.length() and pl.truth( path, I) and truth(f.f, trace, position + 1) else: raise ValueError("Argument not a valid Formula")
class TestPL(unittest.TestCase): """Tests for `pythogic.ltlf` package.""" 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 tearDown(self): """Tear down test fixtures, if any.""" def test_truth(self): self.assertTrue(self.PL.truth(self.a, self.I)) self.assertFalse(self.PL.truth(self.b, self.I)) self.assertTrue(self.PL.truth(self.c, self.I)) self.assertFalse(self.PL.truth(self.not_a, self.I)) self.assertFalse(self.PL.truth(self.not_a_and_b, self.I)) self.assertTrue(self.PL.truth(self.not_a_or_c, self.I)) self.assertTrue(self.PL.truth(self.true, self.I)) self.assertFalse(self.PL.truth(self.false, self.I))