Ejemplo n.º 1
0
        public void MutableNetContent()
        {
            var net    = new PetriNet <int>();
            var place1 = net.AddPlace("P1");

            CollectionAssert.AreEquivalent(new[] { place1 }, net.Places);
            CollectionAssert.IsEmpty(net.Transitions);
            CollectionAssert.IsEmpty(net.Arcs);
            AssertHasVertices(net.Graph, new[] { place1 });
            AssertNoEdge(net.Graph);

            var transition1 = net.AddTransition("T1");

            CollectionAssert.AreEquivalent(new[] { place1 }, net.Places);
            CollectionAssert.AreEquivalent(new[] { transition1 }, net.Transitions);
            CollectionAssert.IsEmpty(net.Arcs);
            AssertHasVertices(net.Graph, new IPetriVertex[] { place1, transition1 });
            AssertNoEdge(net.Graph);

            var place2      = net.AddPlace("P2");
            var transition2 = net.AddTransition("T2");
            var place3      = net.AddPlace("P3");

            CollectionAssert.AreEquivalent(new[] { place1, place2, place3 }, net.Places);
            CollectionAssert.AreEquivalent(new[] { transition1, transition2 }, net.Transitions);
            CollectionAssert.IsEmpty(net.Arcs);
            AssertHasVertices(net.Graph, new IPetriVertex[] { place1, place2, place3, transition1, transition2 });
            AssertNoEdge(net.Graph);

            var arc1 = net.AddArc(place1, transition1);

            CollectionAssert.AreEquivalent(new[] { place1, place2, place3 }, net.Places);
            CollectionAssert.AreEquivalent(new[] { transition1, transition2 }, net.Transitions);
            CollectionAssert.AreEquivalent(new[] { arc1 }, net.Arcs);
            AssertHasVertices(net.Graph, new IPetriVertex[] { place1, place2, place3, transition1, transition2 });
            AssertHasEdges(net.Graph, new[] { arc1 });

            var arc2 = net.AddArc(transition2, place3);
            var arc3 = net.AddArc(place2, transition2);

            CollectionAssert.AreEquivalent(new[] { place1, place2, place3 }, net.Places);
            CollectionAssert.AreEquivalent(new[] { transition1, transition2 }, net.Transitions);
            CollectionAssert.AreEquivalent(new[] { arc1, arc2, arc3 }, net.Arcs);
            AssertHasVertices(net.Graph, new IPetriVertex[] { place1, place2, place3, transition1, transition2 });
            AssertHasEdges(net.Graph, new[] { arc1, arc2, arc3 });
        }
