public abstract class BasicSimEntity extends BasicSimEventSource implements SimEntity
SimEntity
that does not use
reflection but rather relies on the user subclassing and implementing the
SimEventListener
method processSimEvent(SimEvent)
.
The easiest way is for the user to directly dispatch the event based on the
SimEvent
name and parameters. For example, if there is an Event
method called "doThis()
", then a simple implementation of
processSimEvent(SimEvent)
is:
public void processSimEvent(SimEvent event) { if (event.getMethodName().equals("doThis")) {this.doThis();} }The primary disadvantage of this class (vice
SimEntityBase
) is that the user must remember to invoke each
"do" method and pass it the correct parameters, if necessary. Any further
subclasses must remember to override processSimEvent(SimEvent)
to add all additional "do" methods (or else they will be omitted altogether
without comment). The advantages are that it is slightly easier to
understand, since it doesn't rely on the complex reflection of
SimEntityBase
. This also runs about twice as fast as
SimEntityBase
based on very small-sample ad-hoc tests.
One extremely important point about writing a
processSimEvent(SimEvent)
method:
processSimEvent(SimEvent)
must not
execute a doRun())
method if it is not the "owner" (i.e. if it
gets a "Run" event by through listening). Failure to do this will result in
multiple "Run" events for the same object at the start of a simulation.
Modifier and Type | Field and Description |
---|---|
private static java.util.concurrent.atomic.AtomicInteger |
atomicInteger
The unique number to be assigned to the next BasicSimEntity created.
|
private boolean |
clearAddedPropertiesOnReset
If true (default) then all added properties are cleared in the reset()
method.
|
protected BasicEventList |
eventList |
private boolean |
justDefinedProperties |
private java.lang.String |
name
The name of this SimEntity.
|
private boolean |
persistant |
private Priority |
priority
The priority given to this SimEntity's events.
|
protected PropertyChangeDispatcher |
property
Manages Properties for the entity.
|
private int |
serial
A unique identifier for this SimEntity.
|
private boolean |
verbose
If true causes debug/trace information to be output.
|
DEFAULT_ENTITY_NAME, DEFAULT_EVENT_NAME, DEFAULT_PRIORITY, EVENT_METHOD_PREFIX, NL
Constructor and Description |
---|
BasicSimEntity()
Construct a new BasicSimEntity with a default name and priority.
|
BasicSimEntity(Priority priority)
Construct a new BasicSimEntity with a default name and the given
priority.
|
BasicSimEntity(java.lang.String name)
Construct a new BasicSimEntity with the given name and a default
priority.
|
BasicSimEntity(java.lang.String name,
Priority priority) |
BasicSimEntity(java.lang.String name,
Priority priority,
int eventListID)
Construct a new BasicSimEntity with the given name and priority.
|
Modifier and Type | Method and Description |
---|---|
void |
addPropertyChangeListener(java.beans.PropertyChangeListener listener)
Adds a PropertyChangeListener to this PropertyChangeSource.
|
void |
addPropertyChangeListener(java.lang.String propertyName,
java.beans.PropertyChangeListener listener)
Adds a PropertyChangeListener to this PropertyChangeSource for a specific property.
|
protected void |
attemptSchedule(SimEvent event)
Schedules an event.
|
void |
clearAddedProperty(java.lang.String propertyName)
Clears the added property of the given name.
|
int |
compareTo(SimEntity other)
This SimEntity has higher priority if its Priority instance has higher
priority or it has a smaller serial.
|
void |
fireIndexedPropertyChange(int index,
java.lang.String propertyName,
java.lang.Object newValue)
Notify registered PropertyChangeListeners that an element of an indexed
property has changed.
|
void |
fireIndexedPropertyChange(int index,
java.lang.String propertyName,
java.lang.Object oldValue,
java.lang.Object newValue,
java.lang.Object... extraData)
Notify registered PropertyChangeListeners that an element of an indexed
property has changed.
|
void |
firePropertyChange(java.beans.PropertyChangeEvent e)
Notify all PropertyChangeListeners of the PropertyChangeEvent.
|
void |
firePropertyChange(java.lang.String propertyName,
java.lang.Object newValue)
Notify registered PropertChangeListeners that the given Object property
has changed.
|
void |
firePropertyChange(java.lang.String propertyName,
java.lang.Object oldValue,
java.lang.Object newValue,
java.lang.Object... extraData)
Notify registered PropertChangeListeners that the given Object property
has changed.
|
java.lang.String[] |
getAddedProperties() |
BasicEventList |
getEventList() |
int |
getEventListID() |
java.lang.String |
getName() |
Priority |
getPriority()
The scheduling priority for this entity.
|
protected java.lang.String |
getPropertiesString() |
java.lang.Object |
getProperty(java.lang.String name)
Get the value of the given property.
|
java.lang.Object |
getProperty(java.lang.String name,
java.lang.Object defaultValue)
Get the value of the given property or return the supplied default.
|
java.beans.PropertyChangeListener[] |
getPropertyChangeListeners()
Gets an array of all registered PropertyChangeListeners.
|
int |
getSerial() |
abstract void |
handleSimEvent(SimEvent event)
Typically an Event is handled (as opposed to processed, as in SimEventListener)
by actually executing a method.
|
void |
interrupt(java.lang.String eventName)
Cancels the next event for this entity that matches the event name.
|
void |
interrupt(java.lang.String eventName,
java.lang.Object... parameters)
Cancels the next event for this entity that matches the event name and
value of the parameters.
|
void |
interruptAll()
Cancel all events for this entity.
|
void |
interruptAll(java.lang.String eventName)
Cancels all events for this entity that match the event name.
|
void |
interruptAll(java.lang.String eventName,
java.lang.Object... parameters)
Cancels all events for this entity that match the event name and value of
the parameters.
|
void |
interruptAllWithArgs(java.lang.Object parameter)
Interrupt (cancel) all events scheduled by this component having
the given eventName and an argument matching the parameter.
|
void |
interruptAllWithArgs(java.lang.String eventName,
java.lang.Object parameter)
Interrupt (cancel) all events scheduled by this component having
the given eventName and an argument matching the parameter.
|
boolean |
isClearAddedPropertiesOnReset() |
boolean |
isJustDefinedProperties() |
boolean |
isPersistant() |
boolean |
isReRunnable()
A BasicSimEntity is "ReRunnable" if it has a doRun method.
|
boolean |
isVerbose() |
static boolean |
parametersMatch(java.lang.Object[] fromEvent,
java.lang.Object[] fromInterrupt)
TODO: replace this with Arrays.equals(Object[], Object[]).
|
abstract void |
processSimEvent(SimEvent event)
Process the given SimEvent.
|
void |
removePropertyChangeListener(java.beans.PropertyChangeListener listener)
Causes the PropertyChangeListener to stop listening.
|
void |
removePropertyChangeListener(java.lang.String propertyName,
java.beans.PropertyChangeListener listener)
Causes the PropertyChangeListener to stop listening.
|
void |
reset()
Resets this BasicSimEntity by canceling all of its pending SimEvents.
|
static void |
resetNextSerial() |
void |
setClearAddedPropertiesOnReset(boolean clearAddedPropertiesOnReset) |
void |
setEventList(BasicEventList el) |
void |
setEventListID(int id) |
void |
setJustDefinedProperties(boolean b) |
void |
setName(java.lang.String name)
Sets the name of this entity.
|
void |
setPersistant(boolean persist) |
void |
setPriority(Priority p)
Sets the scheduling priority for this entity.
|
void |
setProperty(java.lang.String name,
java.lang.Object value)
Set the property to the given value.
|
void |
setVerbose(boolean verbose) |
java.lang.String |
toString()
A default string description of this entity, name (Entity Priority)
<list of all properties as key = value pairs>
|
SimEvent |
waitDelay(java.lang.String name,
double delay,
java.lang.Object... parameters)
Schedules an event with the default priority /
|
SimEvent |
waitDelay(java.lang.String name,
double delay,
Priority priority,
java.lang.Object... parameters)
Schedules a SimEvent for an event that has multiple parameters.
|
SimEvent |
waitDelay(java.lang.String name,
RandomVariate delayGenerator,
java.lang.Object... parameters)
Schedules an event using a RandomVariate instance to generate the delay.
|
SimEvent |
waitDelay(java.lang.String name,
RandomVariate delayGenerator,
Priority priority,
java.lang.Object... parameters)
Schedules an event using a RandomVariate instance to generate the delay
|
addSimEventListener, getSimEventListeners, notifyListeners, removeSimEventListener
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
addSimEventListener, getSimEventListeners, notifyListeners, removeSimEventListener
private static final java.util.concurrent.atomic.AtomicInteger atomicInteger
private java.lang.String name
private Priority priority
private int serial
private boolean verbose
private boolean persistant
protected PropertyChangeDispatcher property
PropertyChangeDispatcher
protected BasicEventList eventList
private boolean justDefinedProperties
private boolean clearAddedPropertiesOnReset
public BasicSimEntity(java.lang.String name)
name
- The name of the BasicSimEntity.public BasicSimEntity(java.lang.String name, Priority priority, int eventListID)
name
- The name of the BasicSimEntity.priority
- The priority assigned this BasicSimEntity.eventListID
- id of this BasicSimEntity's EventListsetPriority(Priority)
public BasicSimEntity(java.lang.String name, Priority priority)
public BasicSimEntity(Priority priority)
priority
- The priority assigned this BasicSimEntity.setPriority(Priority)
public BasicSimEntity()
public static final void resetNextSerial()
public boolean isReRunnable()
isReRunnable
in interface ReRunnable
public void reset()
reset
in interface ReRunnable
public SimEvent waitDelay(java.lang.String name, double delay, Priority priority, java.lang.Object... parameters)
waitDelay
in interface SimEventScheduler
name
- The name of the event to be scheduled. (The "do" is optional.
"doArrive" and "Arrive" are equivalent.)delay
- The amount of time between now and when the event will
occur.priority
- If two events occur at the same time, the one with the
highest number will be executed first.parameters
- The parameters for the event.SimEvent
public SimEvent waitDelay(java.lang.String name, double delay, java.lang.Object... parameters)
waitDelay
in interface SimEventScheduler
name
- Event name of eventdelay
- simTime until event is to occurparameters
- optional parameters for eventpublic SimEvent waitDelay(java.lang.String name, RandomVariate delayGenerator, java.lang.Object... parameters)
name
- Event name of eventdelayGenerator
- RandomVariate that is used to generate the time
until the event will occurparameters
- optional parameters for eventpublic SimEvent waitDelay(java.lang.String name, RandomVariate delayGenerator, Priority priority, java.lang.Object... parameters)
name
- Event name of eventdelayGenerator
- RandomVariate that is used to generate the time
until the event will occurpriority
- If two events occur at the same time, the one with the
highest number will be executed first.parameters
- optional parameters for eventprotected void attemptSchedule(SimEvent event)
event
- The event to be scheduledpublic void interrupt(java.lang.String eventName, java.lang.Object... parameters)
interrupt
in interface SimEventScheduler
eventName
- name of event to cancelparameters
- Parameters of event to cancelpublic void interrupt(java.lang.String eventName)
interrupt
in interface SimEventScheduler
eventName
- Name of event to cancelpublic void interruptAll(java.lang.String eventName, java.lang.Object... parameters)
interruptAll
in interface SimEventScheduler
eventName
- Given event nameparameters
- Given parameters to be matchedpublic void interruptAll(java.lang.String eventName)
interruptAll
in interface SimEventScheduler
eventName
- Given event namepublic void interruptAll()
interruptAll
in interface SimEventScheduler
public void setName(java.lang.String name)
public java.lang.String getName()
public void setPriority(Priority p)
setPriority
in interface SimEventScheduler
p
- Given Priority of this BasicSimEntitypublic Priority getPriority()
getPriority
in interface SimEventScheduler
public void setVerbose(boolean verbose)
verbose
- If true causes debug/trace information to be output.public boolean isVerbose()
public static boolean parametersMatch(java.lang.Object[] fromEvent, java.lang.Object[] fromInterrupt)
fromEvent
- Parameters from event on Event ListfromInterrupt
- Parameters from interrupt callpublic abstract void handleSimEvent(SimEvent event)
SimEventScheduler
handleSimEvent
in interface SimEventScheduler
event
- The SimEvent to be handled.public abstract void processSimEvent(SimEvent event)
processSimEvent
in interface SimEventListener
event
- The SimEvent to process.public int getSerial()
getSerial
in interface SimEventScheduler
public void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
PropertyChangeSource
addPropertyChangeListener
in interface PropertyChangeSource
listener
- The new listener to all my property changes.public void addPropertyChangeListener(java.lang.String propertyName, java.beans.PropertyChangeListener listener)
PropertyChangeSource
addPropertyChangeListener
in interface PropertyChangeSource
propertyName
- The name of the property the listener is interested in.listener
- The new listener to all my property changes.public void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
PropertyChangeSource
removePropertyChangeListener
in interface PropertyChangeSource
listener
- The listener that will stop listening to my property changes.public void removePropertyChangeListener(java.lang.String propertyName, java.beans.PropertyChangeListener listener)
PropertyChangeSource
removePropertyChangeListener
in interface PropertyChangeSource
propertyName
- The name of the property to stop listening to.listener
- The listener that will stop listening to my property changes.public void firePropertyChange(java.beans.PropertyChangeEvent e)
firePropertyChange
in interface PropertyChangeSource
e
- The event with all the information about what property has
changed and to what value.public void setJustDefinedProperties(boolean b)
public boolean isJustDefinedProperties()
public java.lang.Object getProperty(java.lang.String name)
getProperty
in interface PropertyChangeSource
name
- The name of the property to be retrievedjava.lang.NullPointerException
- If the property doesn't existpublic java.lang.Object getProperty(java.lang.String name, java.lang.Object defaultValue)
getProperty
in interface PropertyChangeSource
name
- The name of the property to be retrieved.defaultValue
- The default value -- returned if property's value
cannot be returned or is null.java.lang.NullPointerException
- If the property doesn't existpublic void setProperty(java.lang.String name, java.lang.Object value)
setProperty
in interface PropertyChangeSource
name
- The name of the property to be setvalue
- The new value of the propertyjava.lang.NullPointerException
- If the property doesn't existpublic void firePropertyChange(java.lang.String propertyName, java.lang.Object oldValue, java.lang.Object newValue, java.lang.Object... extraData)
Note: uses PropertyChangeEventX
rather than
PropertyChangeEvent
propertyName
- The property that changed.oldValue
- The value of the property prior to this change.newValue
- The value of the property after this change.extraData
- Extra data to be added to fired PropertyChangeEventPropertyChangeEventX
public void firePropertyChange(java.lang.String propertyName, java.lang.Object newValue)
propertyName
- The property that changed.newValue
- The value of the property after this change.public void fireIndexedPropertyChange(int index, java.lang.String propertyName, java.lang.Object oldValue, java.lang.Object newValue, java.lang.Object... extraData)
Note: instantiates IndexedPropertyChangeEventX
instead of
IndexedPropertyChangeEvent
index
- The element that has changed.propertyName
- The Indexed property containing the changed element.oldValue
- The value of the property prior to this change.newValue
- The value of the property after this change.extraData
- Extra data added to IndexedPropertyChangeEventXIndexedPropertyChangeEventX
public void fireIndexedPropertyChange(int index, java.lang.String propertyName, java.lang.Object newValue)
index
- The element that has changed.propertyName
- The Indexed property containing the changed element.newValue
- The value of the property after this change.public java.lang.String toString()
toString
in class java.lang.Object
public java.beans.PropertyChangeListener[] getPropertyChangeListeners()
getPropertyChangeListeners
in interface PropertyChangeSource
public boolean isPersistant()
isPersistant
in interface ReRunnable
public void setPersistant(boolean persist)
setPersistant
in interface ReRunnable
persist
- if true, this instance is persistent (i.e. a ReRunnable)public void setEventListID(int id)
setEventListID
in interface SimEntity
public void setEventList(BasicEventList el)
public int getEventListID()
getEventListID
in interface SimEntity
public BasicEventList getEventList()
getEventList
in interface SimEventScheduler
EventList
on which this scheduler will
schedule its events (and receive handleSimEvent()
calls from).public java.lang.String[] getAddedProperties()
getAddedProperties
in interface PropertyChangeSource
protected java.lang.String getPropertiesString()
public void clearAddedProperty(java.lang.String propertyName)
propertyName
- the name of the added property to be cleared.public int compareTo(SimEntity other)
compareTo
in interface java.lang.Comparable<SimEntity>
other
- Other SimEntity to compare topublic boolean isClearAddedPropertiesOnReset()
public void setClearAddedPropertiesOnReset(boolean clearAddedPropertiesOnReset)
public void interruptAllWithArgs(java.lang.String eventName, java.lang.Object parameter)
SimEventScheduler
interruptAllWithArgs
in interface SimEventScheduler
eventName
- name of eventparameter
- parameter to matchpublic void interruptAllWithArgs(java.lang.Object parameter)
SimEventScheduler
interruptAllWithArgs
in interface SimEventScheduler
parameter
- parameter to match