6 #include <base-logging/Logging.hpp> 9 #include <boost/filesystem/path.hpp> 33 FILE* file = fopen(protocolSpec.c_str(),
"r");
38 LOG_ERROR_S <<
"error loading the spec file: '" << protocolSpec <<
"'. File does not exist.";
39 throw std::runtime_error(
"Error loading the specification file: '" + protocolSpec +
"' -- file does not exist");
43 TiXmlDocument file = TiXmlDocument(protocolSpec.c_str());
47 LOG_ERROR_S <<
"error loading the spec file: '" << protocolSpec <<
"'. Please use setProtocolResourceDir(const std::string&) instead to specify the location of your protocol files";
48 throw std::runtime_error(
"Error loading the specification file: '" + protocolSpec +
"' -- tinyxml failed to load the file");
51 LOG_DEBUG_S <<
"loadSpecification: specification file 'protocolSpec'";
52 TiXmlElement* statemachineElement = file.RootElement();
56 boost::filesystem::path path(protocolSpec);
57 statemachine.
setProtocol( path.filename().string());
59 statemachine.generateDefaultTransitions();
60 statemachine.generateDefaultStates();
71 const char *initialState;
73 initialState = statemachineElement->Attribute(StateMachineReader::initial.c_str());
78 throw std::runtime_error(
"Attribute of initial could not be retrieved");
80 LOG_DEBUG_S <<
"parseStateMachineNode: retrieved value of the \"initial\" state: " << std::string(initialState);
82 TiXmlHandle handleSm = TiXmlHandle(statemachineElement);
83 TiXmlElement* stateElement = handleSm.FirstChild(
"state" ).ToElement();
86 LOG_DEBUG_S <<
"parseStateMachineNode: found first state element";
88 LOG_WARN_S <<
"parseStateMachineNode: no state element found";
92 for (; stateElement != NULL; stateElement = stateElement->NextSiblingElement(
"state") )
95 LOG_DEBUG_S <<
"Adding state: " << state.
toString();
107 stateId = stateElement->Attribute(StateMachineReader::id.c_str());
110 state.
setId(std::string(stateId));
113 sprintf(buffer,
"Attribute could not be retrieved %s", StateMachineReader::id.c_str());
114 throw std::runtime_error(std::string(buffer));
117 const char*
final = NULL;
118 final = stateElement->Attribute(StateMachineReader::final.c_str());
121 std::string isFinal(
final);
122 if (isFinal ==
"yes" || isFinal ==
"true" || isFinal ==
"1")
125 }
else if (isFinal ==
"no" || isFinal ==
"false" || isFinal ==
"0")
130 sprintf(buffer,
"Invalid value for attribute \"final\" of state node: %s",
final);
131 throw std::runtime_error(std::string(buffer));
136 TiXmlHandle handleState = TiXmlHandle(stateElement);
137 TiXmlElement *transitionElement = handleState.FirstChildElement(StateMachineReader::transition).ToElement();
138 for (; transitionElement != NULL; transitionElement = transitionElement->NextSiblingElement(StateMachineReader::transition) )
143 LOG_DEBUG_S <<
"parseStateNode: state: " << state.
getId() <<
" -> transition added: from (Role) " << t.
getSenderRole().
getId()
152 TiXmlElement *subProtocolElement = handleState.FirstChildElement(StateMachineReader::subprotocol).ToElement();
153 for (; subProtocolElement != NULL; subProtocolElement = subProtocolElement->NextSiblingElement(StateMachineReader::subprotocol) )
157 const std::string*
name = subProtocolElement->Attribute(StateMachineReader::name);
162 throw new std::runtime_error(
"StateMachineReader::parseStateNode subprotocol is missing 'name' attribute");
165 const std::string*
from = subProtocolElement->Attribute(StateMachineReader::from);
171 throw new std::runtime_error(
"StateMachineReader::parseStateNode mapping is missing 'from' attribute");
174 const std::string*
proxiedTo = subProtocolElement->Attribute(StateMachineReader::proxiedTo);
175 if (proxiedTo != NULL)
182 LOG_DEBUG_S <<
"parseStateNode: state: " << state.
getId() <<
" -> subprotocol added:\n" << esm.
toString();
191 const char *
from = NULL;
192 from = transitionElement->Attribute(StateMachineReader::from.c_str());
197 throw new std::runtime_error(
"StateMachineReader::parseTransitionsNode Transitions is missing 'from' attribute");
200 const char*
to = NULL;
201 to = transitionElement->Attribute(StateMachineReader::to.c_str());
206 throw new std::runtime_error(
"StateMachineReader::parseTransitionsNode transition is missing 'to' attribute");
209 const char* targetState = NULL;
210 targetState = transitionElement->Attribute(StateMachineReader::target.c_str());
216 throw std::runtime_error(
"Could not retrieve attribute for target state");
220 performative = transitionElement->Attribute(StateMachineReader::performative.c_str());
225 throw new std::runtime_error(
"StateMachineReader::parseTransitionsNode transition is missing 'performative' attribute");
State parseStateNode(TiXmlElement *stateElement)
method that parses the <state> element of the spec. file
describes the structure and operation of a transition of the state machine
static const std::string id
void setPerformativeRegExp(const std::string &performativeRegExp)
setter methods for various fields of the class
void setProtocol(const fipa::acl::Protocol &protocol)
void setSenderRole(const Role &role)
void setReceiverRole(const Role &role)
void addEmbeddedStateMachine(fipa::acl::EmbeddedStateMachine embeddedStateMachine)
implements a state of the StateMachine class
static const std::string name
static const std::string target
void setId(const StateId &id)
setter method for the uid field of the state NOTE: maybe should be taken out and name only be allowed...
void setInitialState(const StateId &stateId)
std::string getId() const
getter method for the uid field of the class
Role getReceiverRole() const
static const std::string to
void setFinal(bool final)
setter method for the final field of the class When changing the final state flag make sure you eithe...
An embedded state machine with mappings.
Role getSenderRole() const
std::string toString() const
static const std::string transition
std::string toString() const
static const std::string performative
void addState(const State &state)
static const std::string subprotocol
static const std::string from
void setTargetState(const StateId &stateId)
StateMachine parseStateMachineNode(TiXmlElement *statemachineElement)
static const std::string proxiedTo
static const std::string mapping
StateMachine loadSpecification(const std::string &file)
Transition parseTransitionNode(TiXmlElement *transitionElement)
method that parses <transition> element of the spec. file
static const std::string initial
Foundation of Physical Intelligent Agents.
StateId getTargetStateId() const
Transition addTransition(const Transition &t)
std::string getPerformativeRegExp() const
getter methods for various fields of the class
defines the StateMachineReader Class, used to generate a state macine from a spec. file
static const std::string final