Exemplo n.º 1
0
        public static void TestPetriNetTest1()
        {
            PetriNet p = new PetriNet();

            p.AddPlace(1);
            p.AddPlace(2);
            p.AddPlace(3);
            p.AddPlace(4);
            p.AddTransition(-1, "A");
            p.AddTransition(-2, "B");
            p.AddTransition(-3, "C");
            p.AddTransition(-4, "D");

            p.AddEdge(1, -1);
            p.AddEdge(-1, 2);
            p.AddEdge(2, -2);
            p.AddEdge(-2, 3);
            p.AddEdge(2, -3);
            p.AddEdge(-3, 3);
            p.AddEdge(3, -4);
            p.AddEdge(-4, 4);

            Console.WriteLine(p.IsEnabled(-1).ToString() + " " + p.IsEnabled(-2).ToString() + " " + p.IsEnabled(-3).ToString() + " " + p.IsEnabled(-4).ToString());

            p.AddMarking(1);  // add one token to place id 1
            Console.WriteLine(p.IsEnabled(-1).ToString() + " " + p.IsEnabled(-2).ToString() + " " + p.IsEnabled(-3).ToString() + " " + p.IsEnabled(-4).ToString());

            p.FireTransition(-1);  // fire transition A
            Console.WriteLine(p.IsEnabled(-1).ToString() + " " + p.IsEnabled(-2).ToString() + " " + p.IsEnabled(-3).ToString() + " " + p.IsEnabled(-4).ToString());

            p.FireTransition(-3);  // fire transition C
            Console.WriteLine(p.IsEnabled(-1).ToString() + " " + p.IsEnabled(-2).ToString() + " " + p.IsEnabled(-3).ToString() + " " + p.IsEnabled(-4).ToString());

            p.FireTransition(-4);  // fire transition D
            Console.WriteLine(p.IsEnabled(-1).ToString() + " " + p.IsEnabled(-2).ToString() + " " + p.IsEnabled(-3).ToString() + " " + p.IsEnabled(-4).ToString());

            p.AddMarking(2);  // add one token to place id 2
            Console.WriteLine(p.IsEnabled(-1).ToString() + " " + p.IsEnabled(-2).ToString() + " " + p.IsEnabled(-3).ToString() + " " + p.IsEnabled(-4).ToString());

            p.FireTransition(-2);  // fire transition B
            Console.WriteLine(p.IsEnabled(-1).ToString() + " " + p.IsEnabled(-2).ToString() + " " + p.IsEnabled(-3).ToString() + " " + p.IsEnabled(-4).ToString());

            p.FireTransition(-4);  // fire transition D
            Console.WriteLine(p.IsEnabled(-1).ToString() + " " + p.IsEnabled(-2).ToString() + " " + p.IsEnabled(-3).ToString() + " " + p.IsEnabled(-4).ToString());

            Console.WriteLine(p.GetTokens(4));
        }
Exemplo n.º 2
0
        static void TestShortestPathWithVeryComplexModel()
        {
            PetriNet p = new PetriNet();

            p.AddPlace(1);
            p.AddPlace(2);
            p.AddPlace(3);
            p.AddPlace(4);
            p.AddPlace(5);
            p.AddPlace(6);
            p.AddPlace(7);
            p.AddPlace(8);
            p.AddPlace(9);
            p.AddPlace(10);
            p.AddPlace(11);
            p.AddPlace(12);
            p.AddPlace(13);
            p.AddPlace(14);
            p.AddPlace(15);

            p.AddTransition(-1, "A");
            p.AddTransition(-2, "B");
            p.AddTransition(-3, "C");
            p.AddTransition(-4, "D");
            p.AddTransition(-5, "E");
            p.AddTransition(-6, "F");
            p.AddTransition(-7, "G");
            p.AddTransition(-8, "H");
            p.AddTransition(-9, "I");
            p.AddTransition(-10, "J");
            p.AddTransition(-11, "K");
            p.AddTransition(-12, "L");
            p.AddTransition(-13, "M");

            p.AddEdge(1, -1);

            p.AddEdge(-1, 2);
            p.AddEdge(-1, 3);
            p.AddEdge(-1, 4);

            p.AddEdge(2, -2);
            p.AddEdge(2, -3);
            p.AddEdge(3, -4);
            p.AddEdge(4, -5);

            p.AddEdge(-2, 5);
            p.AddEdge(-3, 6);
            p.AddEdge(-3, 7);
            p.AddEdge(-4, 8);
            p.AddEdge(-5, 9);

            p.AddEdge(5, -6);
            p.AddEdge(6, -7);
            p.AddEdge(7, -7);
            p.AddEdge(8, -9);
            p.AddEdge(9, -12);

            p.AddEdge(-9, 13);
            p.AddEdge(-12, 14);

            p.AddEdge(13, -8);
            p.AddEdge(14, -8);

            p.AddEdge(-6, 10);
            p.AddEdge(-7, 10);
            p.AddEdge(-8, 15);

            p.AddEdge(15, -13);

            p.AddEdge(-13, 11);

            p.AddEdge(10, -10);
            p.AddEdge(11, -10);

            p.AddEdge(-10, 12);


            p.AddMarking(2);
            p.AddMarking(3);
            p.AddMarking(4);

            var cost = p.GetValidShortestPath(-2, 12);

            for (int i = 0; i < cost.Count; i++)
            {
                Console.WriteLine(p.TransitionIdToName(cost[i]));
            }
        }
