Package jade.core.behaviours
Class FSMBehaviour
- java.lang.Object
-
- jade.core.behaviours.Behaviour
-
- jade.core.behaviours.CompositeBehaviour
-
- jade.core.behaviours.SerialBehaviour
-
- jade.core.behaviours.FSMBehaviour
-
- All Implemented Interfaces:
Serializable,Serializable
- Direct Known Subclasses:
AchieveREInitiator,AchieveREResponder,ContractNetInitiator,HandlerSelector,ProposeInitiator,ProposeResponder,SSContractNetResponder,SSIteratedAchieveREResponder,SubscriptionInitiator,SubscriptionResponder,TwoPh0Initiator,TwoPh1Initiator,TwoPh2Initiator,TwoPhInitiator,TwoPhResponder
public class FSMBehaviour extends SerialBehaviour
Composite behaviour with Finite State Machine based children scheduling. It is aCompositeBehaviourthat 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:- register a single Behaviour as the initial state of the FSM by calling
the method
registerFirstState; - register one or more Behaviours as the final states of the FSM
by calling the method
registerLastState; - register one or more Behaviours as the intermediate states of the FSM
by calling the method
registerState; - for each state of the FSM, register the transitions to the other
states by calling the method
registerTransition; - the method
registerDefaultTransitionis also useful in order to register a default transition from a state to another state independently on the termination event of the source state.
- Version:
- $Date$ $Revision$
- Author:
- Giovanni Caire - CSELT
- See Also:
SequentialBehaviour,ParallelBehaviour, Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class jade.core.behaviours.Behaviour
Behaviour.RunnableChangedEvent
-
-
Field Summary
Fields Modifier and Type Field Description protected StringcurrentNameprotected ListlastStates-
Fields inherited from class jade.core.behaviours.CompositeBehaviour
currentExecuted
-
Fields inherited from class jade.core.behaviours.Behaviour
myAgent, myEvent, NOTIFY_DOWN, NOTIFY_UP, parent, STATE_BLOCKED, STATE_READY, STATE_RUNNING
-
-
Constructor Summary
Constructors Constructor Description FSMBehaviour()Default constructor, does not set the owner agent.FSMBehaviour(Agent a)This constructor sets the owner agent.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected booleancheckTermination(boolean currentDone, int currentResult)Check whether thisFSMBehaviourmust terminate.voidderegisterDefaultTransition(String source)Deregister the default transition from a given source state.BehaviourderegisterState(String name)Deregister a state of thisFSMBehaviour.voidderegisterTransition(String source, int event)Deregister the transition from a given source state and identfied by a given termination event.protected voidforceTransitionTo(String next)Temporarily disregards the FSM structure, and jumps to the given state.CollectiongetChildren()Return a Collection view of the children of thisSequentialBehaviourprotected BehaviourgetCurrent()Get the current childintgetLastExitValue()Retrieve the exit value of the most recently executed child.StringgetName(Behaviour state)Retrieve the name of the FSM state associated to the given child behaviour.protected BehaviourgetPrevious()Get the previously executed childBehaviourgetState(String name)Retrieve the child behaviour associated to the FSM state with the given name.protected voidhandleInconsistentFSM(String current, int event)protected voidhandleStateEntered(Behaviour state)booleanhasDefaultTransition(String source)Check if a default transition exits from a given source state.intonEnd()Override the onEnd() method to return the exit value of the last executed state.voidregisterDefaultTransition(String s1, String s2)Register a default transition in the FSM defining the policy for children scheduling of thisFSMBehaviour.voidregisterDefaultTransition(String s1, String s2, String[] toBeReset)Register a default transition in the FSM defining the policy for children scheduling of thisFSMBehaviour.voidregisterFirstState(Behaviour state, String name)Register aBehaviouras the initial state of thisFSMBehaviour.voidregisterLastState(Behaviour state, String name)Register aBehaviouras a final state of thisFSMBehaviour.voidregisterState(Behaviour state, String name)Register aBehaviouras a state of thisFSMBehaviour.voidregisterTransition(String s1, String s2, int event)Register a transition in the FSM defining the policy for children scheduling of thisFSMBehaviour.voidregisterTransition(String s1, String s2, int event, String[] toBeReset)Register a transition in the FSM defining the policy for children scheduling of thisFSMBehaviour.voidreset()Put this FSMBehaviour back in the initial condition.voidresetStates(String[] states)Reset the children behaviours registered in the states indicated in thestatesparameter.protected voidscheduleFirst()Prepare the first child for execution.protected voidscheduleNext(boolean currentDone, int currentResult)This method schedules the next child to be executed.StringstringifyTransitionTable()-
Methods inherited from class jade.core.behaviours.SerialBehaviour
handle
-
Methods inherited from class jade.core.behaviours.CompositeBehaviour
action, done, handleBlockEvent, handleRestartEvent, registerAsChild, resetChildren, setAgent
-
Methods inherited from class jade.core.behaviours.Behaviour
actionWrapper, block, block, getAgent, getBehaviourName, getDataStore, getExecutionState, getParent, getRestartCounter, isRunnable, onStart, restart, root, setBehaviourName, setDataStore, setExecutionState
-
-
-
-
Constructor Detail
-
FSMBehaviour
public FSMBehaviour()
Default constructor, does not set the owner agent.
-
FSMBehaviour
public FSMBehaviour(Agent a)
This constructor sets the owner agent.- Parameters:
a- The agent this behaviour belongs to.
-
-
Method Detail
-
registerState
public void registerState(Behaviour state, String name)
Register aBehaviouras a state of thisFSMBehaviour. When the FSM reaches this state the registeredBehaviourwill be executed.- Parameters:
state- TheBehaviourrepresenting the statename- The name identifying the state.
-
registerFirstState
public void registerFirstState(Behaviour state, String name)
Register aBehaviouras the initial state of thisFSMBehaviour.- Parameters:
state- TheBehaviourrepresenting the statename- The name identifying the state.
-
registerLastState
public void registerLastState(Behaviour state, String name)
Register aBehaviouras a final state of thisFSMBehaviour. When the FSM reaches this state the registeredBehaviourwill be executed and, when completed, theFSMBehaviourwill terminate too.- Parameters:
state- TheBehaviourrepresenting the statename- The name identifying the state.
-
deregisterState
public Behaviour deregisterState(String name)
Deregister a state of thisFSMBehaviour.- Parameters:
name- The name of the state to be deregistered.- Returns:
- the Behaviour if any that was registered as the deregistered state.
-
registerTransition
public void registerTransition(String s1, String s2, int event)
Register a transition in the FSM defining the policy for children scheduling of thisFSMBehaviour.- Parameters:
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 theonEnd()method of theBehaviourrepresenting state s1.- See Also:
Behaviour.onEnd()
-
registerTransition
public void registerTransition(String s1, String s2, int event, String[] toBeReset)
Register a transition in the FSM defining the policy for children scheduling of thisFSMBehaviour. When this transition is fired the states indicated in thetoBeResetparameter are reset. This is particularly useful for transitions that lead to states that have already been visited.- Parameters:
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 theonEnd()method of theBehaviourrepresenting state s1.toBeReset- An array of strings including the names of the states to be reset.- See Also:
Behaviour.onEnd()
-
registerDefaultTransition
public void registerDefaultTransition(String s1, String s2)
Register a default transition in the FSM defining the policy for children scheduling of thisFSMBehaviour. This transition will be fired when state s1 terminates with an event that is not explicitly associated to any transition.- Parameters:
s1- The name of the state this transition starts froms2- The name of the state this transition leads to
-
registerDefaultTransition
public void registerDefaultTransition(String s1, String s2, String[] toBeReset)
Register a default transition in the FSM defining the policy for children scheduling of thisFSMBehaviour. 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 thetoBeResetparameter are reset. This is particularly useful for transitions that lead to states that have already been visited.- Parameters:
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.
-
deregisterTransition
public void deregisterTransition(String source, int event)
Deregister the transition from a given source state and identfied by a given termination event.- Parameters:
source- The name of the source stateevent- The termination event that identifies the transition to be removed
-
deregisterDefaultTransition
public void deregisterDefaultTransition(String source)
Deregister the default transition from a given source state.- Parameters:
source- The name of the source state
-
hasDefaultTransition
public boolean hasDefaultTransition(String source)
Check if a default transition exits from a given source state.- Parameters:
source- The name of the source state- Returns:
trueif a default transition exits from the given source state.falseotherwise.
-
getState
public Behaviour getState(String name)
Retrieve the child behaviour associated to the FSM state with the given name.- Returns:
- the
Behaviourrepresenting the state whose name isname, ornullif no such behaviour exists.
-
getName
public String getName(Behaviour state)
Retrieve the name of the FSM state associated to the given child behaviour.- Returns:
- the name of the state represented by
Behaviourstate, ornullif the given behaviour is not a child of this FSM behaviour.
-
getLastExitValue
public int getLastExitValue()
Retrieve the exit value of the most recently executed child. This is also the trigger value that selects the next FSM transition.- Returns:
- the exit value of the last executed state.
-
onEnd
public int onEnd()
Override the onEnd() method to return the exit value of the last executed state.
-
scheduleFirst
protected void scheduleFirst()
Prepare the first child for execution. The first child is theBehaviourregistered as the first state of thisFSMBehaviour- Specified by:
scheduleFirstin classCompositeBehaviour- See Also:
CompositeBehaviour.scheduleFirst()
-
scheduleNext
protected void scheduleNext(boolean currentDone, int currentResult)This method schedules the next child to be executed. It checks whether the current child is completed and, in this case, fires a suitable transition (according to the termination event of the current child) and schedules the child representing the new state.- Specified by:
scheduleNextin classCompositeBehaviour- Parameters:
currentDone- a flag indicating whether the just executed child has completed or not.currentResult- the termination value (as returned byonEnd()) of the just executed child in the case this child has completed (otherwise this parameter is meaningless)- See Also:
CompositeBehaviour.scheduleNext(boolean, int)
-
handleInconsistentFSM
protected void handleInconsistentFSM(String current, int event)
-
handleStateEntered
protected void handleStateEntered(Behaviour state)
-
checkTermination
protected boolean checkTermination(boolean currentDone, int currentResult)Check whether thisFSMBehaviourmust terminate.- Specified by:
checkTerminationin classCompositeBehaviour- Parameters:
currentDone- a flag indicating whether the just executed child has completed or not.currentResult- the termination value (as returned byonEnd()) of the just executed child in the case this child has completed (otherwise this parameter is meaningless)- Returns:
- true when the last child has terminated and it represents a final state. false otherwise
- See Also:
CompositeBehaviour.checkTermination(boolean, int)
-
getCurrent
protected Behaviour getCurrent()
Get the current child- Specified by:
getCurrentin classCompositeBehaviour- See Also:
CompositeBehaviour.getCurrent()
-
getChildren
public Collection getChildren()
Return a Collection view of the children of thisSequentialBehaviour- Specified by:
getChildrenin classCompositeBehaviour- See Also:
CompositeBehaviour.getChildren()
-
forceTransitionTo
protected void forceTransitionTo(String next)
Temporarily disregards the FSM structure, and jumps to the given state. This method acts as a sort ofGOTOstatement 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.- Parameters:
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.
-
getPrevious
protected Behaviour getPrevious()
Get the previously executed child- See Also:
CompositeBehaviour.getCurrent()
-
reset
public void reset()
Put this FSMBehaviour back in the initial condition.- Overrides:
resetin classCompositeBehaviour
-
resetStates
public void resetStates(String[] states)
Reset the children behaviours registered in the states indicated in thestatesparameter.- Parameters:
states- the names of the states that have to be reset
-
stringifyTransitionTable
public String stringifyTransitionTable()
-
-