コード例 #1
0
ファイル: SimulatorFixture.cs プロジェクト: jhorv/NSimulate
        public void Simulate_Terminated_SimulationEndsAtTerminatedPeriod()
        {
            using (var context = new SimulationContext()){
                var processor1Instructions = new List <InstructionBase>()
                {
                    new WaitInstruction(5),
                    new WaitInstruction(10)
                };

                var processor2Instructions = new List <InstructionBase>()
                {
                    new WaitInstruction(2),
                    new TerminateSimulationInstruction(),
                    new WaitInstruction(9),
                };

                var processor1 = new InstructionListTestProcess(context, processor1Instructions);
                var processor2 = new InstructionListTestProcess(context, processor2Instructions);

                var simulator = new Simulator(context);

                simulator.Simulate();

                Assert.AreEqual(2, context.TimePeriod);

                Assert.IsNull(processor1Instructions[0].CompletedAtTimePeriod);
                Assert.IsNull(processor1Instructions[1].CompletedAtTimePeriod);
                Assert.AreEqual(2, processor2Instructions[0].CompletedAtTimePeriod);
                Assert.AreEqual(2, processor2Instructions[1].CompletedAtTimePeriod);
                Assert.IsNull(processor2Instructions[2].CompletedAtTimePeriod);
            }
        }
コード例 #2
0
        public void CanComplete_BeforeAndAfterEvent_ReturnsTrueOnlyAfterEvent()
        {
            using (var context = new SimulationContext()){
                var waitInstruction = new WaitNotificationInstruction <object>();
                var process         = new InstructionListTestProcess(context, new List <InstructionBase>()
                {
                    waitInstruction
                });

                context.MoveToTimePeriod(0);
                process.SimulationState.InstructionEnumerator = process.Simulate().GetEnumerator();
                process.SimulationState.InstructionEnumerator.MoveNext();

                var testEvent        = new TestNotification();
                var raiseInstruction = new RaiseNotificationInstruction <object>(testEvent);

                long?nextTimePeriodCheck;
                bool canComplete = waitInstruction.CanComplete(context, out nextTimePeriodCheck);

                Assert.IsFalse(canComplete);
                Assert.IsNull(nextTimePeriodCheck);

                raiseInstruction.Complete(context);

                canComplete = waitInstruction.CanComplete(context, out nextTimePeriodCheck);

                Assert.IsTrue(canComplete);
                Assert.IsNull(nextTimePeriodCheck);
            }
        }
コード例 #3
0
        public void Complete_ContextPassed_ProcessActivated()
        {
            using (var context = new SimulationContext())
            {
                var testProcess = new InstructionListTestProcess(context, new WaitInstruction(10));
                var enumerator  = testProcess.Simulate().GetEnumerator();
                testProcess.SimulationState.InstructionEnumerator = enumerator;

                context.MoveToTimePeriod(0);
                Assert.IsTrue(context.ActiveProcesses.Contains(testProcess));
                // clear the remaining process queue
                context.ProcessesRemainingThisTimePeriod.Clear();

                enumerator.MoveNext();

                var instruction = new InterruptInstruction(testProcess);

                long?nextTimePeriodCheck = null;
                bool canComplete         = instruction.CanComplete(context, out nextTimePeriodCheck);

                Assert.IsTrue(canComplete);
                instruction.Complete(context);

                Assert.IsTrue(testProcess.SimulationState.IsInterrupted);
                // the process should be back in the queue
                Assert.IsTrue(context.ProcessesRemainingThisTimePeriod.Contains(testProcess));
                Assert.IsTrue(enumerator.Current.IsInterrupted);
                Assert.IsFalse(enumerator.Current.IsCompleted);
            }
        }
