コード例 #1
1
 public void TestAndConditionBlocked()
 {
     var env = new Environment();
       env.Process(TestAndConditionBlockedProcess(env));
       env.RunD(5);
       Assert.AreEqual(5, env.NowD);
 }
コード例 #2
0
 public void TestGetState()
 {
     // A process is alive until it's generator has not terminated.
       var env = new Environment();
       var procA = env.Process(GetStatePemA(env));
       env.Process(GetStatePemB(env, procA));
       env.Run();
 }
コード例 #3
0
 public void Simulate()
 {
     var env = new Environment(TimeSpan.FromMinutes(1));
       env.Log("== Steel Factory ==");
       var crane = new Resource(env, 1);
       env.Process(Cast(env, crane, "CC1", new[] { new Slab(4), new Slab(4), new Slab(8), new Slab(3), new Slab(2) }));
       env.Process(Cast(env, crane, "CC2", new[] { new Slab(2), new Slab(3), new Slab(3), new Slab(4), new Slab(3) }));
       env.Run(TimeSpan.FromMinutes(100));
 }
コード例 #4
0
 public void Simulate()
 {
     var env = new Environment(randomSeed: 41);
       var packer = new Resource(env, 1);
       env.Process(Machine(env, packer));
       env.Process(Machine(env, packer));
       env.Run(TimeSpan.FromHours(8));
       Console.WriteLine("The machines were delayed for {0}", delay);
 }
コード例 #5
0
 /// <summary>
 /// This method will benchmark Sim#'s performance with respect to
 /// seizing and releasing resources, a common task in DES models.
 /// </summary>
 static long Benchmark3(Environment env)
 {
     perf = 0;
       var res = new Resource(env, capacity: 1);
       env.Process(Benchmark3Proc(env, res));
       env.Process(Benchmark3Proc(env, res));
       var watch = Stopwatch.StartNew();
       env.Run(terminate);
       watch.Stop();
       return watch.ElapsedTicks;
 }
コード例 #6
0
        public void Simulate(int rseed = 42)
        {
            // Setup and start the simulation
              var env = new Environment(rseed, TimeSpan.FromSeconds(1));
              env.Log("== Process communication ==");

              var pipe = new Store(env);
              env.Process(MessageGenerator("Generator A", env, pipe));
              env.Process(MessageConsumer("Consumer A", env, pipe));

              env.Run(TimeSpan.FromSeconds(100));
        }
コード例 #7
0
 public void TestInterruptedJoin()
 {
     /* Tests that interrupts are raised while the victim is waiting for
      another process. The victim should get unregistered from the other
      process.
        */
       var executed = false;
       var env = new Environment(new DateTime(1970, 1, 1, 0, 0, 0));
       var parent = env.Process(InterruptedJoinParent(env, () => executed = true));
       env.Process(InterruptedJoinInterruptor(env, parent));
       env.Run();
       Assert.IsTrue(executed);
 }
コード例 #8
0
 public void TestContainer()
 {
     var start = new DateTime(2014, 4, 2);
       var env = new Environment(start);
       var buf = new Container(env, initial: 0, capacity: 2);
       var log = new List<Tuple<char, DateTime>>();
       env.Process(TestContainerPutter(env, buf, log));
       env.Process(TestContainerGetter(env, buf, log));
       env.Run(TimeSpan.FromSeconds(5));
       var expected = new List<Tuple<char, int>> {
     Tuple.Create('p', 1), Tuple.Create('g', 1), Tuple.Create('g', 2), Tuple.Create('p', 2)
       }.Select(x => Tuple.Create(x.Item1, start + TimeSpan.FromSeconds(x.Item2))).ToList();
       CollectionAssert.AreEqual(expected, log);
 }
コード例 #9
0
        private static readonly TimeSpan TankTruckTime = TimeSpan.FromMinutes(10); // Minutes it takes the tank truck to arrive

        #endregion Fields

        #region Methods

        public void Simulate(int rseed = RandomSeed)
        {
            // Setup and start the simulation
              // Create environment and start processes
              var env = new Environment(rseed);
              env.Log("== Gas Station refuelling ==");
              var gasStation = new Resource(env, 2);
              var fuelPump = new Container(env, GasStationSize, GasStationSize);
              env.Process(GasStationControl(env, fuelPump));
              env.Process(CarGenerator(env, gasStation, fuelPump));

              // Execute!
              env.Run(SimTime);
        }
コード例 #10
0
 public void TestTriggeredTimeout()
 {
     var env = new Environment();
       env.Process(TestTriggeredTimeout(env));
       env.Run();
       Assert.AreEqual(2, env.NowD);
 }
コード例 #11
0
 static IEnumerable<Event> Benchmark2Source(Environment env)
 {
     while (true) {
     yield return env.Process(Benchmark2Sink(env));
     perf++;
       }
 }
