public void Simulate_ActivitySpecified_ActivityFiredAtTheAppropriateTime()
        {
            using(var context = new SimulationContext(isDefaultContextForProcess: true)){

                var notification = new TestNotification();
                var activity = new TestActivity(new List<InstructionBase>() { new RaiseNotificationInstruction<TestNotification>(notification) });
                long waitTime = 10;

                var process = new ActivityHostProcess(activity, waitTime);

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

                var registeredProcesses = context.GetByType<Process>();
                Assert.IsTrue(registeredProcesses.Contains(process));

                var enumerator = process.Simulate();

                bool couldMove = enumerator.MoveNext();
                Assert.IsTrue(couldMove);

                // first instruction should be the wait instruction
                Assert.IsTrue(enumerator.Current is WaitInstruction);
                Assert.AreEqual(waitTime, ((WaitInstruction)enumerator.Current).NumberOfPeriodsToWait);

                couldMove = enumerator.MoveNext();
                Assert.IsTrue(couldMove);

                Assert.IsTrue(enumerator.Current is RaiseNotificationInstruction<TestNotification>);
                Assert.AreEqual(notification, ((RaiseNotificationInstruction<TestNotification>)enumerator.Current).Notification);

                couldMove = enumerator.MoveNext();
                Assert.IsFalse(couldMove);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Complete the instruction.  For this instruction type, allocates the requested resources
        /// </summary>
        /// <param name='context'>
        /// Context providing state information for the current simulation.
        /// </param>
        public override void Complete(SimulationContext context)
        {
            base.Complete(context);

            IEnumerable <TResource> resources = null;

            if (_resourcePriorityFunction == null)
            {
                resources = context.GetByType <TResource>()
                            .Where(_resourceMatchFunction);
            }
            else
            {
                resources = context.GetByType <TResource>()
                            .Where(_resourceMatchFunction)
                            .OrderBy(_resourcePriorityFunction);
            }

            Allocations = new List <KeyValuePair <TResource, int> >();

            var allocated = 0;

            foreach (TResource resource in resources)
            {
                var available = resource.Capacity - resource.Allocated;

                if (available > 0)
                {
                    var amountToAllocate = Math.Min(available, NumberRequested - allocated);

                    Allocations.Add(new KeyValuePair <TResource, int>(resource, amountToAllocate));
                    resource.Allocated += amountToAllocate;
                    allocated          += amountToAllocate;

                    if (allocated == NumberRequested)
                    {
                        break;
                    }
                }
            }

            IsAllocated = true;
            IsReleased  = false;

            CompletedAtTimePeriod = context.TimePeriod;
        }
Exemplo n.º 3
0
        public void Constructor_SimulationContextExists_ResourceRegistered()
        {
            using(var context = new SimulationContext(isDefaultContextForProcess: true)){
                var element = new TestElement();

                var registeredElements = context.GetByType<TestElement>();
                Assert.IsTrue(registeredElements.Contains(element));
            }
        }
        public void Constructor_SimulationContextExists_ResourceRegistered()
        {
            using (var context = new SimulationContext(isDefaultContextForProcess: true)){
                var element = new TestElement();

                var registeredElements = context.GetByType <TestElement>();
                Assert.IsTrue(registeredElements.Contains(element));
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Determines whether this instruction can complete in the current time period
        /// </summary>
        /// <returns>
        /// <c>true</c> if this instance can complete.
        /// </returns>
        /// <param name='context'>
        /// Context providing state information for the current simulation
        /// </param>
        /// <param name='skipFurtherChecksUntilTimePeriod'>
        /// Output parameter used to specify a time period at which this instruction should be checked again.  This should be left null if it is not possible to determine when this instruction can complete.
        /// </param>
        public override bool CanComplete(SimulationContext context, out long?skipFurtherChecksUntilTimePeriod)
        {
            skipFurtherChecksUntilTimePeriod = null;

            // build a set or resources for possible allocation, filtering and prioritising as appropriate
            IEnumerable <TResource> resources = null;

            if (_resourcePriorityFunction == null)
            {
                resources = context.GetByType <TResource>()
                            .Where(_resourceMatchFunction);
            }
            else
            {
                resources = context.GetByType <TResource>()
                            .Where(_resourceMatchFunction)
                            .OrderBy(_resourcePriorityFunction);
            }

            // check if the are enough resources available for allocation
            var enoughAvailable = false;
            var available       = 0;

            foreach (TResource resource in resources)
            {
                var stillAvailable = resource.Capacity - resource.Allocated;

                if (stillAvailable > 0)
                {
                    available += stillAvailable;
                }

                if (available >= NumberRequested)
                {
                    enoughAvailable = true;
                    break;
                }
            }

            // if there are currently enough resources available, the instruction can complete
            return(enoughAvailable);
        }
Exemplo n.º 6
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.º 7
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.º 8
0
        public void Constructor_SimulationContextExists_ResourceRegistered()
        {
            using (var context = new SimulationContext(isDefaultContextForProcess: true)){
                int capacity = 10;
                var resource = new Resource(capacity);

                Assert.AreEqual(0, resource.Allocated);
                Assert.AreEqual(capacity, resource.Capacity);

                var registeredResources = context.GetByType <Resource>();
                Assert.IsTrue(registeredResources.Contains(resource));
            }
        }
Exemplo n.º 9
0
        public void Constructor_SimulationContextExists_ResourceRegistered()
        {
            using(var context = new SimulationContext(isDefaultContextForProcess: true)){
                int capacity = 10;
                var resource = new Resource(capacity);

                Assert.AreEqual(0, resource.Allocated);
                Assert.AreEqual(capacity, resource.Capacity);

                var registeredResources = context.GetByType<Resource>();
                Assert.IsTrue(registeredResources.Contains(resource));
            }
        }
Exemplo n.º 10
0
        public void RegisterAndGet_ObjectRegistered_ObjectCanBeRetrieved()
        {
            var context = new SimulationContext(isDefaultContextForProcess: true);

            TestResource resource1 = new TestResource(1);
            TestResource resource2 = new TestResource(1);

            context.Register<TestResource>(resource1);
            context.Register<TestResource>(resource2);

            Assert.AreEqual(resource1, context.GetByKey<TestResource>(resource1.Key));
            Assert.AreEqual(resource2, context.GetByKey<TestResource>(resource2.Key));

            var registeredObjects = context.GetByType<TestResource>();
            Assert.IsTrue(registeredObjects.Contains(resource1));
            Assert.IsTrue(registeredObjects.Contains(resource2));
        }
Exemplo n.º 11
0
        public void RegisterAndGet_ObjectRegistered_ObjectCanBeRetrieved()
        {
            using (var context = new SimulationContext())
            {
                TestResource resource1 = new TestResource(context, 1);
                TestResource resource2 = new TestResource(context, 1);

                context.Register <TestResource>(resource1);
                context.Register <TestResource>(resource2);

                Assert.AreEqual(resource1, context.GetByKey <TestResource>(resource1.Key));
                Assert.AreEqual(resource2, context.GetByKey <TestResource>(resource2.Key));

                var registeredObjects = context.GetByType <TestResource>();
                Assert.IsTrue(registeredObjects.Contains(resource1));
                Assert.IsTrue(registeredObjects.Contains(resource2));
            }
        }
Exemplo n.º 12
0
        public void RegisterAndGet_ObjectRegistered_ObjectCanBeRetrieved()
        {
            var context = new SimulationContext(isDefaultContextForProcess: true);

            TestResource resource1 = new TestResource(1);
            TestResource resource2 = new TestResource(1);

            context.Register <TestResource>(resource1);
            context.Register <TestResource>(resource2);

            Assert.AreEqual(resource1, context.GetByKey <TestResource>(resource1.Key));
            Assert.AreEqual(resource2, context.GetByKey <TestResource>(resource2.Key));

            var registeredObjects = context.GetByType <TestResource>();

            Assert.IsTrue(registeredObjects.Contains(resource1));
            Assert.IsTrue(registeredObjects.Contains(resource2));
        }
Exemplo n.º 13
0
        public void Simulate_ActivitySpecified_ActivityFiredAtTheAppropriateTime()
        {
            using (var context = new SimulationContext()){
                var notification = new TestNotification();
                var activity     = new TestActivity(new List <InstructionBase>()
                {
                    new RaiseNotificationInstruction <TestNotification>(notification)
                });
                long waitTime = 10;

                var process = new ActivityHostProcess(context, activity, waitTime);

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

                var registeredProcesses = context.GetByType <Process>();
                Assert.IsTrue(registeredProcesses.Contains(process));

                var enumerator = process.Simulate().GetEnumerator();

                bool couldMove = enumerator.MoveNext();
                Assert.IsTrue(couldMove);

                // first instruction should be the wait instruction
                Assert.IsTrue(enumerator.Current is WaitInstruction);
                Assert.AreEqual(waitTime, ((WaitInstruction)enumerator.Current).NumberOfPeriodsToWait);

                couldMove = enumerator.MoveNext();
                Assert.IsTrue(couldMove);

                Assert.IsTrue(enumerator.Current is RaiseNotificationInstruction <TestNotification>);
                Assert.AreEqual(notification, ((RaiseNotificationInstruction <TestNotification>)enumerator.Current).Notification);

                couldMove = enumerator.MoveNext();
                Assert.IsFalse(couldMove);
            }
        }