Exemplo n.º 1
0
        public void ShouldEnumerateTuples()
        {
            SituationDictionary <char> dictionary;

            IAutomatonTableTuple <char>[] result;
            Situation <char>           a, b;
            SituationCollection <char> situations;


            a = new Situation <char>(new MockedRule(), new MockedPredicate(), new MockedReduceInput());
            b = new Situation <char>(new MockedRule(), new MockedPredicate(), new MockedReduceInput());

            dictionary = new SituationDictionary <char>();

            situations = new SituationCollection <char>();
            situations.Add(a); situations.Add(b);
            dictionary.CreateTuple(new State <char>(), situations);

            situations = new SituationCollection <char>();
            situations.Add(a);
            dictionary.CreateTuple(new State <char>(), situations);

            situations = new SituationCollection <char>();
            situations.Add(b);
            dictionary.CreateTuple(new State <char>(), situations);

            result = dictionary.ToArray();
            Assert.AreEqual(3, result.Length);
        }
Exemplo n.º 2
0
        public void ShouldGetReductionSituations()
        {
            Situation <char>           a, b;
            SituationCollection <char> situations;

            ISituation <char>[] reductions;


            a = new Situation <char>(new MockedRule(), new MockedPredicate(), new MockedReduceInput());
            b = new Situation <char>(new MockedRule(), new MockedPredicate(), new MockedReduceInput());

            situations = new SituationCollection <char>();
            situations.Add(a); situations.Add(b);
            reductions = situations.GetReductionSituations().ToArray();
            Assert.AreEqual(1, reductions.Length);


            a = new Situation <char>(new MockedRule(), new MockedPredicate(), new MockedReduceInput());
            b = new Situation <char>(new MockedRule2(), new MockedPredicate(), new MockedReduceInput());

            situations = new SituationCollection <char>();
            situations.Add(a); situations.Add(b);
            reductions = situations.GetReductionSituations().ToArray();
            Assert.AreEqual(2, reductions.Length);
        }
Exemplo n.º 3
0
        public void ShouldAdd()
        {
            Situation <char>           a, b;
            SituationCollection <char> situations;

            a = new Situation <char>(new MockedRule(), new MockedPredicate(), new MockedReduceInput());
            b = new Situation <char>(new MockedRule2(), new MockedPredicate2(), new MockedReduceInput2());

            situations = new SituationCollection <char>();
            Assert.AreEqual(0, situations.Count);
            situations.Add(a);
            Assert.AreEqual(1, situations.Count);
            situations.Add(b);
            Assert.AreEqual(2, situations.Count);
        }
Exemplo n.º 4
0
        public void ShouldNotGetTuple()
        {
            SituationDictionary <char>  dictionary;
            IAutomatonTableTuple <char> result;
            Situation <char>            a, b;
            SituationCollection <char>  situations;

            a = new Situation <char>(new MockedRule(), new MockedPredicate(), new MockedReduceInput());
            b = new Situation <char>(new MockedRule(), new MockedPredicate(), new MockedReduceInput());

            situations = new SituationCollection <char>();
            situations.Add(a); situations.Add(b);

            dictionary = new SituationDictionary <char>();
            result     = dictionary.GetTuple(situations);
            Assert.IsNull(result);
        }
Exemplo n.º 5
0
        public void ShouldContains()
        {
            Situation <char>           a, b, c;
            SituationCollection <char> situations;

            a = new Situation <char>(new MockedRule(), new MockedPredicate(), new MockedReduceInput());
            b = new Situation <char>(new MockedRule2(), new MockedPredicate2(), new MockedReduceInput2());
            c = new Situation <char>(new MockedRule2(), new MockedPredicate2(), new MockedReduceInput());

            situations = new SituationCollection <char>();
            situations.Add(a);
            situations.Add(b);

            Assert.IsTrue(situations.Contains(a));
            Assert.IsTrue(situations.Contains(b));
            Assert.IsFalse(situations.Contains(c));
        }
Exemplo n.º 6
0
        public void ShouldNotEquals()
        {
            Situation <char>           a, b, c;
            SituationCollection <char> situations1, situations2;

            a = new Situation <char>(new MockedRule(), new MockedPredicate(), new MockedReduceInput());
            b = new Situation <char>(new MockedRule2(), new MockedPredicate2(), new MockedReduceInput2());
            c = new Situation <char>(new MockedRule2(), new MockedPredicate2(), new MockedReduceInput());

            situations1 = new SituationCollection <char>();
            situations1.Add(a);
            situations1.Add(b);
            situations1.Add(c);

            situations2 = new SituationCollection <char>();             // different order
            situations2.Add(c);
            situations2.Add(b);

            Assert.IsFalse(situations1.Equals(situations2));
            Assert.IsFalse(situations2.Equals(situations1));
        }
Exemplo n.º 7
0
        public State[] BuildStates(ISituation RootSituation)
        {
            List <State> states;
            List <SituationCollection> situationCollections;
            List <SituationCollection> openList;
            List <ISituation>          nextSituations;

            SituationCollection currentSituationCollection, existingSituationCollection;

            if (RootSituation == null)
            {
                throw new ArgumentNullException("RootSituation");
            }
            states = new List <State>();
            situationCollections = new List <SituationCollection>();
            openList             = new List <SituationCollection>();

            currentSituationCollection       = new SituationCollection();
            currentSituationCollection.State = new State();
            currentSituationCollection.Items.Add(RootSituation);

            situationCollections.Add(currentSituationCollection);
            states.Add(currentSituationCollection.State);

            openList.Add(currentSituationCollection);
            while (openList.Count > 0)
            {
                currentSituationCollection = openList[0];
                openList.RemoveAt(0);

                for (char input = char.MinValue; input < char.MaxValue; input++)
                {
                    nextSituations = new List <ISituation>();
                    foreach (ISituation situation in currentSituationCollection.Items)
                    {
                        foreach (ITransition transition in situation.Transitions)
                        {
                            if (transition is IReductionTransition reductionTransition)
                            {
                                currentSituationCollection.State.CreateReduction(reductionTransition.Rule);
                                continue;
                            }
                            if (transition is IShiftTransition shiftTransition)
                            {
                                if (shiftTransition.Predicate.Accept(input))
                                {
                                    nextSituations.Add(shiftTransition.TargetSituation);
                                }
                            }
                        }
                    }
                    if (nextSituations.Count == 0)
                    {
                        continue;
                    }

                    existingSituationCollection = situationCollections.FirstOrDefault(item => AreEquals(item.Items, nextSituations));
                    if (existingSituationCollection == null)
                    {
                        existingSituationCollection       = new SituationCollection();
                        existingSituationCollection.Items = nextSituations;
                        existingSituationCollection.State = new State();

                        situationCollections.Add(existingSituationCollection);
                        states.Add(existingSituationCollection.State);
                        openList.Add(existingSituationCollection);
                    }

                    currentSituationCollection.State.CreateShift(input, states.IndexOf(existingSituationCollection.State));
                }
            }

            return(states.ToArray());
        }