1 | package desmoj.core.simulator; |
2 | |
3 | /** |
4 | * @deprecated Represents points in simulation time. Is used to indicate points |
5 | * in simulation time at which the state of the model changes. Each |
6 | * point in simulation time is represented by an individual object |
7 | * of this class and offers its own methods for arithmetic |
8 | * operations. Ensures that only valid points of time are generated. |
9 | * |
10 | * @version DESMO-J, Ver. 2.3.3 copyright (c) 2011 |
11 | * @author Tim Lechler |
12 | * @author modified by Felix Klueckmann |
13 | * |
14 | * Licensed under the Apache License, Version 2.0 (the "License"); |
15 | * you may not use this file except in compliance with the License. You |
16 | * may obtain a copy of the License at |
17 | * http://www.apache.org/licenses/LICENSE-2.0 |
18 | * |
19 | * Unless required by applicable law or agreed to in writing, software |
20 | * distributed under the License is distributed on an "AS IS" |
21 | * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
22 | * or implied. See the License for the specific language governing |
23 | * permissions and limitations under the License. |
24 | * |
25 | */ |
26 | @Deprecated |
27 | public class SimTime { |
28 | |
29 | /** |
30 | * NOW defines the value for scheduling an object immediately, even |
31 | * replacing the current SimProcess. Since the value is otherwise arbitrary |
32 | * as long as it is unique and can not be confused with any other legal or |
33 | * illegal (negative) value, it is set to be the constant defined in |
34 | * java.lang.Double and in java.lang.Float representing a non-number value. |
35 | * |
36 | * @see java.lang.Double |
37 | * @see java.lang.Float |
38 | */ |
39 | public static final SimTime NOW = new SimTime(0.0); |
40 | |
41 | /** |
42 | * Stores the point of simulation time as double value for each SimTime |
43 | * object. |
44 | */ |
45 | private final double _myTime; |
46 | |
47 | /** |
48 | * Constructs a simtime object with the given initial time value. It |
49 | * represents a point in simulation time. Note that only points of future |
50 | * simulation time can be accessed and that trying to create a simtime |
51 | * object with negative initial value will stop the simulation immediately. |
52 | * |
53 | * @param time |
54 | * double : The time value of this simtime with a simtime. |
55 | */ |
56 | public SimTime(double time) { |
57 | |
58 | super(); // default object constructor |
59 | |
60 | if (time < 0.0) { // points of time must be postive |
61 | throw (new desmoj.core.exception.SimAbortedException( |
62 | new desmoj.core.report.ErrorMessage(null, |
63 | "Can't create SimTime object! Simulation aborted.", |
64 | "Class : SimTime Constructor : SimTime(double)", |
65 | "the value passed for instantiation is negative : " |
66 | + time, |
67 | "Negative values for simulation time are illegal.", |
68 | null))); |
69 | } |
70 | |
71 | _myTime = time; |
72 | |
73 | } |
74 | |
75 | /** |
76 | * Constructs a simtime object with the given initial time object. It |
77 | * represents a point in simulation time. Note that only points of future |
78 | * simulation time can be accessed and that trying to create a SimTime |
79 | * object with negative initial value will stop the simulation immediately. |
80 | * |
81 | * @param time |
82 | * SimTime : The time of this SimTime |
83 | */ |
84 | public SimTime(SimTime time) { |
85 | |
86 | _myTime = time.getTimeValue(); |
87 | |
88 | } |
89 | |
90 | /** |
91 | * Returns a new simtime object with a time value representing the sum of |
92 | * the time values of the given simtime objects. |
93 | * |
94 | * @return SimTime : A new simtime as the sum of the simtime parameters |
95 | * @param a |
96 | * SimTime : Simtime parameter a |
97 | * @param b |
98 | * SimTime : Simtime parameter b |
99 | */ |
100 | public final static SimTime add(SimTime a, SimTime b) { |
101 | |
102 | return new SimTime(a.getTimeValue() + b.getTimeValue()); |
103 | |
104 | } |
105 | |
106 | /** |
107 | * Returns a new simtime object with a time value representing the absolute |
108 | * difference of the time values of the given simtime objects. Note that |
109 | * since no negative time values are allowed, <code>null</code> will be |
110 | * returned if the result of the subtraction would have resulted in a |
111 | * negative value. That is, a's time value is smaller than b's. |
112 | * |
113 | * @return SimTime : A new SimTime as the absolute difference of the SimTime |
114 | * parameters given |
115 | * @param a |
116 | * SimTime : Simtime parameter a of which parameter b will be |
117 | * subtracted |
118 | * @param b |
119 | * SimTime : Simtime parameter b subtracted from parameter a |
120 | */ |
121 | public final static SimTime diff(SimTime a, SimTime b) { |
122 | |
123 | if (a.getTimeValue() == b.getTimeValue()) |
124 | return new SimTime(0.0); |
125 | |
126 | double delta = a.getTimeValue() - b.getTimeValue(); |
127 | |
128 | if (delta < 0) |
129 | delta = -delta; |
130 | |
131 | if (delta < 0.000001) |
132 | delta = 0; |
133 | return new SimTime(delta); |
134 | |
135 | } // It's as simple as that |
136 | |
137 | /** |
138 | * Returns the value of the simtime object as type double. |
139 | * |
140 | * @return double : The value of the SimTime object as type double |
141 | */ |
142 | public double getTimeValue() { |
143 | |
144 | return _myTime; // not much else to do |
145 | |
146 | } |
147 | |
148 | /** |
149 | * Checks if the two simtime parameters describe the same point of |
150 | * simulation time. Note that this is a static method available through |
151 | * calling the class <code>SimTime</code> i.e. |
152 | * <code>SimTime.isEqual(a,b)</code> where a and b are valid simtime |
153 | * objects. |
154 | * |
155 | * @return boolean : True if both parameters describe same point of |
156 | * simulation time |
157 | * @param a |
158 | * SimTime : first comparand |
159 | * @param b |
160 | * SimTime : second comparand |
161 | */ |
162 | public final static boolean isEqual(SimTime a, SimTime b) { |
163 | |
164 | return (a.getTimeValue() == b.getTimeValue()); |
165 | |
166 | } |
167 | |
168 | /** |
169 | * Checks if the first of two points of simulation time is larger than the |
170 | * second. Larger means, that the time value of simtime a is larger and |
171 | * hence "later" than simtime b. Note that this is a static method available |
172 | * through calling the class <code>SimTime</code> i.e. |
173 | * <code>SimTime.isLargerThan(a,b)</code> where a and b are valid simtime |
174 | * objects. |
175 | * |
176 | * @return boolean : True if a is larger (later) than b |
177 | * @param a |
178 | * SimTime : first comparand |
179 | * @param b |
180 | * SimTime : second comparand |
181 | */ |
182 | public final static boolean isLarger(SimTime a, SimTime b) { |
183 | |
184 | return (a.getTimeValue() > b.getTimeValue()); |
185 | |
186 | } |
187 | |
188 | /** |
189 | * Checks if the first of two points of simulation time is larger or equal |
190 | * to the second. Larger means, that the time value of simtime a is larger |
191 | * and hence "later" than simtime b. Equal means, that they both describe |
192 | * the same point in simulation time. Note that this is a static method |
193 | * available through calling the class <code>SimTime</code> i.e. |
194 | * <code>SimTime.isLargerOrEqual(a,b)</code> where a and b are valid simtime |
195 | * objects. |
196 | * |
197 | * @return boolean : True if a is larger (later) or equal to b |
198 | * @param a |
199 | * SimTime : first comparand |
200 | * @param b |
201 | * SimTime : second comparand |
202 | */ |
203 | public final static boolean isLargerOrEqual(SimTime a, SimTime b) { |
204 | |
205 | return (a.getTimeValue() >= b.getTimeValue()); |
206 | |
207 | } |
208 | |
209 | /** |
210 | * Checks if the two simtime parameters describe the different points of |
211 | * simulation time. Note that this is a static method available through |
212 | * calling the class <code>SimTime</code> i.e. |
213 | * <code>SimTime.isNotEqual(a,b)</code> where a and b are valid simtime |
214 | * objects. |
215 | * |
216 | * @return boolean : True if parameters describe different points of |
217 | * simulation time |
218 | * @param a |
219 | * SimTime : first comparand |
220 | * @param b |
221 | * SimTime : second comparand |
222 | */ |
223 | public final static boolean isNotEqual(SimTime a, SimTime b) { |
224 | |
225 | return (a.getTimeValue() != b.getTimeValue()); |
226 | |
227 | } |
228 | |
229 | /** |
230 | * Checks if the first of two points of simulation time is smaller than the |
231 | * second. Smaller means, that the time value of SimTime a is larger and |
232 | * hence "sooner" than simtime b. Note that this is a static method |
233 | * available through calling the class <code>SimTime</code> i.e. |
234 | * <code>SimTime.isLargerThan(a,b)</code> where a and b are valid simtime |
235 | * objects. |
236 | * |
237 | * @return boolean : True if a is smaller (sooner) than b |
238 | * @param a |
239 | * SimTime : first comparand |
240 | * @param b |
241 | * SimTime : second comparand |
242 | */ |
243 | public final static boolean isSmaller(SimTime a, SimTime b) { |
244 | |
245 | return (a.getTimeValue() < b.getTimeValue()); |
246 | |
247 | } |
248 | |
249 | /** |
250 | * Checks if the first of two points of simulation time is smaller or equal |
251 | * to the second. Smaller means, that the time value of simtime a is smaller |
252 | * and hence "sooner" than simtime b. Equal means, that they both describe |
253 | * the same point in simulation time. Note that this is a static method |
254 | * available through calling the class <code>SimTime</code> i.e. |
255 | * <code>SimTime.isSmallerOrEqual(a,b)</code> where a and b are valid |
256 | * simtime objects. |
257 | * |
258 | * @return boolean : True if a is smaller (sooner) or equal to b |
259 | * @param a |
260 | * SimTime : first comparand |
261 | * @param b |
262 | * SimTime : second comparand |
263 | */ |
264 | public final static boolean isSmallerOrEqual(SimTime a, SimTime b) { |
265 | |
266 | return (a.getTimeValue() <= b.getTimeValue()); |
267 | |
268 | } |
269 | |
270 | /** |
271 | * Returns the special point of time to be used for replacing the current |
272 | * running process or schedule an entity at the first position in the |
273 | * event-list. |
274 | * |
275 | * @return desmoj.SimTime : The special simtime object used to indicate |
276 | * immediate scheduling, preempting a current SimProcess |
277 | * |
278 | * @sharpen.ignore |
279 | */ |
280 | public static final SimTime now() { |
281 | |
282 | return NOW; |
283 | |
284 | } |
285 | |
286 | /** |
287 | * Prints the time value of this simtime with all floating point digits. |
288 | * |
289 | * @return java.lang.String : The string representation of a simtime |
290 | */ |
291 | public String toString() { |
292 | |
293 | // convert time value to string |
294 | |
295 | //changed for sharpen: |
296 | //String s = Double.toString(_myTime); |
297 | String s = ((Double) _myTime).toString(); |
298 | return s; |
299 | |
300 | } |
301 | |
302 | /** |
303 | * Prints the time value of this simtime with the given number of floating |
304 | * point digits. |
305 | * |
306 | * @return java.lang.String : The string representation of a simtime |
307 | * @param floats |
308 | * int : The number of floating point digits to print |
309 | */ |
310 | public String toString(int floats) { |
311 | |
312 | // convert time value to String |
313 | String s = Double.toString(_myTime); |
314 | |
315 | // is the value larger than 10E7, <- that format is used giving the |
316 | // decimal |
317 | // point quite a different meaning |
318 | if (_myTime > 10E7) |
319 | return s; // so return the whole number |
320 | |
321 | // get position of decimal point if available |
322 | int decimalPoint = s.lastIndexOf("."); |
323 | |
324 | if (decimalPoint <= 0) {// no decimal point contained which can't really |
325 | // be |
326 | return s; // print it |
327 | } else { // there is a decimal point at position decimalPoint |
328 | if (floats == 0) |
329 | return s.substring(0, decimalPoint); // no decPoint wanted |
330 | if ((floats + 1) >= (s.length() - decimalPoint)) { // no more than |
331 | // the floats |
332 | return s; |
333 | } else |
334 | return s.substring(0, decimalPoint + floats + 1); // truncate |
335 | // at |
336 | // decimal |
337 | } |
338 | |
339 | } |
340 | |
341 | /** |
342 | * Converts the given TimeInstant object to a SimTime object. |
343 | * |
344 | * @param instant |
345 | * TimeInstant : The TimeInstant to be converted |
346 | * @return SimTime : The resulting SimTime from the conversion |
347 | * @author Felix Klueckmann (07/09) |
348 | */ |
349 | public static SimTime toSimTime(TimeInstant instant) { |
350 | return new SimTime(instant.getTimeAsDouble()); |
351 | } |
352 | |
353 | /** |
354 | * Converts the given TimeSpan object to a SimTime object. |
355 | * |
356 | * @param span |
357 | * TimeSpan : The TimeSpan to be converted |
358 | * @return SimTime : The resulting SimTime from the conversion |
359 | * @author Felix Klueckmann (07/09) |
360 | */ |
361 | public static SimTime toSimTime(TimeSpan span) { |
362 | return new SimTime(span.getTimeAsDouble()); |
363 | } |
364 | |
365 | /** |
366 | * Converts the given SimTime object to a TimeInstant object. Note that only |
367 | * SimTime objects with a time value smaller or equal than Long.MAX_VALUE |
368 | * can be converted and that trying to convert a simtime object with a time |
369 | * value that is bigger than Long.MAX_VALUE will stop the simulation |
370 | * immediately. |
371 | * |
372 | * @param simTime |
373 | * SimTime : The SimTime to be converted |
374 | * @return TimeInstant : The resulting TimeInstant from the conversion |
375 | * @author Felix Klueckmann (07/09) |
376 | */ |
377 | public static TimeInstant toTimeInstant(SimTime simTime) { |
378 | double timeValue = simTime.getTimeValue(); |
379 | if (timeValue > Long.MAX_VALUE) { |
380 | throw (new desmoj.core.exception.SimAbortedException( |
381 | new desmoj.core.report.ErrorMessage( |
382 | null, |
383 | "Can't convert SimTime to TimeInstant object! Simulation aborted.", |
384 | "Class : SimTime Methode : toTimeInstant(SimTime)", |
385 | "the double value passed as a parameter is bigger than Long.MAX_VALUE : " |
386 | + simTime.getTimeValue(), |
387 | "Can not create TimeInstant objects with a time Value bigger than Long.MAX_VALUE.", |
388 | null))); |
389 | } |
390 | return new TimeInstant((long) (timeValue |
391 | * TimeOperations.getEpsilon().convert(1, |
392 | TimeOperations.getReferenceUnit())), |
393 | TimeOperations.getEpsilon()); |
394 | } |
395 | |
396 | /** |
397 | * Converts the given SimTime object to a TimeSpan object. Note that only |
398 | * SimTime objects with a time value smaller or equal than Long.MAX_VALUE |
399 | * can be converted and that trying to convert a simtime object with a time |
400 | * value that is bigger than Long.MAX_VALUE will stop the simulation |
401 | * immediately. |
402 | * |
403 | * @param simTime |
404 | * SimTime : The SimTime to be converted |
405 | * @return TimeSpan : The resulting TimeSpan from the conversion |
406 | * @author Felix Klueckmann (07/09) |
407 | */ |
408 | public static TimeSpan toTimeSpan(SimTime simTime) { |
409 | if (simTime == SimTime.NOW) { |
410 | return TimeSpan.ZERO; |
411 | } |
412 | double timeValue = simTime.getTimeValue(); |
413 | if (timeValue > Long.MAX_VALUE) { |
414 | throw (new desmoj.core.exception.SimAbortedException( |
415 | new desmoj.core.report.ErrorMessage( |
416 | null, |
417 | "Can't convert SimTime to TimeSpan object! Simulation aborted.", |
418 | "Class : SimTime Methode : toTimeSpan(SimTime)", |
419 | "the double value passed as a parameter is bigger than Long.MAX_VALUE : " |
420 | + simTime.getTimeValue(), |
421 | "Can not create TimeSpan objects with a time Value bigger than Long.MAX_VALUE.", |
422 | null))); |
423 | } |
424 | return new TimeSpan(timeValue); |
425 | } |
426 | } |