| 1 | package desmoj.core.simulator; |
| 2 | |
| 3 | import java.util.LinkedList; |
| 4 | |
| 5 | |
| 6 | /** |
| 7 | * Provides the abstract super class for user defined events to change an entity's |
| 8 | * internal state. The state of a discrete model is changed by events that occur |
| 9 | * at distinct points of simulation time. |
| 10 | * <p> |
| 11 | * an event with up to three entities, changing its state according to the |
| 12 | * entity's reaction to the specific Event in the system under inspection. So |
| 13 | * each type of Event acting on one certain type of Entity requires a new |
| 14 | * subclass to be derived from this class. Since events are associated to a |
| 15 | * single entity, the method executing the changes of state of a specific Entity |
| 16 | * gets that entity passed as a parameter. The scheduler takes care that this is |
| 17 | * done at the specified point of simulation time. |
| 18 | * <p> |
| 19 | * For type safety it is recommended to generically assign the entity type an |
| 20 | * Event operates on by using the generic type |
| 21 | * <code>Event<EntityOperatingOn></code> where |
| 22 | * <code>EntityOperatingOn</code> is derived from <code>Entity</code>. |
| 23 | * <p> |
| 24 | * events should be used one time only. They are created to be scheduled |
| 25 | * together with a specific Entity, change that entity's state at the scheduled |
| 26 | * point of simulation time and are destroyed by Java's garbage collector after |
| 27 | * use. They could be reused but at a certain risk of inconsistent states. Since |
| 28 | * each object of a class that is derived from the class |
| 29 | * <code>Schedulable</code> has its unique identification number added as a |
| 30 | * suffix to its name, reusing Event objects would make one event responsible |
| 31 | * for several distinct changes in a model at different simulation times. This |
| 32 | * comes with the danger of of confusing the model's trace and making it more |
| 33 | * difficult to debug a faulty model implementation. Each type of Event needed |
| 34 | * for a model requires a new subclass of Event to be derived by the user. |
| 35 | * <p> |
| 36 | * Embed the changes of state for the specific Entity associated with this event |
| 37 | * by overriding the abstract method <code>eventRoutine(Entity e)</code>. Events |
| 38 | * that do not manipulate a single entity but act on the model's state on a more |
| 39 | * general matter are defined by external events, a subclass of this class. |
| 40 | * |
| 41 | * @see Entity |
| 42 | * @see ExternalEvent |
| 43 | * @see TimeInstant |
| 44 | * @see TimeSpan |
| 45 | * |
| 46 | * @version DESMO-J, Ver. 2.3.3 copyright (c) 2011 |
| 47 | * @author Tim Lechler |
| 48 | * |
| 49 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 50 | * you may not use this file except in compliance with the License. You |
| 51 | * may obtain a copy of the License at |
| 52 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 53 | * |
| 54 | * Unless required by applicable law or agreed to in writing, software |
| 55 | * distributed under the License is distributed on an "AS IS" |
| 56 | * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 57 | * or implied. See the License for the specific language governing |
| 58 | * permissions and limitations under the License. |
| 59 | * |
| 60 | */ |
| 61 | public abstract class EventAbstract extends Schedulable |
| 62 | { |
| 63 | /** |
| 64 | * The realTime deadline for this event in nanoseconds. In case of a |
| 65 | * real-time execution (i. e. the execution speed rate is set to a positive |
| 66 | * value) the Scheduler will produce a warning message if a deadline is |
| 67 | * missed. |
| 68 | */ |
| 69 | private long _realTimeConstraint; |
| 70 | |
| 71 | /** |
| 72 | * The number of entities is saved here. |
| 73 | */ |
| 74 | protected long numberOfEntities; |
| 75 | |
| 76 | /** |
| 77 | * Creates a new event of the given model, with the given name and trace |
| 78 | * option. |
| 79 | * |
| 80 | * @param name |
| 81 | * java.lang.String : The name of this event |
| 82 | * @param owner |
| 83 | * Model : The model this event is associated to |
| 84 | * @param showInTrace |
| 85 | * boolean : Flag for showing event in trace-files. Set it to |
| 86 | * <code>true</code> if event should show up in trace. Set it to |
| 87 | * <code>false</code> if event should not be shown in trace. |
| 88 | */ |
| 89 | public EventAbstract(Model owner, String name, boolean showInTrace) |
| 90 | { |
| 91 | |
| 92 | super(owner, name, showInTrace); |
| 93 | numberOfEntities = 0; |
| 94 | |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Tests if this event actually is an external event which is not used for |
| 99 | * modelling but to control the experiment to act at certain points of |
| 100 | * simulation time. External events must not be connected to an entity. |
| 101 | * |
| 102 | * @return boolean : Is <code>true</code> if this is an instance of class |
| 103 | * <code>ExternalEvent</code>,<code>false</code> otherwise |
| 104 | */ |
| 105 | public boolean isExternal() { |
| 106 | |
| 107 | return (this instanceof ExternalEvent); |
| 108 | |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /**Returns the realTime deadline for this event (in nanoseconds). In case of a |
| 113 | * real-time execution (i. e. the execution speed rate is set to a positive |
| 114 | * value) the Scheduler will produce a warning message if a deadline is |
| 115 | * missed. |
| 116 | * |
| 117 | * @return the realTimeConstraint in nanoseconds |
| 118 | */ |
| 119 | public long getRealTimeConstraint() { |
| 120 | return _realTimeConstraint; |
| 121 | } |
| 122 | |
| 123 | /**Returns the realTime deadline for this event (in nanoseconds). In case of a |
| 124 | * real-time execution (i. e. the execution speed rate is set to a positive |
| 125 | * value) the Scheduler will produce a warning message if a deadline is |
| 126 | * missed. |
| 127 | * |
| 128 | * @return the realTimeConstraint in nanoseconds |
| 129 | */ |
| 130 | public long getNumberOfEntities() |
| 131 | { |
| 132 | return numberOfEntities; |
| 133 | } |
| 134 | |
| 135 | /**Sets the realTime deadline for this event (in nanoseconds). In case of a |
| 136 | * real-time execution (i. e. the execution speed rate is set to a positive |
| 137 | * value) the Scheduler will produce a warning message if a deadline is |
| 138 | * missed. |
| 139 | * |
| 140 | * @param realTimeConstraint the realTimeConstraint in nanoseconds to set |
| 141 | */ |
| 142 | public void setRealTimeConstraint(long realTimeConstraint) { |
| 143 | this._realTimeConstraint = realTimeConstraint; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Utility method to generate trace output for scheduling this event (internal use only). |
| 148 | * |
| 149 | * @param who1 the first entity scheduled with this event (or <code>null</code> if not applicable) |
| 150 | * @param who2 the second entity scheduled with this event (or <code>null</code> if not applicable) |
| 151 | * @param who3 the third entity scheduled with this event (or <code>null</code> if not applicable) |
| 152 | * @param after the Schedulable after which this event is scheduled (or <code>null</code> if not applicable) |
| 153 | * @param before the Schedulable before which this event is scheduled (or <code>null</code> if not applicable) |
| 154 | * @param at the TimeInstant at which this event is scheduled |
| 155 | */ |
| 156 | protected void generateTraceForScheduling(Entity who1, Entity who2, Entity who3, Schedulable after, Schedulable before, TimeInstant at) { |
| 157 | |
| 158 | if (currentlySendTraceNotes()) { |
| 159 | |
| 160 | StringBuilder trace = new StringBuilder("schedules '" + getName() + "'"); |
| 161 | if (who1 != null) { |
| 162 | String who1alias = (who1 == currentEntity() && who2 == null && who3 == null? "itself" : "'" + who1.getName() + "'"); |
| 163 | trace.append(" of " + who1alias); |
| 164 | if (who2 != null) { |
| 165 | trace.append(who3 == null ? " and '" + who2.getName() + "'" : ", '" + who2.getName() + "'"); |
| 166 | if (who3 != null) { |
| 167 | trace.append(" and '" + who3.getName() + "'"); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | if (after != null) { |
| 173 | String afterAlias = (after == currentEntity() ? "itself" : "'" + after.getName() + "'"); |
| 174 | trace.append(" after " + afterAlias); |
| 175 | } else if (before != null) { |
| 176 | String beforeAlias = (before == currentEntity() ? "itself" : "'" + before.getName() + "'"); |
| 177 | trace.append(" before " + beforeAlias); |
| 178 | } |
| 179 | |
| 180 | if (at == this.presentTime()) { |
| 181 | trace.append(" now."); |
| 182 | } else { |
| 183 | trace.append(" at " + at.toString() + "."); |
| 184 | } |
| 185 | |
| 186 | this.sendTraceNote(trace.toString()); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Creates and returns a copy of this event. |
| 192 | * Note that subclasses have to implement the interface |
| 193 | * </code>java.lang.Cloneable</code> to actually use this method as |
| 194 | * otherwise, a </code>CloneNotSupportedException</code> will be thrown. |
| 195 | * |
| 196 | * @return EventAbstract : A copy of this event. |
| 197 | */ |
| 198 | protected EventAbstract clone() throws CloneNotSupportedException { |
| 199 | return (EventAbstract) super.clone(); |
| 200 | } |
| 201 | } |