Package jasima.core.simulation
Class SimProcess<R>
- java.lang.Object
-
- jasima.core.simulation.SimProcess<R>
-
- Type Parameters:
R- The return type of the process. Can beVoid.
- All Implemented Interfaces:
Runnable
public class SimProcess<R> extends Object implements Runnable
Process abstraction for the process-oriented simulation world view. A process is similar to a Java Thread, but its execution can be interrupted/paused until certain events occur or a certain point in simulation time is reached.Behaviour of a SimProcess can be specified by either specifying a
SimProcessUtil.SimAction/SimProcessUtil.SimCallableor alternatively by sub-classing and overriding the methodlifecycle().A
SimProcesscan be in one of the following states ({@see ProcessState}):- PASSIVE
- A process that could be started or resumed by another process or event. This is the initial state of a SimProcess.
- SCHEDULED
- A process that is scheduled for (re-)activation at a certain point in simulation time.
- RUNNING
- A process that is currently executing its lifecycle. At each point in time only a single process can be in state RUNNING.
- TERMINATED
- A process that has completed executing its lifecycle actions normally.
- ERROR
- A processed that finished execution with an unhandled
Exception.
- Since:
- 3.0
- Author:
- torsten.hildebrandt@simplan.de
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSimProcess.MightBlockMarker for all methods that might block, i.e., where execution might not finish at the same simulation time when it was started.static classSimProcess.ProcessStatePossible states of a SimProcess.
-
Constructor Summary
Constructors Constructor Description SimProcess(Simulation sim)SimProcess(Simulation sim, SimProcessUtil.SimAction a)SimProcess(Simulation sim, SimProcessUtil.SimAction a, String name)SimProcess(Simulation sim, SimProcessUtil.SimCallable<R> c)SimProcess(Simulation sim, SimProcessUtil.SimCallable<R> action, String name)SimProcess(Simulation sim, SimProcessUtil.SimRunnable r)SimProcess(Simulation sim, SimProcessUtil.SimRunnable r, String name)SimProcess(Simulation sim, Callable<R> c)SimProcess(Simulation sim, Callable<R> c, String name)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddCompletionNotifier(Consumer<SimProcess<R>> callback)voidawakeAt(double tAbs)Awakes a PASSIVE process at a certain time.voidawakeAt(Instant instant)Awakes a PASSIVE process at a certain time.voidawakeIn(double deltaT)Awakes a PASSIVE process after a certain amount of time.voidawakeIn(long amount, TemporalUnit u)Awakes a PASSIVE process after a certain amount of time.voidawakeIn(Duration d)Awakes a PASSIVE process after a certain amount of time.SimProcess<R>cancel()Cancels execution of a SCHEDULED process and puts it into PASSIVE state.Rget()Returns the result of any computation this process might have completed.Simulation.ErrorHandlergetLocalErrorHandler()StringgetName()SimComponentgetOwner()SimulationgetSim()booleanhasFinished()Returns true if the process has finished (either normally or withn an unhandled Exception).SimProcess<R>join()Blocks the calling process (puts it into PASSIVE state) until the process, this method is called on, has finished.protected Rlifecycle()This is the method to do the real work of the process, i.e., defining its behaviour.SimProcess.ProcessStateprocessState()voidresume()Resumes execution of a PASSIVE process after the current event finished.voidrun()Don't call this method directly.voidsetLocalErrorHandler(Simulation.ErrorHandler h)voidsetOwner(SimComponent owner)SimProcess<R>suspend()Puts the current process into PASSIVE state, waiting re-activation by some other component or event.StringtoString()booleanwaitCondition(ObservableValue<Boolean> triggerCondition)Waits (possibly forever) until some condition, represented by an ObservableValue, evaluates to true.<T1,T2>
booleanwaitCondition(BiFunction<T1,T2,Boolean> triggerCondition, ObservableValue<? extends T1> obs1, ObservableValue<? extends T2> obs2)Same aswaitCondition(Function, ObservableValue), but condition can depend of two values instead of just one.<T> booleanwaitCondition(Function<T,Boolean> triggerCondition, ObservableValue<? extends T> observable)Waits until some condition becomes true.SimProcess<R>waitFor(double deltaT)Pauses execution of the currently RUNNING process for a certain amount of time.SimProcess<R>waitFor(long amount, TemporalUnit u)Pauses execution of the currently RUNNING process for a certain amount of time.SimProcess<R>waitFor(Duration d)Pauses execution of the currently RUNNING process for a certain amount of time.SimProcess<R>waitUntil(double tAbs)Pauses execution of the currently RUNNING process until a certain absolute time.SimProcess<R>waitUntil(Instant instant)Pauses execution of the currently RUNNING process until a certain absolute time.
-
-
-
Constructor Detail
-
SimProcess
public SimProcess(Simulation sim)
-
SimProcess
public SimProcess(Simulation sim, SimProcessUtil.SimRunnable r)
-
SimProcess
public SimProcess(Simulation sim, Callable<R> c)
-
SimProcess
public SimProcess(Simulation sim, SimProcessUtil.SimAction a)
-
SimProcess
public SimProcess(Simulation sim, SimProcessUtil.SimCallable<R> c)
-
SimProcess
public SimProcess(Simulation sim, SimProcessUtil.SimRunnable r, String name)
-
SimProcess
public SimProcess(Simulation sim, Callable<R> c, String name)
-
SimProcess
public SimProcess(Simulation sim, SimProcessUtil.SimAction a, String name)
-
SimProcess
public SimProcess(Simulation sim, SimProcessUtil.SimCallable<R> action, String name)
-
-
Method Detail
-
lifecycle
protected R lifecycle() throws Exception
This is the method to do the real work of the process, i.e., defining its behaviour. If aSimProcessUtil.SimRunnableorCallablewas specified, then these will be executed. If aSimProcessis created by sub-classing, then override this method to define its behaviour.- Returns:
- The process's return value, if any (return null if not needed).
- Throws:
Exception- If there is any uncaught exception during execution.
-
run
public void run()
Don't call this method directly. It is just an internal method and only public to implementRunnable.
-
resume
public void resume()
Resumes execution of a PASSIVE process after the current event finished.
-
awakeIn
public void awakeIn(double deltaT)
Awakes a PASSIVE process after a certain amount of time.
-
awakeIn
public void awakeIn(long amount, TemporalUnit u)Awakes a PASSIVE process after a certain amount of time.
-
awakeIn
public void awakeIn(Duration d)
Awakes a PASSIVE process after a certain amount of time.
-
awakeAt
public void awakeAt(double tAbs)
Awakes a PASSIVE process at a certain time.
-
awakeAt
public void awakeAt(Instant instant)
Awakes a PASSIVE process at a certain time.
-
cancel
public SimProcess<R> cancel()
Cancels execution of a SCHEDULED process and puts it into PASSIVE state.
-
waitFor
public SimProcess<R> waitFor(double deltaT) throws SimProcess.MightBlock
Pauses execution of the currently RUNNING process for a certain amount of time.- Throws:
SimProcess.MightBlock
-
waitFor
public SimProcess<R> waitFor(long amount, TemporalUnit u) throws SimProcess.MightBlock
Pauses execution of the currently RUNNING process for a certain amount of time.- Throws:
SimProcess.MightBlock
-
waitFor
public SimProcess<R> waitFor(Duration d) throws SimProcess.MightBlock
Pauses execution of the currently RUNNING process for a certain amount of time.- Throws:
SimProcess.MightBlock
-
waitUntil
public SimProcess<R> waitUntil(double tAbs) throws SimProcess.MightBlock
Pauses execution of the currently RUNNING process until a certain absolute time.- Throws:
SimProcess.MightBlock
-
waitUntil
public SimProcess<R> waitUntil(Instant instant) throws SimProcess.MightBlock
Pauses execution of the currently RUNNING process until a certain absolute time.- Throws:
SimProcess.MightBlock
-
waitCondition
public boolean waitCondition(ObservableValue<Boolean> triggerCondition) throws SimProcess.MightBlock
Waits (possibly forever) until some condition, represented by an ObservableValue, evaluates to true. The condition is first checked immediately upon calling this method and might therefore return immediately.- Parameters:
triggerCondition- The condition to wait for.- Returns:
trueif the condition was initially true (so no wait happened),falseotherwise.- Throws:
SimProcess.MightBlock- To mark potentially blocking behavior.
-
waitCondition
public <T> boolean waitCondition(Function<T,Boolean> triggerCondition, ObservableValue<? extends T> observable) throws SimProcess.MightBlock
Waits until some condition becomes true. The condition can be an arbitrary function returning a boolean value, taking the value of an observable as its parameter. The condition (usually a lambda expression) is evaluated each time the observable value changes.- Parameters:
triggerCondition- A function/expression producing a boolean result.observable- The value used intriggerCondition.- Returns:
trueif the condition was initially true (so no wait happened),falseotherwise.- Throws:
SimProcess.MightBlock- To mark potentially blocking behavior.- See Also:
waitCondition(ObservableValue),waitCondition(BiFunction, ObservableValue, ObservableValue)
-
waitCondition
public <T1,T2> boolean waitCondition(BiFunction<T1,T2,Boolean> triggerCondition, ObservableValue<? extends T1> obs1, ObservableValue<? extends T2> obs2) throws SimProcess.MightBlock
Same aswaitCondition(Function, ObservableValue), but condition can depend of two values instead of just one.
-
suspend
public SimProcess<R> suspend() throws SimProcess.MightBlock
Puts the current process into PASSIVE state, waiting re-activation by some other component or event.- Returns:
thisto allow chaining of calls.- Throws:
SimProcess.MightBlock
-
join
public SimProcess<R> join() throws SimProcess.MightBlock
Blocks the calling process (puts it into PASSIVE state) until the process, this method is called on, has finished.- Returns:
thisto allow chaining of calls.- Throws:
SimProcess.MightBlock
-
get
@Nullable public R get()
Returns the result of any computation this process might have completed. This method might only be called after the process has finished. If the process terminated with an Exception, then this Exception is re-thrown wrapped in a RuntimeException.- Returns:
- The result of this computation.
- Throws:
IllegalStateException- If the process has not finished yet.RuntimeException- Wraps any unhandled exception that might have occurred.
-
hasFinished
public boolean hasFinished()
Returns true if the process has finished (either normally or withn an unhandled Exception).
-
addCompletionNotifier
public void addCompletionNotifier(Consumer<SimProcess<R>> callback)
-
getSim
public Simulation getSim()
-
processState
public SimProcess.ProcessState processState()
-
getLocalErrorHandler
public Simulation.ErrorHandler getLocalErrorHandler()
-
setLocalErrorHandler
public void setLocalErrorHandler(Simulation.ErrorHandler h)
-
getName
public String getName()
-
getOwner
public SimComponent getOwner()
-
setOwner
public void setOwner(SimComponent owner)
-
-