Exemplo n.º 1
0
        public static GroupedState GetEpsilonClosureFromState(Automata automata, State targetState, GroupedState aggregatedState = null)
        {
            if (targetState is GroupedState)
            {
                return(GetAggregatedEpsilonClosureFromStates(
                           automata, ((GroupedState)targetState).GetSubStates().ToList()
                           ));
            }
            else
            {
                // spontaneous transitions from target state
                var transitionsFromState = automata.GetTransitionsFromState(
                    automata.GetStateLike(targetState), Automata.SYMBOL_SPONTANEOUS_TRANSITION
                    );

                // spontaneously (directly) reachable states from target state
                var reachableStates = transitionsFromState.Select(x => x.To).ToList();

                // epsilon closure includes current state
                reachableStates.Add(targetState);

                // aggregate spontaneously reachable states to a single state
                if (aggregatedState == null)
                {
                    aggregatedState = new GroupedState(reachableStates);
                }
                else
                {
                    aggregatedState.AddSubStates(reachableStates);
                }

                // aggregate spontaneously reachable
                // child states to our state recursively
                if (reachableStates.Count > 1)
                {
                    reachableStates.ForEach(
                        x => {
                        if (x != targetState)
                        {
                            aggregatedState.AddSubState(
                                GetEpsilonClosureFromState(
                                    automata, x, aggregatedState
                                    )
                                );
                        }
                    }
                        );
                }
                return(aggregatedState);
            }
        }
Exemplo n.º 2
0
        public static State DeepCloneState(State state)
        {
            State clonedState = null;

            if (state is GroupedState)
            {
                clonedState = new GroupedState(
                    ((GroupedState)state).GetSubStates()
                    );
            }
            else
            {
                clonedState = new State(state.Name, state.IsFinal);
            }
            return(clonedState);
        }
Exemplo n.º 3
0
        public static GroupedState GetAggregatedEpsilonClosureFromStates(Automata automata, List <State> targetStates)
        {
            GroupedState aggregatedState = null;

            targetStates.ForEach(
                x => {
                var epsilon = GetEpsilonClosureFromState(automata, x);

                if (aggregatedState == null)
                {
                    aggregatedState = new GroupedState(epsilon.GetSubStates());
                }
                else
                {
                    aggregatedState.AddSubStates(epsilon.GetSubStates());
                }
            }
                );
            return(aggregatedState);
        }
Exemplo n.º 4
0
        public static Tuple <List <GroupedState>, List <Transition> > GetEpsilonTuple(
            Automata automata,
            State currentState          = null,
            GroupedState currentEpsilon = null,
            Tuple <List <GroupedState>, List <Transition> > aggregatedValues = null
            )
        {
            // init our tuple
            if (aggregatedValues == null)
            {
                aggregatedValues = new Tuple <List <GroupedState>, List <Transition> >(
                    new List <GroupedState>(),
                    new List <Transition>()
                    );
            }

            var aggregatedStates      = aggregatedValues.Item1;
            var aggregatedTransitions = aggregatedValues.Item2;

            // defining first state
            if (currentState == null)
            {
                currentState = automata.GetInitialState();
            }

            // defining the current epsilon
            if (currentEpsilon == null)
            {
                currentEpsilon = GetEpsilonClosureFromState(
                    automata, currentState
                    );
            }

            // aggregate initial epsilon
            if (aggregatedStates == null)
            {
                aggregatedStates = new List <GroupedState>();
            }

            if (!AggregatedStateListContainsStateLike(aggregatedStates, currentEpsilon))
            {
                aggregatedStates.Add(currentEpsilon);

                // find more epsilons and aggregate them recursively
                foreach (var symbol in automata.GetSymbols())
                {
                    var epsilonCandidates = new List <State>();

                    // get all states found consuming the symbol
                    // from every subState in our epsilon
                    currentEpsilon.GetSubStates().ToList().ForEach(
                        x => epsilonCandidates.AddRange(
                            automata.GetTransitionsFromState(
                                x, symbol
                                ).Select(t => t.To)
                            )
                        );

                    if (epsilonCandidates.Count > 0)
                    {
                        var aggregatedCandidates = new GroupedState(epsilonCandidates);
                        var aggregationEpsilon   = GetEpsilonClosureFromState(
                            automata, aggregatedCandidates
                            );

                        aggregatedTransitions.Add(new Transition(
                                                      currentEpsilon, symbol, aggregationEpsilon
                                                      ));

                        // aggregate new epsilons recursively
                        GetEpsilonTuple(
                            automata,
                            aggregatedCandidates,
                            aggregationEpsilon,
                            aggregatedValues
                            );
                    }
                }
            }

            return(aggregatedValues);
        }