static IEnumerable <SimEvent> CondTester(SimEnvironment env) { var aProc = env.Process(AProcess(env)); var cond = env.AllOf(env.Timeout(5, "VAL_T"), aProc); yield return(cond); Console.WriteLine("ALL: {0}", cond.Value.Select(x => x.Value).Aggregate((s1, s2) => s1 + ", " + s2)); aProc = env.Process(AProcess(env)); cond = env.AnyOf(env.Timeout(5, "VAL_T"), aProc); yield return(cond); Console.WriteLine("ANY: {0}", cond.Value.Select(x => x.Value).Aggregate((s1, s2) => s1 + ", " + s2)); aProc = env.Process(AProcess(env)); var aTime = env.Timeout(5, "VAL_T"); ConditionEval <Timeout <string>, SimProcess> pred = c => c.Ev1.Succeeded && c.Ev2.Succeeded && c.Ev1.Value.Equals("VAL_T") && c.Ev2.Value.Equals("VAL_P"); cond = env.Condition(aTime, aProc, pred); yield return(cond); Console.WriteLine("CUSTOM: {0}", cond.Value.Select(x => x.Value).Aggregate((s1, s2) => s1 + ", " + s2)); }
/// <summary> /// Customer arrives, is served and leaves. /// </summary> private static SimEvents Customer(SimEnvironment env, string name, Resource counter, double timeInBank) { var arrive = env.Now; Console.WriteLine("{0:00.0000} {1}: Here I am", arrive, name); using (var req = counter.Request()) { var patience = env.Random.NextDouble(MinPatience, MaxPatience); // Wait for the counter or abort at the end of our tether yield return(req.Or(env.Timeout(patience))); var wait = env.Now - arrive; if (req.Succeeded) { // We got to the counter Console.WriteLine("{0:00.0000} {1}: Waited {2:0.000}", env.Now, name, wait); var tib = env.Random.Exponential(1.0 / timeInBank); yield return(env.Timeout(tib)); Console.WriteLine("{0:00.0000} {1}: Finished", env.Now, name); } else { // We reneged Console.WriteLine("{0:00.0000} {1}: RENEGED after {2:0.000}", env.Now, name, wait); } } }
protected SimEvents Interrupter(SimProcess victim, String value, Double timeout = 0) { while (true) { victim.Interrupt(value); yield return(Env.Timeout(timeout)); } }
static IEnumerable <SimEvent> Process(SimEnvironment env) { var a = env.Timeout(3, "A"); var b = env.Timeout(5, "B"); var c = env.Timeout(7, "C"); var cond = a.And(b.Or(c)); yield return(cond); Console.WriteLine(env.Now); foreach (var e in cond.Value) { Console.WriteLine(e.Value); } }
static IEnumerable <SimEvent> Worker(SimEnvironment env) { Console.WriteLine("{0}: Carico la macchina...", env.Now); yield return(env.Timeout(LoadTime)); env.Exit(env.Random.Choice(Tasks)); }
static SimEvents Go(SimEnvironment env, int id) { Console.WriteLine("{0} {1} Starting", env.Now, id); yield return(env.Timeout(100)); Console.WriteLine("{0} {1} Arrived", env.Now, id); }
static Timeout <string> NewTarget(SimEnvironment env) { var delay = env.Random.DiscreteUniform(1, 20); var target = env.Random.Choice(Targets); return(env.Timeout(delay, target)); }
/// <summary> /// A process which consumes messages. /// </summary> static IEvents MessageConsumer(string name, SimEnvironment env, Store <Message> inPipe) { while (true) { // Gets event for message pipe. var getEv = inPipe.Get(); yield return(getEv); var msg = getEv.Value; if (msg.Timestamp < env.Now) { // If message was already put into pipe, then // MessageConsumer was late getting to it. // Depending on what is being modeled, this may, or may not, // have some significance. Console.WriteLine("LATE Getting Message: at time {0:.0}, \"{1}\" received message \"{2}\"", env.Now, name, msg.Content); } else { // MessageConsumer is synchronized with MessageGenerator. Console.WriteLine("At time {0:.0}, \"{1}\" received message \"{2}\"", env.Now, name, msg.Content); } // Process does some other work, which may result in missing messages. yield return(env.Timeout(Rand.Next(4, 8))); } }
public SimEvents Fight(SimEnvironment env, Player target) { Console.WriteLine("Three {0} attempting to escape!", target.Name); while (true) { if (_random.Next(0, 10) < 2) { // Checks for hit on player target.Damage += 1; // Hit! Increment damage to player if (target.Damage <= 5) // Target survives... { Console.WriteLine("Ha! {0} hit! Damage = {1}", target.Name, target.Damage); } else { target.LifeEvent.Succeed(); if (target.Lives - 1 == 0) { Console.WriteLine("No more {0} left!", target.Name); } else { Console.WriteLine("Now only {0} {1} left!", target.Lives - 1, target.Name); } } } yield return(env.Timeout(1)); } }
protected IEnumerable <SimEvent> WaitForSend(object packet, int packetLen) { var w = G.Latency + packetLen / G.Bandwidth; Logger.InfoFormat("Sending {0}, waiting for {1}", packet, Ts(w)); yield return(Env.Timeout(w)); }
static IEnumerable <SimEvent> Clock(SimEnvironment env, string name, double tick) { while (true) { Console.WriteLine("{0} {1:0.0}", name, env.Now); yield return(env.Timeout(tick)); } }
static SimEvents Person(SimEnvironment env, string name) { Console.WriteLine("Hi, I'm {0} and I will wait for {1} minutes!", name, WaitTime); // The process will stop its execution for WaitTime time units. yield return(env.Timeout(WaitTime)); Console.WriteLine("Ok, {0}'s wait has finished at {1}. Bye :)", name, env.Now); }
SimEvents Start() { while (true) { Console.WriteLine("Start parking and charging at {0}", _env.Now); const int chargeDuration = 5; // We may get interrupted while charging the battery yield return(_env.Process(Charge(chargeDuration))); if (_env.ActiveProcess.Interrupted()) { Console.WriteLine("Was interrupted. Hope, the battery is full enough ..."); } Console.WriteLine("Start driving at {0}", _env.Now); const int tripDuration = 2; yield return(_env.Timeout(tripDuration)); } }
/// <summary> /// Generates new cars that arrive at the gas station. /// </summary> private static SimEvents CarGenerator(SimEnvironment env, Resource gasStation, Container fuelPump) { foreach (var i in Enumerable.Range(0, int.MaxValue)) { yield return(env.Timeout(env.Random.Next(MinInter, MaxInter))); env.Process(Car("Car " + i, env, gasStation, fuelPump)); } }
static IEvents SayHello(SimEnvironment env) { while (true) { yield return(env.Timeout(2.1)); Console.WriteLine("Hello World at {0}!", env.Now); } }
static SimEvents Execute(SimEnvironment env, int finish) { while (env.Now < finish) { // Creates a new Customer and activates it (with default parameters). env.Process(Customer.Buy(env, "Marta")); Console.WriteLine("{0} {1}", env.Now, "Marta"); yield return(env.Timeout(10)); } }
private static IEvents SayHello(SimEnvironment env, char id) { while (true) { Console.WriteLine("{0} - Sleeping at {1}, real {2}...", id, env.Now, env.RealTime.WallClock.UtcNow); yield return(env.Timeout(3)); Console.WriteLine("{0} - Awake at {1}, real {2}", id, env.Now, env.RealTime.WallClock.UtcNow); } }
static IEnumerable <SimEvent> Spawner(SimEnvironment env, Tally rec) { var queue = Sim.Resource(env, 2); while (true) { env.Process(Person(env, queue, rec)); yield return(env.Timeout(env.Random.Next(2, 5))); } }
private const int MaxPatience = 3; // Max. customer patience /// <summary> /// Source generates customers randomly. /// </summary> private static SimEvents Source(SimEnvironment env, int number, double interval, Resource counter) { foreach (var i in Enumerable.Range(0, number)) { var n = string.Format("Customer{0:00}", i); var c = Customer(env, n, counter, timeInBank: 12.0); env.Process(c); var t = env.Random.Exponential(1.0 / interval); yield return(env.Timeout(t)); } }
static IEnumerable <SimEvent> Machine(SimEnvironment env) { while (true) { var worker = env.Process(Worker(env)); yield return(worker); Console.WriteLine("{0}: Eseguo il comando {1}", env.Now, worker.Value); yield return(env.Timeout(WorkTime)); } }
static IEnumerable <SimEvent> Person(SimEnvironment env, Resource queue, Tally rec) { using (var req = queue.Request()) { var startTime = env.Now; yield return(req); rec.Observe(env.Now - startTime); var workTime = env.Random.Next(3, 10); yield return(env.Timeout(workTime)); } }
static SimEvents PersonSpawner(SimEnvironment env, Resource postOffice) { while (true) { // We first start a new "Person" process... env.Process(Person(env, postOffice, env.Random.Choice(Names))); // And then we sleep for nearly SpawnFrequency minutes. var waitTime = env.Random.Exponential(1.0 / SpawnFrequency); yield return(env.Timeout(waitTime)); } }
/// <summary> /// Arrives at the gas station after a certain delay and refuels it. /// </summary> private static SimEvents TankTruck(SimEnvironment env, Container fuelPump) { yield return(env.Timeout(TankTruckTime)); Console.WriteLine("Tank truck arriving at time {0}", env.Now); var amount = fuelPump.Capacity - fuelPump.Level; Console.WriteLine("Tank truck refueling {0:.0} liters.", amount); yield return(fuelPump.Put(amount)); }
static IEnumerable <SimEvent> Person(SimEnvironment env, string gender, Resource toilet) { using (var req = toilet.Request()) { yield return(req); Console.WriteLine("{0:0.00}: {1} --> Bagno", env.Now, gender); yield return(env.Timeout(env.Random.Exponential(1.0 / AvgTimeInToilet))); Console.WriteLine("{0:0.00}: {1} <-- Bagno", env.Now, gender); } }
public static SimEvents Buy(SimEnvironment env, string name, double budget = 40) { Console.WriteLine("Here I am at the shop {0}", name); for (var i = 0; i < 4; ++i) { yield return(env.Timeout(5)); // Executed 4 times at intervals of 5 time units Console.WriteLine("I just bought something {0}", name); budget -= 10; } Console.WriteLine("All I have left is {0} I am going home {1}", budget, name); }
static IEnumerable <SimEvent> PressureSensor(SimEnvironment env, char tag, IRecorder pressureRecorder) { while (true) { yield return(env.Timeout(MonitoringTime)); // Read the pressure value and record it. var pressure = env.Random.Normal(1000, 50); pressureRecorder.Observe(pressure); Console.WriteLine($"Pressure sensor for machine {tag} has recorded a pressure of {pressure:.00} bar"); } }
static IEnumerable <SimEvent> TemperatureSensor(SimEnvironment env, char tag, IRecorder temperatureRecorder) { while (true) { yield return(env.Timeout(MonitoringTime)); // Read the temperature value and record it. var temperature = env.Random.Poisson(100); temperatureRecorder.Observe(temperature); Console.WriteLine($"Temperature sensor for machine {tag} has recorded a temperature of {temperature} °F"); } }
static IEnumerable <SimEvent> Process(SimEnvironment env) { var t1 = env.Timeout(3); var t2 = env.Timeout(7); var c1 = env.AllOf(t1, t2); yield return(c1); Console.WriteLine(env.Now); // 7 Console.WriteLine(c1.Value.Contains(t1)); // True Console.WriteLine(c1.Value.Contains(t2)); // True t1 = env.Timeout(3); t2 = env.Timeout(7); var c2 = env.Condition(t1, t2, c => c.Ev1.Succeeded || c.Ev2.Succeeded); yield return(c2); Console.WriteLine(env.Now); // 10 Console.WriteLine(c2.Value.Contains(t1)); // True Console.WriteLine(c2.Value.Contains(t2)); // False }
public static IEnumerable <SimEvent> Run(SimEnvironment env, Stats stats) { var currProc = Process.GetCurrentProcess(); while (true) { yield return(env.Timeout(G.MemoryRecordingFrequency)); currProc.Refresh(); var procMemInMb = currProc.WorkingSet64 / (1024 * 1024); stats.AddMemoryUsed(procMemInMb); } }
/// <summary> /// A process which randomly generates messages. /// </summary> static IEvents MessageGenerator(string name, SimEnvironment env, BroadcastPipe <Message> outPipe) { while (true) { // Waits for next transmission. yield return(env.Timeout(Rand.Next(6, 10))); // Messages are time stamped to later check // if the consumer was late getting them. var content = string.Format("{0} says hello at {1:.0}", name, env.Now); yield return(outPipe.Put(new Message(env.Now, content))); } }