24 #include <base-logging/Logging.hpp> 33 std::set<StateId> referencedStates;
34 std::map<StateId, State>::const_iterator statesIt = mStates.begin();
36 for(; statesIt != mStates.end(); ++statesIt)
38 const State& state = statesIt->second;
40 std::vector<Transition>::const_iterator transitionsIt = transitions.begin();
42 for(; transitionsIt != transitions.end(); ++transitionsIt)
44 referencedStates.insert(transitionsIt->getTargetStateId());
45 referencedStates.insert(transitionsIt->getSourceStateId());
49 std::set<StateId>::const_iterator referencedStatesIt = referencedStates.begin();
50 for(; referencedStatesIt != referencedStates.end(); ++referencedStatesIt)
52 statesIt = mStates.find(*referencedStatesIt);
53 if(statesIt == mStates.end())
55 std::string msg =
"Unknown state '" + *referencedStatesIt +
"' referred to in statemachine";
56 LOG_ERROR(
"%s", msg.c_str());
57 throw std::runtime_error(msg);
64 mCurrentStateId = mInitialStateId;
69 std::map<StateId, State>::const_iterator it = mStates.find(state.
getId());
70 if(it != mStates.end())
72 std::string msg =
"State '" + state.
getId() +
"' already defined";
73 LOG_ERROR(
"%s", msg.c_str());
74 throw std::runtime_error(msg);
76 mStates[state.
getId()] = state;
82 std::map<StateId, State>::iterator it = mStates.begin();
83 for(; it != mStates.end(); ++it)
85 std::vector<Transition> transitions = it->second.getTransitions();
86 std::vector<Transition>::const_iterator transitionIt = transitions.begin();
87 for(; transitionIt != transitions.end(); ++transitionIt)
89 mRoleMapping.
addRole(transitionIt->getSenderRole());
90 mRoleMapping.
addRole(transitionIt->getReceiverRole());
96 void StateMachine::generateDefaultTransitions()
98 std::map<StateId, State>::iterator it = mStates.begin();
99 for(; it != mStates.end(); ++it)
101 it->second.generateDefaultTransitions();
105 void StateMachine::generateDefaultStates()
110 addState(*dynamic_cast<State*>(¬Understood));
113 addState(*dynamic_cast<State*>(&cancelling));
116 addState(*dynamic_cast<State*>(&cancelSuccess));
119 addState(*dynamic_cast<State*>(&cancelFailure));
122 addState(*dynamic_cast<State*>(&generalFailure));
124 }
catch (
const std::runtime_error& e)
126 LOG_FATAL(
"%s", e.what());
134 if(mCurrentStateId.empty())
136 throw std::runtime_error(
"Statemachine has not been properly initialized: current state not set");
138 std::map<StateId, State>::iterator it = mStates.find(mCurrentStateId);
139 if(it == mStates.end())
141 throw std::runtime_error(
"Statemachine has not been properly initialized: current state not found");
150 if(mCurrentStateId.empty())
152 throw std::runtime_error(
"Statemachine has not been properly initialized: current state not set");
154 std::map<StateId, State>::const_iterator it = mStates.find(mCurrentStateId);
155 if(it == mStates.end())
157 throw std::runtime_error(
"Statemachine has not been properly initialized: current state not found");
172 AgentIDList::const_iterator it = receivers.begin();
173 for(; it != receivers.end(); ++it)
181 LOG_DEBUG(
"StateMachine consumeSubStateMachineMessage");
189 LOG_DEBUG(
"StateMachine consumeMessage");
201 catch(
const std::exception& e)
203 LOG_DEBUG(
"StateMachine consumeMessage trying substatemachine proxied transition");
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)
239 statemachine << it->second.toString();
241 return statemachine.str();
247 std::stringstream str;
248 str <<
"embedded state machine: protocol: '" << name
249 <<
"', from: '" << from
250 <<
"', proxied_to: '" << proxiedTo <<
"'\n";
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)
262 ss << it->second.toXML();
264 ss <<
"</scxml>" << std::endl;
271 std::stringstream str;
272 str <<
"<subprotocol name=\"" << name <<
"\" " 273 <<
"from=\"" << from <<
"\" " 274 <<
"proxied_to=\"" << proxiedTo <<
"\"";
void addMessage(const ACLMessage &msg)
void consumeMessage(const ACLMessage &msg)
describes the structure and operation of a transition of the state machine
bool isFinished() const
method that returns whether the state is a finished state or not. In the case of a state with embedde...
AgentID getSender() const
implements a state of the StateMachine class
const State & getCurrentState() const
void setSelf(const AgentID &id)
static const StateId GENERAL_FAILURE_STATE
describes the structure and operation of a state machine
static const StateId CONVERSATION_CANCEL_FAILURE
std::string toXML() const
bool inFailureState() const
void consumeSubStateMachineMessage(const ACLMessage &msg, const fipa::acl::StateMachine &stateMachine, const fipa::acl::RoleMapping &roleMapping, int numberOfSubConversations)
Role getReceiverRole() const
std::vector< AgentID > AgentIDList
Role getSenderRole() const
const Transition & getTransition(const ACLMessage &msg, const MessageArchive &archive, const RoleMapping &roleMapping) const
Check whether the received message triggers a transition.
AgentIDList getAllReceivers() const
void setSelf(const AgentID &self)
std::string toString() const
void updateRoleMapping(const ACLMessage &msg, const Transition &transition)
bool inFinalState() const
void addRole(const Role &role)
State & getCurrentStateModifiably()
std::string getId() const
getter method for the uid field of the class
describes the structure and operation of a state, as part of a state machine
This class provides a representation of a message conforming to the FIPA specification SC00061...
const std::vector< Transition > & getTransitions() const
void addState(const State &state)
void consumeSubStateMachineMessage(const ACLMessage &msg, const fipa::acl::StateMachine &stateMachine, int numberOfSubConversations)
void addExpectedAgent(const Role &role, const AgentID &agent)
static const StateId CONVERSATION_CANCEL_SUCCESS
StateId getTargetStateId() const
std::string toXML() const
std::string toString() const
Implements the general AgentID functionality, which is present throughout the fipa specifications(FIP...
Foundation of Physical Intelligent Agents.
const Transition & getSubstateMachineProxiedTransition(const ACLMessage &msg, const MessageArchive &archive, const RoleMapping &roleMapping)
Check whether the received message triggers a substatemachine proxied transition. This method is not ...