Exemplo n.º 1
0
        public Label GetMetaInitialState()
        {
            CheckStatesSimple("Meta state cannot be obtained for non simple states.");

            return(InitialState.ConvertToComplex());
        }
Exemplo n.º 2
0
        public StateMachine ConvertToDeterministicIfNot()
        {
            if (IsDeterministic())
            {
                return(this);
            }

            SortedSet <Transition> newTransitions = new SortedSet <Transition>();

            SortedSet <Label> visitedNewStates = new SortedSet <Label>();

            Label newInitialState = InitialState.ConvertToComplex();

            visitedNewStates.Add(newInitialState);

            Queue <Label> queue = new Queue <Label>();

            queue.Enqueue(newInitialState);

            do
            {
                Label currentState = queue.Dequeue();

                Dictionary <char, HashSet <SingleLabel> > symbolSingleLabelsDictionary = new Dictionary <char, HashSet <SingleLabel> >();

                foreach (Transition transition in Transitions)
                {
                    SingleLabel currentStateSingleLabel = transition.CurrentState.ExtractSingleLabel();

                    if (currentState.Sublabels.Contains(currentStateSingleLabel))
                    {
                        HashSet <SingleLabel> singleLabels;

                        if (symbolSingleLabelsDictionary.ContainsKey(transition.Symbol))
                        {
                            singleLabels = symbolSingleLabelsDictionary[transition.Symbol];
                        }
                        else
                        {
                            symbolSingleLabelsDictionary[transition.Symbol] = singleLabels = new HashSet <SingleLabel>();
                        }

                        SingleLabel nextStateSingleLabel = transition.NextState.ExtractSingleLabel();

                        singleLabels.Add(nextStateSingleLabel);
                    }
                }

                foreach (KeyValuePair <char, HashSet <SingleLabel> > entry in symbolSingleLabelsDictionary)
                {
                    Label nextState = new Label(entry.Value);
                    newTransitions.Add(new Transition(currentState, entry.Key, nextState));

                    if (visitedNewStates.Add(nextState))
                    {
                        queue.Enqueue(nextState);
                    }
                }
            } while (queue.Count > 0);

            HashSet <Label> newFinalStates = new HashSet <Label>();

            foreach (Label state in ExtractStates(newTransitions))
            {
                bool atLeastOneFromFinalStates = false;

                foreach (Label finalState in FinalStates)
                {
                    if (state.Sublabels.Contains(finalState.ExtractSingleLabel()))
                    {
                        atLeastOneFromFinalStates = true;
                        break;
                    }
                }

                if (atLeastOneFromFinalStates)
                {
                    newFinalStates.Add(state);
                }
            }

            return(new StateMachine(newInitialState, newFinalStates, newTransitions));
        }