Ejemplo n.º 2
0
        public void Clone()
        {
            var net = new PetriNet <int>();

            AssertEmpty(net);

            var clonedNet = net.Clone();

            Assert.IsNotNull(clonedNet);
            AssertEmpty(clonedNet);

            clonedNet = (PetriNet <int>)((ICloneable)net).Clone();
            Assert.IsNotNull(clonedNet);
            AssertEmpty(clonedNet);

            var place1      = net.AddPlace("p1");
            var place2      = net.AddPlace("p2");
            var transition1 = net.AddTransition("t1");

            AssertHasVertices(net.Graph, new IPetriVertex[] { place1, place2, transition1 });
            AssertNoEdge(net.Graph);

            clonedNet = net.Clone();
            Assert.IsNotNull(clonedNet);
            AssertAreEqual(net, clonedNet);

            clonedNet = (PetriNet <int>)((ICloneable)net).Clone();
            Assert.IsNotNull(clonedNet);
            AssertAreEqual(net, clonedNet);

            var place3      = net.AddPlace("p3");
            var transition2 = net.AddTransition("t2");
            var arc1        = net.AddArc(place1, transition1);
            var arc2        = net.AddArc(place2, transition1);
            var arc3        = net.AddArc(place3, transition2);
            var arc4        = net.AddArc(transition1, place3);
            var arc5        = net.AddArc(transition2, place1);

            AssertHasVertices(net.Graph, new IPetriVertex[] { place1, place2, place3, transition1, transition2 });
            AssertHasEdges(net.Graph, new[] { arc1, arc2, arc3, arc4, arc5 });

            clonedNet = net.Clone();
            Assert.IsNotNull(clonedNet);
            AssertAreEqual(net, clonedNet);

            clonedNet = (PetriNet <int>)((ICloneable)net).Clone();
            Assert.IsNotNull(clonedNet);
            AssertAreEqual(net, clonedNet);

            var place4      = net.AddPlace("p4");
            var transition3 = net.AddTransition("t3");

            AssertHasVertices(net.Graph, new IPetriVertex[] { place1, place2, place3, place4, transition1, transition2, transition3 });
            AssertHasEdges(net.Graph, new[] { arc1, arc2, arc3, arc4, arc5 });

            clonedNet = net.Clone();
            Assert.IsNotNull(clonedNet);
            AssertAreEqual(net, clonedNet);

            clonedNet = (PetriNet <int>)((ICloneable)net).Clone();
            Assert.IsNotNull(clonedNet);
            AssertAreEqual(net, clonedNet);

            #region Local function

            void AssertEmpty <TToken>(PetriNet <TToken> n)
            {
                CollectionAssert.IsEmpty(n.Places);
                CollectionAssert.IsEmpty(n.Transitions);
                CollectionAssert.IsEmpty(n.Arcs);
                AssertEmptyGraph(n.Graph);
            }

            void AssertAreEqual <TToken>(PetriNet <TToken> expected, PetriNet <TToken> actual)
            {
                CollectionAssert.AreEqual(expected.Places, actual.Places);
                CollectionAssert.AreEqual(expected.Transitions, actual.Transitions);
                CollectionAssert.AreEqual(expected.Arcs, actual.Arcs);
                AssertEquivalentGraphs(expected.Graph, actual.Graph);
            }

            #endregion
        }
Ejemplo n.º 3
0
        public void ObjectToString()
        {
            var    net            = new PetriNet <int>();
            string expectedString =
                "-----------------------------------------------" + Environment.NewLine +
                "Places (0)" + Environment.NewLine +
                "Transitions (0)" + Environment.NewLine +
                "Arcs" + Environment.NewLine;

            Assert.AreEqual(expectedString, net.ToString());

            var place1 = net.AddPlace("TestPlace");

            expectedString =
                "-----------------------------------------------" + Environment.NewLine +
                "Places (1)" + Environment.NewLine +
                "\tP(TestPlace|0)" + Environment.NewLine +
                Environment.NewLine +
                "Transitions (0)" + Environment.NewLine +
                "Arcs" + Environment.NewLine;
            Assert.AreEqual(expectedString, net.ToString());

            place1.Marking.Add(1);
            place1.Marking.Add(5);
            var place2 = net.AddPlace("TestPlace2");

            expectedString =
                "-----------------------------------------------" + Environment.NewLine +
                "Places (2)" + Environment.NewLine +
                "\tP(TestPlace|2)" + Environment.NewLine +
                "\tInt32" + Environment.NewLine +
                "\tInt32" + Environment.NewLine +
                Environment.NewLine +
                "\tP(TestPlace2|0)" + Environment.NewLine +
                Environment.NewLine +
                "Transitions (0)" + Environment.NewLine +
                "Arcs" + Environment.NewLine;
            Assert.AreEqual(expectedString, net.ToString());

            var transition = net.AddTransition("Transition");

            expectedString =
                "-----------------------------------------------" + Environment.NewLine +
                "Places (2)" + Environment.NewLine +
                "\tP(TestPlace|2)" + Environment.NewLine +
                "\tInt32" + Environment.NewLine +
                "\tInt32" + Environment.NewLine +
                Environment.NewLine +
                "\tP(TestPlace2|0)" + Environment.NewLine +
                Environment.NewLine +
                "Transitions (1)" + Environment.NewLine +
                "\tT(Transition)" + Environment.NewLine +
                Environment.NewLine +
                "Arcs" + Environment.NewLine;
            Assert.AreEqual(expectedString, net.ToString());

            net.AddArc(place1, transition);
            net.AddArc(transition, place2);
            expectedString =
                "-----------------------------------------------" + Environment.NewLine +
                "Places (2)" + Environment.NewLine +
                "\tP(TestPlace|2)" + Environment.NewLine +
                "\tInt32" + Environment.NewLine +
                "\tInt32" + Environment.NewLine +
                Environment.NewLine +
                "\tP(TestPlace2|0)" + Environment.NewLine +
                Environment.NewLine +
                "Transitions (1)" + Environment.NewLine +
                "\tT(Transition)" + Environment.NewLine +
                Environment.NewLine +
                "Arcs" + Environment.NewLine +
                "\tP(TestPlace|2) -> T(Transition)" + Environment.NewLine +
                "\tT(Transition) -> P(TestPlace2|0)" + Environment.NewLine;
            Assert.AreEqual(expectedString, net.ToString());
        }
