Class SimEntity
java.lang.Object
simEngine.SimEntity
- Direct Known Subclasses:
NetNode
This class represents entities, or processes, running in the system.
To create a new type of entity, it should be
extended and
a definition for the body() method given. The body()
method is called by the SimSystem and defines the behaviour of
the entity during the simulation. - Author:
- Federico Granata
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final intState FINISHED: the entity has finished its behaviour.protected static final intState HOLDING: the entity has been caused to wait for a specified number of units of simulation time.protected static final intState RUNNING: the entity can be run by the simulationprotected SimSystemprotected intprotected static final intState WAITING: the entity is waiting for an event. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract voidbody()The method which defines the behaviour of the entity.final SimEventgetEvbuf()intgetId()Get the unique id number assigned to this entitygetName()Get the name of this entityfinal SimPredicateReturns the Predicate which this entity is waiting forvoidpoison()Changes the state of the entity into FINISHED.abstract voidrestart()Restarts the entity after an hold periodvoidsetSimSystem(SimSystem simSystem) final intsimCancel(SimPredicate p, SimEvent ev) Cancel the first event waiting for this entity on the future queue, matched by the predicatep.final intGet the id of the currently running entityfinal voidGet the first event from the deferred queue waiting on the entity, or, if there are none, wait for an event to arrive.final voidGets the first event matching a predicate from the deferred queue, or, if none match, wait for a matching event to arrive.final voidsimHold(double delay) Causes the entity to hold fordelayunits of simulation time.final doublesimHoldFor(double delay) An interruptible hold.final RemoveTokensimPutback(SimEvent ev) Puts an event back on the deferred queue.final RemoveTokensimSchedule(int dest, double delay, int tag) Send an event to another entity, by id number and with no data.final RemoveTokensimSchedule(int dest, double delay, int tag, Object data) Send an event to another entity, by id number with data.final voidExtract the first event waiting for this entity on the deferred queue, matched by the predicatep.final booleansimUnschedule(RemoveToken token) Removes a scheduled event from future or deferred queuefinal voidsimWait()Hold until the entity receives an event.final voidRepeatedlysimWait()until the entity receives an event matched by the specified predicate, all the other received events are discarded.final intCount how many events are waiting for this entity on the deferred queuefinal intCount how many events matching a predicate are waiting for this entity on the deferred queue.abstract voidstart()Starts the activity of the SimEntity, call it the first time before calling execute.
-
Field Details
-
name
-
evbuf
-
state
protected int state -
simSystem
-
RUNNABLE
protected static final int RUNNABLEState RUNNING: the entity can be run by the simulation- See Also:
-
WAITING
protected static final int WAITINGState WAITING: the entity is waiting for an event.- See Also:
-
HOLDING
protected static final int HOLDINGState HOLDING: the entity has been caused to wait for a specified number of units of simulation time.- See Also:
-
FINISHED
protected static final int FINISHEDState FINISHED: the entity has finished its behaviour.- See Also:
-
-
Constructor Details
-
SimEntity
The standard constructor.- Parameters:
name- The name to be associated with this entity
-
-
Method Details
-
setSimSystem
-
getName
-
getId
public int getId()Get the unique id number assigned to this entity- Returns:
- The id number
-
body
protected abstract void body()The method which defines the behaviour of the entity. This method should be overridden in subclasses of SimEntity. -
simHold
public final void simHold(double delay) Causes the entity to hold fordelayunits of simulation time.- Parameters:
delay- The amount of time to hold
-
simHoldFor
public final double simHoldFor(double delay) An interruptible hold. Causes the entity to hold fordelayunits of simulation time.- Parameters:
delay- The amount of time to hold- Returns:
- The amount of time left on the hold (0.0 if no interruptions)
-
simSchedule
Send an event to another entity, by id number with data.- Parameters:
dest- The unique id number of the destination entitydelay- How long from the current simulation time the event should be senttag- An user-defined number representing the type of event.data- A reference to data to be sent with the event.- Returns:
- a token to remove scheduled message
-
simSchedule
Send an event to another entity, by id number and with no data.- Parameters:
dest- The unique id number of the destination entitydelay- How long from the current simulation time the event should be senttag- An user-defined number representing the type of event.- Returns:
- a token to remove scheduled message
-
simUnschedule
Removes a scheduled event from future or deferred queue- Parameters:
token- the token to remove the element- Returns:
- true if event was found and removed, false if it was not found
-
simWait
public final void simWait()Hold until the entity receives an event. -
simWaiting
Count how many events matching a predicate are waiting for this entity on the deferred queue.- Parameters:
p- The event selection predicate- Returns:
- The count of matching events
-
simWaiting
public final int simWaiting()Count how many events are waiting for this entity on the deferred queue- Returns:
- The count of events
-
simSelect
Extract the first event waiting for this entity on the deferred queue, matched by the predicatep.- Parameters:
p- An event selection predicate
-
simCancel
Cancel the first event waiting for this entity on the future queue, matched by the predicatep. Returns the number of events cancelled (0 or 1).- Parameters:
p- An event selection predicateev- The event matched is copied intoevif it points to a blank event, or discarded ifevisnull
-
simWaitFor
RepeatedlysimWait()until the entity receives an event matched by the specified predicate, all the other received events are discarded.- Parameters:
p- The event selection predicate
-
simPutback
Puts an event back on the deferred queue.- Parameters:
ev- The event to reinsert- Returns:
- a token to remove scheduled message
-
simGetNext
Gets the first event matching a predicate from the deferred queue, or, if none match, wait for a matching event to arrive.- Parameters:
p- The predicate to match
-
simGetNext
public final void simGetNext()Get the first event from the deferred queue waiting on the entity, or, if there are none, wait for an event to arrive. -
simCurrent
public final int simCurrent()Get the id of the currently running entity- Returns:
- A unique entity id number
-
getEvbuf
-
restart
public abstract void restart()Restarts the entity after an hold period -
poison
public void poison()Changes the state of the entity into FINISHED. -
start
public abstract void start()Starts the activity of the SimEntity, call it the first time before calling execute. -
getWaitingPred
Returns the Predicate which this entity is waiting for- Returns:
-