Changeset 16779 for trunk/HeuristicLab.ExtLibs/HeuristicLab.SimSharp/3.1.1/SimSharp-3.1.1/Core/Resources/Container.cs
- Timestamp:
- 04/12/19 13:45:11 (5 years ago)
- Location:
- trunk/HeuristicLab.ExtLibs/HeuristicLab.SimSharp/3.1.1
- Files:
-
- 1 edited
- 1 copied
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/HeuristicLab.ExtLibs/HeuristicLab.SimSharp/3.1.1/SimSharp-3.1.1/Core/Resources/Container.cs
r15972 r16779 1 1 #region License Information 2 2 /* SimSharp - A .NET port of SimPy, discrete event simulation framework 3 Copyright (C) 201 6Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 Copyright (C) 2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 5 5 This program is free software: you can redistribute it and/or modify … … 19 19 using System; 20 20 using System.Collections.Generic; 21 using System.Linq;22 21 23 22 namespace SimSharp { 23 /// <summary> 24 /// A container holds a variable amount of a single continuous entity, e.g. water, coal, grain, etc. 25 /// 26 /// Put and Get are in FIFO order only when they can be satisfied. 27 /// Any put or get that can be satisfied takes precedence. 28 /// Put events that attempt to add more to the Container than there is capacity for and 29 /// Get events that remove more than there is are backlogged. 30 /// </summary> 24 31 public class Container { 32 33 public double Capacity { get; protected set; } 34 25 35 public double Level { get; protected set; } 26 public double Capacity { get; protected set; } 27 protected EnvironmentEnvironment { get; private set; }36 37 protected Simulation Environment { get; private set; } 28 38 29 39 protected Queue<ContainerPut> PutQueue { get; private set; } 30 40 protected Queue<ContainerGet> GetQueue { get; private set; } 41 protected SimplePriorityQueue<Event, double> WhenAtLeastQueue { get; private set; } 42 protected SimplePriorityQueue<Event, double> WhenAtMostQueue { get; private set; } 43 protected List<Event> WhenChangeQueue { get; private set; } 31 44 32 public Container( Environmentenvironment, double capacity = double.MaxValue, double initial = 0) {45 public Container(Simulation environment, double capacity = double.MaxValue, double initial = 0) { 33 46 if (capacity <= 0) throw new ArgumentException("Capacity must be > 0", "capacity"); 34 47 if (initial < 0) throw new ArgumentException("Initial must be >= 0", "initial"); … … 39 52 PutQueue = new Queue<ContainerPut>(); 40 53 GetQueue = new Queue<ContainerGet>(); 54 WhenAtLeastQueue = new SimplePriorityQueue<Event, double>(); 55 WhenAtMostQueue = new SimplePriorityQueue<Event, double>(new ReverseComparer<double>()); 56 WhenChangeQueue = new List<Event>(); 41 57 } 42 58 … … 55 71 TriggerGet(); 56 72 return get; 73 } 74 75 public virtual Event WhenAtLeast(double level) { 76 var whenAtLeast = new Event(Environment); 77 WhenAtLeastQueue.Enqueue(whenAtLeast, level); 78 TriggerWhenAtLeast(); 79 return whenAtLeast; 80 } 81 82 public virtual Event WhenFull() { 83 return WhenAtLeast(Capacity); 84 } 85 86 public virtual Event WhenAtMost(double level) { 87 var whenAtMost = new Event(Environment); 88 WhenAtMostQueue.Enqueue(whenAtMost, level); 89 TriggerWhenAtMost(); 90 return whenAtMost; 91 } 92 93 public virtual Event WhenEmpty() { 94 return WhenAtMost(0); 95 } 96 97 public virtual Event WhenChange() { 98 var whenChange = new Event(Environment); 99 WhenChangeQueue.Add(whenChange); 100 return whenChange; 57 101 } 58 102 … … 77 121 if (put.IsTriggered) { 78 122 PutQueue.Dequeue(); 123 TriggerWhenAtLeast(); 124 TriggerWhenChange(); 79 125 } else break; 80 126 } … … 87 133 if (get.IsTriggered) { 88 134 GetQueue.Dequeue(); 135 TriggerWhenAtMost(); 136 TriggerWhenChange(); 89 137 } else break; 90 138 } 91 139 } 140 141 protected virtual void TriggerWhenAtLeast() { 142 while (WhenAtLeastQueue.Count > 0 && Level >= WhenAtLeastQueue.Peek) { 143 var whenAtLeast = WhenAtLeastQueue.Dequeue(); 144 whenAtLeast.Succeed(); 145 } 146 } 147 148 protected virtual void TriggerWhenAtMost() { 149 while (WhenAtMostQueue.Count > 0 && Level <= WhenAtMostQueue.Peek) { 150 var whenAtMost = WhenAtMostQueue.Dequeue(); 151 whenAtMost.Succeed(); 152 } 153 } 154 155 protected virtual void TriggerWhenChange() { 156 if (WhenChangeQueue.Count == 0) return; 157 foreach (var evt in WhenChangeQueue) 158 evt.Succeed(); 159 WhenChangeQueue.Clear(); 160 } 92 161 } 93 162 }
Note: See TracChangeset
for help on using the changeset viewer.