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")); }
private IProcedureFunctionChart CreateSchedulePfc(IModel model, string pfcName, double minutesPerTask, ExecutionEngineConfiguration eec) { // Start // | // + T1 // | // Campaigns // | // + T2 // ProcedureFunctionChart pfc = new ProcedureFunctionChart(model, pfcName); pfc.ExecutionEngineConfiguration = eec; pfc.CreateStep("Start", "", Guid.NewGuid()); pfc.CreateStep("Campaigns", "", Guid.NewGuid()); pfc.CreateTransition("T1", "", Guid.NewGuid()); pfc.CreateTransition("T2", "", Guid.NewGuid()); pfc.Bind(pfc.Nodes["Start"], pfc.Nodes["T1"]); pfc.Bind(pfc.Nodes["T1"], pfc.Nodes["Campaigns"]); pfc.Bind(pfc.Nodes["Campaigns"], pfc.Nodes["T2"]); pfc.UpdateStructure(); return(pfc); }
public static ProcedureFunctionChart CreateLoopTestPfc() { ProcedureFunctionChart pfc = new ProcedureFunctionChart(new Highpoint.Sage.SimCore.Model("Test model", Guid.NewGuid()), "SFC 1"); #region Create Nodes A = pfc.CreateStep("Step_A", "", Guid.NewGuid()); B = pfc.CreateStep("Step_B", "", Guid.NewGuid()); C = pfc.CreateStep("Step_C", "", Guid.NewGuid()); nA = (IPfcNode)A; nB = (IPfcNode)B; nC = (IPfcNode)C; #endregion Create Nodes #region Create Structure pfc.Bind(nA, nB); pfc.Bind(nB, nC); pfc.Bind(nB, nB); #endregion Create Structure return(pfc); }
public static ProcedureFunctionChart CreateTestPfc4() { ProcedureFunctionChart pfc = new ProcedureFunctionChart(new Highpoint.Sage.SimCore.Model("Test model", Guid.NewGuid()), "SFC 1"); #region Create Nodes 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()); nA = (IPfcNode)A; nB = (IPfcNode)B; nC = (IPfcNode)C; nD = (IPfcNode)D; nE = (IPfcNode)E; #endregion Create Nodes #region Create Structure pfc.Bind(nA, nB); pfc.Bind(nB, nE); pfc.Bind(nA.SuccessorNodes[0], nC); pfc.Bind(nC, nD); pfc.Bind(nD, nE.PredecessorNodes[0]); PfcLinkElementList links = new PfcLinkElementList(pfc.Links); links.Sort(new Comparison <IPfcLinkElement>(delegate(IPfcLinkElement a, IPfcLinkElement b) { return(Comparer.Default.Compare(a.Name, b.Name)); })); System.Reflection.BindingFlags bf = System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.FlattenHierarchy | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance; foreach (IPfcLinkElement link in links) { typeof(PfcElement).GetFields(bf); typeof(PfcElement).GetField("m_guid", bf).SetValue((PfcElement)link, NextGuid()); // Totally cheating. } #endregion Create Structure return(pfc); }
private void _TestStepToStepBinding() { string testName = "Step-to-Step binding, maintaining SFC Compliance"; Model model = new Model("SFC Test 1"); ProcedureFunctionChart pfc = new ProcedureFunctionChart(model, "SFC 1", "", Guid.NewGuid()); PfcStep s1 = (PfcStep)pfc.CreateStep("Alice", "", Guid.Empty); PfcStep s2 = (PfcStep)pfc.CreateStep("Bob", "", Guid.Empty); //SfcStep s3 = (SfcStep)pfc.CreateStep("Charlie", "", Guid.Empty); pfc.Bind(s1, s2); string structureString = PfcDiagnostics.GetStructure(pfc); Console.WriteLine("After a " + testName + ", structure is \r\n" + structureString); }
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 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 CreateTestPfc5() { // Flip-flop pattern. ProcedureFunctionChart pfc = new ProcedureFunctionChart(new Highpoint.Sage.SimCore.Model("Test model", Guid.NewGuid()), "SFC 1"); #region Create Nodes 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()); nA = (IPfcNode)A; nB = (IPfcNode)B; nC = (IPfcNode)C; nD = (IPfcNode)D; nE = (IPfcNode)E; nF = (IPfcNode)F; #endregion Create Nodes #region Create Structure pfc.BindParallelDivergent(nA, new IPfcNode[] { nB, nC }); pfc.BindSeriesDivergent(nB, new IPfcNode[] { nD, nE }); pfc.BindSeriesDivergent(nC, new IPfcNode[] { nD, nE }); pfc.BindParallelConvergent(new IPfcNode[] { nD, nE }, nF); PfcLinkElementList links = new PfcLinkElementList(pfc.Links); links.Sort(new Comparison <IPfcLinkElement>(delegate(IPfcLinkElement a, IPfcLinkElement b) { return(Comparer.Default.Compare(a.Name, b.Name)); })); System.Reflection.BindingFlags bf = System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.FlattenHierarchy | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance; foreach (IPfcLinkElement link in links) { typeof(PfcElement).GetFields(bf); typeof(PfcElement).GetField("m_guid", bf).SetValue((PfcElement)link, NextGuid()); // Totally cheating. } //pfc.Bind(nD, pfc.Nodes["T_005"]); #endregion Create Structure return(pfc); }
private IPfcNode CreateNode(ProcedureFunctionChart pfc, string name, PfcElementType inType) { switch (inType) { case PfcElementType.Link: break; case PfcElementType.Transition: return(pfc.CreateTransition("T_" + name, "", Guid.NewGuid())); //break; case PfcElementType.Step: return(pfc.CreateStep("S_" + name, "", Guid.NewGuid())); //break; default: break; } return(null); }
private IProcedureFunctionChart CreateRecipePfc(IModel model, string pfcName, double minutesPerTask, ExecutionEngineConfiguration eec) { // Start // | // +T1 ---- // | | | // ------- | // | | // Step1 | // | | // +T2 | // | | // Step2 | // | | // ------- | // T3+ +T4 | // | |--- // Finish // | // T5+ ProcedureFunctionChart pfc = new ProcedureFunctionChart(model, pfcName); pfc.ExecutionEngineConfiguration = eec; pfc.CreateStep("Start", "", Guid.NewGuid()); pfc.CreateStep("Step1", "", Guid.NewGuid()); pfc.CreateStep("Step2", "", Guid.NewGuid()); pfc.CreateStep("Finish", "", Guid.NewGuid()); pfc.CreateTransition("T1", "", Guid.NewGuid()); pfc.CreateTransition("T2", "", Guid.NewGuid()); pfc.CreateTransition("T3", "", Guid.NewGuid()); pfc.CreateTransition("T4", "", Guid.NewGuid()); pfc.CreateTransition("T5", "", Guid.NewGuid()); pfc.Bind(pfc.Nodes["Start"], pfc.Nodes["T1"]); pfc.Bind(pfc.Nodes["T1"], pfc.Nodes["Step1"]); pfc.Bind(pfc.Nodes["Step1"], pfc.Nodes["T2"]); pfc.Bind(pfc.Nodes["T2"], pfc.Nodes["Step2"]); pfc.Bind(pfc.Nodes["Step2"], pfc.Nodes["T3"]); pfc.Bind(pfc.Nodes["Step2"], pfc.Nodes["T4"]); pfc.Bind(pfc.Nodes["T4"], pfc.Nodes["Step1"]); pfc.Bind(pfc.Nodes["T3"], pfc.Nodes["Finish"]); pfc.Bind(pfc.Nodes["Finish"], pfc.Nodes["T5"]); pfc.Steps.ForEach(delegate(IPfcStepNode psn) { psn.LeafLevelAction = new PfcAction(delegate(PfcExecutionContext pfcec, StepStateMachine ssm) { StringBuilder sb = (StringBuilder)pfcec["StringBuilder"]; string stepName = pfc.Name + "." + psn.Name; IExecutive exec = psn.Model.Executive; sb.AppendLine(string.Format("{0} : {1} is running its intrinsic action.", exec.Now, stepName)); exec.CurrentEventController.SuspendUntil(exec.Now + TimeSpan.FromMinutes(minutesPerTask)); }); }); pfc.Transitions["T1"].ExpressionExecutable = new Highpoint.Sage.Graphs.PFC.Execution.ExecutableCondition( delegate(object userData, Highpoint.Sage.Graphs.PFC.Execution.TransitionStateMachine tsm) { IDictionary graphContext = userData as IDictionary; string countKey = pfc.Guid.ToString() + ".Count"; if (!((IDictionary)graphContext).Contains(countKey)) { ((IDictionary)graphContext).Add(countKey, 1); } else { graphContext[countKey] = 1; } return(DEFAULT_EXECUTABLE_EXPRESSION(graphContext, tsm)); }); pfc.Transitions["T3"].ExpressionExecutable = new Highpoint.Sage.Graphs.PFC.Execution.ExecutableCondition( delegate(object userData, Highpoint.Sage.Graphs.PFC.Execution.TransitionStateMachine tsm) { IDictionary graphContext = userData as IDictionary; string countKey = pfc.Guid.ToString() + ".Count"; return(DEFAULT_EXECUTABLE_EXPRESSION(graphContext, tsm) && (((int)graphContext[countKey]) > 5)); }); pfc.Transitions["T4"].ExpressionExecutable = new Highpoint.Sage.Graphs.PFC.Execution.ExecutableCondition( delegate(object userData, Highpoint.Sage.Graphs.PFC.Execution.TransitionStateMachine tsm) { IDictionary graphContext = userData as IDictionary; string countKey = pfc.Guid.ToString() + ".Count"; if ((DEFAULT_EXECUTABLE_EXPRESSION(graphContext, tsm) && (((int)graphContext[countKey]) <= 5))) { graphContext[countKey] = ((int)graphContext[countKey]) + 1; return(true); } else { return(false); } }); pfc.UpdateStructure(); return(pfc); }
public void Test_InsertStepIntoLoop() { string testName = "PFC with loop gets the loop extended"; /* * START * | * |---- +T1 * | | | * | STEP1 * | | | * | T2+ +T3 * ----| | * FINISH * * TO * * START * | * |---- +T1 * | | | * | STEP1 * | | | * | T4+ +T3 * | | | * | STEP2 FINISH * | | * | T2+ * ----- * */ IModel model = new Model(testName); 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 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()); pfc.Bind(startStep, t1); pfc.Bind(t1, step1); pfc.Bind(step1, t2); pfc.Bind(step1, t3); pfc.Bind(t2, step1); pfc.Bind(t3, finishStep); Console.WriteLine(PfcDiagnostics.GetStructure(pfc)); Console.WriteLine(); /* Connect new step (step2) to existing transition (t2) */ pfc.Bind(step2, t2); /* Connect existing step (step1) to new transition (t3)*/ pfc.Bind(step1, t4); /* Connect new transition (t3) to new step (step2)*/ pfc.Bind(t4, step2); /* Unbind existing step (step1) and existing transition (t2) */ pfc.Unbind(step1, t2); string result = PfcDiagnostics.GetStructure(pfc); Console.WriteLine(result); Assert.IsTrue(result.Equals("{START-->[L_000(SFC 1.Root)]-->T1}\r\n{T1-->[L_001(SFC 1.Root)]-->STEP1}\r\n{STEP1-->[L_003(SFC 1.Root)]-->T3}\r\n{T2-->[L_004(SFC 1.Root)]-->STEP1}\r\n{T3-->[L_005(SFC 1.Root)]-->FINISH}\r\n{STEP2-->[L_006(SFC 1.Root)]-->T2}\r\n{STEP1-->[L_007(SFC 1.Root)]-->T4}\r\n{T4-->[L_008(SFC 1.Root)]-->STEP2}\r\n")); }
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 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 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); }
public static ProcedureFunctionChart CreateTestPfc3() { ProcedureFunctionChart pfc = new ProcedureFunctionChart(new Highpoint.Sage.SimCore.Model("Test model", Guid.NewGuid()), "SFC 1"); #region Create Nodes 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()); H = pfc.CreateStep("Step_" + (name++), "", NextGuid()); I = pfc.CreateStep("Step_" + (name++), "", NextGuid()); J = pfc.CreateStep("Step_" + (name++), "", NextGuid()); K = pfc.CreateStep("Step_" + (name++), "", NextGuid()); L = 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; nH = (IPfcNode)H; nI = (IPfcNode)I; nJ = (IPfcNode)J; nK = (IPfcNode)K; nL = (IPfcNode)L; #endregion Create Nodes #region Create Structure pfc.BindParallelDivergent(nA, new IPfcNode[] { nB, nC, nD, nE }); pfc.BindParallelDivergent(nB, new IPfcNode[] { nF, nG }); pfc.BindParallelDivergent(nE, new IPfcNode[] { nJ, nK }); pfc.BindParallelConvergent(new IPfcNode[] { nF, nG }, nH); pfc.BindParallelConvergent(new IPfcNode[] { nC, nD }, nI); pfc.BindParallelConvergent(new IPfcNode[] { nH, nI, nJ, nK }, nL); PfcLinkElementList links = new PfcLinkElementList(pfc.Links); links.Sort(new Comparison <IPfcLinkElement>(delegate(IPfcLinkElement a, IPfcLinkElement b) { return(Comparer.Default.Compare(a.Name, b.Name)); })); System.Reflection.BindingFlags bf = System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.FlattenHierarchy | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance; foreach (IPfcLinkElement link in links) { typeof(PfcElement).GetFields(bf); typeof(PfcElement).GetField("m_guid", bf).SetValue((PfcElement)link, NextGuid()); // Totally cheating. } #endregion Create Structure return(pfc); }
public static ProcedureFunctionChart CreateTestPfc() { ProcedureFunctionChart pfc = new ProcedureFunctionChart(new Highpoint.Sage.SimCore.Model("Test model", Guid.NewGuid()), "SFC 1"); ((PfcElementFactory)pfc.ElementFactory).SetRepeatable(Guid.Empty); // Ensures Guids are repeatable. #region Create Nodes 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()); H = pfc.CreateStep("Step_" + (name++), "", NextGuid()); I = pfc.CreateStep("Step_" + (name++), "", NextGuid()); J = pfc.CreateStep("Step_" + (name++), "", NextGuid()); K = pfc.CreateStep("Step_" + (name++), "", NextGuid()); L = pfc.CreateStep("Step_" + (name++), "", NextGuid()); M = pfc.CreateStep("Step_" + (name++), "", NextGuid()); N = pfc.CreateStep("Step_" + (name++), "", NextGuid()); O = pfc.CreateStep("Step_" + (name++), "", NextGuid()); P = 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; nH = (IPfcNode)H; nI = (IPfcNode)I; nJ = (IPfcNode)J; nK = (IPfcNode)K; nL = (IPfcNode)L; nM = (IPfcNode)M; nN = (IPfcNode)N; nO = (IPfcNode)O; nP = (IPfcNode)P; #endregion Create Nodes #region Create Structure pfc.Bind(nA, nB); pfc.BindSeriesDivergent(nB, new IPfcNode[] { nC, nF }); pfc.Bind(nC, nD); pfc.Bind(nD, nE); pfc.Bind(nF, nG); pfc.BindParallelDivergent(nG, new IPfcNode[] { nH, nI, nP }); pfc.Bind(nH, nJ); pfc.Bind(nI, nK); pfc.BindParallelConvergent(new IPfcNode[] { nJ, nK, nP }, nL); pfc.Bind(nL, nM); pfc.BindSeriesConvergent(new IPfcNode[] { nE, nM }, nN); pfc.Bind(nN, nO); pfc.Bind(nB, nN); #endregion Create Structure pfc.UpdateStructure(); return(pfc); }