def test_specification_ex2(self): with open('./../examples/ex2.monitor', 'r') as m: for line in m: data = json.loads(line) print("Topics: %s" % data['topics']) self.topics = data['topics'] print("Specification: %s" % data['specification']) lexer = SpecificationLexer(InputStream(data['specification'])) parser = SpecificationParser(CommonTokenStream(lexer)) ast = AST().visit(parser.specification()) self.formula = ast.accept(Rewriter()) print("Formula: %s" % self.formula) monitor = Monitor(formula=self.formula, topics=self.topics) with open('./../examples/ex2.trace', 'r') as trace: for event in trace: event = json.loads(event) monitor.step(timestamp=event.pop('timestamp', None), state=event) if len(monitor.boolean_verdicts) != 0: print("Boolean verdicts for time-point %d:" % monitor.now) for boolean_verdict in sorted(monitor.boolean_verdicts): print("(%d,%d):%s" % (boolean_verdict[0][0], boolean_verdict[0][1], boolean_verdict[1])) if len(monitor.equivalence_verdicts) != 0: print("Equivalence verdicts for time-point %d:" % monitor.now) for equivalence_verdict in sorted( monitor.equivalence_verdicts): print("(%d,%d) = (%d,%d)" % (equivalence_verdict[0][0][0], equivalence_verdict[0][0][1], equivalence_verdict[1][0][0], equivalence_verdict[1][0][1])) self.assertEqual(BExp.FalseConstant(), monitor.previous[-1])
def __init__(self, formula, topics): self.debug = False self.formula = formula self.topics = topics self.subformulas = list( reversed(self.formula.accept(SubformulaVisitor()))) self.now = -1 self.off = 0 self.history = {} self.previous = [BExp.FalseConstant()] * len(self.subformulas) self.current = [FBExp.Now(BExp.FalseConstant())] * len( self.subformulas) self.boolean_verdicts = [] self.equivalence_verdicts = []
def progress(self, k: int, delta: int, state: dict) -> FBExp.FutureBooleanExpression: subformula = self.subformulas[k] if isinstance(subformula, F.Atomic): if subformula.identifier in state: if state[subformula.identifier]: return FBExp.Now(BExp.TrueConstant()) else: return FBExp.Now(BExp.FalseConstant()) else: # todo: is there a case where the formula is inconclusive? return FBExp.Now(BExp.FalseConstant()) elif isinstance(subformula, F.Boolean): if subformula.constant: return FBExp.Now(BExp.TrueConstant()) else: return FBExp.Now(BExp.FalseConstant()) elif isinstance(subformula, F.Negation): return FBExp.Negation(self.current[self.subformulas.index( subformula.left)]).simplify() elif isinstance(subformula, F.Disjunction): return FBExp.Disjunction( self.current[self.subformulas.index(subformula.left)], self.current[self.subformulas.index( subformula.right)]).simplify() elif isinstance(subformula, F.Previous): raise NotImplementedError( "Progress not implemented for F.Previous.") elif isinstance(subformula, F.Next): raise NotImplementedError("Progress not implemented for F.Next.") elif isinstance(subformula, F.Since): left, right = None, None if subformula.member(0): left = self.current[self.subformulas.index(subformula.right)] else: left = FBExp.Now(BExp.FalseConstant()) if delta <= subformula.right_bound: right = FBExp.Conjunction( self.current[self.subformulas.index(subformula.left)], self.substitute_future_boolean_expression( self.previous[k - delta])).simplify() else: right = FBExp.Now(BExp.FalseConstant()) return FBExp.Disjunction(left, right).simplify() elif isinstance(subformula, F.Until): left, right = None, None if subformula.member(0): left = self.current[self.subformulas.index(subformula.right)] else: left = FBExp.Now(BExp.FalseConstant()) if subformula.is_unbounded(): right = FBExp.Later(lambda t: BExp.FalseConstant( ) if t > subformula.right_bound else BExp.Conjunction( self.evaluate( t, self.current[self.subformulas.index(subformula.left) ]), BExp.Variable(k))) else: right = FBExp.Later(lambda t: BExp.FalseConstant( ) if t > subformula.right_bound else BExp.Conjunction( self.evaluate( t, self.current[self.subformulas.index(subformula.left) ]), BExp.Variable(k - t))) return FBExp.Disjunction(left, right).simplify() else: raise NotImplementedError( "Progress not implemented for subformula.")