icub-client
relation.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 WYSIWYD Consortium, European Commission FP7 Project ICT-612139
3  * Authors: Stéphane Lallée
4  * email: stephane.lallee@gmail.com * A copy of the license can be found at
5  * website: https://github.com/robotology/icub-client/
6  * Permission is granted to copy, distribute, and/or modify this program
7  * under the terms of the GNU General Public License, version 2 or any
8  * later version published by the Free Software Foundation.
9  *
10  * A copy of the license can be found at
11  * icub-client/license/gpl.txt
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16  * Public License for more details
17  */
18 
19 
21 #include "icubclient/tags.h"
22 
23 using namespace std;
24 using namespace yarp::os;
25 using namespace icubclient;
26 
27 Relation::Relation(
28  string subject,
29  string verb,
30  string object,
31  string complement_place,
32  string complement_time,
33  string complement_manner)
34 {
35  m_opcId = -1;
36  m_subject = subject;
37  m_verb = verb;
38  m_object = object;
39  m_complement_place = complement_place;
40  m_complement_time = complement_time;
41  m_complement_manner = complement_manner;
42 }
43 
44 Relation::Relation(
45  Entity* subject,
46  Entity* verb,
47  Entity* object,
48  Entity* complement_place,
49  Entity* complement_time,
50  Entity* complement_manner
51  )
52 {
53  m_opcId = -1;
54  if (subject)
55  m_subject = subject->name();
56  else
57  m_subject = "none";
58 
59  if (verb)
60  m_verb = verb->name();
61  else
62  m_verb = "none";
63 
64  if (object)
65  m_object = object->name();
66  else
67  m_object = "none";
68 
69  if (complement_place)
70  m_complement_place = complement_place->name();
71  else
72  m_complement_place = "none";
73 
74  if (complement_time)
75  m_complement_time = complement_time->name();
76  else
77  m_complement_time = "none";
78 
79  if (complement_manner)
80  m_complement_manner = complement_manner->name();
81  else
82  m_complement_manner = "none";
83 }
84 
85 Relation::Relation(yarp::os::Bottle &b)
86 {
87  this->fromBottle(b);
88 }
89 
90 Bottle Relation::asBottle(bool ignoreID) const
91 {
92  Bottle b;
93  Bottle bSub;
94  bSub.addString("entity");
95  bSub.addString(ICUBCLIENT_OPC_ENTITY_RELATION);
96  b.addList()=bSub;
97 
98  if (!ignoreID)
99  {
100  bSub.clear();
101  bSub.addString("id");
102  bSub.addInt(m_opcId);
103  b.addList()=bSub;
104  }
105  bSub.clear();
106  bSub.addString("rSubject");
107  bSub.addString(m_subject.c_str());
108  b.addList()=bSub;
109  bSub.clear();
110  bSub.addString("rVerb");
111  bSub.addString(m_verb.c_str());
112  b.addList()=bSub;
113  bSub.clear();
114  bSub.addString("rObject");
115  bSub.addString(m_object.c_str());
116  b.addList()=bSub;
117  bSub.clear();
118  bSub.addString("rCompPlace");
119  bSub.addString(m_complement_place.c_str());
120  b.addList()=bSub;
121  bSub.clear();
122  bSub.addString("rCompTime");
123  bSub.addString(m_complement_time.c_str());
124  b.addList()=bSub;
125  bSub.clear();
126  bSub.addString("rCompManner");
127  bSub.addString(m_complement_manner.c_str());
128  b.addList()=bSub;
129  return b;
130 }
131 
132 Bottle Relation::asLightBottle() const
133 {
134  Bottle b;
135  Bottle bSub;
136  bSub.addString("entity");
137  bSub.addString(ICUBCLIENT_OPC_ENTITY_RELATION);
138  b.addList()=bSub;
139 
140  bSub.clear();
141  bSub.addString("rSubject");
142  bSub.addString(m_subject.c_str());
143  b.addList()=bSub;
144  bSub.clear();
145  bSub.addString("rVerb");
146  bSub.addString(m_verb.c_str());
147  b.addList()=bSub;
148  bSub.clear();
149  bSub.addString("rObject");
150  bSub.addString(m_object.c_str());
151  b.addList()=bSub;
152  if (m_complement_place != "none")
153  {
154  bSub.clear();
155  bSub.addString("rCompPlace");
156  bSub.addString(m_complement_place.c_str());
157  b.addList()=bSub;
158  }
159  if (m_complement_time != "none")
160  {
161  bSub.clear();
162  bSub.addString("rCompTime");
163  bSub.addString(m_complement_time.c_str());
164  b.addList()=bSub;
165  }
166  if (m_complement_manner != "none")
167  {
168  bSub.clear();
169  bSub.addString("rCompManner");
170  bSub.addString(m_complement_manner.c_str());
171  b.addList()=bSub;
172  }
173  return b;
174 }
175 
176 
177 
178 
179 void Relation::fromBottle(const Bottle &b)
180 {
181  m_opcId = b.find("id").asInt();
182  m_subject = b.find("rSubject").toString().c_str();
183  m_verb = b.find("rVerb").asString().c_str();
184  m_object = b.find("rObject").asString().c_str();
185  m_complement_place = b.find("rCompPlace").asString().c_str();
186  m_complement_time = b.find("rCompTime").asString().c_str();
187  m_complement_manner = b.find("rCompManner").asString().c_str();
188 }
189 
190 string Relation::toString() const
191 {
192  std::ostringstream oss;
193  oss<< subject() << " " <<verb()<<" ";
194  if (object() != "none")
195  oss<<object()<<" ";
196  if (complement_place() != "none")
197  oss<<complement_place()<<" ";
198  if (complement_time() != "none")
199  oss<<complement_time()<<" ";
200  if (complement_manner() != "none")
201  oss<<complement_manner()<<" ";
202  //oss<<" (ID="<<m_opcId<<")"<<endl;
203  return oss.str();
204 }
205 
206 int Relation::ID() const
207 {
208  return m_opcId;
209 }
210 
211 string Relation::subject() const
212 {
213  return m_subject;
214 }
215 
216 string Relation::object() const
217 {
218  return m_object;
219 }
220 
221 string Relation::verb() const
222 {
223  return m_verb;
224 }
225 
226 string Relation::complement_place() const
227 {
228  return m_complement_place;
229 }
230 
231 string Relation::complement_time() const
232 {
233  return m_complement_time;
234 }
235 
236 string Relation::complement_manner() const
237 {
238  return m_complement_manner;
239 }
240 
241 bool Relation::operator>(const Relation &b) const
242 {
243  if (this->m_subject != b.m_subject)
244  {
245  return (this->m_subject > b.m_subject);
246  }
247  else if (this->m_opcId != b.m_opcId)
248  {
249  return (this->m_opcId > b.m_opcId);
250  }
251  else if (this->m_verb != b.m_verb)
252  {
253  return (this->m_verb > b.m_verb);
254  }
255  else if (this->m_object != b.m_object)
256  {
257  return (this->m_object > b.m_object);
258  }
259  else if (this->m_complement_place != b.m_complement_place)
260  {
261  return (this->m_complement_place > b.m_complement_place);
262  }
263  else if (this->m_complement_time != b.m_complement_time)
264  {
265  return (this->m_complement_time > b.m_complement_time);
266  }
267  else if (this->m_complement_manner != b.m_complement_manner)
268  {
269  return (this->m_complement_manner > b.m_complement_manner);
270  }
271  return false;
272 
273 }
274 
275 bool Relation::operator<(const Relation &b) const
276 {
277  return (!(this->operator>(b))&&!(this->operator==(b)));
278 }
279 
280 bool Relation::operator==(const Relation &b) const
281 {
282  return (
283  this->m_opcId == b.m_opcId &&
284  this->m_subject == b.m_subject &&
285  this->m_verb == b.m_verb &&
286  this->m_object == b.m_object &&
287  this->m_complement_place == b.m_complement_place &&
288  this->m_complement_time == b.m_complement_time &&
289  this->m_complement_manner == b.m_complement_manner
290  );
291 }
Represent any entity that can be stored within the OPC.
Definition: entity.h:40
std::string m_object
Definition: relation.h:41
STL namespace.
#define ICUBCLIENT_OPC_ENTITY_RELATION
Definition: tags.h:42
std::string m_complement_place
Definition: relation.h:42
std::string m_complement_manner
Definition: relation.h:44
std::string name() const
Return the name of an entity (which has to be unique within the OPC)
Definition: entity.h:93
std::string m_subject
Definition: relation.h:40
Represent a relation between two entities.
Definition: relation.h:31
std::string m_complement_time
Definition: relation.h:43
std::string m_verb
Definition: relation.h:39