1 | #region License Information
|
---|
2 | /* SimSharp - A .NET port of SimPy, discrete event simulation framework
|
---|
3 | Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 |
|
---|
5 | This program is free software: you can redistribute it and/or modify
|
---|
6 | it under the terms of the GNU General Public License as published by
|
---|
7 | the Free Software Foundation, either version 3 of the License, or
|
---|
8 | (at your option) any later version.
|
---|
9 |
|
---|
10 | This program is distributed in the hope that it will be useful,
|
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | GNU General Public License for more details.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU General Public License
|
---|
16 | along with this program. If not, see <http://www.gnu.org/licenses/>.*/
|
---|
17 | #endregion
|
---|
18 |
|
---|
19 | using System;
|
---|
20 | using System.Collections.Generic;
|
---|
21 | using System.Linq;
|
---|
22 |
|
---|
23 | namespace SimSharp {
|
---|
24 | public class Container {
|
---|
25 | public double Level { get; protected set; }
|
---|
26 | public double Capacity { get; protected set; }
|
---|
27 | protected Environment Environment { get; private set; }
|
---|
28 |
|
---|
29 | protected Queue<ContainerPut> PutQueue { get; private set; }
|
---|
30 | protected Queue<ContainerGet> GetQueue { get; private set; }
|
---|
31 |
|
---|
32 | public Container(Environment environment, double capacity = double.MaxValue, double initial = 0) {
|
---|
33 | if (capacity <= 0) throw new ArgumentException("Capacity must be > 0", "capacity");
|
---|
34 | if (initial < 0) throw new ArgumentException("Initial must be >= 0", "initial");
|
---|
35 | if (initial > capacity) throw new ArgumentException("Initial must be <= capacity", "initial");
|
---|
36 | Environment = environment;
|
---|
37 | Capacity = capacity;
|
---|
38 | Level = initial;
|
---|
39 | PutQueue = new Queue<ContainerPut>();
|
---|
40 | GetQueue = new Queue<ContainerGet>();
|
---|
41 | }
|
---|
42 |
|
---|
43 | public virtual ContainerPut Put(double amount) {
|
---|
44 | if (amount > Capacity) throw new ArgumentException("Cannot put more than capacity", "amount");
|
---|
45 | var put = new ContainerPut(Environment, TriggerGet, amount);
|
---|
46 | PutQueue.Enqueue(put);
|
---|
47 | TriggerPut();
|
---|
48 | return put;
|
---|
49 | }
|
---|
50 |
|
---|
51 | public virtual ContainerGet Get(double amount) {
|
---|
52 | if (amount > Capacity) throw new ArgumentException("Cannot get more than capacity", "amount");
|
---|
53 | var get = new ContainerGet(Environment, TriggerPut, amount);
|
---|
54 | GetQueue.Enqueue(get);
|
---|
55 | TriggerGet();
|
---|
56 | return get;
|
---|
57 | }
|
---|
58 |
|
---|
59 | protected virtual void DoPut(ContainerPut put) {
|
---|
60 | if (Capacity - Level >= put.Amount) {
|
---|
61 | Level += put.Amount;
|
---|
62 | put.Succeed();
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | protected virtual void DoGet(ContainerGet get) {
|
---|
67 | if (Level >= get.Amount) {
|
---|
68 | Level -= get.Amount;
|
---|
69 | get.Succeed();
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | protected virtual void TriggerPut(Event @event = null) {
|
---|
74 | while (PutQueue.Count > 0) {
|
---|
75 | var put = PutQueue.Peek();
|
---|
76 | DoPut(put);
|
---|
77 | if (put.IsTriggered) {
|
---|
78 | PutQueue.Dequeue();
|
---|
79 | } else break;
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | protected virtual void TriggerGet(Event @event = null) {
|
---|
84 | while (GetQueue.Count > 0) {
|
---|
85 | var get = GetQueue.Peek();
|
---|
86 | DoGet(get);
|
---|
87 | if (get.IsTriggered) {
|
---|
88 | GetQueue.Dequeue();
|
---|
89 | } else break;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|