View Javadoc

1   /*
2    * @(#)Generator.java Feb 1, 2003
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.dsol.formalisms.flow;
11  
12  import java.lang.reflect.Constructor;
13  import java.rmi.RemoteException;
14  
15  import nl.tudelft.simulation.dsol.SimRuntimeException;
16  import nl.tudelft.simulation.dsol.formalisms.devs.SimEvent;
17  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
18  import nl.tudelft.simulation.event.EventType;
19  import nl.tudelft.simulation.jstats.distributions.DistContinuous;
20  import nl.tudelft.simulation.jstats.distributions.DistDiscrete;
21  import nl.tudelft.simulation.language.reflection.ClassUtil;
22  import nl.tudelft.simulation.language.reflection.SerializableConstructor;
23  import nl.tudelft.simulation.logger.Logger;
24  
25  /***
26   * This class defines a generator <br>
27   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
28   * University of Technology </a>, the Netherlands. <br>
29   * See for project information <a
30   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
31   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
32   * License (GPL) </a>, no warranty <br>
33   * 
34   * @version 2.0 21.09.2003 <br>
35   * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
36   *         Jacobs </a>, <a
37   *         href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
38   *         Verbraeck </a>
39   */
40  public class Generator extends Station
41  {
42  
43  	/*** CREATE_EVENT is fired on creation */
44  	public static final EventType CREATE_EVENT = new EventType("CREATE_EVENT");
45  
46  	/***
47  	 * constructorArguments refer to the arguments invoked by the
48  	 */
49  	protected Object[] constructorArguments;
50  
51  	/*** interval defines the inter construction time */
52  	protected DistContinuous interval;
53  
54  	/*** startTime defines the absolute startTime for the generator */
55  	protected DistContinuous startTime;
56  
57  	/*** batchsize refers to the number of objects constructed */
58  	private DistDiscrete batchSize;
59  
60  	/*** constructor refers to the constructor to be invoked */
61  	protected SerializableConstructor constructor;
62  
63  	/***
64  	 * maxNumber is the max number of objects to be created. -1=Long.infinity
65  	 */
66  	private long maxNumber = -1;
67  
68  	/*** number refers to the currently constructed number */
69  	private long number = 0;
70  
71  	/*** nextEvent refers to the next simEvent */
72  	protected SimEvent nextEvent = null;
73  
74  	/***
75  	 * constructs a new generator for objects in a simulation. Constructed
76  	 * objects are sent to the 'destination' of the Generator when a destination
77  	 * has been indicated with the setDestination method. This constructor has a
78  	 * maximum number of entities generated, which results in stopping the
79  	 * generator when the maximum number of entities has been reached.
80  	 * 
81  	 * @param simulator is the on which the construction of the objects must be
82  	 *        scheduled.
83  	 * @param myClass is the class of which entities are created
84  	 * @param constructorArguments are the parameters for the constructor of
85  	 *        myClass. of arguments.
86  	 *        <code>constructorArgument[n]=new Integer(12)</code> may have
87  	 *        constructorArgumentClasses[n]=int.class;
88  	 * @throws SimRuntimeException on constructor invokation.
89  	 */
90  	public Generator(final DEVSSimulatorInterface simulator,
91  			final Class myClass, final Object[] constructorArguments)
92  			throws SimRuntimeException
93  	{
94  		super(simulator);
95  		try
96  		{
97  			Constructor constructor = ClassUtil.resolveConstructor(myClass,
98  					constructorArguments);
99  			this.constructor = new SerializableConstructor(constructor);
100 		} catch (Exception exception)
101 		{
102 			throw new SimRuntimeException(exception);
103 		}
104 		this.constructorArguments = constructorArguments;
105 	}
106 
107 	/***
108 	 * generates a new entity with the basic constructorArguments
109 	 * 
110 	 * @throws SimRuntimeException on construction failure
111 	 */
112 	public void generate() throws SimRuntimeException
113 	{
114 		this.generate(this.constructorArguments);
115 	}
116 
117 	/***
118 	 * generates a new entity
119 	 * 
120 	 * @param constructorArguments are the parameters used in the constructor.
121 	 * @throws SimRuntimeException on construction failure
122 	 */
123 	public synchronized void generate(final Object[] constructorArguments)
124 			throws SimRuntimeException
125 	{
126 		try
127 		{
128 			if (this.maxNumber == -1 || this.number < this.maxNumber)
129 			{
130 				this.number++;
131 				for (int i = 0; i < this.batchSize.draw(); i++)
132 				{
133 					Object object = this.constructor.deSerialize().newInstance(
134 							constructorArguments);
135 					Logger.finest(this, "generate", "created "
136 							+ this.number
137 							+ "th instance of "
138 							+ this.constructor.deSerialize()
139 									.getDeclaringClass());
140 					this.fireEvent(Generator.CREATE_EVENT, 1);
141 					this.releaseObject(object);
142 				}
143 				this.nextEvent = new SimEvent(this.simulator.getSimulatorTime()
144 						+ this.interval.draw(), this, this, "generate", null);
145 				this.simulator.scheduleEvent(this.nextEvent);
146 			}
147 		} catch (Exception exception)
148 		{
149 			throw new SimRuntimeException(exception);
150 		}
151 	}
152 
153 	/***
154 	 * @see StationInterface#receiveObject(Object)
155 	 */
156 	public void receiveObject(final Object object)
157 	{
158 		try
159 		{
160 			this.releaseObject(object);
161 		} catch (RemoteException remoteException)
162 		{
163 			Logger.warning(this, "receiveObject", remoteException);
164 		}
165 	}
166 
167 	/***
168 	 * returns the batchSize
169 	 * 
170 	 * @return DistDiscrete
171 	 */
172 	public DistDiscrete getBatchSize()
173 	{
174 		return this.batchSize;
175 	}
176 
177 	/***
178 	 * returns the interarrival intercal
179 	 * 
180 	 * @return DistContinuous
181 	 */
182 	public DistContinuous getInterval()
183 	{
184 		return this.interval;
185 	}
186 
187 	/***
188 	 * returns the maximum number of entities to be created
189 	 * 
190 	 * @return long the maxNumber
191 	 */
192 	public long getMaxNumber()
193 	{
194 		return this.maxNumber;
195 	}
196 
197 	/***
198 	 * sets the batchsize of the generator
199 	 * 
200 	 * @param batchSize is the number of entities simultaniously constructed
201 	 */
202 	public void setBatchSize(final DistDiscrete batchSize)
203 	{
204 		this.batchSize = batchSize;
205 	}
206 
207 	/***
208 	 * sets the interarrival distribution
209 	 * 
210 	 * @param interval is the interarrival time
211 	 */
212 	public void setInterval(final DistContinuous interval)
213 	{
214 		this.interval = interval;
215 	}
216 
217 	/***
218 	 * sets the maximum number of entities to be created
219 	 * 
220 	 * @param maxNumber is the maxNumber
221 	 */
222 	public void setMaxNumber(final long maxNumber)
223 	{
224 		this.maxNumber = maxNumber;
225 	}
226 
227 	/***
228 	 * returns the startTime of the generator
229 	 * 
230 	 * @return DistContinuous
231 	 */
232 	public DistContinuous getStartTime()
233 	{
234 		return this.startTime;
235 	}
236 
237 	/***
238 	 * sets the startTime
239 	 * 
240 	 * @param startTime is the absolute startTime
241 	 */
242 	public synchronized void setStartTime(final DistContinuous startTime)
243 	{
244 		this.startTime = startTime;
245 		try
246 		{
247 			this.nextEvent = new SimEvent(startTime.draw(), this, this,
248 					"generate", null);
249 			this.simulator.scheduleEvent(this.nextEvent);
250 		} catch (Exception exception)
251 		{
252 			Logger.warning(this, "setStartTime", exception);
253 		}
254 	}
255 }