public class FSMBehaviour extends SerialBehaviour
CompositeBehaviour
that executes its children
behaviours according to a FSM defined by the user. More specifically
each child represents a state in the FSM.
The class provides methods to register states (sub-behaviours) and
transitions that defines how sub-behaviours will be scheduled.
At a minimum, the following steps are needed in order to properly
define a FSMBehaviour
:
registerFirstState
;
registerLastState
;
registerState
;
registerTransition
;
registerDefaultTransition
is also useful
in order to register a default transition from a state to another state
independently on the termination event of the source state.
SequentialBehaviour
,
ParallelBehaviour
,
Serialized FormModifier and Type | Field and Description |
---|---|
protected java.lang.String |
currentName |
protected List |
lastStates |
Constructor and Description |
---|
FSMBehaviour()
Default constructor, does not set the owner agent.
|
FSMBehaviour(Agent a)
This constructor sets the owner agent.
|
Modifier and Type | Method and Description |
---|---|
protected boolean |
checkTermination(boolean currentDone,
int currentResult)
Check whether this
FSMBehaviour must terminate. |
void |
deregisterDefaultTransition(java.lang.String source)
Deregister the default transition from a given source state.
|
Behaviour |
deregisterState(java.lang.String name)
Deregister a state of this
FSMBehaviour . |
void |
deregisterTransition(java.lang.String source,
int event)
Deregister the transition from a given source state and identfied by a
given termination event.
|
protected void |
forceTransitionTo(java.lang.String next)
Temporarily disregards the FSM structure, and jumps to the given
state.
|
Collection |
getChildren()
Return a Collection view of the children of
this
SequentialBehaviour |
protected Behaviour |
getCurrent()
Get the current child
|
int |
getLastExitValue()
Retrieve the exit value of the most recently executed
child.
|
java.lang.String |
getName(Behaviour state)
Retrieve the name of the FSM state associated to the given child
behaviour.
|
protected Behaviour |
getPrevious()
Get the previously executed child
|
Behaviour |
getState(java.lang.String name)
Retrieve the child behaviour associated to the FSM state with
the given name.
|
protected void |
handleInconsistentFSM(java.lang.String current,
int event) |
protected void |
handleStateEntered(Behaviour state) |
boolean |
hasDefaultTransition(java.lang.String source)
Check if a default transition exits from a given source state.
|
int |
onEnd()
Override the onEnd() method to return the exit value of the
last executed state.
|
void |
registerDefaultTransition(java.lang.String s1,
java.lang.String s2)
Register a default transition in the FSM defining the policy for
children scheduling of this
FSMBehaviour . |
void |
registerDefaultTransition(java.lang.String s1,
java.lang.String s2,
java.lang.String[] toBeReset)
Register a default transition in the FSM defining the policy for
children scheduling of this
FSMBehaviour . |
void |
registerFirstState(Behaviour state,
java.lang.String name)
Register a
Behaviour as the initial state of this
FSMBehaviour . |
void |
registerLastState(Behaviour state,
java.lang.String name)
Register a
Behaviour as a final state of this
FSMBehaviour . |
void |
registerState(Behaviour state,
java.lang.String name)
Register a
Behaviour as a state of this
FSMBehaviour . |
void |
registerTransition(java.lang.String s1,
java.lang.String s2,
int event)
Register a transition in the FSM defining the policy for
children scheduling of this
FSMBehaviour . |
void |
registerTransition(java.lang.String s1,
java.lang.String s2,
int event,
java.lang.String[] toBeReset)
Register a transition in the FSM defining the policy for
children scheduling of this
FSMBehaviour . |
void |
reset()
Put this FSMBehaviour back in the initial condition.
|
void |
resetStates(java.lang.String[] states)
Reset the children behaviours registered in the states indicated in
the
states parameter. |
protected void |
scheduleFirst()
Prepare the first child for execution.
|
protected void |
scheduleNext(boolean currentDone,
int currentResult)
This method schedules the next child to be executed.
|
java.lang.String |
stringifyTransitionTable() |
action, done, resetChildren, setAgent
block, block, getAgent, getBehaviourName, getDataStore, getParent, isRunnable, onStart, restart, root, setBehaviourName, setDataStore
protected List lastStates
protected java.lang.String currentName
public FSMBehaviour()
public FSMBehaviour(Agent a)
a
- The agent this behaviour belongs to.public void registerState(Behaviour state, java.lang.String name)
Behaviour
as a state of this
FSMBehaviour
. When the FSM reaches this state
the registered Behaviour
will be executed.state
- The Behaviour
representing the statename
- The name identifying the state.public void registerFirstState(Behaviour state, java.lang.String name)
Behaviour
as the initial state of this
FSMBehaviour
.state
- The Behaviour
representing the statename
- The name identifying the state.public void registerLastState(Behaviour state, java.lang.String name)
Behaviour
as a final state of this
FSMBehaviour
. When the FSM reaches this state
the registered Behaviour
will be executed and,
when completed, the FSMBehaviour
will terminate too.state
- The Behaviour
representing the statename
- The name identifying the state.public Behaviour deregisterState(java.lang.String name)
FSMBehaviour
.name
- The name of the state to be deregistered.public void registerTransition(java.lang.String s1, java.lang.String s2, int event)
FSMBehaviour
.s1
- The name of the state this transition starts froms2
- The name of the state this transition leads toevent
- The termination event that fires this transition
as returned by the onEnd()
method of the
Behaviour
representing state s1.Behaviour.onEnd()
public void registerTransition(java.lang.String s1, java.lang.String s2, int event, java.lang.String[] toBeReset)
FSMBehaviour
.
When this transition is fired the states indicated in the
toBeReset
parameter are reset. This is
particularly useful for transitions that lead to states that
have already been visited.s1
- The name of the state this transition starts froms2
- The name of the state this transition leads toevent
- The termination event that fires this transition
as returned by the onEnd()
method of the
Behaviour
representing state s1.toBeReset
- An array of strings including the names of
the states to be reset.Behaviour.onEnd()
public void registerDefaultTransition(java.lang.String s1, java.lang.String s2)
FSMBehaviour
.
This transition will be fired when state s1 terminates with
an event that is not explicitly associated to any transition.s1
- The name of the state this transition starts froms2
- The name of the state this transition leads topublic void registerDefaultTransition(java.lang.String s1, java.lang.String s2, java.lang.String[] toBeReset)
FSMBehaviour
.
This transition will be fired when state s1 terminates with
an event that is not explicitly associated to any transition.
When this transition is fired the states indicated in the
toBeReset
parameter are reset. This is
particularly useful for transitions that lead to states that
have already been visited.s1
- The name of the state this transition starts froms2
- The name of the state this transition leads totoBeReset
- An array of strings including the names of
the states to be reset.public void deregisterTransition(java.lang.String source, int event)
source
- The name of the source stateevent
- The termination event that identifies the transition to be removedpublic void deregisterDefaultTransition(java.lang.String source)
source
- The name of the source statepublic boolean hasDefaultTransition(java.lang.String source)
source
- The name of the source statetrue
if a default transition exits from the given source state. false
otherwise.public Behaviour getState(java.lang.String name)
Behaviour
representing the state whose
name is name
, or null
if no such
behaviour exists.public java.lang.String getName(Behaviour state)
Behaviour
state, or null
if the given
behaviour is not a child of this FSM behaviour.public int getLastExitValue()
public int onEnd()
protected void scheduleFirst()
Behaviour
registered as the first state of this
FSMBehaviour
scheduleFirst
in class CompositeBehaviour
CompositeBehaviour.scheduleFirst()
protected void scheduleNext(boolean currentDone, int currentResult)
scheduleNext
in class CompositeBehaviour
currentDone
- a flag indicating whether the just executed
child has completed or not.currentResult
- the termination value (as returned by
onEnd()
) of the just executed child in the case this
child has completed (otherwise this parameter is meaningless)CompositeBehaviour.scheduleNext(boolean, int)
protected void handleInconsistentFSM(java.lang.String current, int event)
protected void handleStateEntered(Behaviour state)
protected boolean checkTermination(boolean currentDone, int currentResult)
FSMBehaviour
must terminate.checkTermination
in class CompositeBehaviour
currentDone
- a flag indicating whether the just executed
child has completed or not.currentResult
- the termination value (as returned by
onEnd()
) of the just executed child in the case this
child has completed (otherwise this parameter is meaningless)CompositeBehaviour.checkTermination(boolean, int)
protected Behaviour getCurrent()
getCurrent
in class CompositeBehaviour
CompositeBehaviour.getCurrent()
public Collection getChildren()
SequentialBehaviour
getChildren
in class CompositeBehaviour
CompositeBehaviour.getChildren()
protected void forceTransitionTo(java.lang.String next)
GOTO
statement
between states, and replaces the currently active state without
considering the trigger event or whether a transition was
registered. It should be used only to handle exceptional
conditions, if default transitions are not effective enough.next
- The name of the state to jump to at the next FSM
cheduling quantum. If the FSM has no state with the given name,
this method does nothing.protected Behaviour getPrevious()
CompositeBehaviour.getCurrent()
public void reset()
reset
in class CompositeBehaviour
public void resetStates(java.lang.String[] states)
states
parameter.states
- the names of the states that have to be resetpublic java.lang.String stringifyTransitionTable()