Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.ExtLibs/HeuristicLab.SimSharp/3.0.7/SimSharp-3.0.7/Core/Resources/Resource.cs

Last change on this file was 12657, checked in by abeham, 9 years ago

#2420: Added Sim# as an ExtLib plugin

File size: 3.1 KB
Line 
1#region License Information
2/* SimSharp - A .NET port of SimPy, discrete event simulation framework
3Copyright (C) 2014  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 Resource {
25
26    public int Capacity { get; protected set; }
27
28    public int InUse { get { return Users.Count; } }
29
30    public int Remaining { get { return Capacity - InUse; } }
31
32    protected Environment Environment { get; private set; }
33
34    protected Queue<Request> RequestQueue { get; private set; }
35    protected Queue<Release> ReleaseQueue { get; private set; }
36    protected HashSet<Request> Users { get; private set; }
37
38    public Resource(Environment environment, int capacity = 1) {
39      if (capacity <= 0) throw new ArgumentException("Capacity must > 0.", "capacity");
40      Environment = environment;
41      Capacity = capacity;
42      RequestQueue = new Queue<Request>();
43      ReleaseQueue = new Queue<Release>();
44      Users = new HashSet<Request>();
45    }
46
47    public virtual Request Request() {
48      var request = new Request(Environment, TriggerRelease, DisposeCallback);
49      RequestQueue.Enqueue(request);
50      TriggerRequest();
51      return request;
52    }
53
54    public virtual Release Release(Request request) {
55      var release = new Release(Environment, request, TriggerRequest);
56      ReleaseQueue.Enqueue(release);
57      TriggerRelease();
58      return release;
59    }
60
61    protected virtual void DisposeCallback(Event @event) {
62      var request = @event as Request;
63      if (request != null) Release(request);
64    }
65
66    protected virtual void DoRequest(Request request) {
67      if (Users.Count < Capacity) {
68        Users.Add(request);
69        request.Succeed();
70      }
71    }
72
73    protected virtual void DoRelease(Release release) {
74      Users.Remove(release.Request);
75      release.Succeed();
76    }
77
78    protected virtual void TriggerRequest(Event @event = null) {
79      while (RequestQueue.Count > 0) {
80        var request = RequestQueue.Peek();
81        DoRequest(request);
82        if (request.IsTriggered) {
83          RequestQueue.Dequeue();
84        } else break;
85      }
86    }
87
88    protected virtual void TriggerRelease(Event @event = null) {
89      while (ReleaseQueue.Count > 0) {
90        var release = ReleaseQueue.Peek();
91        DoRelease(release);
92        if (release.IsTriggered) {
93          ReleaseQueue.Dequeue();
94        } else break;
95      }
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.