Interface Simulation
-
- All Implemented Interfaces:
-
java.lang.Runnable
public interface Simulation<T, P extends Position<out P>> implements Runnable
This interface forces simulations to be independent threads, and make them controllable from an external console.
-
-
Method Summary
Modifier and Type Method Description abstract void
addOutputMonitor(OutputMonitor<T, P> op)
Adds an OutputMonitor to this simulation. abstract Environment<T, P>
getEnvironment()
Allows to access the current environment. abstract Optional<Throwable>
getError()
abstract Status
getStatus()
Allows to access the current status. abstract long
getStep()
Allows to access the current simulation step. abstract Time
getTime()
Allows to know which is the current simulation time. abstract CompletableFuture<Void>
goToStep(long steps)
Executes a certain number of steps, then pauses it. abstract CompletableFuture<Void>
goToTime(Time t)
Executes the simulation until the target time is reached, then pauses it. abstract void
neighborAdded(Node<T> node, Node<T> n)
This method must get called in case a communication link connecting two nodes gets created during the simulation. abstract void
neighborRemoved(Node<T> node, Node<T> n)
This method must get called in case a a communication link connecting two nodes gets broken during the simulation. abstract void
nodeAdded(Node<T> node)
This method must get called in case a node is added to the environment during the simulation and after its neighborhood has been computed (or can be consistently computed by the simulated environment). abstract void
nodeMoved(Node<T> node)
This method must get called in case a node is moved in the environment during the simulation and after its neighborhood has been computed (or can be consistently computed by the simulated environment). abstract void
nodeRemoved(Node<T> node, Neighborhood<T> oldNeighborhood)
This method must get called in case a node is removed from the environment during the simulation and after its neighborhood has been computed (or can be consistently computed by the simulated environment). abstract CompletableFuture<Void>
pause()
Sends a pause command to the simulation. abstract CompletableFuture<Void>
play()
Sends a play command to the simulation. abstract void
reactionAdded(Actionable<T> reactionToAdd)
Adds a reaction during the simulation to the scheduler and start to execute it. abstract void
reactionRemoved(Actionable<T> reactionToRemove)
Removes a reaction during the simulation from the scheduler and stop to execute it. abstract void
removeOutputMonitor(OutputMonitor<T, P> op)
Removes an OutputMonitor to this simulation. abstract void
schedule(CheckedRunnable r)
Schedules a runnable to be executed by the Simulation thread, useful for synchronization purposes (e.g. abstract CompletableFuture<Void>
terminate()
Sends a terminate command to the simulation. abstract Status
waitFor(Status s, long timeout, TimeUnit timeunit)
Suspends the caller until the simulation reaches the selected Status or the timeout ends. abstract List<OutputMonitor<T, P>>
getOutputMonitors()
Allows to access a list of all the OutputMonitor currently attached to this simulation. -
-
Method Detail
-
addOutputMonitor
abstract void addOutputMonitor(OutputMonitor<T, P> op)
Adds an OutputMonitor to this simulation.
- Parameters:
op
- the OutputMonitor to add
-
getEnvironment
abstract Environment<T, P> getEnvironment()
Allows to access the current environment.
- Returns:
a reference to the current Environment. The environment is not a copy but back-ends the real environment used in the simulation. Manipulate it carefully
-
getStatus
abstract Status getStatus()
Allows to access the current status.
- Returns:
the current Status of the simulation
-
getStep
abstract long getStep()
Allows to access the current simulation step.
- Returns:
the current step
-
getTime
abstract Time getTime()
Allows to know which is the current simulation time.
- Returns:
the current time
-
goToStep
abstract CompletableFuture<Void> goToStep(long steps)
Executes a certain number of steps, then pauses it.
- Parameters:
steps
- the number of steps to execute- Returns:
a CompletableFuture that will be completed when the steps are executed
-
goToTime
abstract CompletableFuture<Void> goToTime(Time t)
Executes the simulation until the target time is reached, then pauses it.
- Parameters:
t
- the target time- Returns:
a CompletableFuture that will be completed when the target time is reached
-
neighborAdded
abstract void neighborAdded(Node<T> node, Node<T> n)
This method must get called in case a communication link connecting two nodes gets created during the simulation. This method provides dependency and scheduling times re-computation for all the reactions interested by such change.
- Parameters:
node
- the noden
- the second node
-
neighborRemoved
abstract void neighborRemoved(Node<T> node, Node<T> n)
This method must get called in case a a communication link connecting two nodes gets broken during the simulation. This method provides dependency and scheduling times re-computation for all the reactions interested by such change.
- Parameters:
node
- the noden
- the second node
-
nodeAdded
abstract void nodeAdded(Node<T> node)
This method must get called in case a node is added to the environment during the simulation and after its neighborhood has been computed (or can be consistently computed by the simulated environment). This method provides dependency computation and is responsible of correctly scheduling the Node's new reactions.
- Parameters:
node
- the freshly added node
-
nodeMoved
abstract void nodeMoved(Node<T> node)
This method must get called in case a node is moved in the environment during the simulation and after its neighborhood has been computed (or can be consistently computed by the simulated environment). This method provides dependency computation and is responsible of correctly scheduling the Node's reactions.
- Parameters:
node
- the node
-
nodeRemoved
abstract void nodeRemoved(Node<T> node, Neighborhood<T> oldNeighborhood)
This method must get called in case a node is removed from the environment during the simulation and after its neighborhood has been computed (or can be consistently computed by the simulated environment). This method provides dependency computation and is responsible of correctly removing the Node's reactions from the scheduler.
- Parameters:
node
- the freshly removed nodeoldNeighborhood
- the neighborhood of the node as it was before it was removed (used to calculate reverse dependencies)
-
pause
abstract CompletableFuture<Void> pause()
Sends a pause command to the simulation. There is no guarantee on when this command will be actually processed.
- Returns:
a CompletableFuture that will be completed when the simulation is paused
-
play
abstract CompletableFuture<Void> play()
Sends a play command to the simulation. There is no guarantee on when this command will be actually processed.
- Returns:
a CompletableFuture that will be completed when the simulation is played
-
reactionAdded
abstract void reactionAdded(Actionable<T> reactionToAdd)
Adds a reaction during the simulation to the scheduler and start to execute it. The reaction addition is not propagated in the Node entity. To do that call also the method addReaction.
- Parameters:
reactionToAdd
- the reaction to add
-
reactionRemoved
abstract void reactionRemoved(Actionable<T> reactionToRemove)
Removes a reaction during the simulation from the scheduler and stop to execute it. The reaction removal is not propagated in the Node entity. To do that call also the method removeReaction.
- Parameters:
reactionToRemove
- the reaction to remove
-
removeOutputMonitor
abstract void removeOutputMonitor(OutputMonitor<T, P> op)
Removes an OutputMonitor to this simulation. If the OutputMonitor was not among those already added, this method does nothing.
- Parameters:
op
- the OutputMonitor to add
-
schedule
abstract void schedule(CheckedRunnable r)
Schedules a runnable to be executed by the Simulation thread, useful for synchronization purposes (e.g. make sure that the environment is not being changed while the requested operation is being executed). An exception thrown by the passed runnable will make the simulation terminate.
- Parameters:
r
- the runnable to execute
-
terminate
abstract CompletableFuture<Void> terminate()
Sends a terminate command to the simulation. There is no guarantee on when this command will be actually processed.
- Returns:
a CompletableFuture that will be completed when the simulation is terminated
-
waitFor
abstract Status waitFor(Status s, long timeout, TimeUnit timeunit)
Suspends the caller until the simulation reaches the selected Status or the timeout ends. Please note that waiting for a status does not mean that every OutputMonitor will already be notified of the update.
-
getOutputMonitors
abstract List<OutputMonitor<T, P>> getOutputMonitors()
Allows to access a list of all the OutputMonitor currently attached to this simulation.
- Returns:
the list of OutputMonitor
-
-
-
-