public abstract class FactoryBehavior extends Object implements Behavior
Factory behaviors implement lazy initialisation for behaviors. That is, the creation of a behavior is delayed until it is actually needed.
This feature is particularly useful when implementing complex finite-state machine behaviors. Due to the nature of finite-state machines, some states might be never reached in some executions. However, some state behaviors may be quite heavy-weight, e.g., because they depend on database connections. Therefore, it is not reasonable to instantiate them already when the overall finite-state machine is instantiated, but dynamically on demand.
After the first execution, the wrapping factory behavior delegates all method
calls to the created inner behavior. The only exception is the
isFinished()
method which is not delegated until the inner behavior
has been executed for the first time. This ensures that the factory wrapper
is not unscheduled if the inner behavior signalises that it is finished
before the first run (which is acceptable because isFinished()
should only be checked after executing a behavior).
The full life cycle is as follows:
createBehavior()
.Behavior.waitingFor()
pattern of inner behavior
demands for immediate execution.true
: delegate all behavior methods to the inner behavior
and execute it immediately.false
: finish execution and delegate all methods but
isFinished()
to the inner behavior. If called again, delegate
everything to the inner behavior and execute it immediately.
It is possible to specify a flag that makes the factory resettable. If the flag is set, a new inner behavior is set every time the factory is finished and started again.
Constructor and Description |
---|
FactoryBehavior(BehaviorController controller)
Instantiates this factory behavior.
|
FactoryBehavior(BehaviorController controller,
boolean resettableFactory)
Instantiates this factory behavior.
|
Modifier and Type | Method and Description |
---|---|
abstract Behavior |
createBehavior()
Creates the actual behavior.
|
boolean |
isFinished()
Indicates whether the behavior is finished or requires re-scheduling.
|
Result |
result()
Returns the result of the behavior.
|
void |
run()
The implementation of the actual behavior.
|
EventPattern |
waitingFor()
Defines the event pattern for re-scheduling.
|
public FactoryBehavior(BehaviorController controller)
controller
- the controller of this behaviorpublic FactoryBehavior(BehaviorController controller, boolean resettableFactory)
true
means that a new inner behavior is instantiated
every time the factory is finished and started again.controller
- the controller of this behaviorresettableFactory
- a flag whether the factory is resettablepublic abstract Behavior createBehavior()
public EventPattern waitingFor()
Behavior
waitingFor
in interface Behavior
null
if the behavior is finishedpublic void run()
Behavior
public boolean isFinished()
Behavior
isFinished
in interface Behavior
true
if the behavior is finished,
false
otherwiseCopyright © Aimpulse Intelligent Systems GmbH
All rights reserved.