コード例 #12
0
 public void TestFilterCallsWorstCase()
 {
     var env = new Environment();
       var store = new FilterStore(env, 4);
       var log = new List<string>();
       Func<object, bool> filterLogger = o => { log.Add(string.Format("check {0}", o)); return (int)o >= 3; };
       env.Process(TestFilterCallsWorseCaseGetProcess(store, filterLogger, log));
       env.Process(TestFilterCallsWorstCasePutProcess(store, log));
       env.Run();
       Assert.IsTrue(log.SequenceEqual(new[] {
     "put 0", "check 0",
     "put 1", "check 0", "check 1",
     "put 2", "check 0", "check 1", "check 2",
     "put 3", "check 0", "check 1", "check 2", "check 3", "get 3"
       }));
 }
コード例 #13
0
 public void TestErrorAndInterruptedJoin()
 {
     var executed = false;
       var env = new Environment(new DateTime(1970, 1, 1, 0, 0, 0));
       env.Process(ErrorAndInterruptedJoinParent(env, () => executed = true));
       env.Run();
       Assert.IsTrue(executed);
 }
コード例 #14
0
 private IEnumerable<Event> Source(Environment env, Resource counter)
 {
     for (int i = 0; i < NewCustomers; i++) {
     var c = Customer(env, "Customer " + i, counter, TimeSpan.FromMinutes(12.0));
     env.Process(c);
     yield return env.TimeoutExponential(IntervalCustomers);
       }
 }
コード例 #15
0
 /// <summary>
 /// This method will benchmark Sim#'s performance with respect to creation
 /// of entities. In SimPy and also Sim# the equivalence of an entity is a
 /// process. This stress tests the performance of creating processes.
 /// </summary>
 static long Benchmark2(Environment env)
 {
     perf = 0;
       env.Process(Benchmark2Source(env));
       var watch = Stopwatch.StartNew();
       env.Run(terminate);
       watch.Stop();
       return watch.ElapsedTicks;
 }
コード例 #16
0
 public void TestExit()
 {
     // Processes can set a return value
       var executed = false;
       var env = new Environment(new DateTime(1970, 1, 1, 0, 0, 0));
       env.Process(ExitParent(env, () => executed = true));
       env.Run();
       Assert.IsTrue(executed);
 }
コード例 #17
0
 public void TestChildException()
 {
     // A child catches an exception and sends it to its parent.
       // This is the same as TestExit
       var executed = false;
       var env = new Environment(new DateTime(1970, 1, 1, 0, 0, 0));
       env.Process(ChildExceptionParent(env, () => executed = true));
       env.Run();
       Assert.IsTrue(executed);
 }
コード例 #18
0
 static IEnumerable<Event> Machine(Environment env, Resource packer)
 {
     while (true) {
     yield return env.TimeoutNormalPositive(MachineProcTimeMu, MachineProcTimeSigma);
     var token = packer.Request();
     yield return token;
     delay += env.Now - token.Time;
     env.Process(Pack(env, packer, token));
       }
 }
コード例 #19
0
 private IEnumerable<Event> Cast(Environment env, Resource crane, string name, IEnumerable<Slab> castQueue)
 {
     foreach (var slab in castQueue) {
     yield return env.TimeoutD(slab.CastTime);
     env.Log("Caster {0} finished at {1}", name, env.Now);
     var token = crane.Request();
     yield return token;
     env.Process(Transport(env, crane, token, name));
       }
 }
コード例 #20
0
 public void TestFilterCallsBestCase()
 {
     var env = new Environment();
       var store = new FilterStore(env, new object[] { 1, 2, 3 }, 3);
       var log = new List<string>();
       Func<object, bool> filterLogger = o => { log.Add(string.Format("check {0}", o)); return true; };
       env.Process(TestFilterCallsBestCaseProcess(store, filterLogger, log));
       env.Run();
       Assert.IsTrue(log.SequenceEqual(new[] { "check 1", "get 1", "check 2", "get 2", "check 3", "get 3" }));
 }
コード例 #21
0
 public void TestEventQueueEmptyDApi()
 {
     /*The simulation should stop if there are no more events, that means, no
     more active process.*/
       var log = new List<string>();
       var env = new Environment(defaultStep: TimeSpan.FromMinutes(1));
       env.Process(AProcess(env, log));
       env.RunD(10);
       Assert.IsTrue(log.SequenceEqual(new[] { "00", "01" }));
 }
コード例 #22
0
 /// <summary>
 /// This method will benchmark Sim#'s performance with respect to the list
 /// of future events. A large number of processes that exist in the system
 /// stress tests the performance of operations on the event queue.
 /// </summary>
 /// <param name="n">The number of concurrent processes.</param>
 static long Benchmark1(Environment env, int n)
 {
     perf = 0;
       for (var i = 0; i < n; i++) {
     env.Process(Benchmark1Proc(env, n));
       }
       var watch = Stopwatch.StartNew();
       env.Run(terminate);
       watch.Stop();
       return watch.ElapsedTicks;
 }
