Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.SimSharp/3.0.7/SimSharp-3.0.7/Core/Resources/Container.cs @ 14186

Last change on this file since 14186 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 3.2 KB
Line 
1#region License Information
2/* SimSharp - A .NET port of SimPy, discrete event simulation framework
3Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 3 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
17#endregion
18
19using System;
20using System.Collections.Generic;
21using System.Linq;
22
23namespace 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}
Note: See TracBrowser for help on using the repository browser.