Ejemplo n.º 1
0
        private void Prepare (ModelProgram program) {
            var actionSymbols = program.ActionSymbols ();
            var _states = new Dictionary <SimpleState, int> ();
            var _trans = new Dictionary <SimpleState, List <SimpleState>> ();
            var frontier = new LinkedList <IState> (new [] { program.InitialState });
            var stateIndex = 0;

            while (frontier.Count > 0) {
                var state = (SimpleState) frontier.First.Value;
                frontier.RemoveFirst ();

                if (! _states.ContainsKey (state))
                    _states.Add (state, stateIndex ++);

                foreach (var action in actionSymbols.
                    Where (symbol => program.IsPotentiallyEnabled (state, symbol)).
                    SelectMany (symbol => program.GetActions (state, symbol))) {
                    TransitionProperties properties;
                    var target =
                        (SimpleState) program.GetTargetState (state, action, Set <string>.EmptySet, out properties);

                    if (! _trans.ContainsKey (state))
                        _trans.Add (state, new List <SimpleState> ());
                    _trans [state].Add (target);

                    if (! (_states.ContainsKey (target) || frontier.Contains (target)))
                        frontier.AddFirst (target);
                }
            }

            foreach (var state in _states) {
                var list = new List <string> ();

                for (var i = 0; i < state.Key.LocationValuesCount; ++ i)
                    list.Add (string.Format ("{0}={1}", state.Key.GetLocationName (i), state.Key.GetLocationValue (i)));

                this.states.Add (state.Value, list);
            }

            foreach (var transition in _trans) {
                this.transitions.Add (_states [transition.Key],
                                      transition.Value.Select (state => _states [state]).ToList ());
            }

            for (var i = 0; i < stateIndex; ++ i) {
                if (! this.transitions.ContainsKey (i))
                    this.transitions.Add (i, new List <int> ());
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Explores the model associated with this instance.
        /// The dictionary stateMap (if not null) is used to record the mapping
        /// from generated finite automata states to IStates.
        /// Explore(null) is the same as Explore()
        /// </summary>
        /// <returns>A list of transitions. Each transition is a start state,
        /// an action label and an end state.</returns>
        public FSM Explore(Dictionary <Term, IState> generatedStateMap)
        {
            Set <Symbol> actionSymbols = modelProgram.ActionSymbols();
            IState       initialState  = modelProgram.InitialState;

            Dictionary <IState, int> states   = new Dictionary <IState, int>();
            LinkedList <IState>      frontier = new LinkedList <IState>();

            frontier.AddFirst(initialState);
            int nextStateId = 0;

            Dictionary <Transition, Transition> transitions = new Dictionary <Transition, Transition>();

            while (frontier.Count > 0)
            {
                IState startState = frontier.First.Value;
                frontier.RemoveFirst();
                if (!states.ContainsKey(startState))
                {
                    states.Add(startState, nextStateId++);
                }

                foreach (Symbol actionSymbol in actionSymbols)
                {
                    if (modelProgram.IsPotentiallyEnabled(startState, actionSymbol))
                    {
                        IEnumerable <CompoundTerm> actions = modelProgram.GetActions(startState, actionSymbol);
                        foreach (CompoundTerm action in actions)
                        {
                            TransitionProperties transitionProperties;
                            IState targetState = modelProgram.GetTargetState(startState, action, Set <string> .EmptySet, out transitionProperties);
                            if (IncludeTransition(startState, action, targetState, transitionProperties.Properties))
                            {
                                Transition t = new Transition(startState, action, targetState);
                                transitions.Add(t, t);
                                if (!states.ContainsKey(targetState) && !frontier.Contains(targetState))
                                {
                                    frontier.AddFirst(targetState);
                                }
                            }
                        }
                    }
                }
            }
            Term automatonInitialState = new Literal(states[initialState]);

            Set <Term> automatonStates = Set <Term> .EmptySet;
            Set <Term> acceptingStates = Set <Term> .EmptySet;

            foreach (KeyValuePair <IState, int> kv in states)
            {
                Term automatonState = new Literal(kv.Value);
                automatonStates = automatonStates.Add(automatonState);
                if (modelProgram.IsAccepting(kv.Key))
                {
                    acceptingStates = acceptingStates.Add(automatonState);
                }
                if (generatedStateMap != null)
                {
                    generatedStateMap[automatonState] = kv.Key;
                }
            }

            Set <Triple <Term, CompoundTerm, Term> > automatonTransitions = Set <Triple <Term, CompoundTerm, Term> > .EmptySet;

            foreach (KeyValuePair <Transition, Transition> kv in transitions)
            {
                Transition t = kv.Key;
                automatonTransitions = automatonTransitions.Add(new Triple <Term, CompoundTerm, Term>(new Literal(states[t.startState]),
                                                                                                      t.action,
                                                                                                      new Literal(states[t.targetState])));
            }
            return(new FSM(automatonInitialState, automatonStates, automatonTransitions, acceptingStates));
        }