static void DumpElementContents(IPfcElement element, StringBuilder sb, int indent) { sb.Append("\r\n"); for (int i = 0; i < indent; i++) { sb.Append("\t"); } if (element == null) { sb.Append("<null>"); } else { sb.Append(element.Name); if (element is IPfcStepNode) { IPfcStepNode node = (IPfcStepNode)element; sb.Append(" [ " + node.Predecessors.Count + ", " + node.Successors.Count + " ]"); foreach (IProcedureFunctionChart childPfc in node.Actions.Values) { foreach (IPfcElement child in childPfc.Elements) { DumpElementContents(child, sb, indent + 1); } } } } }
public void Test_InsertStepAndTransition() { Model model = new Model("SFC Test 1"); ProcedureFunctionChart pfc = new ProcedureFunctionChart(model, "SFC 1", "", Guid.NewGuid()); IPfcStepNode t0 = pfc.CreateStep("START", "", Guid.Empty); IPfcStepNode t1 = pfc.CreateStep("FINISH", "", Guid.Empty); pfc.Bind(t0, t1); string structureString = PfcDiagnostics.GetStructure(pfc); Console.WriteLine("Structure is \r\n" + structureString); // Get reference to old successor IPfcNode pfcNode = pfc.Nodes["T_000"]; IPfcNode oldSuccessorNode = pfcNode.SuccessorNodes[0]; // Add the step IPfcStepNode newStep = pfc.CreateStep("STEP_1", "", Guid.Empty); IPfcTransitionNode newTrans = pfc.CreateTransition(); // We are adding a step following a transition - binding is from selectedTrans-newStep-newTrans-oldSuccessorStep pfc.Bind(pfcNode, newStep); pfc.Bind(newStep, newTrans); pfc.Bind(newTrans, oldSuccessorNode); // Disconnect old successor pfc.Unbind(pfcNode, oldSuccessorNode); structureString = PfcDiagnostics.GetStructure(pfc); Console.WriteLine("Structure is \r\n" + structureString); Assert.IsTrue(structureString.Equals("{START-->[L_000(SFC 1.Root)]-->T_000}\r\n{T_000-->[L_002(SFC 1.Root)]-->STEP_1}\r\n{STEP_1-->[L_003(SFC 1.Root)]-->T_001}\r\n{T_001-->[L_004(SFC 1.Root)]-->FINISH}\r\n")); }
public PfcExecutionContext(IProcedureFunctionChart pfc, string name, string description, Guid guid, PfcExecutionContext parent) : base(pfc.Model, name, description, guid, parent) { if (s_diagnostics) { string parentName = (parent == null ? "<null>" : parent.Name); Console.WriteLine("Creating PFCEC \"" + name + "\" under PFCEC \"" + parentName + "\" For parent " + pfc.Name + " and numbered " + guid); } m_pfc = pfc; m_step = null; m_timePeriod = new TimePeriodEnvelope(name, GuidOps.XOR(guid, s_time_Period_Mask)); m_timePeriod.Subject = this; if (parent != null) { ((TimePeriodEnvelope)parent.TimePeriod).AddTimePeriod(m_timePeriod); } m_timePeriod.ChangeEvent += new ObservableChangeHandler(m_timePeriod_ChangeEvent); }
public void Test_SynchronizerConstruct_Steps() { Model model = new Model("SFC Test 1"); ProcedureFunctionChart pfc = new ProcedureFunctionChart(model, "SFC 1", "", Guid.NewGuid()); IPfcStepNode t0 = pfc.CreateStep("S_Alice", "", Guid.Empty); IPfcStepNode t1 = pfc.CreateStep("S_Bob", "", Guid.Empty); IPfcStepNode t2 = pfc.CreateStep("S_Charley", "", Guid.Empty); IPfcStepNode t3 = pfc.CreateStep("S_David", "", Guid.Empty); IPfcStepNode t4 = pfc.CreateStep("S_Edna", "", Guid.Empty); IPfcStepNode t5 = pfc.CreateStep("S_Frank", "", Guid.Empty); IPfcStepNode t6 = pfc.CreateStep("S_Gary", "", Guid.Empty); IPfcStepNode t7 = pfc.CreateStep("S_Hailey", "", Guid.Empty); pfc.Synchronize(new IPfcStepNode[] { t0, t1, t2, t3 }, new IPfcStepNode[] { t4, t5, t6, t7 }); string structureString = PfcDiagnostics.GetStructure(pfc); structureString = structureString.Replace("SFC 1.Root", "SFC 1.Root"); string shouldBe = "{S_Alice-->[L_000(SFC 1.Root)]-->T_000}\r\n{S_Bob-->[L_001(SFC 1.Root)]-->T_000}\r\n{S_Charley-->[L_002(SFC 1.Root)]-->T_000}\r\n{S_David-->[L_003(SFC 1.Root)]-->T_000}\r\n{T_000-->[L_004(SFC 1.Root)]-->S_Edna}\r\n{T_000-->[L_005(SFC 1.Root)]-->S_Frank}\r\n{T_000-->[L_006(SFC 1.Root)]-->S_Gary}\r\n{T_000-->[L_007(SFC 1.Root)]-->S_Hailey}\r\n"; Console.WriteLine("After a synchronization of steps, structure is \r\n" + structureString); Assert.AreEqual(structureString, shouldBe, "Structure should have been\r\n" + shouldBe + "\r\nbut it was\r\n" + structureString + "\r\ninstead."); if (m_runSFCs) { TestEvaluator testEvaluator = new TestEvaluator(new IPfcStepNode[] { t0, t1, t2, t3, t4, t5, t6, t7 }); pfc.Synchronize(new IPfcStepNode[] { t0, t1, t2, t3 }, new IPfcStepNode[] { t4, t5, t6, t7 }); testEvaluator.NextExpectedActivations = new IPfcStepNode[] { t0, t1, t2, t3, t4, t5, t6, t7 }; foreach (IPfcNode ilinkable in new IPfcStepNode[] { t0, t1, t2, t3 }) { Console.WriteLine("Incrementing " + ilinkable.Name + "."); //ilinkable.Increment(); throw new ApplicationException("PFCs are not currently executable."); } testEvaluator.NextExpectedActivations = new IPfcTransitionNode[] { }; // Ensure it's empty and all have fired. } }
public static ProcedureFunctionChart CreateOffsetParallelPFC() { ProcedureFunctionChart pfc = new ProcedureFunctionChart(null, "OffsetParallelPfc"); IPfcStepNode start = pfc.CreateStep("Start", "", Guid.Empty); IPfcStepNode finish = pfc.CreateStep("Finish", "", Guid.Empty); char name = 'A'; A = pfc.CreateStep("Step_" + (name++), "", NextGuid()); B = pfc.CreateStep("Step_" + (name++), "", NextGuid()); C = pfc.CreateStep("Step_" + (name++), "", NextGuid()); D = pfc.CreateStep("Step_" + (name++), "", NextGuid()); E = pfc.CreateStep("Step_" + (name++), "", NextGuid()); F = pfc.CreateStep("Step_" + (name++), "", NextGuid()); G = pfc.CreateStep("Step_" + (name++), "", NextGuid()); nA = (IPfcNode)A; nB = (IPfcNode)B; nC = (IPfcNode)C; nD = (IPfcNode)D; nE = (IPfcNode)E; nF = (IPfcNode)F; nG = (IPfcNode)G; pfc.Bind(start, nA); pfc.Bind(nA, nB); pfc.Bind(nB, nE); pfc.Bind(nE, nF); pfc.Bind(nF, nG); pfc.Bind(nG, finish); pfc.Bind(((PfcTransition)((PfcStep)nA).SuccessorNodes[0]), nC); pfc.Bind(nC, ((PfcTransition)((PfcStep)nE).SuccessorNodes[0])); pfc.Bind(((PfcTransition)((PfcStep)nB).SuccessorNodes[0]), nD); pfc.Bind(nD, ((PfcTransition)((PfcStep)nF).SuccessorNodes[0])); pfc.UpdateStructure(); return(pfc); }
public PfcExecutionContext(IPfcStepNode stepNode, string name, string description, Guid guid, PfcExecutionContext parent) : base(stepNode.Parent.Model, name, description, guid, parent) { if (s_diagnostics) { Console.WriteLine("Creating PfcEC \"" + name + "\" under PfcEC \"" + parent.Name + "\" For parent " + stepNode.Name + " and numbered " + guid); } m_pfc = stepNode.Parent; m_step = stepNode; if (stepNode.Actions.Count == 0) { m_timePeriod = new TimePeriod(name, GuidOps.XOR(guid, s_time_Period_Mask), TimeAdjustmentMode.InferDuration); m_timePeriod.Subject = this; ((TimePeriodEnvelope)parent.TimePeriod).AddTimePeriod(m_timePeriod); } else { m_timePeriod = new TimePeriodEnvelope(name, GuidOps.XOR(guid, s_time_Period_Mask)); m_timePeriod.Subject = this; ((TimePeriodEnvelope)parent.TimePeriod).AddTimePeriod(m_timePeriod); } m_timePeriod.ChangeEvent += new ObservableChangeHandler(m_timePeriod_ChangeEvent); }
public void Test_RemoveStep() { string testName = "PFC With Simultaneous Branch"; IModel model = new Model(testName); /* * START * + {T1} * STEP1 * + (T2) * STEP2 STEP3 * | + (T3) * | STEP4 * + (T4) * STEP5 * + (T5) * FINISH */ ProcedureFunctionChart pfc = new ProcedureFunctionChart(model, testName); IPfcNode startStep = pfc.CreateStep("START", string.Empty, Guid.NewGuid()); IPfcNode step1 = pfc.CreateStep("STEP1", string.Empty, Guid.NewGuid()); IPfcNode step2 = pfc.CreateStep("STEP2", string.Empty, Guid.NewGuid()); IPfcNode step3 = pfc.CreateStep("STEP3", string.Empty, Guid.NewGuid()); IPfcNode step4 = pfc.CreateStep("STEP4", string.Empty, Guid.NewGuid()); IPfcNode step5 = pfc.CreateStep("STEP5", string.Empty, Guid.NewGuid()); IPfcNode finishStep = pfc.CreateStep("FINISH", string.Empty, Guid.NewGuid()); IPfcNode t1 = pfc.CreateTransition("T1", string.Empty, Guid.NewGuid()); IPfcNode t2 = pfc.CreateTransition("T2", string.Empty, Guid.NewGuid()); IPfcNode t3 = pfc.CreateTransition("T3", string.Empty, Guid.NewGuid()); IPfcNode t4 = pfc.CreateTransition("T4", string.Empty, Guid.NewGuid()); IPfcNode t5 = pfc.CreateTransition("T4", string.Empty, Guid.NewGuid()); pfc.Bind(startStep, t1); pfc.Bind(t1, step1); pfc.Bind(step1, t2); pfc.Bind(t2, step2); pfc.Bind(t2, step3); pfc.Bind(step2, t4); pfc.Bind(step3, t3); pfc.Bind(t3, step4); pfc.Bind(step4, t4); pfc.Bind(t4, step5); pfc.Bind(step5, t5); pfc.Bind(t5, finishStep); /* Delete Step4 and T3*/ // Need to delete the predecessor transition IPfcTransitionNode predecessorTrans = (IPfcTransitionNode)step4.PredecessorNodes[0]; Assert.IsTrue(predecessorTrans.Name == t3.Name, "The predecessor trans should be T3"); IPfcStepNode predecessorStep = (IPfcStepNode)predecessorTrans.PredecessorNodes[0]; Assert.IsTrue(predecessorStep.Name == step3.Name, "The predecessor step should be STEP3"); IPfcTransitionNode successorTrans = (IPfcTransitionNode)step4.SuccessorNodes[0]; Assert.IsTrue(successorTrans.Name == t4.Name, "The successor trans should be T4"); // Connect the predecessor step to the successor transition pfc.Bind(predecessorStep, successorTrans); // Unbind the existing path from the predecessor step to the successor transition pfc.Unbind(predecessorStep, predecessorTrans); pfc.Unbind(predecessorTrans, step4); pfc.Unbind(step4, successorTrans); // Delete the predecessor transition pfc.Delete(predecessorTrans); // Delete step pfc.Delete(step4); Assert.IsTrue(pfc.Transitions[t3.Name] == null, "T3 Should be Deleted"); Assert.IsTrue(pfc.Steps[step4.Name] == null, "Step4 Should be Deleted"); }
public StepStateMachine StateMachineForStep(IPfcStepNode step) { return(m_stepStateMachines[step]); }
public static IPfcTransitionNode Between(IPfcStepNode before, IPfcStepNode after) { return((IPfcTransitionNode)before.SuccessorNodes.Find(delegate(IPfcNode trans) { return trans.SuccessorNodes.Contains(after); })); }
public void TestSequencers() { Model model = new Model("MyTestModel"); ProcedureFunctionChart pfc = new ProcedureFunctionChart(model, "RootPfc"); pfc.ExecutionEngineConfiguration = new ExecutionEngineConfiguration(); // Start // | // + T_Start // | // ===================================== // | | | | | // Step0 Step1 Step2 Step3 Step4 // | | | | | // ===================================== // | // + Finis // // (We want to run these in order "Step3", "Step1", "Step4", "Step2", "Step0") #region Create PFC IPfcStepNode start = pfc.CreateStep("Start", null, Guid.NewGuid()); IPfcTransitionNode startTrans = pfc.CreateTransition("T_Start", null, Guid.NewGuid()); pfc.Bind(start, startTrans); IPfcTransitionNode finis = pfc.CreateTransition("Finish", null, Guid.NewGuid()); for (int i = 0; i < 5; i++) { IPfcStepNode step = pfc.CreateStep("Step" + i, null, Guid.NewGuid()); pfc.Bind(startTrans, step); pfc.Bind(step, finis); } #endregion Create PFC Guid sequencerKey = Guid.NewGuid(); string[] stepSeq = new string[] { "Step4", "Step3", "Step2", "Step1", "Step0" }; for (int n = 0; n < 10; n++) { Console.WriteLine("\r\n\r\n========================================================\r\nStarting test iteration # " + n + ":\r\n"); stepSeq = Shuffle(stepSeq); Console.WriteLine("Expecting sequence " + StringOperations.ToCommasAndAndedList(new List <string>(stepSeq))); int j = 0; StringBuilder sb = new StringBuilder(); foreach (string stepNodeName in stepSeq) { IPfcStepNode step = pfc.Steps[stepNodeName]; step.Precondition = new Sequencer(sequencerKey, j++).Precondition; step.LeafLevelAction = new PfcAction(delegate(PfcExecutionContext pfcec, StepStateMachine ssm) { sb.Append("Running " + ssm.MyStep.Name + " "); }); } model.Executive.RequestEvent( new ExecEventReceiver(pfc.Run), DateTime.MinValue, 0.0, new PfcExecutionContext(pfc, "PFCEC", null, Guid.NewGuid(), null), ExecEventType.Detachable); pfc.Model.Start(); Console.Out.Flush(); string tgtString = string.Format(@"Running {0} Running {1} Running {2} Running {3} Running {4} ", stepSeq[0], stepSeq[1], stepSeq[2], stepSeq[3], stepSeq[4]); System.Diagnostics.Debug.Assert(sb.ToString().Equals(tgtString)); pfc.Model.Executive.Reset(); Console.WriteLine("========================================================"); } }
public SimpleDelay(IModel model, string name, Guid guid, IPfcStepNode myStepNode, ITimeSpanDistribution tsd) : base(model, name, guid, myStepNode) { m_tsd = tsd; }
public static ProcedureFunctionChart CreateRandomPFC(int nSteps, int seed) { Guid mask = GuidOps.FromString(string.Format("{0}, {1}", nSteps, seed)); Guid seedGuid = GuidOps.FromString(string.Format("{0}, {1}", seed, nSteps)); int rotate = 3; GuidGenerator guidGen = new GuidGenerator(seedGuid, mask, rotate); PfcElementFactory pfcef = new PfcElementFactory(guidGen); ProcedureFunctionChart pfc = new ProcedureFunctionChart(new Highpoint.Sage.SimCore.Model("Test model", Guid.NewGuid()), "Name", "", guidGen.Next(), pfcef); IPfcStepNode start = pfc.CreateStep("Start", "", Guid.Empty); IPfcStepNode step1 = pfc.CreateStep("Step1", "", Guid.Empty); IPfcStepNode finish = pfc.CreateStep("Finish", "", Guid.Empty); pfc.Bind(start, step1); pfc.Bind(step1, finish); Console.WriteLine("Seed = {0}.", seed); Random r = new Random(seed); while (pfc.Steps.Count < nSteps) { double steeringValue = r.NextDouble(); if (steeringValue < .5) { // Insert a step in series. IPfcLinkElement link = pfc.Links[r.Next(0, pfc.Links.Count - 1)]; IPfcStepNode stepNode = pfc.CreateStep(); pfc.Bind(link.Predecessor, stepNode); pfc.Bind(stepNode, link.Successor); //Console.WriteLine("Inserted {0} between {1} and {2}.", stepNode.Name, link.Predecessor.Name, link.Successor.Name); link.Detach(); } else if (steeringValue < .666) { // Insert a step in parallel. for (int i = 0; i < 50; i++) // Try, but give up if don't find suitable step. { IPfcStepNode target = pfc.Steps[r.Next(0, pfc.Steps.Count - 1)]; if (target.PredecessorNodes.Count == 1 && target.SuccessorNodes.Count == 1) { IPfcStepNode stepNode = pfc.CreateStep(); pfc.Bind(target.PredecessorNodes[0], stepNode); pfc.Bind(stepNode, target.SuccessorNodes[0]); //Console.WriteLine("Inserted {0} parallel to {1}.", stepNode.Name, target.Name); break; } } } else if (steeringValue < .833) { // Insert a branch for (int i = 0; i < 50; i++) // Try, but give up if don't find suitable step. { IPfcStepNode step = pfc.Steps[r.Next(0, pfc.Steps.Count - 1)]; if (step.PredecessorNodes.Count == 1 && step.SuccessorNodes.Count == 1) { IPfcStepNode entryStep = pfc.CreateStep(step.Name + "_IN", null, Guid.Empty); IPfcStepNode exitStep = pfc.CreateStep(step.Name + "_OUT", null, Guid.Empty); IPfcStepNode leftStep = pfc.CreateStep(step.Name + "_LFT", null, Guid.Empty); IPfcStepNode rightStep = pfc.CreateStep(step.Name + "_RGT", null, Guid.Empty); pfc.Bind(step.PredecessorNodes[0], entryStep); pfc.Bind(entryStep, leftStep); pfc.Bind(entryStep, rightStep); pfc.Bind(leftStep, exitStep); pfc.Bind(rightStep, exitStep); pfc.Bind(exitStep, step.SuccessorNodes[0]); pfc.Unbind(step.PredecessorNodes[0], step); pfc.Unbind(step, step.SuccessorNodes[0]); //Console.WriteLine("Inserted a branch in place of {0}.", step.Name); break; } } } else { for (int i = 0; i < 50; i++) // Try, but give up if don't find suitable step. { IPfcTransitionNode trans = pfc.Transitions[r.Next(0, pfc.Transitions.Count - 1)]; if (trans.PredecessorNodes.Count == 1 && trans.SuccessorNodes.Count == 1) { IPfcStepNode successor = (IPfcStepNode)trans.SuccessorNodes[0]; IPfcStepNode subject = pfc.CreateStep(); pfc.Bind(trans, subject); pfc.Bind(subject, successor); pfc.Unbind(trans, successor); IPfcStepNode loopback = pfc.CreateStep(); pfc.Bind(subject, loopback); pfc.Bind(loopback, subject); //Console.WriteLine("Inserted {0} between {1} and {2}, and created a branchback around it using {3}.", // subject.Name, trans.PredecessorNodes[0].Name, successor.Name, loopback.Name); break; } } //// insert a loopback //IPfcStepNode step; //do { step = pfc.Steps[r.Next(0, pfc.Steps.Count - 1)]; } while (step == start || step == finish); //IPfcStepNode newNode = pfc.CreateStep(); //pfc.Bind(step, newNode); //pfc.Bind(newNode, step); //Console.WriteLine("Inserted a loopback around {0} using new step, {1}.", step.Name, newNode.Name); } //IPfcStepNode origin = pfc.Steps[r.Next(0, pfc.Steps.Count - 1)]; //if (origin.Equals(finish)) continue; //if (r.NextDouble() < .2) { // IPfcStepNode stepNode = pfc.CreateStep(); // IPfcNode target = origin.SuccessorNodes[r.Next(0, origin.SuccessorNodes.Count - 1)]; // // Insert a step in series. // pfc.Bind(origin, stepNode); // pfc.Bind(stepNode, target); // pfc.Unbind(origin, target); // Console.WriteLine("Inserting {0} between {1} and {2}.", // stepNode.Name, origin.Name, target.Name); //} else if (r.NextDouble() < .55) { // // Insert a step in parallel // if (origin.PredecessorNodes.Count == 1 && origin.SuccessorNodes.Count == 1) { // origin = origin.PredecessorNodes[0]; // target = origin.SuccessorNodes[0]; // pfc.Bind(origin, stepNode); // pfc.Bind(stepNode, target); // Console.WriteLine("Inserting {0} parallel to {1} - between {2} and {3}.", // stepNode.Name, parallelTo.Name, origin.Name, target.Name); // } //} else { // // Insert a loopback or branchforward. // IPfcNode target = null; // string parallelType = null; // if (!origin.PredecessorNodes.Contains(start) && r.NextDouble() < .5) { // target = origin; // parallelType = "loopback"; // } else if (origin.SuccessorNodes.Count==1 && origin.PredecessorNodes==1) { // target = origin.SuccessorNodes[r.Next(0, origin.SuccessorNodes.Count - 1)]; // parallelType = "branch forward"; // } // if (target != null) { // IPfcStepNode stepNode = pfc.CreateStep(); // pfc.Bind(origin, stepNode); // pfc.Bind(stepNode, target); // Console.WriteLine("Inserting {0} around {1} to {2}, with {3} on the new alternate path.", // parallelType, origin.Name, target.Name, stepNode.Name); // } //} } return(pfc); }
/// <summary> /// Initializes a new instance of the <see cref="StepStateMachine"/> class. /// </summary> /// <param name="myStep">My step.</param> public StepStateMachine(IPfcStepNode myStep) { m_myStep = myStep; m_successorStateMachines = new List <TransitionStateMachine>(); }
protected PfcActor(IModel model, string name, Guid guid, IPfcStepNode myStepNode) { model.Starting += delegate { m_peers = null; }; InitializeIdentity(model, name, null, guid); m_myStepNode = myStepNode; }