public void AssertMarkings(Marking m, Dictionary<int, int> markingsExpected) { foreach (var marking in markingsExpected) { Assert.AreEqual(marking.Value, m[marking.Key]); } }
/// <summary> /// invokes the first enabled transition in the petri net under the supplied <see cref="Marking"/>. /// </summary> /// <param name="m">The marking under which transition activation is calculated.</param> /// <returns>A new <see cref="Marking"/> containing the result of transition firing on the marking.</returns> /// <remarks> /// This method will not have any side effects on the <see cref="Marking"/> passed into the function or on the net itself. /// /// This method works by choosing the next transition to fire randomly. /// </remarks> public virtual Marking Fire(Marking m) { var result = new Marking(m); int?transitionId = GetNextTransitionToFire(m); if (!transitionId.HasValue) { return(result); } int tran = transitionId.Value; foreach (var place in GetInArcs(tran).Where(x => x.IsInhibitor == false)) { result[place.Source] = m[place.Source] - 1; } foreach (var arc in GetOutArcs(tran)) { result[arc.Target] = result[arc.Target] + arc.Weight; } if (TransitionFunctions.ContainsKey(tran)) { TransitionFunctions[tran].ForEach(a => a(tran)); } return(result); }
public void TestConflictDetection() { var m = new Marking(3, new Dictionary<int, int> { { 0, 1 } , { 1, 1 } , { 2, 1 } }); var p = new GraphPetriNet("p", new Dictionary<int, string> { {0, "p0"}, {1, "p1"}, {2, "p2"} }, new Dictionary<int, string> { { 0, "t1" }, { 1, "t2" } }, new Dictionary<int, List<InArc>>(){ {0, new List<InArc>(){new InArc(0),new InArc(1)}}, {1, new List<InArc>(){new InArc(1),new InArc(2)}} }, new Dictionary<int, List<OutArc>>() { }, new Dictionary<int, int>() { { 0, 1 }, { 1, 2 } }); var enabledTransitions = p.AllEnabledTransitions(m); Assert.AreEqual(2, enabledTransitions.Count()); Assert.IsTrue(enabledTransitions.Contains(0)); Assert.IsTrue(enabledTransitions.Contains(1)); Assert.IsTrue(p.IsConflicted(m)); }
public void Test2to1EnablementAndFiring() { var m = new Marking(3, new Dictionary<int, int> { { 0, 1 }, { 1, 0 }, { 2, 0 } }); var p = new MatrixPetriNet("p", new Dictionary<int, string> { {0, "p0"}, {1, "p1"}, {2, "p2"} }, new Dictionary<int, string> { { 0, "t0" } }, new Dictionary<int, List<InArc>>{ {0, new List<InArc>(){new InArc(0),new InArc(2)}} }, new Dictionary<int, List<OutArc>>{ {0, new List<OutArc>(){new OutArc(1)}} }); Assert.AreEqual(false, p.IsEnabled(0, m)); m[2] = 1; Assert.AreEqual(true, p.IsEnabled(0, m)); m = p.Fire(m); Assert.AreEqual(0, m[0]); Assert.AreEqual(1, m[1]); Assert.AreEqual(0, m[2]); }
bool AllInArcPlacesHaveMoreTokensThanTheArcWeight(int transitionId, Marking m) { var arcs = NonInhibitorsIntoTransition(transitionId); var result = arcs.All(placeid => m[placeid] >= GetWeight(placeid, transitionId)); return(result); }
public IEnumerable<int> GetEnabledTransitionsAdjacentToPlace(int placeId, Marking m) { var q = (from outArc in GetPlaceOutArcs(placeId) where IsEnabled(outArc, m) select outArc).ToArray(); return q; }
public IEnumerable <int> GetEnabledTransitionsAdjacentToPlace(int placeId, Marking m) { var q = (from outArc in GetPlaceOutArcs(placeId) where IsEnabled(outArc, m) select outArc).ToArray(); return(q); }
public int?GetNextTransitionToFire(Marking m) { var ets = GetEnabledTransitions(m); return((from t in ets orderby GetTransitionPriority(t) descending select t).FirstOrDefault()); }
public IEnumerable <int> GetConflictedPlaces(Marking m) { var q = from p in AllPlaces() where PlaceIsConflicted(p, m) select p; return(q); }
bool AllInArcPlacesHaveMoreTokensThanTheArcWeight(int transitionId, Marking m) { Contract.Requires(Transitions.ContainsKey(transitionId)); var arcs = NonInhibitorsIntoTransition(transitionId); var result = arcs.All(ia => m[ia] >= InMatrix[ia, transitionId]); return(result); }
public bool IsEnabled(int transitionId, Marking m) { var a = IsEmptyTransition(transitionId); var b = AllInhibitorsAreFromEmptyPlaces(transitionId, m); var c = AllInArcPlacesHaveMoreTokensThanTheArcWeight(transitionId, m); // if all inhibitors are empty and all non inhibitors have as many tokens in their origin place as their weight bool result = (a || (b && c)); return result; }
public bool IsEnabled(int transitionId, Marking m) { var a = IsEmptyTransition(transitionId); var b = AllInhibitorsAreFromEmptyPlaces(transitionId, m); var c = AllInArcPlacesHaveMoreTokensThanTheArcWeight(transitionId, m); // if all inhibitors are empty and all non inhibitors have as many tokens in their origin place as their weight bool result = (a || (b && c)); return(result); }
internal IEnumerable <int> AllMarkedPlaces(Marking m) { for (int i = 0; i < m.Count; i++) { if (m[i] > 0) { yield return(i); } } }
public IEnumerable <int> GetEnabledTransitions(Marking m) { // subtract the weights from the markings then sum each element in the resulting vectir // if the result is greater than or equal to zero then the transition is enabled for (int i = 0; i < OutMatrix.Columns; i++) { if (IsEnabled(i, m)) { yield return(i); } } }
public int?GetNextTransitionToFire(Marking m) { var ets = AllEnabledTransitions(m); if (ets.Count() < 1) { return(null); } return((from t in ets orderby GetTransitionPriority(t) descending select t).First()); }
public Marking CreateMarking() { var result = new Marking(Places.Count); foreach (var marking in PlaceMarkings) { if (Places.ContainsValue(marking.Key)) { result[PlaceIndex(marking.Key)] = marking.Value; } } return(result); }
public virtual Marking Fire(Marking m) { var firingTransitions = GetEnabledTransitions(m).ToList(); // no laziness here, since enabled trans will change after the flow equation has been evaluated var result = new Marking(m + (OutMatrix - InMatrix) * CreateFiringPlan(m)); foreach (var transId in firingTransitions) { if (TransitionFunctions.ContainsKey(transId)) { TransitionFunctions[transId].ForEach(a => a(transId)); } } return(result); }
public void Test1() { var m = new Marking(3, new Dictionary<int, int>{ { 0, 1 }, { 1, 1 }, { 2, 0 } }); var p = CreatePNTwoInOneOut(); AssertMarkings(m, new Dictionary<int, int>{ { 0, 1 }, { 1, 1 }, { 2, 0 } }); m = p.Fire(m); AssertMarkings(m, new Dictionary<int, int>{ { 0, 0 }, { 1, 0 }, { 2, 1 } }); }
public void Test1to1Enablement() { var m = new Marking(2, new Dictionary<int, int> { { 0, 0 }, { 1, 0 } }); var p = new MatrixPetriNet("p", new Dictionary<int, string> { {0, "p0"}, {1, "p1"} }, new Dictionary<int, string> { { 0, "t0" } }, new Dictionary<int, List<InArc>>(){ {0, new List<InArc>(){new InArc(0)}} }, new Dictionary<int, List<OutArc>>(){ {0, new List<OutArc>(){new OutArc(1)}} }); m[0] = 1; Assert.AreEqual(true, p.IsEnabled(0, m)); }
SparseVector CreateFiringPlan(Marking m) { SparseVector result = new SparseVector(Transitions.Count); if (IsConflicted(m)) { var next = GetNextTransitionToFire(m); if (next.HasValue) { result[next.Value] = 1.0; } } else { foreach (var transId in GetEnabledTransitions(m)) { result[transId] = 1; } } return(result); }
public void TestBifurcatingTransition() { var m = new Marking(3, new Dictionary<int, int> { { (int)Places.p1, 0 }, { (int)Places.p2, 0 }, { (int)Places.p3, 0 } }); var p = CreatePetriNet.Parse(StdPetriNets.Bifurcation).CreateNet<GraphPetriNet>(); m.Assert(p.Places, new Dictionary<string, int>{ { "p1", 0 }, { "p2", 0 }, { "p3", 0 } }); m.Set(p.Places, "p1", 1); m.Assert(p.Places, new Dictionary<string, int>{ { "p1", 1 }, { "p2", 0 }, { "p3", 0 } }); m = p.Fire(m); m.Assert(p.Places, new Dictionary<string, int>{ { "p1", 0 }, { "p2", 1 }, { "p3", 1 } }); }
public void TestCreateMarkingWithInvalidSize4() { var m = new Marking(int.MaxValue); }
public void TestCreateMarkingWithInvalidSize2() { var m = new Marking(-1); }
public void TestCreateMarking() { var m = new Marking(1); Assert.IsNotNull(m); Assert.AreEqual(0, m[0]); }
public void TestTransitionFunctionExecution() { var m = new Marking(2, new Dictionary<int, int> { { 0, 2 } , { 1, 0 } }); var p = new MatrixPetriNet("p", new Dictionary<int, string> { {0, "p0"}, {1, "p1"} }, new Dictionary<int, string> { { 0, "t0" } }, new Dictionary<int, List<InArc>>(){ {0, new List<InArc>(){new InArc(0)}} }, new Dictionary<int, List<OutArc>>(){ {0, new List<OutArc>(){new OutArc(1)}} }); Assert.AreEqual(2, m[0]); var someLocal = 0; m[0] = 1; p.RegisterFunction(0, (t) => someLocal += 1); m = p.Fire(m); Assert.AreEqual(1, someLocal); }
internal void AssertMarkings(Marking m, Dictionary<Places, double> markingsExpected) { foreach (var marking in markingsExpected) { Assert.AreEqual(marking.Value, m[Convert.ToInt32(marking.Key)]); } }
public bool PlaceIsConflicted(int placeId, Marking m) { return(GetEnabledTransitionsAdjacentToPlace(placeId, m).Count() > 1); }
public bool IsConflicted(Marking m) { return(AllPlaces().Any(pid => PlaceIsConflicted(pid, m))); }
public Marking(Marking m) : base(m) { }
public void TestCreateMarkingWithInvalidSize5() { var m = new Marking(GraphPetriNet.MaxSize + 1); }
/// <summary> /// invokes the first enabled transition in the petri net under the supplied <see cref="Marking"/>. /// </summary> /// <param name="m">The marking under which transition activation is calculated.</param> /// <returns>A new <see cref="Marking"/> containing the result of transition firing on the marking.</returns> /// <remarks> /// This method will not have any side effects on the <see cref="Marking"/> passed into the function or on the net itself. /// /// This method works by choosing the next transition to fire randomly. /// </remarks> public virtual Marking Fire(Marking m) { var result = new Marking(m); int? transitionId = GetNextTransitionToFire(m); if (!transitionId.HasValue) return result; int tran = transitionId.Value; foreach (var place in GetInArcs(tran).Where(x => x.IsInhibitor == false)) result[place.Source] = m[place.Source] - 1; foreach (var arc in GetOutArcs(tran)) result[arc.Target] = result[arc.Target] + arc.Weight; if (TransitionFunctions.ContainsKey(tran)) TransitionFunctions[tran].ForEach(a => a(tran)); return result; }
public void TestDrainTransition() { var m = new Marking(1, new Dictionary<int, int> { { 0, 5 } }); var p = new MatrixPetriNet("p", new Dictionary<int, string> { {0, "p0"} }, new Dictionary<int, string> { { 0, "Ti" } }, new Dictionary<int, List<InArc>>() { {0, new List<InArc>(){new InArc(0)}} }, new Dictionary<int, List<OutArc>>() { }); for (int i = 5; i >= 0; i--) { Assert.AreEqual(i, m[0]); Assert.AreEqual(i > 0, p.IsEnabled(0, m)); m = p.Fire(m); } }
public void TestFireConflictingTransitions() { var m = new Marking(3, new Dictionary<int, int> { { 0, 1 } , { 2, 0 } , { 1, 0 } }); var p = new MatrixPetriNet("p", new Dictionary<int, string> { {0, "p0"}, {1, "p1"}, {2, "p2"} }, new Dictionary<int, string> { { 0, "t1" } }, new Dictionary<int, List<InArc>>(){ {0, new List<InArc>(){new InArc(0),new InArc(2)}} }, new Dictionary<int, List<OutArc>>(){ {0, new List<OutArc>(){new OutArc(1)}} }); Assert.AreEqual(false, p.IsEnabled(0, m)); m[2] = 1; Assert.AreEqual(true, p.IsEnabled(0, m)); }
public void TestCreateMarkingWithMaxSize() { var m = new Marking(GraphPetriNet.MaxSize); }
public void TestSelfTransition() { var m = new Marking(1, new Dictionary<int, int> { { (int)Places.p1, 0 } }); var p = new MatrixPetriNet("p", new Dictionary<int, string> { {(int)Places.p1, "p1"} }, new Dictionary<int, string> { { (int)Transitions.t1, "t1" } }, new Dictionary<int, List<InArc>>(){ {(int)Transitions.t1, new List<InArc>(){new InArc((int)Places.p1)}} }, new Dictionary<int, List<OutArc>>(){ {(int)Transitions.t1, new List<OutArc>(){new OutArc((int)Places.p1)}} }); m[(int)Places.p1] = 1; Assert.AreEqual(1, m[(int)Places.p1]); m = p.Fire(m); Assert.AreEqual(1, m[(int)Places.p1]); }
bool AllInhibitorsAreFromEmptyPlaces(int transitionId, Marking m) { Contract.Requires(Transitions.ContainsKey(transitionId)); return(InhibitorsIntoTransition(transitionId).All(ia => m[ia] == 0)); }
public void TestSetMarkingAffectsEnablementszx() { var p = TestFixture2.CreatePNTwoInOneOut(); var m = new Marking(3, new Dictionary<int, int>(){ {0,1}, {1,1}, {2,0} }); Assert.AreEqual(true, p.IsEnabled(0, m)); m[0] = 0; Assert.AreEqual(false, p.IsEnabled(0, m)); }
public int? GetNextTransitionToFire(Marking m) { var ets = AllEnabledTransitions(m); if (ets.Count()< 1) { return null; } return (from t in ets orderby GetTransitionPriority(t) descending select t).First(); }
public IEnumerable <int> AllEnabledTransitions(Marking m) { return(from t in Transitions where IsEnabled(t.Key, m) select t.Key); }
public void TestInputTransition() { var m = new Marking(1, new Dictionary<int, int> { { 0, 0 } }); var p = new MatrixPetriNet("p", new Dictionary<int, string> { {0, "p0"} }, new Dictionary<int, string> { { 0, "Ti" } }, new Dictionary<int, List<InArc>>() { }, new Dictionary<int, List<OutArc>>(){ {0, new List<OutArc>(){new OutArc(0)}} }); Assert.AreEqual(0, m[0]); Assert.IsTrue(p.IsEnabled(0, m)); m = p.Fire(m); Assert.AreEqual(1, m[0]); Assert.IsTrue(p.IsEnabled(0, m)); m = p.Fire(m); Assert.AreEqual(2, m[0]); Assert.IsTrue(p.IsEnabled(0, m)); }
bool AllInhibitorsAreFromEmptyPlaces(int transitionId, Marking m) { var inhibitorsIntoTransition = InhibitorsIntoTransition(transitionId); return(inhibitorsIntoTransition.All(placeid => m[placeid] == 0)); }
public void TestMarkingFlowInBifurcatedTransition() { var m = new Marking(3); var p = new MatrixPetriNet("p", new Dictionary<int, string> { {(int)Places.p1, "p1"}, {(int)Places.p2, "p2"}, {(int)Places.p3, "p3"} }, new Dictionary<int, string> { { (int)Transitions.t1, "t1" } }, new Dictionary<int, List<InArc>>(){ {(int)Transitions.t1, new List<InArc>(){new InArc((int)Places.p1)}} }, new Dictionary<int, List<OutArc>>(){ {(int)Transitions.t1, new List<OutArc>(){new OutArc((int)Places.p2), new OutArc((int)Places.p3)}} }); AssertMarkings(m, new Dictionary<Places, double>{ { Places.p1, 0 }, { Places.p2, 0 }, { Places.p3, 0 } }); m[(int)Places.p1] = 1; AssertMarkings(m, new Dictionary<Places, double>{ { Places.p1, 1 }, { Places.p2, 0 }, { Places.p3, 0 } }); m = p.Fire(m); AssertMarkings(m, new Dictionary<Places, double>{ { Places.p1, 0 }, { Places.p2, 1 }, { Places.p3, 1 } }); }
public IEnumerable<int> AllEnabledTransitions(Marking m) { return (from t in Transitions where IsEnabled(t.Key, m) select t.Key); }
public void TestMarkingFlowInComplexNet() { var m = new Marking(4, new Dictionary<int, int> { { (int)Places.p1, 0 }, { (int)Places.p2, 0 }, { (int)Places.p3, 0 }, { (int)Places.p4, 0 } }); var p = new MatrixPetriNet("p", new Dictionary<int, string> { {(int)Places.p1, "p1"}, {(int)Places.p2, "p2"}, {(int)Places.p3, "p3"}, {(int)Places.p4, "p4"} }, new Dictionary<int, string> { { (int)Transitions.t1, "t1" }, { (int)Transitions.t2, "t2" }, { (int)Transitions.t3, "t3" } }, new Dictionary<int, List<InArc>>(){ {(int)Transitions.t1, new List<InArc>(){new InArc((int)Places.p1)}}, {(int)Transitions.t2, new List<InArc>(){new InArc((int)Places.p2)}}, {(int)Transitions.t3, new List<InArc>(){new InArc((int)Places.p4)}} }, new Dictionary<int, List<OutArc>>(){ {(int)Transitions.t1, new List<OutArc>(){new OutArc((int)Places.p2)}}, {(int)Transitions.t2, new List<OutArc>(){new OutArc((int)Places.p3)}}, {(int)Transitions.t3, new List<OutArc>(){new OutArc((int)Places.p2)}} }); /* * This model is a petri net in this shape * * P1 --> T1 --> P2 --> T2 --> P3 * ^ * | * T3 * ^ * | * P4 * */ AssertMarkings(m, new Dictionary<Places, double>{ { Places.p1, 0 }, { Places.p2, 0 }, { Places.p3, 0 }, { Places.p4, 0 } }); m[(int)Places.p1] = 1; AssertMarkings(m, new Dictionary<Places, double>{ { Places.p1, 1 }, { Places.p2, 0 }, { Places.p3, 0 }, { Places.p4, 0 } }); m = p.Fire(m); AssertMarkings(m, new Dictionary<Places, double>{ { Places.p1, 0 }, { Places.p2, 1 }, { Places.p3, 0 }, { Places.p4, 0 } }); m = p.Fire(m); AssertMarkings(m, new Dictionary<Places, double>{ { Places.p1, 0 }, { Places.p2, 0 }, { Places.p3, 1 }, { Places.p4, 0 } }); m[(int)Places.p4] = 1; AssertMarkings(m, new Dictionary<Places, double>{ { Places.p1, 0 }, { Places.p2, 0 }, { Places.p3, 1 }, { Places.p4, 1 } }); m = p.Fire(m); AssertMarkings(m, new Dictionary<Places, double>{ { Places.p1, 0 }, { Places.p2, 1 }, { Places.p3, 1 }, { Places.p4, 0 } }); m = p.Fire(m); AssertMarkings(m, new Dictionary<Places, double>{ { Places.p1, 0 }, { Places.p2, 0 }, { Places.p3, 2 }, { Places.p4, 0 } }); }
public IEnumerable<int> GetConflictedPlaces(Marking m) { var q = from p in AllPlaces() where PlaceIsConflicted(p, m) select p; return q; }
public void TestPrioritySelection2() { var m = new Marking(4, new Dictionary<int, int> { { 0, 1 } , { 1, 1 } , { 2, 1 } , { 3, 1 } }); var p = new MatrixPetriNet("p", new Dictionary<int, string> { {0, "p0"}, {1, "p1"}, {2, "p2"}, {3, "p3"} }, new Dictionary<int, string> { { 0, "t1" }, { 1, "t2" }, { 2, "t3" } }, new Dictionary<int, List<InArc>>(){ {0, new List<InArc>(){new InArc(0),new InArc(1)}}, {1, new List<InArc>(){new InArc(1),new InArc(2)}}, {2, new List<InArc>(){new InArc(1),new InArc(2),new InArc(3)}} }, new Dictionary<int, List<OutArc>>() { }, new Dictionary<int, int>() { { 0, 1 } , { 1, 1 } , { 2, 4 } }); // t0 will be baseline at 0 and t1 will be 1, therefore next enabled transition should always be 1 int? transId = p.GetNextTransitionToFire(m); Assert.IsTrue(transId.HasValue); Assert.AreEqual(2, transId.Value); }
internal IEnumerable<int> AllMarkedPlaces(Marking m) { for (int i = 0; i < m.Count; i++) { if (m[i] > 0) { yield return i; } } }
public void TestPrioritySetup() { var m = new Marking(3, new Dictionary<int, int> { { 0, 1 } , { 1, 1 } , { 2, 1 } }); var p = new MatrixPetriNet("p", new Dictionary<int, string> { {0, "p0"}, {1, "p1"}, {2, "p2"} }, new Dictionary<int, string> { { 0, "t1" }, { 1, "t2" } }, new Dictionary<int, List<InArc>>(){ {0, new List<InArc>(){new InArc(0),new InArc(1)}}, {1, new List<InArc>(){new InArc(1),new InArc(2)}} }, new Dictionary<int, List<OutArc>>() { }, new Dictionary<int, int>() { { 0, 1 }, { 1, 2 } }); Assert.AreEqual(1, p.GetTransitionPriority(0)); Assert.AreEqual(2, p.GetTransitionPriority(1)); }