コード例 #4
0
        public void CanComplete_BeforeAndAfterEventWithCondition_ReturnsTrueOnlyAfterEventMatchingCondition()
        {
            using (var context = new SimulationContext()){
                var waitInstruction = new WaitNotificationInstruction <TestNotification>((e) => e.Data > 0);
                var process         = new InstructionListTestProcess(context, new List <InstructionBase>()
                {
                    waitInstruction
                });
                context.MoveToTimePeriod(0);
                process.SimulationState.InstructionEnumerator = process.Simulate().GetEnumerator();
                process.SimulationState.InstructionEnumerator.MoveNext();

                var testEvent1 = new TestNotification()
                {
                    Data = 0
                };
                var raiseInstruction1 = new RaiseNotificationInstruction <TestNotification>(testEvent1);

                var testEvent2 = new TestNotification()
                {
                    Data = 1
                };
                var raiseInstruction2 = new RaiseNotificationInstruction <TestNotification>(testEvent2);

                long?nextTimePeriodCheck;
                bool canComplete = waitInstruction.CanComplete(context, out nextTimePeriodCheck);

                Assert.IsFalse(canComplete);
                Assert.IsNull(nextTimePeriodCheck);

                raiseInstruction1.Complete(context);

                canComplete = waitInstruction.CanComplete(context, out nextTimePeriodCheck);

                Assert.IsFalse(canComplete);
                Assert.IsNull(nextTimePeriodCheck);

                raiseInstruction2.Complete(context);

                canComplete = waitInstruction.CanComplete(context, out nextTimePeriodCheck);

                Assert.IsTrue(canComplete);
                Assert.IsNull(nextTimePeriodCheck);
                Assert.AreEqual(1, waitInstruction.Notifications.Count);
                Assert.IsTrue(waitInstruction.Notifications.Contains(testEvent2));
            }
        }
コード例 #5
0
ファイル: SimulatorFixture.cs プロジェクト: jhorv/NSimulate
        public void Simulate_VariousProcesses_SimulateAsExpected()
        {
            using (var context = new SimulationContext()){
                var processor1Instructions = new List <InstructionBase>()
                {
                    new WaitInstruction(5),
                    new WaitInstruction(10)
                };

                var processor2Instructions = new List <InstructionBase>()
                {
                    new WaitInstruction(2),
                    new WaitInstruction(12),
                };

                var processor3Instructions = new List <InstructionBase>()
                {
                    new WaitInstruction(9),
                };

                var processor1 = new InstructionListTestProcess(context, processor1Instructions);
                var processor2 = new InstructionListTestProcess(context, processor2Instructions);
                var processor3 = new InstructionListTestProcess(context, processor3Instructions);

                processor3.SimulationState.IsActive = false;
                processor2Instructions.Add(new ActivateInstruction(processor3));

                var simulator = new Simulator(context);

                simulator.Simulate();

                Assert.AreEqual(23, context.TimePeriod);

                Assert.AreEqual(5, processor1Instructions[0].CompletedAtTimePeriod);
                Assert.AreEqual(15, processor1Instructions[1].CompletedAtTimePeriod);

                Assert.AreEqual(2, processor2Instructions[0].CompletedAtTimePeriod);
                Assert.AreEqual(14, processor2Instructions[1].CompletedAtTimePeriod);

                Assert.AreEqual(23, processor3Instructions[0].CompletedAtTimePeriod);
            }
        }
コード例 #6
0
ファイル: SimulatorFixture.cs プロジェクト: jhorv/NSimulate
        public void Simulate_ResourceContention_SimulationEndsAtExpectedPeriod()
        {
            using (var context = new SimulationContext())
            {
                new TestResource(context, 1);

                var firstAllocation  = new AllocateInstruction <TestResource>(1);
                var secondAllocation = new AllocateInstruction <TestResource>(1);
                var firstRelease     = new ReleaseInstruction <TestResource>(firstAllocation);
                var secondRelease    = new ReleaseInstruction <TestResource>(secondAllocation);

                var processor1Instructions = new List <InstructionBase>()
                {
                    firstAllocation,
                    new WaitInstruction(5),
                    firstRelease,
                    new WaitInstruction(10)
                };

                var processor2Instructions = new List <InstructionBase>()
                {
                    secondAllocation,
                    new WaitInstruction(2),
                    secondRelease,
                    new WaitInstruction(9),
                };

                var processor1 = new InstructionListTestProcess(context, processor1Instructions);
                var processor2 = new InstructionListTestProcess(context, processor2Instructions);

                var simulator = new Simulator(context);

                simulator.Simulate();

                // simulation time is extended due to resource contention
                Assert.AreEqual(16, context.TimePeriod);
            }
        }
コード例 #7
0
        public void Process_EndConditionSpecified_EndConditionMetAtExpectedTime()
        {
            using (var context = new SimulationContext()){
                var waitInstruction1 = new WaitInstruction(2);
                var waitInstruction2 = new WaitInstruction(4);
                var waitInstruction3 = new WaitInstruction(4);

                var process = new InstructionListTestProcess(context, new List <InstructionBase>()
                {
                    waitInstruction1, waitInstruction2, waitInstruction3
                });
                var endTrigger = new SimulationEndTrigger(context, () => context.TimePeriod >= 5);

                var simulator = new Simulator(context);

                simulator.Simulate();

                Assert.AreEqual(6, context.TimePeriod);

                // the simulation should have ended at th expected time
                Assert.IsTrue(process.SimulationState.IsActive);
            }
        }