Ejemplo n.º 4
0
        public void Simulate()
        {
            #region Petri Net setup

            var net = new PetriNet <Person>();

            // Places
            var customerAtEntrance = net.AddPlace("Customer in front of barber");
            var customerWaiting    = net.AddPlace("Customer waiting");
            var cutting            = net.AddPlace("Cutting");
            var idle              = net.AddPlace("Idle barber");
            var customerPaying    = net.AddPlace("Customer paying");
            var customerNotPaying = net.AddPlace("Customer not paying");
            var customerOut       = net.AddPlace("Customer out");

            // Transitions
            var enter = net.AddTransition("Enter");

            var startCutting = net.AddTransition("Start cutting");
            startCutting.Condition = new CustomerWaitingAndBarberReady();

            var finishCutting = net.AddTransition("Finish cutting");
            finishCutting.Condition = new CustomerHairCutAndBarberFinished();

            var exit = net.AddTransition("Exit");
            exit.Condition = new CustomerPaid();

            // Arcs
            net.AddArc(customerAtEntrance, enter);

            net.AddArc(enter, customerWaiting);

            var goToChair = net.AddArc(customerWaiting, startCutting);
            goToChair.Annotation = new GoToChair();

            net.AddArc(startCutting, cutting);
            net.AddArc(cutting, finishCutting);

            var goToIdle = net.AddArc(finishCutting, idle);
            goToIdle.Annotation = new GoToIdle();

            net.AddArc(idle, startCutting);

            var goToPay = net.AddArc(finishCutting, customerPaying);
            goToPay.Annotation = new GoToPay();

            var notPay = net.AddArc(finishCutting, customerNotPaying);
            notPay.Annotation = new NoRobber();

            net.AddArc(customerPaying, exit);
            net.AddArc(customerNotPaying, exit);
            net.AddArc(exit, customerOut);

            var jean   = new Customer("Jean");
            var daniel = new Customer("Daniel");
            customerAtEntrance.Marking.Add(jean);
            customerAtEntrance.Marking.Add(daniel);

            var joe = new Barber("Joe");
            idle.Marking.Add(joe);

            #endregion

            // Run simulation
            var simulator = new PetriNetSimulator <Person>(net);
            simulator.Initialize();
            CollectionAssert.AreEquivalent(new[] { jean, daniel }, customerAtEntrance.Marking);
            CollectionAssert.IsEmpty(customerWaiting.Marking);
            CollectionAssert.IsEmpty(cutting.Marking);
            CollectionAssert.AreEquivalent(new[] { joe }, idle.Marking);
            CollectionAssert.IsEmpty(customerPaying.Marking);
            CollectionAssert.IsEmpty(customerNotPaying.Marking);
            CollectionAssert.IsEmpty(customerOut.Marking);

            simulator.SimulateStep();
            CollectionAssert.IsEmpty(customerAtEntrance.Marking);
            CollectionAssert.AreEquivalent(new[] { jean, daniel }, customerWaiting.Marking);
            CollectionAssert.IsEmpty(cutting.Marking);
            CollectionAssert.AreEquivalent(new[] { joe }, idle.Marking);
            CollectionAssert.IsEmpty(customerPaying.Marking);
            CollectionAssert.IsEmpty(customerNotPaying.Marking);
            CollectionAssert.IsEmpty(customerOut.Marking);

            simulator.SimulateStep();
            CollectionAssert.IsEmpty(customerAtEntrance.Marking);
            CollectionAssert.AreEquivalent(new[] { daniel }, customerWaiting.Marking);
            CollectionAssert.AreEquivalent(new Person[] { joe, jean }, cutting.Marking);
            CollectionAssert.IsEmpty(idle.Marking);
            CollectionAssert.IsEmpty(customerPaying.Marking);
            CollectionAssert.IsEmpty(customerNotPaying.Marking);
            CollectionAssert.IsEmpty(customerOut.Marking);

            simulator.SimulateStep();
            CollectionAssert.IsEmpty(customerAtEntrance.Marking);
            CollectionAssert.AreEquivalent(new[] { daniel }, customerWaiting.Marking);
            CollectionAssert.IsEmpty(cutting.Marking);
            CollectionAssert.AreEquivalent(new[] { joe }, idle.Marking);
            CollectionAssert.AreEquivalent(new[] { jean }, customerPaying.Marking);
            CollectionAssert.IsEmpty(customerNotPaying.Marking);
            CollectionAssert.IsEmpty(customerOut.Marking);

            simulator.SimulateStep();
            CollectionAssert.IsEmpty(customerAtEntrance.Marking);
            CollectionAssert.IsEmpty(customerWaiting.Marking);
            CollectionAssert.AreEquivalent(new Person[] { joe, daniel }, cutting.Marking);
            CollectionAssert.IsEmpty(idle.Marking);
            CollectionAssert.IsEmpty(customerPaying.Marking);
            CollectionAssert.IsEmpty(customerNotPaying.Marking);
            CollectionAssert.AreEquivalent(new[] { jean }, customerOut.Marking);

            simulator.SimulateStep();
            CollectionAssert.IsEmpty(customerAtEntrance.Marking);
            CollectionAssert.IsEmpty(customerWaiting.Marking);
            CollectionAssert.IsEmpty(cutting.Marking);
            CollectionAssert.AreEquivalent(new Person[] { joe }, idle.Marking);
            CollectionAssert.AreEquivalent(new Person[] { daniel }, customerPaying.Marking);
            CollectionAssert.IsEmpty(customerNotPaying.Marking);
            CollectionAssert.AreEquivalent(new[] { jean }, customerOut.Marking);

            simulator.SimulateStep();
            CollectionAssert.IsEmpty(customerAtEntrance.Marking);
            CollectionAssert.IsEmpty(customerWaiting.Marking);
            CollectionAssert.IsEmpty(cutting.Marking);
            CollectionAssert.AreEquivalent(new Person[] { joe }, idle.Marking);
            CollectionAssert.IsEmpty(customerPaying.Marking);
            CollectionAssert.IsEmpty(customerNotPaying.Marking);
            CollectionAssert.AreEquivalent(new[] { jean, daniel }, customerOut.Marking);

            // No more move
            simulator.SimulateStep();
            CollectionAssert.IsEmpty(customerAtEntrance.Marking);
            CollectionAssert.IsEmpty(customerWaiting.Marking);
            CollectionAssert.IsEmpty(cutting.Marking);
            CollectionAssert.AreEquivalent(new Person[] { joe }, idle.Marking);
            CollectionAssert.IsEmpty(customerPaying.Marking);
            CollectionAssert.IsEmpty(customerNotPaying.Marking);
            CollectionAssert.AreEquivalent(new[] { jean, daniel }, customerOut.Marking);
        }