public class ModelRunner
extends java.lang.Object
The runner is configured by the model it owns. Each real user needs an instance of a runner, as the runner determines the user journey.
| Modifier and Type | Class and Description |
|---|---|
class |
ModelRunner.As |
| Constructor and Description |
|---|
ModelRunner()
Constructor for creating a model runner.
|
| Modifier and Type | Method and Description |
|---|---|
ModelRunner.As |
as(AbstractActor runActor)
After you called this method, the runner will only react in steps that have
explicitly set the specified actor as one of its actors, or that are declared
as "autonomous system reactions".
|
boolean |
canReactTo(java.lang.Class<? extends java.lang.Object> messageClass)
Returns whether at least one step can react to a message of the specified
class.
|
java.util.Optional<Flow> |
getLatestFlow()
Returns the flow the latest step that has been run is contained in.
|
java.util.Optional<Step> |
getLatestStep()
Returns the latest step that has been run by this runner.
|
java.util.Set<java.lang.Class<?>> |
getReactToTypes()
Returns the classes of messages the runner can react to.
|
java.lang.Object[] |
getRecordedMessages()
Returns the recorded messages that the runner reacted to so far.
|
java.lang.String[] |
getRecordedStepNames()
Returns the recorded names of the steps that have been run so far.
|
java.util.Set<Step> |
getStepsThatCanReactTo(java.lang.Class<? extends java.lang.Object> messageClass)
Returns the steps in the model that can react to the specified message class.
|
protected void |
handleException(java.lang.Exception e)
Overwrite this method to control what happens exactly when an exception is
thrown by a system reaction.
|
ModelRunner |
handleUnhandledWith(java.util.function.Consumer<java.lang.Object> unhandledMessageHandler)
Define handler for messages that the runner doesn't react to.
|
ModelRunner |
handleWith(java.util.function.Consumer<StepToBeRun> messageHandler)
Define a custom message handler.
|
boolean |
isRunning()
Returns whether the runner is currently running.
|
ModelRunner |
publishWith(java.util.function.Consumer<java.lang.Object> eventPublisher)
Define a custom publisher for events.
|
<U> java.util.Optional<U> |
reactTo(java.lang.Object... messages)
Call this method to provide several messages to the runner.
|
<T,U> java.util.Optional<U> |
reactTo(T message)
Call this method to provide a message (i.e. command or event object) to the
runner.
|
void |
restart()
Restarts the runner, resetting it to its original defaults ("no flow has been
run, no step has been run").
|
ModelRunner |
run(Model model)
Configures the runner to use the specified model.
|
void |
setLatestStep(Step latestStep)
Sets the latest step run by the runner.
|
ModelRunner |
startRecording()
After calling this method, until recording is stopped, messages and step names
are recorded.
|
void |
stop()
Stops the runner.
|
ModelRunner |
stopRecording()
When calling this method, recording is stopped.
|
public ModelRunner handleWith(java.util.function.Consumer<StepToBeRun> messageHandler)
messageHandler - the custom message handlerpublic ModelRunner handleUnhandledWith(java.util.function.Consumer<java.lang.Object> unhandledMessageHandler)
unhandledMessageHandler - the handler for messages not handled by the runnerpublic ModelRunner publishWith(java.util.function.Consumer<java.lang.Object> eventPublisher)
eventPublisher - the custom event eventPublisherpublic void restart()
public ModelRunner run(Model model)
reactTo(Object).
As a side effect, this method immediately triggers "autonomous system reactions".
model - the model that defines the runner's behaviorpublic ModelRunner.As as(AbstractActor runActor)
runActor - the actor to run aspublic boolean isRunning()
run(Model)public void stop()
run(Model) is called again.public <U> java.util.Optional<U> reactTo(java.lang.Object... messages)
reactTo(Object) is called.U - the return type that you as the user expects.messages - the message objectspublic <T,U> java.util.Optional<U> reactTo(T message)
If it is running, the runner will then check which steps can react. If a
single step can react, the runner will call the message handler with it. If
no step can react, the runner will either call the handler defined with
handleUnhandledWith(Consumer), or if no such handler exists, consume
the message silently.
If more than one step can react, the runner will throw an exception.
After that, the runner will trigger "autonomous system reactions".
Note that if you provide a collection as the first and only argument, this
will be flattened to the objects in the collection, and for each object
reactTo(Object) is called.
See canReactTo(Class) for a description of what "can react" means.
T - the type of messageU - the return type that you as the user expects.message - the message objectMoreThanOneStepCanReact - when more than one step can reactInfiniteRepetition - when a step has an always true condition, or
there is an infinite loop.java.lang.ClassCastException - when type of the returned instance isn't Upublic boolean canReactTo(java.lang.Class<? extends java.lang.Object> messageClass)
A step "can react" if all of the following conditions are met: a) the runner is running b) one of the step's actors matches the actor the runner is run as c) the step's message class is the same or a superclass of the specified message class d) the step has a condition that is true
messageClass - the type of message to check steps forpublic java.util.Set<java.lang.Class<?>> getReactToTypes()
See canReactTo(Class) for a description of what "can react" means.
public java.util.Set<Step> getStepsThatCanReactTo(java.lang.Class<? extends java.lang.Object> messageClass)
messageClass - the class of messagesprotected void handleException(java.lang.Exception e)
e - the exception that has been thrown by the system reactionpublic java.util.Optional<Step> getLatestStep()
public void setLatestStep(Step latestStep)
Use this method if you want to restore some previous state, normally you
should influence the behavior of the runner by calling
reactTo(Object).
latestStep - the latest step runpublic java.util.Optional<Flow> getLatestFlow()
public ModelRunner startRecording()
public ModelRunner stopRecording()
startRecording() is called again.public java.lang.String[] getRecordedStepNames()
If no step has been run, an empty array is returned. For example, this method can used with the assertArrayEquals method of JUnit to compare the actual names of steps that have been run (returned by this method) to the expected step names.
public java.lang.Object[] getRecordedMessages()
If no messages have caused a system reaction, an empty array is returned. For example, this method can used with the assertArrayEquals method of JUnit to compare the actual messages that caused a reaction (returned by this method) to the expected messages.