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)); }
public static void Run() { // Sets up and starts simulation Console.WriteLine("Movie renege"); _env = Sim.Environment(RandomSeed); // Creates movie theater var counter = Sim.Resource(_env, 1); var titles = new[] { ".NET Unchained", "Kill Process", "Pulp Implementation" }; var movies = new List <MovieInfo>(titles.Length); movies.AddRange(titles.Select(t => new MovieInfo(t, _env.Event()))); _theater = new Theater(counter, movies); // Starts process and simulates _env.Process(CustomerArrivals()); _env.Run(until: SimTime); // Analysis and results foreach (var movie in movies.Where(m => m.SoldOut.Succeeded)) { Console.WriteLine("Movie \"{0}\" sold out {1:.0} minutes after ticket counter opening.", movie.Title, movie.WhenSoldOut); Console.WriteLine(" Number of people leaving queue when film sold out: {0}", movie.RenegerCount); } }
/// <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 IEnumerable <SimEvent> Machine(SimEnvironment env, char tag, IRecorder pressureRecorder, IRecorder temperatureRecorder) { Console.WriteLine($"Machine {tag} has powered up"); // Start sensor processes. env.Process(PressureSensor(env, tag, pressureRecorder)); env.Process(TemperatureSensor(env, tag, temperatureRecorder)); Console.WriteLine($"All sensors for machine {tag} are active"); for (var i = 1; ; ++i) { // Perform machine work. Console.WriteLine($"Machine {tag} has started work cycle {i}"); yield return(env.Timeout(WorkTime)); Console.WriteLine($"Machine {tag} has ended work cycle {i}"); } }
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)); } }
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 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)); } }
static IEnumerable <SimEvent> Process(SimEnvironment env) { var ev = env.Event <string>(); env.Process(EventFailer(ev)); yield return(ev); if (ev.Failed) { Console.WriteLine(ev.Value); } }
static IEnumerable <SimEvent> Proc(SimEnvironment env) { var ev1 = env.Event(); env.Process(DoSucceed(env, ev1)); yield return(ev1); if (ev1.Succeeded) { Console.WriteLine(ev1.Value); } var ev2 = env.Event(); env.Process(DoFail(env, ev2)); yield return(ev2); if (ev2.Failed) { Console.WriteLine(ev2.Value); } }
/// <summary> /// Periodically checks the level of the <see cref="fuelPump"/> and calls the tank truck if /// the level falls below a threshold. /// </summary> private static SimEvents GasStationControl(SimEnvironment env, Container fuelPump) { while (true) { if (fuelPump.Level / fuelPump.Capacity * 100 < Threshold) { // We need to call the tank truck now! Console.WriteLine("Calling tank truck at {0}", env.Now); // Waits for the tank truck to arrive and refuel the station. yield return(env.Process(TankTruck(env, fuelPump))); } yield return(env.Timeout(10)); // Checks every 10 seconds. } }
static IEnumerable <SimEvent> PersonGenerator(SimEnvironment env) { var womenToilet = Sim.Resource(env, 1); var menToilet = Sim.Resource(env, 1); var count = 0; while (true) { var rnd = env.Random.NextDouble(); var gender = ((rnd < 0.5) ? "Donna" : "Uomo") + count++; var toilet = (rnd < 0.5) ? womenToilet : menToilet; Console.WriteLine("{0:0.00}: {1} in coda", env.Now, gender); env.Process(Person(env, gender, toilet)); yield return(env.Timeout(env.Random.Exponential(1.0 / AvgPersonArrival))); } }
static IEnumerable <SimEvent> Process(SimEnvironment env) { yield return(env.Process(Interrupter(env.ActiveProcess))); object cause; if (env.ActiveProcess.Interrupted(out cause)) { Console.WriteLine("Interrupted at: " + cause); } // Following instructions are commented, because // they would result in an exception. In fact, // second interrupt would be uncaught. // // yield return env.Start(Interrupter(env.ActiveProcess)); // yield return env.Timeout(5); }
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)); } }
public Car(SimEnvironment env) { _env = env; Action = env.Process(Start()); }