Exemplo n.º 1
0
        public bool Equals(SetsOfEquivalence other)
        {
            if (other == null)
            {
                return(false);
            }

            return(this.SequenceEqual(other));
        }
Exemplo n.º 2
0
        public MinimizingBeginPostReport(SetsOfEquivalence setsOfEquivalence, int iteration)
        {
            if (setsOfEquivalence == null)
            {
                throw new ArgumentNullException(nameof(setsOfEquivalence));
            }

            SetsOfEquivalence = setsOfEquivalence;
            Iteration         = iteration;
        }
Exemplo n.º 3
0
 public static int Compare(SetsOfEquivalence objA, SetsOfEquivalence objB)
 {
     if (objA == null)
     {
         if (objB == null)
         {
             return(0);
         }
         else
         {
             return(-1);
         }
     }
     return(objA.CompareTo(objB));
 }
Exemplo n.º 4
0
 public static bool Equals(SetsOfEquivalence objA, SetsOfEquivalence objB)
 {
     if ((object)objA == null)
     {
         if ((object)objB == null)
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
     return(objA.Equals(objB));
 }
Exemplo n.º 5
0
 private IDictionary <Label, Label> GetOldNewStatesMap(SetsOfEquivalence setsOfEquivalence)
 {
     return(setsOfEquivalence.SelectMany(item => item.Select(subitem => new Tuple <Label, Label>(subitem, item.First()))).
            ToDictionary(item => item.Item1, item => item.Item2));
 }
Exemplo n.º 6
0
 private IDictionary <Label, int> GetStatesMap(SetsOfEquivalence setsOfEquivalence)
 {
     return(setsOfEquivalence.SelectMany((set, index) => set.Select(item => new KeyValuePair <Label, int>(item, index))).ToDictionary());
 }
Exemplo n.º 7
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));
        }
Exemplo n.º 8
0
 public int CompareTo(SetsOfEquivalence other)
 {
     return(this.SequenceCompare(other));
 }
Exemplo n.º 9
0
        public override bool Equals(object obj)
        {
            SetsOfEquivalence setsOfEquivalence = obj as SetsOfEquivalence;

            return(Equals(setsOfEquivalence));
        }