private IEnumerable <Event> TestPreemptiveResourceTimeoutB(Environment env, PreemptiveResource res, int prio) { using (var req = res.Request(priority: prio, preempt: true)) { yield return(req); } }
private IEnumerable <Event> ShavingJob(Simulation env, PreemptiveResource shaver) { while (true) { // Start a new job var doneIn = Environment.RandNormal(PtMeanShaving, PtSigmaShaving); while (doneIn > TimeSpan.Zero) { // Retry the job until it is done. // It's priority is lower than that of milking using (var req = shaver.Request(priority: 2)) { yield return(req); var start = env.Now; yield return(env.Timeout(doneIn)); if (env.ActiveProcess.HandleFault()) { doneIn -= env.Now - start; } else { doneIn = TimeSpan.Zero; } } } } }
private IEnumerable <Event> OtherJobs(Simulation env, PreemptiveResource repairman) { // The repairman's other (unimportant) job. while (true) { // Start a new job var doneIn = JobDuration; while (doneIn > TimeSpan.Zero) { // Retry the job until it is done. // It's priority is lower than that of machine repairs. using (var req = repairman.Request(priority: 2)) { yield return(req); var start = env.Now; yield return(env.Timeout(doneIn)); if (env.ActiveProcess.HandleFault()) { doneIn -= env.Now - start; } else { doneIn = TimeSpan.Zero; } } } } }
SimEvents ResourceOccupier(PreemptiveResource resource, double timeout) { using (var req = resource.Request()) { yield return(req); yield return(Env.Timeout(timeout)); } }
SimEvents ResourceRequester(PreemptiveResource resource, ILinkedQueue <SimProcess> completed) { using (var req = resource.Request()) { yield return(req); completed.Enqueue(Env.ActiveProcess); yield return(Env.Timeout(10)); } }
SimEvents ResourceRequester_WithCount(PreemptiveResource resource, int expectedCount) { using (var req = resource.Request()) { yield return(req); Assert.AreEqual(expectedCount, resource.Count); yield return(Env.Timeout(10)); } }
SimEvents WithTimeout_PEM_A(PreemptiveResource resource, double priority) { using (var req = resource.Request(priority)) { yield return(req); Assert.True(Env.ActiveProcess.Preempted()); yield return(Env.Event()); } }
SimEvents ResourceRequester_WithPriority(PreemptiveResource resource, ILinkedQueue <SimProcess> completed, double priority) { using (var req = resource.Request(priority, false)) { yield return(req); completed.Enqueue(Env.ActiveProcess); yield return(Env.Timeout(10)); } }
SimEvents ResourceRequester_Occupied_WithTimeout(PreemptiveResource resource, double timeout) { var req = resource.Request(); var cond = req.Or(Env.Timeout(timeout)); yield return(cond); // Resource is occupied Assert.False(cond.Ev1.Succeeded); Assert.True(cond.Ev2.Succeeded); }
private IEnumerable <Event> TestPreemptiveResourceTimeoutA(Environment env, PreemptiveResource res, int prio) { using (var req = res.Request(priority: prio, preempt: true)) { yield return(req); Assert.IsTrue(env.ActiveProcess.HandleFault()); yield return(env.Timeout(TimeSpan.FromSeconds(1))); Assert.IsFalse(env.ActiveProcess.HandleFault()); } }
SimEvents Simple_PEM(int id, PreemptiveResource res, double priority, ICollection <Tuple <double, int, PreemptionInfo> > log) { using (var req = res.Request(priority)) { yield return(req); yield return(Env.Timeout(5)); PreemptionInfo info; log.Add(Env.ActiveProcess.Preempted(out info) ? Tuple.Create(Env.Now, id, info) : Tuple.Create(Env.Now, id, (PreemptionInfo)null)); } }
private IEnumerable <Event> TestMixedPreemtion(int id, Environment env, PreemptiveResource res, int delay, int prio, bool preempt, List <Tuple <int, int> > log) { yield return(env.Timeout(TimeSpan.FromSeconds(delay))); using (var req = res.Request(priority: prio, preempt: preempt)) { yield return(req); yield return(env.Timeout(TimeSpan.FromSeconds(5))); if (!env.ActiveProcess.HandleFault()) { log.Add(Tuple.Create(env.Now.Second, id)); } } }
private IEnumerable <Event> TestPreemtiveResource(int id, Environment env, PreemptiveResource res, int delay, int prio, Dictionary <DateTime, int> log) { yield return(env.Timeout(TimeSpan.FromSeconds(delay))); using (var req = res.Request(priority: prio, preempt: true)) { yield return(req); yield return(env.Timeout(TimeSpan.FromSeconds(5))); if (!env.ActiveProcess.HandleFault()) { log.Add(env.Now, id); } } }
/// <summary> /// A daily event that indicates the yak can be milked. /// </summary> /// <returns>Scheduled Event</returns> private IEnumerable <Event> LifeCycle(PreemptiveResource milker) { while (true) { yield return(Environment.Timeout(TimeSpan.FromDays(1))); // Ready to milk the Cow // Request a milker. The Cow will be milked as soon as the milker is available. var timeToMilk = Environment.RandNormal(PtMeanMilking, PtSigmaMilking); using (var req = milker.Request(priority: 1, preempt: false)) { yield return(req); // Milker available, start milking the yak. var time = Environment.RandNormal(PtMeanMilking, PtSigmaMilking); TotalMilkingTime += time; yield return(Environment.Timeout(time)); // The yak is milked. ProductionData.Add(new Milk() { Animal = new Animal() { Code = "", Name = Name }, Composition = new MilkComposition() { Fats = Math.Round(Environment.RandUniform(0.055, 0.072), precision), Lactose = Math.Round(Environment.RandUniform(0.045, 0.05), precision), Minerals = Math.Round(Environment.RandUniform(0.08, 0.09), precision), Protein = Math.Round(Environment.RandUniform(0.049, 0.053), precision), Solids = Math.Round(Environment.RandUniform(0.169, 0.177), precision), }, Kg = Math.Round(Environment.RandUniform(0.3, 2.9), 2), ProductionInfo = new ProductionInfo() { DateOfProduction = Environment.Now, ProductionTime = time, Costs = hourlySalary / 60 * time.Minutes } });; Milked++; } } }
private IEnumerable <Event> Working(PreemptiveResource repairman) { /* * Produce parts as long as the simulation runs. * * While making a part, the machine may break multiple times. * Request a repairman when this happens. */ while (true) { // Start making a new part var doneIn = Environment.RandNormal(PtMean, PtSigma); while (doneIn > TimeSpan.Zero) { // Working on the part var start = Environment.Now; yield return(Environment.Timeout(doneIn)); if (Environment.ActiveProcess.HandleFault()) { Broken = true; doneIn -= Environment.Now - start; // How much time left? // Request a repairman. This will preempt its "other_job". using (var req = repairman.Request(priority: 1, preempt: true)) { yield return(req); yield return(Environment.Timeout(RepairTime)); } Broken = false; } else { doneIn = TimeSpan.Zero; // Set to 0 to exit while loop. } } // Part is done. PartsMade++; } }
private IEnumerable<Event> TestMixedPreemtion(int id, Environment env, PreemptiveResource res, int delay, int prio, bool preempt, List<Tuple<int, int>> log) { yield return env.Timeout(TimeSpan.FromSeconds(delay)); using (var req = res.Request(priority: prio, preempt: preempt)) { yield return req; yield return env.Timeout(TimeSpan.FromSeconds(5)); if (!env.ActiveProcess.HandleFault()) log.Add(Tuple.Create(env.Now.Second, id)); } }
private IEnumerable<Event> TestPreemptiveResourceTimeoutB(Environment env, PreemptiveResource res, int prio) { using (var req = res.Request(priority: prio, preempt: true)) { yield return req; } }
private IEnumerable<Event> TestPreemptiveResourceTimeoutA(Environment env, PreemptiveResource res, int prio) { using (var req = res.Request(priority: prio, preempt: true)) { yield return req; Assert.IsTrue(env.ActiveProcess.HandleFault()); yield return env.Timeout(TimeSpan.FromSeconds(1)); Assert.IsFalse(env.ActiveProcess.HandleFault()); } }
private IEnumerable<Event> TestPreemtiveResource(int id, Environment env, PreemptiveResource res, int delay, int prio, Dictionary<DateTime, int> log) { yield return env.Timeout(TimeSpan.FromSeconds(delay)); using (var req = res.Request(priority: prio, preempt: true)) { yield return req; yield return env.Timeout(TimeSpan.FromSeconds(5)); if (!env.ActiveProcess.HandleFault()) log.Add(env.Now, id); } }
SimEvents WithTimeout_PEM_B(PreemptiveResource resource, double priority) { using (var req = resource.Request(priority)) { yield return(req); } }