| 1 | package desmoj.core.simulator; |
| 2 | |
| 3 | /** |
| 4 | * Event notes contain any information needed for any type of <Code>Schedulable</Code>, event |
| 5 | * or process. Since <Code>EventNote</Code> are tightly coupled with the <Code>EventList</Code>, a change |
| 6 | * of the <Code>EventList</Code> might alo force the <Code>EventNote</Code> to contain other information |
| 7 | * especially concerning the data structures used in the <Code>EventList</Code> as well. To adopt |
| 8 | * the <Code>Event notes</Code> extend them to carry the specific data and methods you need |
| 9 | * for a possible change of the <Code>EventList</Code>. The implementation here includes |
| 10 | * data and methods for event information only, since the default implementation |
| 11 | * of class <code>EventVector</code> needs no special information stored |
| 12 | * inside the <Code>Event notes</Code>. |
| 13 | * |
| 14 | * @version DESMO-J, Ver. 2.3.3 copyright (c) 2011 |
| 15 | * @author Tim Lechler |
| 16 | * @author modified by Justin Neumann |
| 17 | * |
| 18 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 19 | * you may not use this file except in compliance with the License. You |
| 20 | * may obtain a copy of the License at |
| 21 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 22 | * |
| 23 | * Unless required by applicable law or agreed to in writing, software |
| 24 | * distributed under the License is distributed on an "AS IS" |
| 25 | * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 26 | * or implied. See the License for the specific language governing |
| 27 | * permissions and limitations under the License. |
| 28 | * |
| 29 | */ |
| 30 | public class EventNote implements Comparable<EventNote> |
| 31 | { |
| 32 | |
| 33 | /** |
| 34 | * The Entity associated to an event and a point of simulation time. Can be |
| 35 | * <code>null</code> in case of an external event associated to this |
| 36 | * EventNote. |
| 37 | */ |
| 38 | private Entity _myEntity1; // Entity associated with this event-note |
| 39 | |
| 40 | /** |
| 41 | * The Entity associated to an event and a point of simulation time. Can be |
| 42 | * <code>null</code> in case of an external event associated to this |
| 43 | * EventNote. |
| 44 | */ |
| 45 | private Entity _myEntity2; // Entity associated with this event-note |
| 46 | |
| 47 | /** |
| 48 | * The Entity associated to an event and a point of simulation time. Can be |
| 49 | * <code>null</code> in case of an external event associated to this |
| 50 | * EventNote. |
| 51 | */ |
| 52 | private Entity _myEntity3; // Entity associated with this event-note |
| 53 | |
| 54 | /** |
| 55 | * The event associated to an entity and a point of simulation time. Can be |
| 56 | * <code>null</code> in case of a sim-process associated to this event-note. |
| 57 | */ |
| 58 | private EventAbstract _myEvent; // type of Event that is going to happen |
| 59 | |
| 60 | /** |
| 61 | * The point of simulation time associated to an event and an entity. Must |
| 62 | * never be <code>null</code> since changes in the state of a model always |
| 63 | * happen to a certain discrete point of time. |
| 64 | */ |
| 65 | private TimeInstant _myTimeInstant; // time that the event is supposed to happen |
| 66 | /** |
| 67 | * If an event-note is connected, <code>RandomizingEventVector</code> or |
| 68 | * <code>RandomizingEventTreeList</code> will not insert another event-note |
| 69 | * (but not explicitly scheduled using |
| 70 | * <code>scheduleBefore()</code> or <code>scheduelAfter()</code>) |
| 71 | * between this event-note and its predecessor scheduled for |
| 72 | * the same instant. |
| 73 | */ |
| 74 | private boolean _isConnected; // flag for connection to predecessor |
| 75 | |
| 76 | /** |
| 77 | * Event notes can only be created if all relevant data can be supplied at |
| 78 | * creation time. Note that all relevant associations of the given |
| 79 | * Schedulables towards this event-note are built in this constructor |
| 80 | * ensuring that each scheduled Schedulable has access to its associated |
| 81 | * EventNote. |
| 82 | * |
| 83 | * @param who1 |
| 84 | * Entity : The Entity that is scheduled to be change during the |
| 85 | * course of the event or the sim-process |
| 86 | * @param what |
| 87 | * Event : The type of Event that scheduled to happen to the |
| 88 | * Entity |
| 89 | * @param when |
| 90 | * TimeInstant : The point of time that this event-note is supposed to |
| 91 | * be processed by the scheduling mechanism |
| 92 | */ |
| 93 | public EventNote(Entity who1, EventAbstract what, TimeInstant when) |
| 94 | { |
| 95 | |
| 96 | _myEntity1 = who1; |
| 97 | _myEntity2 = null; |
| 98 | _myEntity3 = null; |
| 99 | _myEvent = what; |
| 100 | _myTimeInstant = when; |
| 101 | |
| 102 | if (what != null) |
| 103 | what.addEventNote(this); |
| 104 | |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Event notes can only be created if all relevant data can be supplied at |
| 109 | * creation time. Note that all relevant associations of the given |
| 110 | * Schedulables towards this event-note are built in this constructor |
| 111 | * ensuring that each scheduled Schedulable has access to its associated |
| 112 | * EventNote. |
| 113 | * |
| 114 | * @param who1 |
| 115 | * Entity : The first entity that is scheduled to be change during the |
| 116 | * course of the event or the sim-process |
| 117 | * |
| 118 | * @param who2 |
| 119 | * Entity : The second entity that is scheduled to be change during the |
| 120 | * course of the event or the sim-process |
| 121 | * |
| 122 | * @param what |
| 123 | * Event2 : The type of Event that scheduled to happen to the |
| 124 | * entities |
| 125 | * |
| 126 | * @param when |
| 127 | * TimeInstant : The point of time that this event-note is supposed to |
| 128 | * be processed by the scheduling mechanism |
| 129 | */ |
| 130 | public EventNote(Entity who1, Entity who2, EventAbstract what, TimeInstant when) |
| 131 | { |
| 132 | |
| 133 | _myEntity1 = who1; |
| 134 | _myEntity2 = who2; |
| 135 | _myEntity3 = null; |
| 136 | _myEvent = what; |
| 137 | _myTimeInstant = when; |
| 138 | |
| 139 | if (what != null) |
| 140 | what.addEventNote(this); |
| 141 | |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Event notes can only be created if all relevant data can be supplied at |
| 146 | * creation time. Note that all relevant associations of the given |
| 147 | * Schedulables towards this event-note are built in this constructor |
| 148 | * ensuring that each scheduled Schedulable has access to its associated |
| 149 | * EventNote. |
| 150 | * |
| 151 | * @param who1 |
| 152 | * Entity : The first entity that is scheduled to be change during the |
| 153 | * course of the event or the sim-process |
| 154 | * |
| 155 | * @param who2 |
| 156 | * Entity : The second entity that is scheduled to be change during the |
| 157 | * course of the event or the sim-process |
| 158 | * |
| 159 | * @param who3 |
| 160 | * Entity : The third entity that is scheduled to be change during the |
| 161 | * course of the event or the sim-process |
| 162 | * |
| 163 | * @param what |
| 164 | * Event3 : The type of Event that scheduled to happen to the |
| 165 | * Entity |
| 166 | * |
| 167 | * @param when |
| 168 | * TimeInstant : The point of time that this event-note is supposed to |
| 169 | * be processed by the scheduling mechanism |
| 170 | */ |
| 171 | public EventNote(Entity who1, Entity who2, Entity who3, EventAbstract what, TimeInstant when) |
| 172 | { |
| 173 | |
| 174 | _myEntity1 = who1; |
| 175 | _myEntity2 = who2; |
| 176 | _myEntity3 = who3; |
| 177 | _myEvent = what; |
| 178 | _myTimeInstant = when; |
| 179 | |
| 180 | if (what != null) |
| 181 | what.addEventNote(this); |
| 182 | |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Copies the event-note. |
| 187 | * WARNING: No additional reference is set to linked Entity. |
| 188 | * |
| 189 | * @return The Entity associated to this event-note |
| 190 | */ |
| 191 | public EventNote copy() |
| 192 | { |
| 193 | EventNote evn = null; |
| 194 | |
| 195 | if (getNumberOfEntities()<=1) |
| 196 | { |
| 197 | evn = new EventNote(_myEntity1, _myEvent, _myTimeInstant); |
| 198 | evn._isConnected = this.isConnected(); |
| 199 | } |
| 200 | else if (getNumberOfEntities()==2) |
| 201 | { |
| 202 | evn = new EventNote(_myEntity1, _myEntity2, _myEvent, _myTimeInstant); |
| 203 | evn._isConnected = this.isConnected(); |
| 204 | } |
| 205 | else if (getNumberOfEntities()==3) |
| 206 | { |
| 207 | evn = new EventNote(_myEntity1, _myEntity2, _myEntity3, _myEvent, _myTimeInstant); |
| 208 | evn._isConnected = this.isConnected(); |
| 209 | } |
| 210 | { |
| 211 | return evn; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Returns whether this <code>EventNote</code> equals another one or not. |
| 217 | * |
| 218 | * @return <code>true</code> or <code>false</code>. |
| 219 | */ |
| 220 | public boolean equals(Object object) |
| 221 | { |
| 222 | if (object != null && object instanceof EventNote) |
| 223 | { |
| 224 | EventNote note = (EventNote) object; |
| 225 | if (this.compareTo(note) == 0) |
| 226 | { |
| 227 | if (this.toString().equals(note.toString())) |
| 228 | { |
| 229 | return true; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Compares the given EventNote to this event-note. This method |
| 239 | * implements the Comparable<EventNote> Interface |
| 240 | * |
| 241 | * @param note The event-note to be compared to this event-note |
| 242 | * |
| 243 | * @return Returns a negative integer, zero, or a positive integer as |
| 244 | * this event-note is before, at the same time, or after the given |
| 245 | * EventNote. |
| 246 | */ |
| 247 | public int compareTo(EventNote note) |
| 248 | { |
| 249 | if (note == null ) |
| 250 | { |
| 251 | return 0; //TODO? |
| 252 | } |
| 253 | if ((note.getTime() == null) && this.getTime() == null) |
| 254 | { |
| 255 | return 0; //TODO? |
| 256 | } |
| 257 | else if (note == null | note.getTime() == null) |
| 258 | { |
| 259 | return -1; //TODO? |
| 260 | } |
| 261 | else if (this.getTime() == null) |
| 262 | { |
| 263 | return +1; //TODO? |
| 264 | } |
| 265 | return this.getTime().compareTo(note.getTime()); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Returns the entity that is associated with this event-note. |
| 270 | * |
| 271 | * @return Entity : The Entity associated to this event-note |
| 272 | */ |
| 273 | public Entity getEntity1() |
| 274 | { |
| 275 | |
| 276 | return _myEntity1; |
| 277 | |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Returns the entity that is associated with this event-note. |
| 282 | * |
| 283 | * @return Entity : The Entity associated to this event-note |
| 284 | */ |
| 285 | public Entity getEntity2() |
| 286 | { |
| 287 | |
| 288 | return _myEntity2; |
| 289 | |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Returns the entity that is associated with this event-note. |
| 294 | * |
| 295 | * @return Entity : The Entity associated to this event-note |
| 296 | */ |
| 297 | public Entity getEntity3() |
| 298 | { |
| 299 | |
| 300 | return _myEntity3; |
| 301 | |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Returns the event associated with this event-note. |
| 306 | * |
| 307 | * @return Event : The event associated with this event-note |
| 308 | */ |
| 309 | public EventAbstract getEvent() |
| 310 | { |
| 311 | |
| 312 | return _myEvent; |
| 313 | |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Returns the number of included entities. |
| 318 | * |
| 319 | * @return <code>int</code> : The number of entities |
| 320 | */ |
| 321 | public long getNumberOfEntities() |
| 322 | { |
| 323 | int i = 0; |
| 324 | if (_myEntity1!=null) |
| 325 | { |
| 326 | i++; |
| 327 | } |
| 328 | if (_myEntity2!=null) |
| 329 | { |
| 330 | i++; |
| 331 | } |
| 332 | if (_myEntity3!=null) |
| 333 | { |
| 334 | i++; |
| 335 | } |
| 336 | |
| 337 | return i; |
| 338 | |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Returns the point of time associated with this event-note. |
| 343 | * |
| 344 | * @return TimeInstant : Point of time in simulation associated with this |
| 345 | * EventNote |
| 346 | */ |
| 347 | public TimeInstant getTime() |
| 348 | { |
| 349 | |
| 350 | return _myTimeInstant; |
| 351 | |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Associates this event-note with the given Entity. This is a package |
| 356 | * visibility method for internal framework use only. |
| 357 | * |
| 358 | * @param e |
| 359 | * Entity : The Entity to be associated with this event-note |
| 360 | */ |
| 361 | void setEntity1(Entity e) |
| 362 | { |
| 363 | |
| 364 | _myEntity1 = e; |
| 365 | |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Associates this event-note with the given Entity. This is a package |
| 370 | * visibility method for internal framework use only. |
| 371 | * |
| 372 | * @param e |
| 373 | * Entity : The Entity to be associated with this event-note |
| 374 | */ |
| 375 | void setEntity2(Entity e) |
| 376 | { |
| 377 | |
| 378 | _myEntity2 = e; |
| 379 | |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Associates this event-note with the given Entity. This is a package |
| 384 | * visibility method for internal framework use only. |
| 385 | * |
| 386 | * @param e |
| 387 | * Entity : The Entity to be associated with this event-note |
| 388 | */ |
| 389 | void setEntity3(Entity e) |
| 390 | { |
| 391 | |
| 392 | _myEntity3 = e; |
| 393 | |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Associates this event-note with the given event. This is a package |
| 398 | * visibility method for internal framework use only. |
| 399 | * |
| 400 | * @param e |
| 401 | * Event : The event to be associated with this event-note |
| 402 | */ |
| 403 | void setEvent(EventAbstract e) |
| 404 | { |
| 405 | |
| 406 | _myEvent = e; |
| 407 | |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Sets the point of time for this event-note to the time given as parameter. |
| 412 | * This method is to be used by the scheduler to correct the point of time |
| 413 | * of an event-note after inserting it relative to some other EventNote to |
| 414 | * preserve the temporal order of the event-list. This is a package |
| 415 | * visibility method for internal framework use only. |
| 416 | * |
| 417 | * @param time |
| 418 | * TimeInstant : the new point of simulation time this event-note is |
| 419 | * associated with. |
| 420 | */ |
| 421 | void setTime(TimeInstant time) { |
| 422 | |
| 423 | _myTimeInstant = time; |
| 424 | |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * Tests if this event-note is connected to its predecessor. |
| 429 | * If a connection exists (true), <code>RandomizingEventVector</code> or |
| 430 | * <code>RandomizingEventTreeList</code> will not insert another event-note scheduled for |
| 431 | * the same instant (but not explicity scheduled using |
| 432 | * <code>scheduleBefore()</code> or <code>scheduelAfter()</code>) |
| 433 | * between this event-note and its predecessor scheduled for |
| 434 | * the same instant. This is a package |
| 435 | * visibility method for internal framework use only. |
| 436 | * |
| 437 | * @return boolean : Is <code>true</code> if a connection to the predecessor exists, |
| 438 | * <code>false</code> otherwise. |
| 439 | */ |
| 440 | boolean isConnected() { |
| 441 | return _isConnected; |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Sets this event node connected to its predecessor (true), so that |
| 446 | * <code>RandomizingEventVector</code> or |
| 447 | * <code>RandomizingEventTreeList</code> will not insert another EventNote |
| 448 | * (but not explicity scheduled using |
| 449 | * <code>scheduleBefore()</code> or <code>scheduelAfter()</code>) |
| 450 | * between this event-note and its predecessor scheduled for |
| 451 | * the same instant or removes |
| 452 | * such a connection (false). This is a package |
| 453 | * visibility method for internal framework use only. |
| 454 | * |
| 455 | * @param isConnected |
| 456 | * boolean : establishes (true) or removes (false) a connection between |
| 457 | * this event-note and its predecessor. |
| 458 | */ |
| 459 | void setConnected(boolean isConnected) { |
| 460 | this._isConnected = isConnected; |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Returns a string representing the elements bundled in this event-note. It |
| 465 | * calls the <code>toString()</code> methods of every element putting each |
| 466 | * in brackets containing one or two letters to indicate the type of |
| 467 | * element. |
| 468 | * <p> |
| 469 | * <ul> |
| 470 | * <li>En: Entity, SimProcess or <code>null</code></li> |
| 471 | * <li>Ev: Event, external event or <code>null</code></li> |
| 472 | * <li>t: simulation time</li> |
| 473 | * </ul> |
| 474 | * |
| 475 | * @return java.lang.String : String representing the contained elements. |
| 476 | */ |
| 477 | public String toString() { |
| 478 | |
| 479 | String EntityString = ""; |
| 480 | |
| 481 | if (getNumberOfEntities()==1) |
| 482 | { |
| 483 | EntityString = "En: " + _myEntity1 + " "; |
| 484 | } |
| 485 | if (getNumberOfEntities()==2) |
| 486 | { |
| 487 | EntityString = "En:" + _myEntity1 + "," + _myEntity2 + " "; |
| 488 | } |
| 489 | if (getNumberOfEntities()==3) |
| 490 | { |
| 491 | EntityString = "En:" + _myEntity1 + "," + _myEntity2 + "," + _myEntity3 + " "; |
| 492 | } |
| 493 | |
| 494 | return (EntityString + "Ev:" + _myEvent + " t:" + _myTimeInstant); |
| 495 | |
| 496 | } |
| 497 | |
| 498 | } |