Exemplo n.º 1
0
        public int CompareTo(ClassOfEquivalenceSet other)
        {
            if (other == null)
            {
                return(1);
            }

            return(this.SequenceCompare(other));
        }
Exemplo n.º 2
0
        public bool Equals(ClassOfEquivalenceSet other)
        {
            if (other == null)
            {
                return(false);
            }

            return(this.SequenceEqual(other));
        }
Exemplo n.º 3
0
 public static int Compare(ClassOfEquivalenceSet objA, ClassOfEquivalenceSet objB)
 {
     if (objA == null)
     {
         if (objB == null)
         {
             return(0);
         }
         else
         {
             return(-1);
         }
     }
     return(objA.CompareTo(objB));
 }
Exemplo n.º 4
0
 public static bool Equals(ClassOfEquivalenceSet objA, ClassOfEquivalenceSet objB)
 {
     if ((object)objA == null)
     {
         if ((object)objB == null)
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
     return(objA.Equals(objB));
 }
Exemplo n.º 5
0
        public override bool Equals(object obj)
        {
            ClassOfEquivalenceSet classOfEquivalenceSet = obj as ClassOfEquivalenceSet;

            return(Equals(classOfEquivalenceSet));
        }
Exemplo n.º 6
0
        public StateMachine Minimize(
            Action <MinimizingBeginPostReport> onBegin       = null,
            Action <MinimizingIterationPostReport> onIterate = null,
            Action <bool> onEnd = null)
        {
            ILookup <Label, Transition> transitionsByCurrentState = Transitions.ToLookup(transition => transition.CurrentState);

            ISet <Label> nonFinalStates = new SortedSet <Label>(States);

            nonFinalStates.ExceptWith(FinalStates);

            SetsOfEquivalence setsOfEquivalence = new SetsOfEquivalence(
                EnumerateHelper.Sequence(
                    new SetOfEquivalence(nonFinalStates),
                    new SetOfEquivalence(FinalStates)
                    )
                );

            int i = 0;

            if (onBegin != null)
            {
                onBegin(new MinimizingBeginPostReport(setsOfEquivalence, i));
            }

            bool changed;

            do
            {
                changed = false;

                i++;

                List <SetOfEquivalence> buildingSetsOfEquivalence = new List <SetOfEquivalence>();

                List <SetOfEquivalenceTransition> setOfEquivalenceTransitions = new List <SetOfEquivalenceTransition>();

                IDictionary <Label, int> statesMap = GetStatesMap(setsOfEquivalence);

                foreach (SetOfEquivalence setOfEquivalence in setsOfEquivalence)
                {
                    IDictionary <ClassOfEquivalenceSet, ISet <Label> > classEquivalenceSetMap = new Dictionary <ClassOfEquivalenceSet, ISet <Label> >();

                    foreach (Label state in setOfEquivalence)
                    {
                        IDictionary <int, ISet <char> > groupMap = new Dictionary <int, ISet <char> >();

                        foreach (Transition transition in transitionsByCurrentState[state])
                        {
                            int groupNum = statesMap[transition.NextState];

                            ISet <char> equality;

                            if (groupMap.ContainsKey(groupNum))
                            {
                                equality = groupMap[groupNum];
                            }
                            else
                            {
                                groupMap[groupNum] = equality = new HashSet <char>();
                            }

                            equality.Add(transition.Symbol);
                        }

                        ClassOfEquivalenceSet classOfEquivalenceSet = new ClassOfEquivalenceSet(groupMap.Select(item => new ClassOfEquivalence(item.Key, item.Value)));

                        ISet <Label> states;

                        if (classEquivalenceSetMap.ContainsKey(classOfEquivalenceSet))
                        {
                            states = classEquivalenceSetMap[classOfEquivalenceSet];
                        }
                        else
                        {
                            classEquivalenceSetMap[classOfEquivalenceSet] = states = new HashSet <Label>();
                        }

                        states.Add(state);
                    }

                    foreach (KeyValuePair <ClassOfEquivalenceSet, ISet <Label> > entry in classEquivalenceSetMap)
                    {
                        SetOfEquivalence set = new SetOfEquivalence(entry.Value, entry.Key.Select(c => new SetOfEquivalenceTransition(c.Symbols, c.SetNum)));
                        buildingSetsOfEquivalence.Add(set);
                    }
                }

                SetsOfEquivalence nextSetsOfEquivalence = new SetsOfEquivalence(buildingSetsOfEquivalence);

                changed = setsOfEquivalence != nextSetsOfEquivalence;

                setsOfEquivalence = nextSetsOfEquivalence;

                if (onIterate != null)
                {
                    onIterate(new MinimizingIterationPostReport(setsOfEquivalence, i, !changed));
                }
            } while (changed);

            if (onEnd != null)
            {
                onEnd(setsOfEquivalence.Count != States.Count);
            }

            if (setsOfEquivalence.Count == States.Count)
            {
                return(this);
            }

            IDictionary <Label, Label> newStatesMap = GetOldNewStatesMap(setsOfEquivalence);

            ISet <Transition> transitions = Transitions.Select(item => new Transition(newStatesMap[item.CurrentState], item.Symbol, newStatesMap[item.NextState])).ToHashSet();

            ISet <Label> finalStates = FinalStates.Select(item => newStatesMap[item]).ToHashSet();

            Label initialState = newStatesMap[InitialState];

            return(new StateMachine(initialState, finalStates, transitions));
        }