1
2
3
4
5
6
7
8
9
10 package nl.tudelft.simulation.dsol.formalisms.flow;
11
12 import java.rmi.RemoteException;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.Iterator;
16 import java.util.List;
17
18 import nl.tudelft.simulation.dsol.formalisms.Resource;
19 import nl.tudelft.simulation.dsol.formalisms.ResourceRequestorInterface;
20 import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
21 import nl.tudelft.simulation.event.EventType;
22 import nl.tudelft.simulation.logger.Logger;
23
24 /***
25 * The Seize requests a resource and releases an entity whenever this resource
26 * is actually claimed.
27 * <p>
28 * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
29 * University of Technology </a>, the Netherlands. <br>
30 * See for project information <a href="http://www.simulation.tudelft.nl">
31 * www.simulation.tudelft.nl </a> <br>
32 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
33 * License (GPL) </a>, no warranty <br>
34 *
35 * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
36 * Jacobs </a>
37 * @version 1.5 2004-03-26
38 * @since 1.0
39 */
40 public class Seize extends Station implements ResourceRequestorInterface
41 {
42 /*** QUEUE_LENGTH_EVENT is fired when the queue length is changed */
43 public static final EventType QUEUE_LENGTH_EVENT = new EventType(
44 "QUEUE_QUEUE_LENGTH_EVENTLENGTH_EVENT");
45
46 /*** DELAY_TIME is fired when a new delayTime is computed */
47 public static final EventType DELAY_TIME = new EventType("DELAY_TIME");
48
49 /*** queue refers to the list of waiting requestors */
50 private List queue = Collections.synchronizedList(new ArrayList());
51
52 /*** requestedCapacity is the amount of resource requested on the resource */
53 private double requestedCapacity = Double.NaN;
54
55 /*** resource on which the capacity is requested */
56 private Resource resource;
57
58 /***
59 * Constructor for Seize.
60 *
61 * @param simulator on which behavior is scheduled
62 * @param resource which is claimed
63 */
64 public Seize(final DEVSSimulatorInterface simulator, final Resource resource)
65 {
66 this(simulator, resource, 1.0);
67 }
68
69 /***
70 * Constructor for Seize.
71 *
72 * @param simulator on which behavior is scheduled
73 * @param resource which is claimed
74 * @param requestedCapacity is the amount which is claimed by the seize
75 */
76 public Seize(final DEVSSimulatorInterface simulator,
77 final Resource resource, final double requestedCapacity)
78 {
79 super(simulator);
80 if (requestedCapacity < 0.0)
81 {
82 throw new IllegalArgumentException("requestedCapacity cannot < 0.0");
83 }
84 this.requestedCapacity = requestedCapacity;
85 this.resource = resource;
86 }
87
88 /***
89 * receives an object which request an amount
90 *
91 * @param object the object
92 * @param requestedCapacity the requested capacity
93 * @throws RemoteException on network failures
94 */
95 public synchronized void receiveObject(final Object object,
96 final double requestedCapacity) throws RemoteException
97 {
98 super.receiveObject(object);
99 Request request = new Request(object, requestedCapacity, this.simulator
100 .getSimulatorTime());
101 synchronized (this.queue)
102 {
103 this.queue.add(request);
104 }
105 try
106 {
107 this.fireEvent(Seize.QUEUE_LENGTH_EVENT,
108 (double) this.queue.size(), this.simulator
109 .getSimulatorTime());
110 this.resource.requestCapacity(requestedCapacity, this);
111 } catch (Exception exception)
112 {
113 Logger.warning(this, "receiveObject", exception);
114 }
115 }
116
117 /***
118 * @see StationInterface#receiveObject(Object)
119 */
120 public void receiveObject(final Object object) throws RemoteException
121 {
122 this.receiveObject(object, this.requestedCapacity);
123 }
124
125 /***
126 * sets the queue to this seize. This enables seize blocks to share one
127 * queue.
128 *
129 * @param queue is a new queue.
130 */
131 public void setQueue(final List queue)
132 {
133 this.queue = queue;
134 }
135
136 /***
137 * returns the queue
138 *
139 * @return List the queue
140 */
141 public List getQueue()
142 {
143 return this.queue;
144 }
145
146 /***
147 * @see nl.tudelft.simulation.dsol.formalisms.ResourceRequestorInterface
148 * #receiveRequestedResource(double,
149 * nl.tudelft.simulation.dsol.formalisms.Resource)
150 */
151 public void receiveRequestedResource(final double requestedCapacity,
152 final Resource resource) throws RemoteException
153 {
154 for (Iterator i = this.queue.iterator(); i.hasNext();)
155 {
156 Request request = (Request) i.next();
157 if (request.getAmount() == requestedCapacity)
158 {
159 synchronized (this.queue)
160 {
161 this.queue.remove(request);
162 }
163 this.fireEvent(Seize.QUEUE_LENGTH_EVENT, (double) this.queue
164 .size(), this.simulator.getSimulatorTime());
165 double delay = this.simulator.getSimulatorTime()
166 - request.getCreationTime();
167 this.fireEvent(Seize.DELAY_TIME, delay, this.simulator
168 .getSimulatorTime());
169 this.releaseObject(request.getEntity());
170 return;
171 }
172 }
173 }
174
175 /***
176 * The private RequestClass defines the requests for resource
177 */
178 private class Request
179 {
180 /*** amount is the requested amount */
181 private double amount;
182
183 /*** entity is the object requesting the amount */
184 private Object entity;
185
186 /*** creationTime refers to the moment the request was created */
187 private double creationTime = Double.NaN;
188
189 /***
190 * Method Request.
191 *
192 * @param entity the requesting entity
193 * @param amount is the requested amount
194 * @param creationTime the time the request was created
195 */
196 public Request(final Object entity, final double amount,
197 final double creationTime)
198 {
199 this.entity = entity;
200 this.amount = amount;
201 this.creationTime = creationTime;
202 }
203
204 /***
205 * Returns the amount.
206 *
207 * @return double
208 */
209 public double getAmount()
210 {
211 return this.amount;
212 }
213
214 /***
215 * Returns the entity.
216 *
217 * @return Object
218 */
219 public Object getEntity()
220 {
221 return this.entity;
222 }
223
224 /***
225 * Returns the creationTime
226 *
227 * @return double
228 */
229 public double getCreationTime()
230 {
231 return this.creationTime;
232 }
233 }
234 }