Exemplo n.º 3
0
        static void TestShortestPath()
        {
            PetriNet p = new PetriNet();

            p.AddPlace(1);
            p.AddPlace(2);
            p.AddPlace(3);
            p.AddPlace(4);
            p.AddPlace(5);
            p.AddPlace(6);
            p.AddPlace(7);

            p.AddTransition(-1, "A");
            p.AddTransition(-2, "B");
            p.AddTransition(-3, "C");
            p.AddTransition(-4, "D");
            p.AddTransition(-5, "E");
            p.AddTransition(-6, "G");
            p.AddTransition(-7, "H");

            p.AddEdge(1, -1);
            p.AddEdge(-1, 2);
            p.AddEdge(-1, 3);

            p.AddEdge(2, -2);
            p.AddEdge(2, -3);

            p.AddEdge(3, -4);

            p.AddEdge(-2, 4);
            p.AddEdge(-3, 4);

            p.AddEdge(-4, 5);

            p.AddEdge(4, -5);
            p.AddEdge(5, -5);

            p.AddEdge(-5, 6);

            p.AddEdge(6, -6);
            p.AddEdge(6, -7);

            p.AddEdge(-6, 7);
            p.AddEdge(-7, 7);

            //Simulate marking
            p.AddMarking(2);
            p.AddMarking(3);


            var cost = p.GetValidShortestPath(-2, 7);
        }
Exemplo n.º 4
0
        public static PetriNet mine(Dictionary <List <string>, int> trace_frequencies)
        {
            var sets           = getSets(trace_frequencies);
            var startSet       = sets[0];
            var transitionsSet = sets[1];
            var endSet         = sets[2];

            var directSuccessions = GetDirectSuccessions(trace_frequencies);
            var causalities       = GetCausalities(trace_frequencies, directSuccessions);
            // Check not_connected should return 48
            var notConnected = GetNotConnected(transitionsSet, directSuccessions);

            var xl = MakeXLSet(transitionsSet, directSuccessions, causalities, notConnected);
            var yl = MakeYLSet(xl);

            // Fill the petri net
            var p = new PetriNet();

            int placeId      = 1;
            int transitionId = -1;
            Dictionary <Tuple <List <string>, List <string> >, int> placeDictionary = new Dictionary <Tuple <List <string>, List <string> >, int>();

            p.AddPlace(placeId);
            placeId++;

            // adding all transitions
            foreach (var transition in transitionsSet)
            {
                p.AddTransition(transitionId, transition);
                transitionId--;
            }

            // adding all middle transitions
            foreach (var place in yl)
            {
                p.AddPlace(placeId);
                placeDictionary.Add(place, placeId);
                placeId++;
            }

            // adding the "out" place
            p.AddPlace(placeId);

            // Adding the connection between in place to all start transitions
            foreach (var start in startSet)
            {
                p.AddEdge(1, p.TransitionNameToId(start));
            }

            // Adding the connection between all ending transitions to out place
            foreach (var end in endSet)
            {
                p.AddEdge(p.TransitionNameToId(end), placeId);
            }

            foreach (var place in yl)
            {
                placeId = placeDictionary[place];
                foreach (var transition in place.Item1)
                {
                    p.AddEdge(p.TransitionNameToId(transition), placeId);
                }

                foreach (var transition in place.Item2)
                {
                    p.AddEdge(placeId, p.TransitionNameToId(transition));
                }
            }

            // should I add the marking
            p.AddMarking(1);

            return(p);
        }