コード例 #23
0
        private static readonly TimeSpan MinPatience = TimeSpan.FromMinutes(1); // Min. customer patience

        #endregion Fields

        #region Methods

        public void Simulate(int rseed = 41)
        {
            // Setup and start the simulation
              var start = new DateTime(2014, 2, 1);
              // Create an environment and start the setup process
              var env = new Environment(start, 41);
              env.Log("== Bank renege ==");
              var counter = new Resource(env, capacity: 1);
              env.Process(Source(env, counter));
              env.Run();
        }
コード例 #24
0
 public void Simulate()
 {
     queueSize = 0;
       env = new Environment();
       server = new Resource(env, capacity: 1);
       statistics = new ContinuousStatistics(env);
       env.Log("== m/m/1 queuing system ==");
       env.Process(Source());
       env.Run(TimeSpan.FromDays(180));
       Console.WriteLine("QueueSize Statistics:");
       Console.WriteLine("Min: {0}; Max: {1}; Mean: {2:F2}; StdDev: {3:F2}", statistics.Min, statistics.Max, statistics.Mean, statistics.StdDev);
 }
コード例 #25
0
 public void Simulate()
 {
     completedOrders = 0;
       env = new Environment();
       env.Log("== Kanban controlled production system ==");
       kanban = new Resource(env, capacity: 15);
       server = new Resource(env, capacity: 1);
       stockStat = new ContinuousStatistics(env);
       env.Process(Source());
       env.Run(TimeSpan.FromDays(180));
       Console.WriteLine("Stock: {0} ; {1:F3}±{2:F3} ; {3} (Min;Mean±StdDev;Max) kanbans ", stockStat.Min, stockStat.Mean, stockStat.StdDev, stockStat.Max);
       Console.WriteLine("Produced kanbans: {0:N0}", completedOrders);
 }
コード例 #26
0
        public void TestDiscreteTimeSteps()
        {
            var start = new DateTime(2014, 4, 1);
              var env = new Environment(start);
              var log = new List<DateTime>();
              env.Process(TestDiscreteTimeSteps(env, log));
              env.Run(TimeSpan.FromSeconds(3));

              Assert.AreEqual(3, log.Count);
              for (int i = 0; i < 3; i++)
            Assert.IsTrue(log.Contains(start + TimeSpan.FromSeconds(i)));
              Assert.AreEqual(3, env.ProcessedEvents);
        }
コード例 #27
0
        public void TestSharedTimeout()
        {
            var start = new DateTime(2014, 4, 1);
              var env = new Environment(start);
              var timeout = env.Timeout(TimeSpan.FromSeconds(1));
              var log = new Dictionary<int, DateTime>();
              for (int i = 0; i < 3; i++)
            env.Process(TestSharedTimeout(env, timeout, i, log));
              env.Run();

              Assert.AreEqual(3, log.Count);
              foreach (var l in log.Values)
            Assert.AreEqual(start + TimeSpan.FromSeconds(1), l);
        }
コード例 #28
0
 public void TestFilterStore()
 {
     var start = new DateTime(1970, 1, 1, 0, 0, 0);
       var sb = new StringBuilder();
       var env = new Environment(start) {
     Logger = new StringWriter(sb)
       };
       var sto = new FilterStore(env, capacity: 1);
       env.Process(FilterStoreProducer(env, sto));
       env.Process(FilterStoreConsumerA(env, sto));
       env.Process(FilterStoreConsumerB(env, sto));
       env.Run(TimeSpan.FromSeconds(20));
       Assert.AreEqual(sb.ToString(),
     @"4: Produce A
     4: Consume A
     6: Produce B
     6: Consume B
     10: Produce A
     14: Consume A
     14: Produce B
     14: Consume B
     18: Produce A
     ");
 }
コード例 #29
0
        private IEnumerable<Event> GasStationControl(Environment env, Container fuelPump)
        {
            /*
               * Periodically check the level of the *fuel_pump* and call the tank
               * truck if the level falls below a threshold.
               */
              while (true) {
            if (fuelPump.Level / fuelPump.Capacity * 100 < Threshold) {
              // We need to call the tank truck now!
              env.Log("Calling tank truck at {0}", env.Now);
              // Wait for the tank truck to arrive and refuel the station
              yield return env.Process(TankTruck(env, fuelPump));

            }
            yield return env.Timeout(TimeSpan.FromSeconds(10)); // Check every 10 seconds
              }
        }
コード例 #30
0
 private IEnumerable<Event> CarGenerator(Environment env, Resource gasStation, Container fuelPump)
 {
     // Generate new cars that arrive at the gas station.
       var i = 0;
       while (true) {
     i++;
     yield return env.Timeout(env.RandUniform(MinTInter, MaxTInter));
     env.Process(Car("Car " + i, env, gasStation, fuelPump));
       }
 }