fipa_acl  1.4
Public Member Functions | Protected Member Functions | Friends | List of all members
fipa::acl::StateMachine Class Reference

#include <statemachine.h>

Public Member Functions

std::map< StateId, StategetStates () const
 
const RoleMappinggetRoleMapping () const
 
const StategetCurrentState () const
 
StategetCurrentStateModifiably ()
 
void setCurrentStateId (const StateId &stateId)
 
StateId getCurrentStateId () const
 
StateId getInitialStateId () const
 
void setProtocol (const fipa::acl::Protocol &protocol)
 
fipa::acl::Protocol getProtocol () const
 
void setSelf (const AgentID &self)
 
void consumeMessage (const ACLMessage &msg)
 
void consumeSubStateMachineMessage (const ACLMessage &msg, const fipa::acl::StateMachine &stateMachine, int numberOfSubConversations)
 
bool inFinalState () const
 
bool inFailureState () const
 
bool cancelled () const
 
std::string toString () const
 
std::string toXML () const
 

Protected Member Functions

void validate () const
 
void reset ()
 
void setInitialState (const StateId &stateId)
 
void addState (const State &state)
 
void updateRoles ()
 
void updateRoleMapping (const ACLMessage &msg, const Transition &transition)
 

Friends

class StateMachineReader
 

Detailed Description

Definition at line 22 of file statemachine.h.

Member Function Documentation

void fipa::acl::StateMachine::addState ( const State state)
protected

Add a state to the state machine – the state might already contain transition definitions in order to check for properly defined transitions, validate needs to be called

Definition at line 67 of file statemachine.cpp.

68 {
69  std::map<StateId, State>::const_iterator it = mStates.find(state.getId());
70  if(it != mStates.end())
71  {
72  std::string msg = "State '" + state.getId() + "' already defined";
73  LOG_ERROR("%s", msg.c_str());
74  throw std::runtime_error(msg);
75  } else {
76  mStates[state.getId()] = state;
77  }
78 }
bool fipa::acl::StateMachine::cancelled ( ) const

Check whether the conversation has been successfully cancelled

Returns
true if the conversation was successfully cancelled

Definition at line 227 of file statemachine.cpp.

228 {
229  return mCurrentStateId == State::CONVERSATION_CANCEL_SUCCESS;
230 }
static const StateId CONVERSATION_CANCEL_SUCCESS
Definition: state.h:106
void fipa::acl::StateMachine::consumeMessage ( const ACLMessage msg)

Consume a message If this is the first message it will initialize the roles for the first state

Parameters
msgMessage which should be consumed
Exceptions
std::runtime_errorif message could not be consumed

Definition at line 187 of file statemachine.cpp.

188 {
189  LOG_DEBUG("StateMachine consumeMessage");
190 
191  try
192  {
193  const State& currentState = getCurrentState();
194  const Transition& transition = currentState.getTransition(msg, mMessageArchive, mRoleMapping);
195  updateRoleMapping(msg, transition);
196  mMessageArchive.addMessage(msg);
197 
198  // Perform transition
199  mCurrentStateId = transition.getTargetStateId();
200  }
201  catch(const std::exception& e)
202  {
203  LOG_DEBUG("StateMachine consumeMessage trying substatemachine proxied transition");
204  // Retry with substatemachineproxied transition
205  State& currentState = getCurrentStateModifiably();
206  const Transition& transition = currentState.getSubstateMachineProxiedTransition(msg, mMessageArchive, mRoleMapping);
207  updateRoleMapping(msg, transition);
208  mMessageArchive.addMessage(msg);
209 
210  // Perform transition
211  mCurrentStateId = transition.getTargetStateId();
212  }
213 }
void addMessage(const ACLMessage &msg)
void updateRoleMapping(const ACLMessage &msg, const Transition &transition)
State & getCurrentStateModifiably()
const State & getCurrentState() const
void fipa::acl::StateMachine::consumeSubStateMachineMessage ( const ACLMessage msg,
const fipa::acl::StateMachine stateMachine,
int  numberOfSubConversations 
)

Consume a message meant for sub state machine. The given stateMachine is only used to be copied, if necessary.

Parameters
msgMessage which should be consumed
Exceptions
std::runtime_errorif message could not be consumed

Definition at line 179 of file statemachine.cpp.

