Exemplo n.º 1
0
        public void Simulate_EnumerableReturned()
        {
            var process = new Process();

            var enumerator = process.Simulate();

            Assert.IsNotNull(enumerator);
            Assert.IsFalse(enumerator.MoveNext());
        }
Exemplo n.º 2
0
        public void Constructor_SimulationContextExists_ProcessRegistered()
        {
            using(var context = new SimulationContext(isDefaultContextForProcess: true)){
                var process = new Process();

                Assert.IsNotNull(process.SimulationState);
                Assert.IsTrue(process.SimulationState.IsActive);

                var registeredProcesses = context.GetByType<Process>();
                Assert.IsTrue(registeredProcesses.Contains(process));
            }
        }
Exemplo n.º 3
0
        public void MoveToTimePeriod_TimePeriodSpecified_ProcessCollectionsInitialised()
        {
            using (var context = new SimulationContext(isDefaultContextForProcess: true)){
                var process1 = new Process();
                var process2 = new Process();
                var process3 = new Process();

                var processes = new List<Process>(){
                    process1,
                    process2,
                    process3
                };

                Assert.AreEqual(0, context.TimePeriod);
                context.MoveToTimePeriod(1);
                Assert.AreEqual(1, context.TimePeriod);

                Assert.AreEqual(processes.Count, context.ActiveProcesses.Count);
                foreach(var process in processes){
                    Assert.IsTrue(context.ActiveProcesses.Contains(process));
                    Assert.IsTrue(context.ProcessesRemainingThisTimePeriod.Contains(process));
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Simulate a single process at the current time period
        /// </summary>
        /// <param name='process'>
        /// The process to be simulated
        /// </param>
        /// <param name='nextTimePeriod'>
        /// The next time period in which this process should be simulated (if known)
        /// </param>
        private void SimulateProcessAtTimePeriod(Process process, ref long? nextTimePeriod)
        {
            bool shouldMoveNext = true;
            while (shouldMoveNext && !Context.IsSimulationTerminating) {
                InstructionBase currentInstruction = process.SimulationState.InstructionEnumerator.Current;
                bool didComplete = false;

                if (currentInstruction != null) {
                    long? nextTimePeriodCheck = null;

                    if (process.SimulationState.IsInterrupted) {
                        currentInstruction.Interrupt(Context);
                    }
                    else if (currentInstruction.IsInterrupted || currentInstruction.IsCompleted){
                        // no further processing of the instruction is needed
                    }
                    else if (currentInstruction.CanComplete(Context, out nextTimePeriodCheck)) {
                        currentInstruction.Complete(Context);
                        didComplete = true;
                    }
                    else {
                        shouldMoveNext = false;
                    }

                    if (!didComplete){
                        if (nextTimePeriodCheck != null && (nextTimePeriod == null || nextTimePeriodCheck < nextTimePeriod)) {
                            nextTimePeriod = nextTimePeriodCheck.Value;
                        }
                    }
                }

                if (shouldMoveNext) {
                    bool couldMoveNext = process.SimulationState.InstructionEnumerator.MoveNext();
                    if (couldMoveNext){
                        process.SimulationState.InstructionEnumerator.Current.RaisedInTimePeriod = Context.TimePeriod;
                    }
                    else {
                        shouldMoveNext = false;
                        process.SimulationState.IsComplete = true;
                        Context.ActiveProcesses.Remove(process);
                    }
                }
            }

            if (process.SimulationState.IsInterrupted)
            {
                // ensure the process is not in the interrupted state
                process.SimulationState.IsInterrupted = false;
            }
        }