public void MoveUp_SecondJobInQueue_MoveToThird()
        {
            var mockJob  = new JobDetails("TestName", 1000);
            var mockJob2 = new JobDetails("TestName2", 2000);
            var mockJob3 = new JobDetails("TestName3", 2000);

            IEnumerable <JobDetails> initialQueue = new List <JobDetails>()
            {
                mockJob,
                mockJob2,
                mockJob3
            };

            IDynamicQueue <JobDetails> queue = new JobQueue(initialQueue);

            int index = queue.MoveDown(mockJob2);

            Assert.AreEqual(2, index);
            JobDetails topJob = queue.Dequeue();

            topJob = queue.Dequeue();
            Assert.AreEqual(topJob, mockJob3);
            topJob = queue.Dequeue();
            Assert.AreEqual(topJob, mockJob2);
        }
        public void Remove_ThirdJobInQueue_RemoveFromQueue()
        {
            var mockJob  = new JobDetails("TestName", 1000);
            var mockJob2 = new JobDetails("TestName2", 2000);
            var mockJob3 = new JobDetails("TestName3", 2000);

            IEnumerable <JobDetails> initialQueue = new List <JobDetails>()
            {
                mockJob,
                mockJob2,
                mockJob3
            };

            IDynamicQueue <JobDetails> queue = new JobQueue(initialQueue);

            bool success = queue.Remove(mockJob3);

            Assert.IsTrue(success);
            JobDetails topJob = queue.Dequeue();

            Assert.AreEqual(topJob, mockJob);
            topJob = queue.Dequeue();
            Assert.AreEqual(topJob, mockJob2);
            topJob = queue.Peek();
            Assert.IsNull(topJob);
        }
Esempio n. 3
0
        public void TestDequeue_MultipleJobs()
        {
            JobQueue       queue = new JobQueue();
            IJobDefinition d     = new DudDefinition();
            JobRequest     r     = new JobRequest(d);
            JobTicket      t     = new JobTicket(r, new DudHandler());

            queue.Enqueue(t);
            JobRequest r2 = new JobRequest(d);
            JobTicket  t2 = new JobTicket(r2, new DudHandler());

            queue.Enqueue(t2);

            IJobTicket ta = queue.Dequeue();

            Assert.AreEqual(t, ta);
            Assert.IsTrue(queue.HasPendingJobs);
            Assert.AreEqual(1, queue.NumberOfJobs);

            IJobTicket tb = queue.Dequeue();

            Assert.AreEqual(t2, tb);
            Assert.IsFalse(queue.HasPendingJobs);
            Assert.AreEqual(0, queue.NumberOfJobs);
        }
Esempio n. 4
0
        public override ThinkResult TryIssueJobPackage(Pawn pawn, JobIssueParams jobParams)
        {
            JobQueue jobQueue = pawn.jobs.jobQueue;

            if (pawn.Downed || jobQueue.AnyCanBeginNow(pawn, inBedOnly))
            {
                while (jobQueue.Count > 0 && !jobQueue.Peek().job.CanBeginNow(pawn, inBedOnly))
                {
                    QueuedJob queuedJob = jobQueue.Dequeue();
                    pawn.ClearReservationsForJob(queuedJob.job);
                    if (pawn.jobs.debugLog)
                    {
                        pawn.jobs.DebugLogEvent("   Throwing away queued job that I cannot begin now: " + queuedJob.job);
                    }
                }
            }
            if (jobQueue.Count > 0 && jobQueue.Peek().job.CanBeginNow(pawn, inBedOnly))
            {
                QueuedJob queuedJob2 = jobQueue.Dequeue();
                if (pawn.jobs.debugLog)
                {
                    pawn.jobs.DebugLogEvent("   Returning queued job: " + queuedJob2.job);
                }
                return(new ThinkResult(queuedJob2.job, this, queuedJob2.tag, fromQueue: true));
            }
            return(ThinkResult.NoJob);
        }
Esempio n. 5
0
        public void TestDequeue_NoJobs()
        {
            JobQueue   queue = new JobQueue();
            IJobTicket req   = queue.Dequeue();

            Assert.IsNull(req);
        }