180 {
181  LOG_DEBUG("StateMachine consumeSubStateMachineMessage");
182 
183  State& currentState = getCurrentStateModifiably();
184  currentState.consumeSubStateMachineMessage(msg, stateMachine, mRoleMapping, numberOfSubConversations);
185 }
State & getCurrentStateModifiably()
const State & fipa::acl::StateMachine::getCurrentState ( ) const

Get current state.

Exceptions
std::runtime_errorif statemachine has not been properly initialized

Definition at line 148 of file statemachine.cpp.

149 {
150  if(mCurrentStateId.empty())
151  {
152  throw std::runtime_error("Statemachine has not been properly initialized: current state not set");
153  } else {
154  std::map<StateId, State>::const_iterator it = mStates.find(mCurrentStateId);
155  if(it == mStates.end())
156  {
157  throw std::runtime_error("Statemachine has not been properly initialized: current state not found");
158  }
159  return it->second;
160  }
161 }
StateId fipa::acl::StateMachine::getCurrentStateId ( ) const
inline

Get the current state

Returns
state id

Definition at line 129 of file statemachine.h.

129 { return mCurrentStateId; }
State & fipa::acl::StateMachine::getCurrentStateModifiably ( )

Get current state. Non const!

Exceptions
std::runtime_errorif statemachine has not been properly initialized

Definition at line 132 of file statemachine.cpp.

133 {
134  if(mCurrentStateId.empty())
135  {
136  throw std::runtime_error("Statemachine has not been properly initialized: current state not set");
137  } else {
138  std::map<StateId, State>::iterator it = mStates.find(mCurrentStateId);
139  if(it == mStates.end())
140  {
141  throw std::runtime_error("Statemachine has not been properly initialized: current state not found");
142  }
143  return it->second;
144  }
145 }
StateId fipa::acl::StateMachine::getInitialStateId ( ) const
inline

Get the id initial state

Returns
id of the initial state

Definition at line 135 of file statemachine.h.

135 { return mInitialStateId; }
fipa::acl::Protocol fipa::acl::StateMachine::getProtocol ( ) const
inline

Get protocol (which is set by the initiating message)

Returns
Protocol

Definition at line 146 of file statemachine.h.

146 { return mProtocol; }
const RoleMapping& fipa::acl::StateMachine::getRoleMapping ( ) const
inline

Get the RoleMapping

Definition at line 109 of file statemachine.h.

109 { return mRoleMapping; }
std::map<StateId, State> fipa::acl::StateMachine::getStates ( ) const
inline

Get the states mapping

Returns
states container

Definition at line 104 of file statemachine.h.

104 { return mStates; }
bool fipa::acl::StateMachine::inFailureState ( ) const

Check if state machine is in a known failure state

Returns
true if the current state of the machine is in a (known) error state

Definition at line 222 of file statemachine.cpp.

223 {
224  return mCurrentStateId == State::GENERAL_FAILURE_STATE || mCurrentStateId == State::CONVERSATION_CANCEL_FAILURE;
225 }
static const StateId GENERAL_FAILURE_STATE
Definition: state.h:108
static const StateId CONVERSATION_CANCEL_FAILURE
Definition: state.h:107
bool fipa::acl::StateMachine::inFinalState ( ) const

Check if the state machine is in a final state, i.e. the conversation has ended

Returns
true if the current state of the state machine is final

Definition at line 215 of file statemachine.cpp.

216 {
217  const State& currentState = getCurrentState();
218  // Use isFinished instead of isFinal
219  return currentState.isFinished();
220 }
const State & getCurrentState() const
void fipa::acl::StateMachine::reset ( )
protected

Reset the current state to the initial state

Definition at line 62 of file statemachine.cpp.

63 {
64  mCurrentStateId = mInitialStateId;
65 }
void fipa::acl::StateMachine::setCurrentStateId ( const StateId stateId)
inline

Definition at line 123 of file statemachine.h.

123 { mCurrentStateId = stateId; }
void fipa::acl::StateMachine::setInitialState ( const StateId stateId)
inlineprotected

Set the initial state

Parameters
stateIdState to which the state machine should be set

Definition at line 79 of file statemachine.h.

79 { mInitialStateId = stateId; }
void fipa::acl::StateMachine::setProtocol ( const fipa::acl::Protocol protocol)
inline

Set the protocol for this conversation

Definition at line 140 of file statemachine.h.

140 { mProtocol = protocol; }
void fipa::acl::StateMachine::setSelf ( const AgentID self)

