1 | package desmoj.core.simulator; |
2 | |
3 | import java.util.Observable; |
4 | |
5 | /** |
6 | * The simulation clock shows the actual simulation time. The actual simulation |
7 | * time can be polled by any object but can only be set by the scheduler |
8 | * responsible for the actual model. The simulation clock is extending class |
9 | * <code>java.util.Observable</code> thus representing the 'observable' part in |
10 | * a 'observer'-design pattern as described in [Gamm95] page 107. This enables |
11 | * observers to register themselves at the simulation clock to be notified |
12 | * whenever the simulation time changes. This can be easily used to provide |
13 | * fully automatic statistical counters. Each time the simulation time changes, |
14 | * a counter registered at the simulation clock is notofied and can poll the |
15 | * value it is observing (most likely from a special <code>ValueSupplier</code> |
16 | * object). This way, no explicit calls for the counter to update its observed |
17 | * value are needed. Note that on the other hand this might reduce performance |
18 | * in comparison to explicit update call since the value under observation might |
19 | * not change each time the simulation time is changed. |
20 | * |
21 | * @see java.util.Observable |
22 | * |
23 | * @version DESMO-J, Ver. 2.3.3 copyright (c) 2011 |
24 | * @author Tim Lechler |
25 | * @author modified by Soenke Claassen |
26 | * @author modified by Felix Klueckmann |
27 | * |
28 | * Licensed under the Apache License, Version 2.0 (the "License"); you |
29 | * may not use this file except in compliance with the License. You may |
30 | * obtain a copy of the License at |
31 | * http://www.apache.org/licenses/LICENSE-2.0 |
32 | * |
33 | * Unless required by applicable law or agreed to in writing, software |
34 | * distributed under the License is distributed on an "AS IS" BASIS, |
35 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
36 | * implied. See the License for the specific language governing |
37 | * permissions and limitations under the License. |
38 | * |
39 | */ |
40 | public class SimClock extends Observable { |
41 | |
42 | /** |
43 | * The Simclock's Name |
44 | */ |
45 | String name; |
46 | |
47 | /** |
48 | * Stores internally the actual simulation time. |
49 | */ |
50 | private TimeInstant _timeNow; |
51 | |
52 | /** |
53 | * Constructs a simulation clock with no parameters given. By default the |
54 | * actual simulation time is set to zero. |
55 | * |
56 | * @author Tim Lechler |
57 | * @author modified by Felix Klueckmann |
58 | * @param name |
59 | * String : The name of the simulation clock |
60 | */ |
61 | public SimClock(String name) { |
62 | |
63 | super(); // create a NamedObject |
64 | this.name = name + "_clock"; |
65 | // set the simulation clock to 0 |
66 | _timeNow = new TimeInstant(0); // the birth of time ;-) |
67 | |
68 | } |
69 | /** |
70 | * Returns the clock's name as string. This method has become necessary |
71 | * since the simulation clock does not extend class |
72 | * <code>NamedObjectImp</code>. |
73 | * |
74 | * @return java.lang.String : The clock's name |
75 | */ |
76 | public String getName() { |
77 | |
78 | return name; |
79 | |
80 | } |
81 | |
82 | /** |
83 | * Returns the actual simulation time. |
84 | * |
85 | * @return TimeInstant : The actual simulation time |
86 | */ |
87 | public TimeInstant getTime() { |
88 | return _timeNow; |
89 | } |
90 | |
91 | /** |
92 | * Sets the actual simulation time to a new value. This method has to be |
93 | * protected from user access since it must not be manipulated by anyone but |
94 | * the scheduler. |
95 | * |
96 | * @param newTime |
97 | * TimeInstant : The new simulation time |
98 | */ |
99 | void setTime(TimeInstant newTime) { |
100 | //check if newTime is in the future |
101 | if(TimeInstant.isBeforeOrEqual(newTime, _timeNow)){ |
102 | // check for legal parameter (newTime>oldTime) |
103 | if (TimeInstant.isBefore(newTime, _timeNow)) { |
104 | //TODO Exception (Wrong Time) |
105 | } |
106 | return; |
107 | } |
108 | |
109 | |
110 | // note all observers of change before setting the new time!!!! |
111 | setChanged(); // set the status to changed |
112 | |
113 | // tell every Observer registered the actual TimeInstant which will be |
114 | // changed now |
115 | notifyObservers(_timeNow); |
116 | |
117 | _timeNow = newTime; // now make the move for the next time change. |
118 | } |
119 | |
120 | /** |
121 | * Sets the initial simulation time. Allows negative values. This method has to be |
122 | * protected from user access since it must not be manipulated by anyone but |
123 | * the scheduler. |
124 | * |
125 | * @param initTime |
126 | * TimeInstant : The initial simulation time |
127 | */ |
128 | void setInitTime(TimeInstant initTime){ |
129 | _timeNow= initTime; |
130 | } |
131 | |
132 | /** |
133 | * Returns the clock's name as string. This method has become necessary |
134 | * since the simulation clock does not extend class |
135 | * <code>NamedObjectImp</code>. |
136 | * |
137 | * @return java.lang.String : The clock's name |
138 | */ |
139 | public String toString() { |
140 | |
141 | return name; |
142 | |
143 | } |
144 | } |