示例#1
0
        private static TlaExpr Translate(this exprItem item, AutomatonParsingContext ctx)
        {
            TlaExpr expr;

            if (item.identifier != null)
            {
                bool constValue;

                if (bool.TryParse(item.identifier.@string, out constValue))
                {
                    expr = new TlaExpr.Const(constValue);
                }
                else
                {
                    expr = new TlaExpr.Var(ctx.Unescape(item.identifier.@string));
                }
            }
            else if (item.exprGroup != null)
            {
                expr = item.exprGroup.exprSeq.TranslateToTlaExpr(ctx);
            }
            else
            {
                throw new NotImplementedException();
            }

            if (item.not != null)
            {
                expr = new TlaExpr.Not(expr);
            }

            return(expr);
        }
        private static TransitionConditionExpr Translate(this exprItem item, AutomatonParsingContext ctx)
        {
            TransitionConditionExpr expr;

            if (item.identifier != null)
            {
                expr = new TransitionConditionExpr.VarExpr(ctx.Unescape(item.identifier.@string));
            }
            else if (item.exprGroup != null)
            {
                expr = item.exprGroup.exprSeq.TranslateToConditionExpr(ctx);
            }
            else if (item.literal != null)
            {
                expr = new TransitionConditionExpr.ConstExpr(true);
            }
            else
            {
                throw new NotImplementedException();
            }

            if (item.not != null)
            {
                expr = new TransitionConditionExpr.NotExpr(expr);
            }

            return(expr);
        }
示例#3
0
        private static TlaExpr TranslateToTlaExpr(this exprSeq expr, AutomatonParsingContext ctx)
        {
            if (expr.exprItems.Length != expr.boolOperators.Length + 1)
            {
                throw new ApplicationException();
            }

            var items = new ExprItem[expr.exprItems.Length + expr.boolOperators.Length];

            items[0] = new ExprItem(expr.exprItems[0].Translate(ctx));
            for (int i = 0, j = 1; i < expr.boolOperators.Length; i++, j += 2)
            {
                items[j + 0] = new ExprItem(_opsByStr[expr.boolOperators[i].strings.First()]);
                items[j + 1] = new ExprItem(expr.exprItems[i + 1].Translate(ctx));
            }

            return(TranslateExprPart(items, 0, items.Length));
        }
        public static Automaton TranslateToModel(this automaton root, AutomatonParsingContext ctx)
        {
            var transitionIdCount = 0;

            var a = new Automaton()
            {
                States = root.states.Select((s, n) => new State(n)
                {
                    Name        = s.stateName.identifier.@string,
                    IsInitial   = [email protected]("_init"),
                    IsAccepting = [email protected]("accept_"),
                }).ToList()
            };

            foreach (var state in root.states)
            {
                var st = a.States.First(s => s.Name == state.stateName.identifier.@string);

                st.Outgoing = state.transitions != null?state.transitions.Select(t => new Transition(transitionIdCount++)
                {
                    ToId      = a.States.First(s => s.Name == t.stateName.identifier.@string).Id,
                    FromId    = st.Id,
                    Condition = t.condition.exprSeq.TranslateToConditionExpr(ctx)
                }).ToList() : new List <Transition>();

                if (state.skip != null)
                {
                    st.Outgoing.Add(new Transition(transitionIdCount++)
                    {
                        ToId = st.Id, FromId = st.Id
                    });
                }
            }

            foreach (var st in a.States)
            {
                st.Incoming = a.States.SelectMany(s => s.Outgoing).Where(t => t.ToId == st.Id).ToList();
            }

            return(a);
        }
        public TlaAutomaton ParseTlaAutomaton(string text, bool useTransitionConditions, AutomatonParsingContext ctx)
        {
            var tree = this.ParseText(text);

            return(tree == null ? null : tree.TranslateToTlaAutomaton(useTransitionConditions, ctx));
        }
        public Automaton TryParseModelAutomaton(string text, AutomatonParsingContext ctx)
        {
            var tree = this.ParseText(text);

            return(tree == null ? null : tree.TranslateToModel(ctx));
        }
示例#7
0
        static TlaFormula Translate(this condition cond, bool useTransitionConditions, AutomatonParsingContext ctx)
        {
            TlaFormula result;

            if (useTransitionConditions)
            {
                result = new TlaTransitionConditionFormula(cond.exprSeq.TranslateToConditionExpr(ctx));
            }
            else
            {
                result = new TlaExprFormula(cond.exprSeq.TranslateToTlaExpr(ctx));
            }

            return(result);
        }
示例#8
0
        public static TlaAutomaton TranslateToTlaAutomaton(this automaton root, bool useTransitionConditions, AutomatonParsingContext ctx)
        {
            var automaton = new TlaAutomaton();

            foreach (var state in root.states)
            {
                var name = state.stateName.identifier.@string;
                automaton.CreateState(name, name.EndsWith("_init"), name.StartsWith("accept_"));
            }

            foreach (var state in root.states)
            {
                foreach (var target in state.transitions)
                {
                    var condition = target.condition.Translate(useTransitionConditions, ctx);
                    automaton.CreateTransition(state.stateName.identifier.@string, target.stateName.identifier.@string, condition);
                }

                if (state.skip != null)
                {
                    automaton.CreateTransition(
                        state.stateName.identifier.@string, state.stateName.identifier.@string,
                        useTransitionConditions ? new TlaTransitionConditionFormula(new Model.TransitionConditionExpr.ConstExpr(true))
                                                : (TlaFormula) new TlaExprFormula(new TlaExpr.Const(true))
                        );
                }
            }

            return(automaton);
        }