public void SimpleFlowArrives() { var supply = new WaterSupply(); var pump = new Pump(); var drain = new Drain(); var combinator = new DialyzingFluidFlowCombinator(); pump.PumpSpeed = 7; combinator.ConnectOutWithIn(supply.MainFlow,pump.MainFlow); combinator.ConnectOutWithIn(pump.MainFlow, drain.MainFlow); combinator.CommitFlow(); var model = new DialyzingFluidFlowModel { FlowElements = new IComponent[] { supply, pump, drain, combinator } }; var simulator = new Simulator(model); //Important: Call after all objects have been created simulator.SimulateStep(); var modelAfterStep = (DialyzingFluidFlowModel) simulator.Model; var supplyAfterStep = (WaterSupply)modelAfterStep.Components[0]; var pumpAfterStep = (Pump)modelAfterStep.Components[1]; var drainAfterStep = (Drain)modelAfterStep.Components[2]; pumpAfterStep.MainFlow.Incoming.Backward.CustomSuctionValue.Should().Be(7); supplyAfterStep.MainFlow.Outgoing.Forward.Quantity.Should().Be(7); drainAfterStep.MainFlow.Incoming.Forward.Quantity.Should().Be(7); }
public static void PrintTrace(Simulator simulator, int steps) { var model = (Model)simulator.Model; for (var i = 0; i < steps; ++i) { WriteLine($"================= Step: {i} ====================================="); if (model.ObserverController.ReconfigurationState == ReconfStates.Failed) WriteLine("Reconfiguration failed."); else { foreach (var robot in model.RobotAgents) WriteLine(robot); foreach (var cart in model.CartAgents) WriteLine(cart); foreach (var workpiece in model.Workpieces) WriteLine(workpiece); foreach (var robot in model.Robots) WriteLine(robot); foreach (var cart in model.Carts) WriteLine(cart); } simulator.SimulateStep(); } }
/// <summary> /// Initializes a new instance. /// </summary> /// <param name="simulator">The simulator that should be used to simulate the model.</param> /// <param name="stepDelay">The step delay in milliseconds, i.e., time to wait between two steps in running mode.</param> public RealTimeSimulator(Simulator simulator, int stepDelay) { Requires.NotNull(simulator, nameof(simulator)); Requires.That(SynchronizationContext.Current != null, "The simulator requires a valid synchronization context to be set."); _simulator = simulator; _stepDelay = stepDelay; }
public void Simulate_1Step() { var testModel = new Model(); var simulator = new Simulator(testModel); //Important: Call after all objects have been created simulator.SimulateStep(); var flowCombinatorAfterStep = (DialyzingFluidFlowCombinator)simulator.Model.Roots[0]; }
public void Simulate() { var model = new Model(); model.InitializeDefaultInstance(); model.CreateObserverController<FastObserverController>(); model.Faults.SuppressActivations(); var simulator = new Simulator(model); PrintTrace(simulator, steps: 120); }
protected override void Check() { var simulator = new Simulator(TestModel.InitializeModel(new C { X = 44 })); var c = (C)simulator.Model.Roots[0]; c.X.ShouldBe(44); simulator.SimulateStep(); c.X.ShouldBe(1); }
public void NoCollisionsWhenNoFaultsOccur() { var model = Model.CreateOriginal(); model.Faults.SuppressActivations(); var simulator = new Simulator(model); simulator.FastForward(steps: 20); foreach (var vehicle in ((Model)simulator.Model).Vehicles) vehicle.IsCollided.Should().BeFalse(); }
public void TankDoesNotRuptureWhenNoFaultsOccur() { var model = new Model(); model.Faults.SuppressActivations(); var simulator = new Simulator(model); model = (Model)simulator.Model; simulator.FastForward(steps: 120); model.Tank.IsRuptured.Should().BeFalse(); }
public void TankDoesNotRuptureWhenSensorDoesNotReportTankFull() { var model = new Model(); model.Faults.SuppressActivations(); model.Sensor.SuppressIsFull.ForceActivation(); var simulator = new Simulator(model); model = (Model)simulator.Model; simulator.FastForward(steps: 120); model.Tank.IsRuptured.Should().BeFalse(); }
protected override void Check() { var c = new C(); var d = new D(); Component.Bind(nameof(c.Y), nameof(d.Z)); var simulator = new Simulator(TestModel.InitializeModel(c, d)); c = (C)simulator.Model.Roots[0]; simulator.SimulateStep(); c.X.ShouldBe(7); }
protected override void Check() { var simulator = new Simulator(TestModel.InitializeModel(new C())); var c = (C)simulator.Model.Roots[0]; c.X.ShouldBe(0); c.Y.ShouldBe(0); simulator.SimulateStep(); c.X.ShouldBe(3320); c.Y.ShouldBe(2480); }
protected override void Check() { var m1 = new M(); var s = new Simulator(m1); var m2 = (M)s.Model; m2.A.I.ShouldBe(m1.A.I); m2.B.I.ShouldBe(m1.B.I); m2.Roots.ShouldBe(new IComponent[] { m2.A }, ignoreOrder: true); m2.Components.ShouldBe(new IComponent[] { m2.A }, ignoreOrder: true); m2.Faults.ShouldBeEmpty(); }
protected override void Check() { var simulator = new Simulator(TestModel.InitializeModel(new C { X = 44 })); var c = (C)simulator.Model.Roots[0]; c.F.Activation = Activation.Suppressed; c.X.ShouldBe(44); for (var i = 0; i < 10; ++i) { simulator.FastForward(10); c.X.ShouldBe(44 + (i + 1) * 10); } for (var i = 0; i < 12; ++i) { simulator.Rewind(10); if (i < 10) c.X.ShouldBe(44 + (10 - i - 1) * 10); else c.X.ShouldBe(44); } for (var i = 0; i < 12; ++i) { simulator.FastForward(10); c.X.ShouldBe(44 + (i + 1) * 10); } simulator.Rewind(61); c.X.ShouldBe(103); c.F.Activation = Activation.Forced; simulator.SimulateStep(); c.X.ShouldBe(105); simulator.Rewind(2); c.X.ShouldBe(102); c.F.Activation = Activation.Suppressed; simulator.Prune(); c.X.ShouldBe(102); simulator.SimulateStep(); c.X.ShouldBe(103); simulator.FastForward(10); c.X.ShouldBe(113); }
protected override void Check() { var simulator = new Simulator(TestModel.InitializeModel(new C())); var c = (C)simulator.Model.Roots[0]; c.F.Activation = Activation.Forced; simulator.SimulateStep(); c.X.ShouldBe(1); c.F.Activation = Activation.Suppressed; simulator.SimulateStep(); c.X.ShouldBe(1); simulator.Model.Faults.ShouldBeEmpty(); }
protected override void Check() { var m1 = new M(); var s = new Simulator(m1); var m2 = (M)s.Model; var c1 = m1.GetRoots().OfType<C>().ToArray(); var c2 = m2.GetRoots().OfType<C>().ToArray(); for (var i = 0; i < c1.Length; ++i) c2[i].I.ShouldBe(c1[i].I); m2.E.C.I.ShouldBe(m1.E.C.I); m2.Roots.ShouldBe(m2.GetRoots(), ignoreOrder: true); m2.Components.ShouldBe(m2.GetRoots().Concat(new[] { m2.E.C }), ignoreOrder: true); m2.Faults.ShouldBe(new[] { m2.E.F }); }
protected override void Check() { var simulator = new Simulator(TestModel.InitializeModel(new C { X = 1 })); var c = (C)simulator.Model.Roots[0]; var y = 0; c.Y += x => y = x; c.X.ShouldBe(1); y.ShouldBe(0); simulator.SimulateStep(); y.ShouldBe(0); simulator.SimulateStep(); y.ShouldBe(3); y = 0; simulator.SimulateStep(); y.ShouldBe(0); }
protected override void Check() { var m1 = new M(); var s = new Simulator(m1, m1.Formula); var m2 = (M)s.Model; m1.A.I.ShouldBe(m2.A.I); m1.B.I.ShouldBe(m2.B.I); m1.C.I.ShouldBe(m2.C.I); m1.D().I.ShouldBe(m2.D().I); m1.E.I.ShouldBe(m2.E.I); m1.F.I.ShouldBe(m2.F.I); m1.G.I.ShouldBe(m2.G.I); m1.H().I.ShouldBe(m2.H().I); m2.Formula.Evaluate().ShouldBe(false); m2.Roots.ShouldBe(new IComponent[] { m2.A, m2.B, m2.C, m2.D() }, ignoreOrder: true); m2.Components.ShouldBe(new IComponent[] { m2.A, m2.B, m2.C, m2.D() }, ignoreOrder: true); m2.Faults.ShouldBeEmpty(); }
public void Simulate_10_Step_DialyzerRuptured() { var testModel = new Model(); var simulator = new Simulator(testModel); //Important: Call after all objects have been created var patient = simulator.Model.Roots.OfType<Patient>().First(); var hdMachine = simulator.Model.Roots.OfType<HdMachine>().First(); hdMachine.Dialyzer.DialyzerMembraneRupturesFault.Activation = Activation.Forced; simulator.SimulateStep(); patient = simulator.Model.Roots.OfType<Patient>().First(); simulator.SimulateStep(); patient = simulator.Model.Roots.OfType<Patient>().First(); simulator.SimulateStep(); patient = simulator.Model.Roots.OfType<Patient>().First(); simulator.SimulateStep(); simulator.SimulateStep(); simulator.SimulateStep(); simulator.SimulateStep(); simulator.SimulateStep(); simulator.SimulateStep(); simulator.SimulateStep(); }
public void TankRupturesWhenSensorDoesNotReportTankFullAndTimerDoesNotTimeout() { var model = new Model(); model.Faults.SuppressActivations(); model.Sensor.SuppressIsFull.ForceActivation(); model.Timer.SuppressTimeout.ForceActivation(); // Check that the tank is ruptured after 62 steps var simulator = new Simulator(model); model = (Model)simulator.Model; simulator.FastForward(steps: 62); model.Tank.IsRuptured.Should().BeTrue(); // Check that the tank does not rupture in the first 61 steps simulator.Reset(); for (var i = 0; i < 61; ++i) { simulator.SimulateStep(); model.Tank.IsRuptured.Should().BeFalse(); } }
protected override void Check() { var simulator = new Simulator(TestModel.InitializeModel(new C())); var c = (C)simulator.Model.Roots[0]; c.X.ShouldBe(0); c.Y.ShouldBe(0); c.Z.ShouldBe(0); simulator.SimulateStep(); c.X.ShouldBe(1); c.Y.ShouldBe(1); c.Z.ShouldBe(1); simulator.SimulateStep(); c.X.ShouldBe(2); c.Y.ShouldBe(0); c.Z.ShouldBe(2); simulator.SimulateStep(); c.X.ShouldBe(2); c.Y.ShouldBe(1); c.Z.ShouldBe(3); var exception = Should.Throw<RangeViolationException>(() => simulator.SimulateStep()); exception.Field.ShouldBe(typeof(C).GetField("Z")); exception.Object.ShouldBe(c); exception.FieldValue.ShouldBe(4); exception.Range.LowerBound.ShouldBe(0); exception.Range.UpperBound.ShouldBe(3); exception.Range.OverflowBehavior.ShouldBe(OverflowBehavior.Error); simulator.Reset(); c.X.ShouldBe(0); c.Y.ShouldBe(0); c.Z.ShouldBe(0); }
protected override void Check() { var m = new M(); var simulator = new Simulator(m, m.F); m = (M)simulator.Model; var f = m.F.Compile(); var g = m.G.Compile(); m.C.X.ShouldBe(0); m.F.Evaluate().ShouldBe(true); m.G.Evaluate().ShouldBe(true); f().ShouldBe(true); g().ShouldBe(true); simulator.SimulateStep(); m.C.X.ShouldBe(1); m.F.Evaluate().ShouldBe(false); m.G.Evaluate().ShouldBe(false); f().ShouldBe(false); g().ShouldBe(false); }
public void MergeFlowWorksWithComposite() { var testModel = new IntFlowModel(); var sourceOutside = new IntFlowSource(); var sourceInside = new IntFlowSource(); var composite = new IntFlowComposite(); var way1Direct = new IntFlowInToOut(); var way2Direct = new IntFlowInToOut(); var sink = new IntFlowSink(); testModel.Components = new IntFlowComponentCollection(sourceOutside, sourceInside, composite, way1Direct, way2Direct, sink); sourceOutside.SendForward = testModel.CreateForward; sourceOutside.ReceivedBackward = testModel.PrintReceivedBackward; sourceInside.SendForward = testModel.CreateForward; sourceInside.ReceivedBackward = testModel.PrintReceivedBackward; sink.SendBackward = testModel.CreateBackward; sink.ReceivedForward = testModel.PrintReceivedForward; testModel.Combinator.ConnectOutWithIn(sourceOutside, composite); testModel.Combinator.ConnectInWithIn(composite, way1Direct); testModel.Combinator.ConnectOutWithIn(sourceInside, way2Direct); testModel.Combinator.ConnectOutsWithOut(new IFlowComponentUniqueOutgoing<Int, Int>[] { way1Direct, way2Direct }, composite); testModel.Combinator.ConnectOutWithIn(composite, sink); testModel.Combinator.CommitFlow(); var simulator = new Simulator(testModel); //Important: Call after all objects have been created simulator.SimulateStep(); var flowCombinatorAfterStep = (IntFlowCombinator)simulator.Model.Roots[0]; var flowComponentsAfterStep = ((IntFlowComponentCollection)simulator.Model.Roots[1]).Components; var sourceInsideAfterStep = (IntFlowSource)flowComponentsAfterStep[1]; var sourceOutsideAfterStep = (IntFlowSource)flowComponentsAfterStep[0]; var sinkAfterStep = (IntFlowSink)flowComponentsAfterStep[5]; sinkAfterStep.Incoming.Forward.Should().Be(7); sourceOutsideAfterStep.Outgoing.Backward.Should().Be(1); sourceInsideAfterStep.Outgoing.Backward.Should().Be(1); }
public void SimpleFlowArrives() { var testModel = new IntFlowModel(); var source = new IntFlowSource(); var direct = new IntFlowInToOut(); var sink = new IntFlowSink(); testModel.Components = new IntFlowComponentCollection(source, direct, sink); source.SendForward = testModel.CreateForward; source.ReceivedBackward = testModel.PrintReceivedBackward; sink.SendBackward = testModel.CreateBackward; sink.ReceivedForward = testModel.PrintReceivedForward; testModel.Combinator.ConnectOutWithIn(source, direct); testModel.Combinator.ConnectOutWithIn(direct, sink); testModel.Combinator.CommitFlow(); var simulator = new Simulator(testModel); //Important: Call after all objects have been created simulator.SimulateStep(); var flowCombinatorAfterStep = (IntFlowCombinator)simulator.Model.Roots[0]; var flowComponentsAfterStep = ((IntFlowComponentCollection)simulator.Model.Roots[1]).Components; var sourceAfterStep = (IntFlowSource)flowComponentsAfterStep[0]; var directAfterStep = (IntFlowInToOut)flowComponentsAfterStep[1]; var sinkAfterStep = (IntFlowSink)flowComponentsAfterStep[2]; sourceAfterStep.Outgoing.Forward.Should().Be((Int)7); directAfterStep.Incoming.Forward.Should().Be((Int)7); directAfterStep.Outgoing.Forward.Should().Be((Int)7); sinkAfterStep.Incoming.Forward.Should().Be((Int)7); sinkAfterStep.Incoming.Backward.Should().Be(1); directAfterStep.Outgoing.Backward.Should().Be(1); directAfterStep.Incoming.Backward.Should().Be(1); sourceAfterStep.Outgoing.Backward.Should().Be(1); }
public void SplitFlowWorks() { var testModel = new IntFlowModel(); var source = new IntFlowSource(); var way1Direct = new IntFlowInToOut(); var way2Direct = new IntFlowInToOut(); var way1Sink = new IntFlowSink(); var way2Sink = new IntFlowSink(); testModel.Components = new IntFlowComponentCollection(source, way1Direct, way2Direct, way1Sink, way2Sink); source.SendForward = testModel.CreateForward; source.ReceivedBackward = testModel.PrintReceivedBackward; way1Sink.SendBackward = testModel.CreateBackward; way1Sink.ReceivedForward = testModel.PrintReceivedForward; way2Sink.SendBackward = testModel.CreateBackward; way2Sink.ReceivedForward = testModel.PrintReceivedForward; testModel.Combinator.ConnectOutWithIns(source, new IFlowComponentUniqueIncoming<Int, Int>[] { way1Direct, way2Direct }); testModel.Combinator.ConnectOutWithIn(way1Direct, way1Sink); testModel.Combinator.ConnectOutWithIn(way2Direct, way2Sink); testModel.Combinator.CommitFlow(); var simulator = new Simulator(testModel); //Important: Call after all objects have been created simulator.SimulateStep(); var flowCombinatorAfterStep = (IntFlowCombinator)simulator.Model.Roots[0]; var flowComponentsAfterStep = ((IntFlowComponentCollection)simulator.Model.Roots[1]).Components; var sourceAfterStep = (IntFlowSource)flowComponentsAfterStep[0]; var sink1AfterStep = (IntFlowSink)flowComponentsAfterStep[3]; var sink2AfterStep = (IntFlowSink)flowComponentsAfterStep[4]; sink1AfterStep.Incoming.Forward.Should().Be(7); sink2AfterStep.Incoming.Forward.Should().Be(7); sourceAfterStep.Outgoing.Backward.Should().Be(1); }
public void ExtracorporealBloodCircuitWorks_Simulation() { var specification = new ExtracorporealBloodCircuitTestEnvironment(); var simulator = new Simulator(specification); //Important: Call after all objects have been created var extracorporealBloodCircuitAfterStep0 = simulator.Model.Roots.OfType<ExtracorporealBloodCircuit>().First(); var patientAfterStep0 = simulator.Model.Roots.OfType<Patient>().First(); Console.Out.WriteLine("Initial"); patientAfterStep0.ArteryFlow.Outgoing.Forward.PrintBloodValues("outgoing Blood"); patientAfterStep0.VeinFlow.Incoming.Forward.PrintBloodValues("incoming Blood"); patientAfterStep0.PrintBloodValues(""); Console.Out.WriteLine("Step 1"); simulator.SimulateStep(); /* //dialyzerAfterStep1.Should().Be(1); patientAfterStep4.BigWasteProducts.Should().Be(0); patientAfterStep4.SmallWasteProducts.Should().Be(2); */ }
public void Simulate_10_Step_HeaterAndPumpToBalanceChamberDefect() { var testModel = new Model(); var simulator = new Simulator(testModel); //Important: Call after all objects have been created var patient = simulator.Model.Roots.OfType<Patient>().First(); var hdMachine = simulator.Model.Roots.OfType<HdMachine>().First(); hdMachine.DialyzingFluidDeliverySystem.WaterPreparation.WaterHeaterDefect.Activation = Activation.Forced; hdMachine.DialyzingFluidDeliverySystem.PumpToBalanceChamber.PumpDefect.Activation = Activation.Forced; simulator.SimulateStep(); var patient2 = simulator.Model.Roots.OfType<Patient>().First(); simulator.SimulateStep(); var patient3 = simulator.Model.Roots.OfType<Patient>().First(); simulator.SimulateStep(); var patient4 = simulator.Model.Roots.OfType<Patient>().First(); var hdMachine4 = simulator.Model.Roots.OfType<HdMachine>().First(); simulator.SimulateStep(); simulator.SimulateStep(); simulator.SimulateStep(); simulator.SimulateStep(); simulator.SimulateStep(); simulator.SimulateStep(); simulator.SimulateStep(); }
public void DialyzerWorks_Simulation() { var specification = new DialyzerTestEnvironment(); var simulator = new Simulator(specification); //Important: Call after all objects have been created var dialyzerAfterStep0 = (Dialyzer)simulator.Model.Roots.OfType<Dialyzer>().First(); var patientAfterStep0 = (DialyzerTestEnvironmentPatient) simulator.Model.Roots.OfType<DialyzerTestEnvironmentPatient>().First(); Console.Out.WriteLine("Initial"); patientAfterStep0.ArteryFlow.Outgoing.Forward.PrintBloodValues("outgoing Blood"); patientAfterStep0.VeinFlow.Incoming.Forward.PrintBloodValues("incoming Blood"); patientAfterStep0.PrintBloodValues(""); Console.Out.WriteLine("Step 1"); simulator.SimulateStep(); var dialyzerAfterStep1 = (Dialyzer)simulator.Model.Roots.OfType<Dialyzer>().First(); var patientAfterStep1 = (DialyzerTestEnvironmentPatient) simulator.Model.Roots.OfType<DialyzerTestEnvironmentPatient>().First(); patientAfterStep1.ArteryFlow.Outgoing.Forward.PrintBloodValues("outgoing Blood"); patientAfterStep1.VeinFlow.Incoming.Forward.PrintBloodValues("incoming Blood"); patientAfterStep1.PrintBloodValues(""); Console.Out.WriteLine("Step 2"); simulator.SimulateStep(); var dialyzerAfterStep2 = (Dialyzer)simulator.Model.Roots.OfType<Dialyzer>().First(); var patientAfterStep2 = (DialyzerTestEnvironmentPatient) simulator.Model.Roots.OfType<DialyzerTestEnvironmentPatient>().First(); patientAfterStep2.ArteryFlow.Outgoing.Forward.PrintBloodValues("outgoing Blood"); patientAfterStep2.VeinFlow.Incoming.Forward.PrintBloodValues("incoming Blood"); patientAfterStep2.PrintBloodValues(""); Console.Out.WriteLine("Step 3"); simulator.SimulateStep(); var dialyzerAfterStep3 = (Dialyzer)simulator.Model.Roots.OfType<Dialyzer>().First(); var patientAfterStep3 = (DialyzerTestEnvironmentPatient) simulator.Model.Roots.OfType<DialyzerTestEnvironmentPatient>().First(); patientAfterStep3.ArteryFlow.Outgoing.Forward.PrintBloodValues("outgoing Blood"); patientAfterStep3.VeinFlow.Incoming.Forward.PrintBloodValues("incoming Blood"); patientAfterStep3.PrintBloodValues(""); Console.Out.WriteLine("Step 4"); simulator.SimulateStep(); var dialyzerAfterStep4 = (Dialyzer)simulator.Model.Roots.OfType<Dialyzer>().First(); var patientAfterStep4 = (DialyzerTestEnvironmentPatient) simulator.Model.Roots.OfType<DialyzerTestEnvironmentPatient>().First(); patientAfterStep4.ArteryFlow.Outgoing.Forward.PrintBloodValues("outgoing Blood"); patientAfterStep4.VeinFlow.Incoming.Forward.PrintBloodValues("incoming Blood"); patientAfterStep4.PrintBloodValues(""); //dialyzerAfterStep1.Should().Be(1); patientAfterStep4.BigWasteProducts.Should().Be(0); patientAfterStep4.SmallWasteProducts.Should().Be(2); }
public void DialyzingFluidDeliverySystemWorks_Simulation() { var specification = new DialyzingFluidDeliverySystemTestEnvironment(); var simulator = new Simulator(specification); //Important: Call after all objects have been created var dialyzerAfterStep0 = simulator.Model.Roots.OfType<DialyzingFluidDeliverySystemTestEnvironmentDialyzer>().First(); var dialyzingFluidDeliverySystemAfterStep0 = simulator.Model.Roots.OfType<DialyzingFluidDeliverySystem>().First(); Console.Out.WriteLine("Initial"); //dialyzingFluidDeliverySystemAfterStep0.ArteryFlow.Outgoing.ForwardToSuccessor.PrintBloodValues("outgoing Blood"); //patientAfterStep0.VeinFlow.Incoming.ForwardFromPredecessor.PrintBloodValues("incoming Blood"); //patientAfterStep0.PrintBloodValues(""); Console.Out.WriteLine("Step 1"); simulator.SimulateStep(); /* //dialyzerAfterStep1.Should().Be(1); patientAfterStep4.BigWasteProducts.Should().Be(0); patientAfterStep4.SmallWasteProducts.Should().Be(2);*/ }