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 test_eventually_bounded(self): lexer = SpecificationLexer(InputStream("eventually[0,1] a")) stream = CommonTokenStream(lexer) parser = SpecificationParser(stream) ast = AST().visit(parser.specification()) specification = ast.accept(Rewriter()) self.assertEqual(specification, Until(Boolean(True), Atomic("a"), 0, 1))
def test_always(self): lexer = SpecificationLexer(InputStream("always a")) stream = CommonTokenStream(lexer) parser = SpecificationParser(stream) ast = AST().visit(parser.specification()) specification = ast.accept(Rewriter()) self.assertEqual( specification, Negation(Until(Boolean(True), Negation(Atomic("a")), 0, math.inf)))
def add_monitor(self, specification, topics): print("Topics: %s" % topics) print("Specification: %s" % specification) lexer = SpecificationLexer(InputStream(specification)) parser = SpecificationParser(CommonTokenStream(lexer)) ast = AST().visit(parser.specification()) formula = ast.accept(Rewriter()) print("Formula: %s" % formula) self.monitors[Monitor(formula=formula, topics=topics)] = self.horizon(formula) for topic in topics: self.subscribe(topic)
def test_combination(self): lexer = SpecificationLexer( InputStream("always(E implies eventually[0, 10] R)")) stream = CommonTokenStream(lexer) parser = SpecificationParser(stream) ast = AST().visit(parser.specification()) specification = ast.accept(Rewriter()) self.assertEqual( specification, Negation( Until( Boolean(True), Negation( Disjunction(Negation(Atomic("E")), Until(Boolean(True), Atomic("R"), 0, 10))), 0, math.inf)))
def test_since_bounded(self): lexer = SpecificationLexer(InputStream("a since[0,1] b")) stream = CommonTokenStream(lexer) parser = SpecificationParser(stream) ast = AST().visit(parser.specification()) self.assertEqual(ast, Since(Atomic("a"), Atomic("b"), 0, 1))
def test_until_bounded(self): lexer = SpecificationLexer(InputStream("a until[0,4] b")) stream = CommonTokenStream(lexer) parser = SpecificationParser(stream) ast = AST().visit(parser.specification()) self.assertEqual(ast, Until(Atomic("a"), Atomic("b"), 0, 4))
def test_implication(self): lexer = SpecificationLexer(InputStream("a implies b")) stream = CommonTokenStream(lexer) parser = SpecificationParser(stream) ast = AST().visit(parser.specification()) self.assertEqual(ast, Implication(Atomic("a"), Atomic("b")))