def test_verifyExample(env: Env): ltl = LTL.future(LTL.var('S(Dot)')) diagram = env.diagramService.parseDiagram("data/VarParser.xstd") automaton = env.automatonService.createFromDiagram(diagram) iterable = env.verifierService.verify(automaton, ltl) if not iterable: for f in iterable: log.info(str(f))
def createFormulaFromActions_list(self, actions: list) -> Formula: if not actions: return LTL.t() # be accurate strings = list(map(self.createFormulaFromActions_action, actions)) current = strings[0] for i in range(1, len(strings)): current = LTL.and_(current, strings[i]) return current
def createFromLtl_str(self, ltl: str) -> Automaton: states = self.createStateListFromLtl(ltl) ids = itertools.count() idMap = defaultdict(lambda: next(ids)) automaton = Automaton() for state in states: nodeId = idMap[state.name] if self.isAcceptance(state.name): automaton.setAccepting(nodeId) if self.isInit(state.name): automaton.initialState = nodeId for transition in state.transitions: nextId = idMap[transition.stateName] formula = transition.expression automaton.addTransition(nodeId, nextId, formula) if self.isAcceptance(transition.stateName): automaton.setAccepting(nextId) if self.isInit(transition.stateName): automaton.initialState = nextId for nodeId in automaton.nodes: if len(automaton.get(nodeId)) == 0: automaton.addTransition(nodeId, nodeId, LTL.t()) return automaton
def verify(self, automaton: Automaton, ltl: Formula) -> list: ltlAutomaton = self.automatonService.createFromLtl_formula( LTL.not_(ltl)) intersector = LTLIntersector() c = self.automatonService.intersect(automaton, ltlAutomaton, intersector) example = c.findPath() return example
def intersect(self, a, b): variables = set() if a != LTL.t(): visitor = VariableVisitor() a.accept(visitor) variables = visitor.getResult() markVisitor = MarkVisitor(variables) b.accept(markVisitor) return a if markVisitor.getMark() else None
def test_Intersection(ltlIntersector): common = "E(machine_type)" a = LTL.var(common) b = LTL.and_(LTL.not_(LTL.var("S(Dot")), LTL.var(common)) expected = LTL.var(common) actual = ltlIntersector.intersect(a, b) assert expected == actual
def test_createFromLtlFormula(env: Env): ltl = LTL.not_(LTL.globally(LTL.future(LTL.var("p")))) automaton = env.automatonService.createFromLtl_formula(ltl) reference = Automaton() reference.addTransition(0, 0, LTL.t()) notP = LTL.not_(LTL.var("p")) reference.addTransition(0, 1, notP) reference.addTransition(1, 1, notP) reference.setAccepting(1) reference.initialState = 0 assert reference == automaton
def constant(self): localctx = LTLParser.ConstantContext(self, self._ctx, self.state) self.enterRule(localctx, 4, self.RULE_constant) try: self.state = 79 self._errHandler.sync(self) token = self._input.LA(1) if token in [LTLParser.TRUE]: self.enterOuterAlt(localctx, 1) self.state = 73 self.match(LTLParser.TRUE) localctx.f = LTL.t() pass elif token in [LTLParser.FALSE]: self.enterOuterAlt(localctx, 2) self.state = 75 self.match(LTLParser.FALSE) localctx.f = LTL.f() pass elif token in [LTLParser.StringLiteral]: self.enterOuterAlt(localctx, 3) self.state = 77 localctx._StringLiteral = self.match(LTLParser.StringLiteral) name = (None if localctx._StringLiteral is None else localctx._StringLiteral.text) localctx.f = LTL.var(name[1:-1]) pass else: raise NoViableAltException(self) except RecognitionException as re: localctx.exception = re self._errHandler.reportError(self, re) self._errHandler.recover(self, re) finally: self.exitRule() return localctx
def primary(self): localctx = LTLParser.PrimaryContext(self, self._ctx, self.state) self.enterRule(localctx, 2, self.RULE_primary) try: self.state = 71 self._errHandler.sync(self) token = self._input.LA(1) if token in [ LTLParser.TRUE, LTLParser.FALSE, LTLParser.StringLiteral ]: self.enterOuterAlt(localctx, 1) self.state = 59 localctx._constant = self.constant() localctx.f = localctx._constant.f pass elif token in [LTLParser.LP]: self.enterOuterAlt(localctx, 2) self.state = 62 self.match(LTLParser.LP) self.state = 63 localctx._ltl = self.ltl(0) self.state = 64 self.match(LTLParser.RP) localctx.f = localctx._ltl.f pass elif token in [LTLParser.NOT]: self.enterOuterAlt(localctx, 3) self.state = 67 self.match(LTLParser.NOT) self.state = 68 localctx._ltl = self.ltl(0) localctx.f = LTL.not_(localctx._ltl.f) pass else: raise NoViableAltException(self) except RecognitionException as re: localctx.exception = re self._errHandler.reportError(self, re) self._errHandler.recover(self, re) finally: self.exitRule() return localctx
def expression(self, _p:int=0): _parentctx = self._ctx _parentState = self.state localctx = BuchiParser.ExpressionContext(self, self._ctx, _parentState) _prevctx = localctx _startState = 8 self.enterRecursionRule(localctx, 8, self.RULE_expression, _p) try: self.enterOuterAlt(localctx, 1) self.state = 79 self._errHandler.sync(self) la_ = self._interp.adaptivePredict(self._input,4,self._ctx) if la_ == 1: pass elif la_ == 2: self.state = 64 self.match(BuchiParser.T__11) self.state = 65 localctx._expression = self.expression(0) self.state = 66 self.match(BuchiParser.T__12) localctx.f = localctx._expression.f pass elif la_ == 3: self.state = 69 localctx._literal = self.literal() localctx.f = LTL.var((None if localctx._literal is None else self._input.getText((localctx._literal.start,localctx._literal.stop)))) pass elif la_ == 4: self.state = 72 localctx.unaryOperator = self.match(BuchiParser.T__13) self.state = 73 localctx._expression = self.expression(4) localctx.f = LTL.not_(localctx._expression.f) pass elif la_ == 5: self.state = 76 self.unit() localctx.f = LTL.t() pass self._ctx.stop = self._input.LT(-1) self.state = 93 self._errHandler.sync(self) _alt = self._interp.adaptivePredict(self._input,6,self._ctx) while _alt!=2 and _alt!=ATN.INVALID_ALT_NUMBER: if _alt==1: if self._parseListeners is not None: self.triggerExitRuleEvent() _prevctx = localctx self.state = 91 self._errHandler.sync(self) la_ = self._interp.adaptivePredict(self._input,5,self._ctx) if la_ == 1: localctx = BuchiParser.ExpressionContext(self, _parentctx, _parentState) localctx.left = _prevctx self.pushNewRecursionContext(localctx, _startState, self.RULE_expression) self.state = 81 if not self.precpred(self._ctx, 3): from antlr4.error.Errors import FailedPredicateException raise FailedPredicateException(self, "self.precpred(self._ctx, 3)") self.state = 82 localctx.binaryOperator = self.match(BuchiParser.T__14) self.state = 83 localctx.right = localctx._expression = self.expression(4) localctx.f = LTL.and_(localctx.left.f, localctx.right.f) pass elif la_ == 2: localctx = BuchiParser.ExpressionContext(self, _parentctx, _parentState) localctx.left = _prevctx self.pushNewRecursionContext(localctx, _startState, self.RULE_expression) self.state = 86 if not self.precpred(self._ctx, 2): from antlr4.error.Errors import FailedPredicateException raise FailedPredicateException(self, "self.precpred(self._ctx, 2)") self.state = 87 localctx.binaryOperator = self.match(BuchiParser.T__15) self.state = 88 localctx.right = localctx._expression = self.expression(3) localctx.f = LTL.or_(localctx.left.f, localctx.right.f) pass self.state = 95 self._errHandler.sync(self) _alt = self._interp.adaptivePredict(self._input,6,self._ctx) except RecognitionException as re: localctx.exception = re self._errHandler.reportError(self, re) self._errHandler.recover(self, re) finally: self.unrollRecursionContexts(_parentctx) return localctx
def createFormulaFromActions_action(self, action) -> Formula: return LTL.var("A(" + action['name'] + ")")
def createFormulaFromEvent(self, event) -> Formula: return LTL.var("E(" + event['name'] + ")")
def createFormulaFromState(self, state) -> Formula: return LTL.var("S(" + state.attributes.name.cdata + ")")
def createFromDiagram(self, diagram) -> Automaton: widgetMap = {w['id']: w for w in diagram.widget} successors = dict() for widget in diagram.widget: if widget['type'] == L.L_WIDGET_STATE: if not hasattr(widget.attributes, 'incoming'): continue for incoming in widget.attributes.incoming: prev = widgetMap.get(incoming['id']) if prev['type'] == L.L_WIDGET_TRANSITION: successors.update({incoming['id']: widget['id']}) else: raise IllegalStateException('IllegalStateException') automaton = Automaton() ids = itertools.count(10000) initId = None for widget in diagram.widget: if widget['type'] == L.L_WIDGET_STATE: stateNode = int(widget['id']) if State.INITIAL == int(widget.attributes.type.cdata): if initId is None: initId = next(ids) automaton.initialState = initId stateEdge = self.createFormulaFromState(widget) automaton.addTransition(initId, stateNode, stateEdge) if not hasattr(widget.attributes, 'outgoing'): continue for outgoing in widget.attributes.outgoing: next_ = widgetMap.get(outgoing['id']) if next_['type'] == L.L_WIDGET_TRANSITION: # event eventEdge = self.createFormulaFromEvent( next_.attributes.event) eventNodeId = next(ids) automaton.addTransition(stateNode, eventNodeId, eventEdge) lastNodeId = eventNodeId # actions if hasattr(next_.attributes, 'action'): for action in next_.attributes.action: actionEdge = self.createFormulaFromActions_action( action) actionNodeId = next(ids) automaton.addTransition( lastNodeId, actionNodeId, actionEdge) lastNodeId = actionNodeId # next state successorId = successors.get(outgoing['id']) nextState = widgetMap.get(successorId) nextNode = int(nextState['id']) nextEdge = self.createFormulaFromState(nextState) automaton.addTransition(lastNodeId, nextNode, nextEdge) else: raise IllegalStateException('IllegalStateException2') for nodeId in automaton.nodes: automaton.setAccepting(nodeId) if len(automaton.get(nodeId)) == 0: automaton.addTransition(nodeId, nodeId, LTL.t()) return automaton
def ltl(self, _p: int = 0): _parentctx = self._ctx _parentState = self.state localctx = LTLParser.LtlContext(self, self._ctx, _parentState) _prevctx = localctx _startState = 0 self.enterRecursionRule(localctx, 0, self.RULE_ltl, _p) try: self.enterOuterAlt(localctx, 1) self.state = 22 self._errHandler.sync(self) la_ = self._interp.adaptivePredict(self._input, 0, self._ctx) if la_ == 1: pass elif la_ == 2: self.state = 7 localctx._primary = self.primary() localctx.f = localctx._primary.f pass elif la_ == 3: self.state = 10 self.match(LTLParser.NEXT) self.state = 11 localctx._ltl = self.ltl(9) localctx.f = LTL.next_(localctx._ltl.f) pass elif la_ == 4: self.state = 14 self.match(LTLParser.GLOB) self.state = 15 localctx._ltl = self.ltl(8) localctx.f = LTL.globally(localctx._ltl.f) pass elif la_ == 5: self.state = 18 self.match(LTLParser.FUTURE) self.state = 19 localctx._ltl = self.ltl(7) localctx.f = LTL.future(localctx._ltl.f) pass self._ctx.stop = self._input.LT(-1) self.state = 56 self._errHandler.sync(self) _alt = self._interp.adaptivePredict(self._input, 2, self._ctx) while _alt != 2 and _alt != ATN.INVALID_ALT_NUMBER: if _alt == 1: if self._parseListeners is not None: self.triggerExitRuleEvent() _prevctx = localctx self.state = 54 self._errHandler.sync(self) la_ = self._interp.adaptivePredict(self._input, 1, self._ctx) if la_ == 1: localctx = LTLParser.LtlContext( self, _parentctx, _parentState) localctx.l = _prevctx self.pushNewRecursionContext(localctx, _startState, self.RULE_ltl) self.state = 24 if not self.precpred(self._ctx, 6): from antlr4.error.Errors import FailedPredicateException raise FailedPredicateException( self, "self.precpred(self._ctx, 6)") self.state = 25 self.match(LTLParser.RELEASE) self.state = 26 localctx.r = localctx._ltl = self.ltl(7) localctx.f = LTL.release(localctx.l.f, localctx.r.f) pass elif la_ == 2: localctx = LTLParser.LtlContext( self, _parentctx, _parentState) localctx.l = _prevctx self.pushNewRecursionContext(localctx, _startState, self.RULE_ltl) self.state = 29 if not self.precpred(self._ctx, 5): from antlr4.error.Errors import FailedPredicateException raise FailedPredicateException( self, "self.precpred(self._ctx, 5)") self.state = 30 self.match(LTLParser.UNTIL) self.state = 31 localctx.r = localctx._ltl = self.ltl(6) localctx.f = LTL.until(localctx.l.f, localctx.r.f) pass elif la_ == 3: localctx = LTLParser.LtlContext( self, _parentctx, _parentState) localctx.l = _prevctx self.pushNewRecursionContext(localctx, _startState, self.RULE_ltl) self.state = 34 if not self.precpred(self._ctx, 4): from antlr4.error.Errors import FailedPredicateException raise FailedPredicateException( self, "self.precpred(self._ctx, 4)") self.state = 35 self.match(LTLParser.CON) self.state = 36 localctx.r = localctx._ltl = self.ltl(5) localctx.f = LTL.and_(localctx.l.f, localctx.r.f) pass elif la_ == 4: localctx = LTLParser.LtlContext( self, _parentctx, _parentState) localctx.l = _prevctx self.pushNewRecursionContext(localctx, _startState, self.RULE_ltl) self.state = 39 if not self.precpred(self._ctx, 3): from antlr4.error.Errors import FailedPredicateException raise FailedPredicateException( self, "self.precpred(self._ctx, 3)") self.state = 40 self.match(LTLParser.DIS) self.state = 41 localctx.r = localctx._ltl = self.ltl(4) localctx.f = LTL.or_(localctx.l.f, localctx.r.f) pass elif la_ == 5: localctx = LTLParser.LtlContext( self, _parentctx, _parentState) localctx.l = _prevctx self.pushNewRecursionContext(localctx, _startState, self.RULE_ltl) self.state = 44 if not self.precpred(self._ctx, 2): from antlr4.error.Errors import FailedPredicateException raise FailedPredicateException( self, "self.precpred(self._ctx, 2)") self.state = 45 self.match(LTLParser.IMPL) self.state = 46 localctx.r = localctx._ltl = self.ltl(2) localctx.f = LTL.impl(localctx.l.f, localctx.r.f) pass elif la_ == 6: localctx = LTLParser.LtlContext( self, _parentctx, _parentState) localctx.l = _prevctx self.pushNewRecursionContext(localctx, _startState, self.RULE_ltl) self.state = 49 if not self.precpred(self._ctx, 1): from antlr4.error.Errors import FailedPredicateException raise FailedPredicateException( self, "self.precpred(self._ctx, 1)") self.state = 50 self.match(LTLParser.EQ) self.state = 51 localctx.r = localctx._ltl = self.ltl(2) localctx.f = LTL.eq(localctx.l.f, localctx.r.f) pass self.state = 58 self._errHandler.sync(self) _alt = self._interp.adaptivePredict(self._input, 2, self._ctx) except RecognitionException as re: localctx.exception = re self._errHandler.reportError(self, re) self._errHandler.recover(self, re) finally: self.unrollRecursionContexts(_parentctx) return localctx