Esempio n. 6
0
        private void LookForJob()
        {
            if (JobQueue.Peek() != null && Skills.Contains(JobQueue.Peek().JobType))
            {
                _job = JobQueue.Dequeue();
            }

            // No job
            if (_job == null)
            {
                return;
            }

            DestinationTile = _job.Tile;

            _job.JobStopped += OnJobStopped;

            _aStar = new AStar(CurrentTile, DestinationTile);

            // The first tile can be ignored, because that's where the minion currently is in
            _nextTile = _aStar.Pop();

            // No path was found
            if (_aStar.Length() == 0)
            {
                AbandonJob();
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Simulate this process
        /// </summary>
        public override IEnumerator <InstructionBase> Simulate()
        {
            // while the simulation is running
            while (true)
            {
                // check if the queue for this machine is empty
                if (JobQueue.Count == 0)
                {
                    // if it is, wait until there is something in the queue
                    yield return(new WaitConditionInstruction(() => JobQueue.Count > 0));
                }
                else
                {
                    // take a job from the queue
                    var jobToProcess = JobQueue.Dequeue();

                    // simulate processing the job
                    // which takes time
                    yield return(new WaitInstruction(jobToProcess.ProcessingTimeRequiredByJobQueue[JobQueue]));

                    // use the reliability indicator to determine if the machine is broken down
                    if (CheckForRandomBreakdown())
                    {
                        BreakdownCount++;
                        // the machine has broken down
                        // add the job it was processing back to the queue
                        JobQueue.Enqueue(jobToProcess);

                        // obtain a repair person
                        var allocateInstruction = new AllocateInstruction <RepairPerson>(1);
                        yield return(allocateInstruction);

                        // and wait for the machine to be fixed
                        yield return(new WaitInstruction(RepairTimeRequired));

                        // then release the repair person resource
                        yield return(new ReleaseInstruction <RepairPerson>(allocateInstruction));
                    }
                    else
                    {
                        ProcessedCount++;
                        // record the fact that the job has been processed by this machine type
                        jobToProcess.ProcessingTimeRequiredByJobQueue.Remove(JobQueue);

                        // if the job still requires other processing
                        if (jobToProcess.RequiresMoreWork)
                        {
                            // add it to the next queue
                            jobToProcess.ProcessingTimeRequiredByJobQueue.Keys.First().Enqueue(jobToProcess);
                        }
                        else
                        {
                            // otherwise remove it from the all unprocessed jobs list
                            _unprocessedJobsList.Remove(jobToProcess);
                        }
                    }
                }
            }
        }
        public void Dequeue_EmptyInitialQueue_ReturnNull()
        {
            IDynamicQueue <JobDetails> queue = new JobQueue(null);

            JobDetails topJob = queue.Dequeue();

            Assert.IsNull(topJob);
        }
        public void Dequeue_NonEmptyInitialQueue_ReturnCorrectJobsInOrder()
        {
            var mockJob  = new JobDetails("TestName", 1000);
            var mockJob2 = new JobDetails("TestName2", 2000);
            IEnumerable <JobDetails> initialQueue = new List <JobDetails>()
            {
                mockJob,
                mockJob2
            };
            //

            IDynamicQueue <JobDetails> queue = new JobQueue(initialQueue);

            JobDetails topJob = queue.Dequeue();

            Assert.AreEqual(mockJob, topJob);
            topJob = queue.Dequeue();
            Assert.AreEqual(mockJob2, topJob);
        }
Esempio n. 10
0
 private Task DequeueJob()
 {
     return(Task.Run(() =>
     {
         //lock (JobQueue.SyncRoot)
         if (JobQueue.Count != 0)
         {
             JobQueue.Dequeue();
         }
     }));
 }
        public void Dequeue_AfterEnqueue_ReturnCorrectJobsInOrder()
        {
            var mockJob  = new JobDetails("TestName", 1000);
            var mockJob2 = new JobDetails("TestName2", 2000);
            var mockJob3 = new JobDetails("TestName3", 2000);

            //

            IDynamicQueue <JobDetails> queue = new JobQueue(null);

            queue.Enqueue(mockJob);
            queue.Enqueue(mockJob2);
            queue.Enqueue(mockJob3);

            JobDetails topJob = queue.Dequeue();

            Assert.AreEqual(mockJob, topJob);
            topJob = queue.Dequeue();
            Assert.AreEqual(mockJob2, topJob);
            topJob = queue.Dequeue();
            Assert.AreEqual(mockJob3, topJob);
        }
Esempio n. 12
0
        public static bool TryIssueJobPackage(ThinkNode_QueuedJob __instance, ref ThinkResult __result, Pawn pawn, JobIssueParams jobParams)
        {
            JobQueue jobQueue = pawn.jobs.jobQueue;

            if (pawn.Downed || jobQueue.AnyCanBeginNow(pawn, __instance.inBedOnly))
            {
                while (jobQueue.Count > 0 && !jobQueue.Peek().job.CanBeginNow(pawn, __instance.inBedOnly))
                {
                    QueuedJob queuedJob = jobQueue.Dequeue();
                    pawn.ClearReservationsForJob(queuedJob.job);
                    if (pawn.jobs.debugLog)
                    {
                        pawn.jobs.DebugLogEvent("   Throwing away queued job that I cannot begin now: " + queuedJob.job);
                    }
                }
            }

            if (jobQueue.Count > 0)
            {
                QueuedJob jqpeek = jobQueue.Peek();
                if (jqpeek != null)
                {
                    if (jqpeek.job.CanBeginNow(pawn, __instance.inBedOnly))
                    {
                        QueuedJob queuedJob2 = jobQueue.Dequeue();
                        if (pawn.jobs.debugLog)
                        {
                            pawn.jobs.DebugLogEvent("   Returning queued job: " + queuedJob2.job);
                        }

                        __result = new ThinkResult(queuedJob2.job, __instance, queuedJob2.tag, fromQueue: true);
                        return(false);
                    }
                }
            }

            __result = ThinkResult.NoJob;
            return(false);
        }
Esempio n. 13
0
        public void TestDequeue_OneJob()
        {
            JobQueue queue = new JobQueue();

            IJobDefinition d = new DudDefinition();
            JobRequest     r = new JobRequest(d);
            JobTicket      t = new JobTicket(r, new DudHandler());

            queue.Enqueue(t);

            IJobTicket t1 = queue.Dequeue();

            Assert.AreEqual(t, t1);
            Assert.AreEqual(0, queue.NumberOfJobs);
            Assert.IsFalse(queue.HasPendingJobs);
        }
Esempio n. 14
0
        /// <summary>
        /// Funkcja wywoływana przy kazdym odswierzeniu okranu
        /// </summary>
        /// <param name="sender">Obiekt wysylajacy zdazenie</param>
        /// <param name="e">Argumenty zdarzenia</param>
        public void UpdateTime(object sender, UpdateEventArgs e)
        {
            for (int index = 0; index < colonists_.Count;)
            {
                if (colonists_[index].HP.Value > 0f)
                {
                    index++;
                    continue;
                }

                colonists_[index].Die();
                colonists_.Remove(colonists_[index]);
            }

            foreach (Men colonist in Colonist.Where(c => c.Job == null))
            {
                string stateSleep = MakeDecision(colonist.RestF, colonist.Laziness);
                string stateWork  = MakeDecision(colonist.HP, colonist.Laziness);

                if (stateSleep == "Sleep")
                {
                    Bed bed = Constructs.Where(c => c is Bed)
                              .Cast <Bed>()
                              .FirstOrDefault(b => b.State == Construct.Status.Done && b.IsFree);

                    colonist.Job = new SleepJob(colonist, bed);
                }
                else if (stateWork == "Work" && JobQueue.Count > 0)
                {
                    colonist.Job = JobQueue.Dequeue();
                }
                else if (stateWork == "RestF" || colonist.HP != colonist.HP.MaxHP)
                {
                    colonist.Job = colonist.Rest;
                }
            }
        }
Esempio n. 15
0
        private static void WorkThreadOutOfOrder()
        {
            var frameCount = exitChildFrameCount;

            while (true)
            {
                var newJob = jobOutOfOrder.Dequeue();
                if (newJob != null)
                {
                    var jobStatus = newJob.Execute();
                    if (jobStatus != RuntimeJob.JobStatus.Working)
                    {
                        jobFinished.Enqueue(newJob);
                    }
                    else
                    {
                        jobOutOfOrder.Enqueue(newJob);
                    }

                    frameCount = exitChildFrameCount;
                }
                else
                {
                    frameCount--;
                }

                if (frameCount < 0)
                {
                    break;
                }
                else
                {
                    Thread.Sleep(waitMilliseconds);
                }
            }
        }
Esempio n. 16
0
        private static void DispatchThread()
        {
            var frameCount = exitMainFrameCount;

            while (true)
            {
                var tempJobs = new Queue <EditorJob>();
                jobEntering.Dequeue(tempJobs);

                var inOrderJobs    = new Queue <EditorJob>();
                var outOfOrderJobs = new Queue <EditorJob>();
                while (tempJobs.Count > 0)
                {
                    var job = tempJobs.Dequeue();
                    if (job.DispatchInOrder)
                    {
                        inOrderJobs.Enqueue(job);
                    }
                    else
                    {
                        outOfOrderJobs.Enqueue(job);
                    }
                }

                jobInOrder.Enqueue(inOrderJobs);
                jobOutOfOrder.Enqueue(outOfOrderJobs);

                if (inOrderJobs.Count > 0)
                {
                    StartInOrderThread();
                }

                if (outOfOrderJobs.Count > 0)
                {
                    StartOutOfOrderThreads();
                }

                jobFinished.Dequeue(tempJobs);
                jobExiting.Enqueue(tempJobs);

                bool resetFrameCount = false;
                lock (inOrderLock)
                {
                    if (inOrderThread == null || !inOrderThread.IsAlive)
                    {
                        resetFrameCount = true;
                    }
                }

                lock (outOfOrderLock)
                {
                    if (outOfOrderThreads.Count > 0)
                    {
                        UnsafeCleanOutOfOrderThreads();
                        resetFrameCount = true;
                    }
                }

                if (resetFrameCount)
                {
                    frameCount = exitMainFrameCount;
                }
                else
                {
                    frameCount--;
                }

                if (frameCount < 0 || emergencyExit)
                {
                    break;
                }
                else
                {
                    Thread.Sleep(waitMilliseconds);
                }
            }

            mainThread = null;
        }