fipa_acl  1.4
bitefficient_message_parser.cpp
Go to the documentation of this file.
1 
6 #include <set>
7 
8 #include <boost/type_traits/remove_cv.hpp>
9 #include <boost/variant/get.hpp>
10 #include <boost/variant/recursive_variant.hpp>
11 #include <boost/variant/variant.hpp>
12 #include <base-logging/Logging.hpp>
13 
16 
17 
18 namespace fipa {
19 namespace acl {
20 
21 bool BitefficientMessageParser::parseData(const std::string& storage, ACLMessage &msg)
22 {
23  typedef fipa::acl::bitefficient::Message<std::string::const_iterator> bitefficient_message_grammar;
24  bitefficient_message_grammar grammar;
25  fipa::acl::Message parseTree;
26 
27  std::string::const_iterator iter = storage.begin();
28  std::string::const_iterator end = storage.end();
29  bool r = parse(iter, end, grammar, parseTree);
30 
31  if(r && iter == end)
32  {
33  return buildMessage(parseTree, msg);
34  }
35 
36  return false;
37 
38 }
39 
41 {
42  msg.setPerformative(parsedMsg.type);
43  buildParameters(parsedMsg.parameters, msg);
44 
45  return true;
46 }
47 
48 void BitefficientMessageParser::buildParameters(const std::vector<message::Parameter>& parsedParams, ACLMessage &msg)
49 {
50  std::vector<message::Parameter>::const_iterator it = parsedParams.begin();
51 
52  for (; it != parsedParams.end(); ++it)
53  {
54  try {
55  buildPredefMessageParameters(*it,msg);
56  } catch(const std::runtime_error& e)
57  {
58  UserdefParam param = buildUserdefParameter(*it);
59  LOG_INFO("Creating userdefined parameter name: '%s' value: '%s'", param.getName().c_str(), param.getValue().c_str());
60  msg.addUserdefParam(param);
61  }
62  }
63 }
64 
65 void BitefficientMessageParser::buildPredefMessageParameters(const message::Parameter& param, ACLMessage &msg)
66 {
67  // Parameter that require custom conversion from internal data types
68  if (param.name == "sender")
69  {
70  buildSender(param, msg);
71  return;
72  }
73  if (param.name == "receiver")
74  {
75  buildReceiver(param, msg);
76  return;
77  }
78  if (param.name == "content")
79  {
80  buildContent(param, msg);
81  return;
82  }
83  if (param.name == "reply-by")
84  {
85  buildReplyBy(param, msg);
86  return;
87  }
88  if (param.name == "reply-to")
89  {
90  buildReplyTo(param, msg);
91  return;
92  }
93 
94  // Parameters that use string data type
95  std::string value;
96  try {
97  value = boost::get<std::string>(param.data);
98  } catch (const boost::bad_get& e)
99  {
100  LOG_ERROR("Failed getting data for parameter '%s'", param.name.c_str());
101  throw e;
102  }
103 
104  if (param.name == "in-reply-to")
105  {
106  msg.setInReplyTo(value);
107  return;
108  }
109  if (param.name == "reply-with")
110  {
111  msg.setReplyWith(value);
112  return;
113  }
114  if (param.name == "language")
115  {
116  msg.setLanguage(value);
117  return;
118  }
119  if (param.name == "encoding")
120  {
121  msg.setEncoding(value);
122  return;
123  }
124  if (param.name == "ontology")
125  {
126  msg.setOntology(value);
127  return;
128  }
129  if (param.name == "protocol")
130  {
131  msg.setProtocol(value);
132  return;
133  }
134 
135  if (param.name == "conversation-id")
136  {
137  msg.setConversationID(value);
138  return;
139  }
140 
141  std::string errorMsg = "Message parameter '" + param.name + "' is not predefined";
142  LOG_ERROR("%s",errorMsg.c_str());
143  throw std::runtime_error(errorMsg);
144 }
145 
146 void BitefficientMessageParser::buildSender(const message::Parameter& param, ACLMessage &msg)
147 {
148  const AgentIdentifier& temp = boost::get<AgentIdentifier>(param.data);
149  AgentID sender = buildAgentID(temp);
150 
151  msg.setSender(sender);
152 }
153 
154 AgentID BitefficientMessageParser::buildAgentID(const AgentIdentifier& agent)
155 {
156  AgentID retAg;
157  retAg.setName(agent.name);
158  // setting the addresses
159  if (!agent.addresses.empty())
160  {
161  std::vector<std::string>::const_iterator it = agent.addresses.begin();
162  for(; it != agent.addresses.end(); ++it)
163  {
164  retAg.addAddress(*it);
165  }
166  }
167  // building resolvers
168  if (!agent.resolvers.empty())
169  {
170  buildResolvers(agent, retAg);
171  }
172  //building set of userdefined parameters
173  if (!agent.parameters.empty())
174  {
175  buildAgentParameters(agent, retAg);
176  }
177 
178 
179  return retAg;
180 }
181 
182 void BitefficientMessageParser::buildResolvers(const AgentIdentifier& agentIdentifier, AgentID& outAgentID)
183 {
184  std::vector<fipa::acl::Resolver>::const_iterator it = agentIdentifier.resolvers.begin();
185  for (; it != agentIdentifier.resolvers.end(); ++it)
186  {
187  const AgentIdentifier& resolver = it->get(); // get function of the boost::recursive_wrapper
188  AgentID agent = buildAgentID(resolver);
189  outAgentID.addResolver(agent);
190  }
191 }
192 
193 void BitefficientMessageParser::buildAgentParameters(const AgentIdentifier& agentIdentifier, AgentID& outAgentID)
194 {
195  std::vector<UserDefinedParameter>::const_iterator it = agentIdentifier.parameters.begin();
196  for (; it != agentIdentifier.parameters.end(); ++it)
197  {
198  UserdefParam parameter = buildUserdefParameter(*it);
199  outAgentID.addUserdefParam(parameter);
200  }
201 }
202 
203 UserdefParam BitefficientMessageParser::buildUserdefParameter(const UserDefinedParameter& param)
204 {
205  // boost::get() -> to return a specific type value from a boost::variant
206  const std::string& value = boost::get<std::string>(param.data);
207 
208  UserdefParam retParam;
209  retParam.setName(param.name);
210  retParam.setValue(value);
211 
212  return retParam;
213 }
214 
215 UserdefParam BitefficientMessageParser::buildUserdefParameter(const message::Parameter& param)
216 {
217  const std::string& value = boost::get<std::string>(param.data); // boost::get() -> to return a specific type value from a boost::variant
218 
219  UserdefParam retParam;
220  retParam.setName(param.name);
221  retParam.setValue(value);
222 
223  return retParam;
224 
225 }
226 
227 void BitefficientMessageParser::buildReceiver(const message::Parameter& param, ACLMessage &msg)
228 {
229  // boost::get() -> to return a specific type value from a boost::variant
230  const std::vector<AgentIdentifier>& unbuiltReceivers = boost::get<std::vector<AgentIdentifier> >(param.data);
231  std::vector<AgentIdentifier>::const_iterator it = unbuiltReceivers.begin();
232  for (; it!= unbuiltReceivers.end(); ++it)
233  {
234  AgentID receiver = buildAgentID(*it);
235  msg.addReceiver(receiver);
236  }
237 }
238 
239 void BitefficientMessageParser::buildReplyBy(const message::Parameter& param, ACLMessage &msg)
240 {
241  const DateTime& dateTime = boost::get<DateTime>(param.data);
242  base::Time date = dateTime.toTime();
243  msg.setReplyBy(date);
244 }
245 
246 void BitefficientMessageParser::buildReplyTo(const message::Parameter& param, ACLMessage &msg)
247 {
248  const std::vector<AgentIdentifier>& agentIds = boost::get<std::vector<AgentIdentifier> >(param.data);
249 
250  std::vector<AgentIdentifier>::const_iterator it = agentIds.begin();
251  for (; it != agentIds.end(); ++it)
252  {
253  AgentID agentId = buildAgentID(*it);
254  msg.addReplyTo(agentId);
255  }
256 }
257 
258 void BitefficientMessageParser::buildContent(const message::Parameter& param,ACLMessage &msg)
259 {
260  const ByteSequence& byteSequence = boost::get<ByteSequence>(param.data);
261  std::string content;
262  byteSequence.toRawDataString(&content);
263  msg.setContent(content);
264 }
265 
266 }}
const std::string & getValue() const
Definition: userdef_param.h:65
void setReplyWith(const std::string &str)
Definition: acl_message.h:212
void setConversationID(const std::string &str)
Definition: acl_message.h:222
const std::string & getName() const
Definition: userdef_param.h:77
void setName(const std::string &name)
the method checks whether the passed name string is a word or not(according to the fipa spec) ...
Definition: agent_id.cpp:36
void setLanguage(const std::string &str)
Definition: acl_message.h:263
void addResolver(const AgentID &aid)
Add resolver.
Definition: agent_id.cpp:59
void addAddress(const std::string &adr)
the method checks whether the passed address string is a word or not(according to the fipa spec) ...
Definition: agent_id.cpp:48
std::vector< message::Parameter > parameters
Definition: types.h:85
std::vector< std::string > addresses
Definition: agent_id.h:33
void setInReplyTo(const std::string &str)
Definition: acl_message.h:202
Representation of a byte sequence. We also embed information about the encoding.
void addReplyTo(const AgentID &aid)
void addUserdefParam(const UserdefParam &p)
Add a userdefined parameter.
Definition: agent_id.cpp:80
ParameterValue data
Definition: types.h:50
rebuilds an ACLMessage from a parsed bit-efficient encoded received message
fipa::acl::UserDefinedParameterValue data
Definition: agent_id.h:24
std::string toRawDataString() const
void setContent(const std::string &content)
Definition: acl_message.h:274
void setSender(const AgentID &sender)
Definition: acl_message.h:305
void setName(const std::string &name)
the method checks whether the passed name string is a word or not(according to the fipa spec) ...
void addUserdefParam(const UserdefParam &p)
Definition for AgentIdentifier.
Definition: agent_id.h:30
bool parseData(const std::string &storage, ACLMessage &msg)
parses a correctly encoded message according to grammar_bitefficient.h and creates a Message object f...
This class provides a representation of a message conforming to the FIPA specification SC00061...
Definition: acl_message.h:55
void setReplyBy(const base::Time &time)
Definition: acl_message.h:340
bool buildMessage(const Message &parsedMsg, ACLMessage &msg)
creates an unpopulated message object and passes it around to have its fields populated, along with the parsed message where the data is extracted from
std::vector< fipa::acl::UserDefinedParameter > parameters
Definition: agent_id.h:35
overloaded equality operator for UserdefParam class objects
Definition: userdef_param.h:25
void addReceiver(const AgentID &aid)
std::vector< fipa::acl::Resolver > resolvers
Definition: agent_id.h:34
base::Time toTime() const
Definition: date_time.cpp:28
void setPerformative(Performative perf)
Set performative.
std::string type
Definition: types.h:84
Implements the general AgentID functionality, which is present throughout the fipa specifications(FIP...
Definition: agent_id.h:37
Foundation of Physical Intelligent Agents.
Definition: conversation.cpp:6
void setEncoding(const std::string &str)
Definition: acl_message.h:253
void setProtocol(const std::string &str)
the method checks whether the passed protocol string is a word or not(according to the fipa spec) ...
void setOntology(const std::string &str)
Definition: acl_message.h:243