Set self agents id – can only be called once per state machine

Definition at line 163 of file statemachine.cpp.

164 {
165  mRoleMapping.setSelf(self);
166 }
void setSelf(const AgentID &id)
Definition: role.cpp:42
std::string fipa::acl::StateMachine::toString ( ) const

Convert statemachine to string

Definition at line 232 of file statemachine.cpp.

233 {
234  std::stringstream statemachine;
235  statemachine << "Statemachine: (" << mProtocol << ")\n";
236  std::map<StateId, State>::const_iterator it = mStates.begin();
237  for(; it != mStates.end(); ++it)
238  {
239  statemachine << it->second.toString();
240  }
241  return statemachine.str();
242 }
std::string fipa::acl::StateMachine::toXML ( ) const

Definition at line 255 of file statemachine.cpp.

256 {
257  std::stringstream ss;
258  ss << "<scxml version=\"1.0\" initial=\"1\">" << std::endl;
259  std::map<StateId, State>::const_iterator it = mStates.begin();
260  for(; it != mStates.end(); ++it)
261  {
262  ss << it->second.toXML();
263  }
264  ss << "</scxml>" << std::endl;
265 
266  return ss.str();
267 }
void fipa::acl::StateMachine::updateRoleMapping ( const ACLMessage msg,
const Transition transition 
)
protected

Update the role mapping

Parameters
msgMessage
transitionTransition which is associated with the given message

Definition at line 168 of file statemachine.cpp.

169 {
170  mRoleMapping.addExpectedAgent(transition.getSenderRole(), msg.getSender());
171  AgentIDList receivers = msg.getAllReceivers();
172  AgentIDList::const_iterator it = receivers.begin();
173  for(; it != receivers.end(); ++it)
174  {
175  mRoleMapping.addExpectedAgent(transition.getReceiverRole(), *it);
176  }
177 }
std::vector< AgentID > AgentIDList
Definition: agent_id.h:20
void addExpectedAgent(const Role &role, const AgentID &agent)
Definition: role.cpp:83
void fipa::acl::StateMachine::updateRoles ( )
protected

Given all states update the roles

Definition at line 80 of file statemachine.cpp.

81 {
82  std::map<StateId, State>::iterator it = mStates.begin();
83  for(; it != mStates.end(); ++it)
84  {
85  std::vector<Transition> transitions = it->second.getTransitions();
86  std::vector<Transition>::const_iterator transitionIt = transitions.begin();
87  for(; transitionIt != transitions.end(); ++transitionIt)
88  {
89  mRoleMapping.addRole(transitionIt->getSenderRole());
90  mRoleMapping.addRole(transitionIt->getReceiverRole());
91  }
92  }
93 
94 }
void addRole(const Role &role)
Definition: role.cpp:78
void fipa::acl::StateMachine::validate ( ) const
protected

Check wether the state and transition in this state machine are valid

Exceptions
std::runtime_errorif the machine cannot be properly validated

Definition at line 29 of file statemachine.cpp.

30 {
31  // Testing whether there are any undefined state referred to
32  // Selection of unique state ids
33  std::set<StateId> referencedStates;
34  std::map<StateId, State>::const_iterator statesIt = mStates.begin();
35 
36  for(; statesIt != mStates.end(); ++statesIt)
37  {
38  const State& state = statesIt->second;
39  std::vector<Transition> transitions = state.getTransitions();
40  std::vector<Transition>::const_iterator transitionsIt = transitions.begin();
41 
42  for(; transitionsIt != transitions.end(); ++transitionsIt)
43  {
44  referencedStates.insert(transitionsIt->getTargetStateId());
45  referencedStates.insert(transitionsIt->getSourceStateId());
46  }
47  }
48 
49  std::set<StateId>::const_iterator referencedStatesIt = referencedStates.begin();
50  for(; referencedStatesIt != referencedStates.end(); ++referencedStatesIt)
51  {
52  statesIt = mStates.find(*referencedStatesIt);
53  if(statesIt == mStates.end())
54  {
55  std::string msg = "Unknown state '" + *referencedStatesIt + "' referred to in statemachine";
56  LOG_ERROR("%s", msg.c_str());
57  throw std::runtime_error(msg);
58  }
59  }
60 }

Friends And Related Function Documentation

friend class StateMachineReader
friend

Definition at line 24 of file statemachine.h.


The documentation for this class was generated from the following files: