icub-client
opcClient.h
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
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 #ifndef __ICUBCLIENT_OPCC_H__
20 #define __ICUBCLIENT_OPCC_H__
21 
22 #include <memory>
24 #include "icubclient/tags.h"
25 
26 namespace icubclient{
27 
35 class OPCClient
36 {
37  friend class ICubClient;
38 
39 protected:
40  yarp::os::Port opc;
48  bool write(yarp::os::Bottle &cmd, yarp::os::Bottle &reply, bool Verbose=false);
49 
50  std::map<int, Entity*> entitiesByID;
51 
56  void addEntity(Entity* e);
57 
58  int getRelationID(
59  Entity* subject,
60  Entity* verb,
61  Entity* object = nullptr,
62  Entity* complement_place = nullptr,
63  Entity* complement_time = nullptr,
64  Entity* complement_manner = nullptr);
65 
71  bool changeName(Entity *e, const std::string &newName);
72 
73  std::string opcName;
74 
75 public:
80  OPCClient(const std::string &moduleName);
81 
82  bool isVerbose;
83 
91  template <class T>
92  T* addEntity(const std::string &name) {
93  std::string name_appended = name;
94  int appendix = 0;
95  Entity *e = getEntity(name, true);
96  // loop until we find the first appendix which is not used yet
97  while(e!=nullptr) {
98  ++appendix;
99  name_appended = name + "_" + std::to_string(appendix);
100  e = getEntity(name_appended,true);
101  }
102 
103  //Else we create it in the OPC.
104  T *o = new T();
105  o->m_name = name_appended;
106  addEntity(o);
107  return o;
108  }
109 
118  template <class T>
119  T* addOrRetrieveEntity(const std::string &name)
120  {
121  Entity *e = getEntity(name,true);
122  if ( e != nullptr)
123  {
124  if (isVerbose)
125  yError() <<"Trying to add an already existing entity (" << name << ")";
126  return dynamic_cast<T*>(e);
127  }
128 
129  //Else we create it in the OPC.
130  T* o = new T();
131  o->m_name = name;
132  addEntity(o);
133  return o;
134  }
135 
141  bool isConnected() {
142  if(opcName=="") {
143  return false;
144  } else {
145  if(yarp::os::Network::isConnected(opc.getName().c_str(), ("/" + opcName + "/rpc").c_str())) {
146  return true;
147  } else {
148  return connect(opcName);
149  }
150  }
151  }
152 
157  bool connect(const std::string &opcName)
158  {
159  this->opcName = opcName;
160  return yarp::os::Network::connect(opc.getName().c_str(), ("/" + opcName + "/rpc").c_str());
161  }
162 
166  void interrupt();
167 
171  void close();
172 
176  void clear();
177 
184  bool setEntityProperty(std::string sourceEntityName, std::string propertyName, std::string targetEntityName);
185 
193  bool addRelation(const Relation &r, double lifeTime = -1);
194 
201  bool addRelation(
202  Entity* subject,
203  Entity* verb,
204  Entity* object = nullptr,
205  double lifeTime = -1,
206  Entity* complement_place = nullptr,
207  Entity* complement_time = nullptr,
208  Entity* complement_manner = nullptr
209  );
210 
217  bool removeRelation(const Relation &r);
218 
224  bool removeRelation(
225  Entity* subject,
226  Entity* verb,
227  Entity* object = nullptr,
228  Entity* complement_place = nullptr,
229  Entity* complement_time = nullptr,
230  Entity* complement_manner = nullptr
231  );
232 
237  bool containsRelation(
238  Entity* subject,
239  Entity* verb,
240  Entity* object = nullptr,
241  Entity* complement_place = nullptr,
242  Entity* complement_time = nullptr,
243  Entity* complement_manner = nullptr
244  );
245 
251  bool containsRelation(const Relation &r);
252 
256  std::list<Relation> getRelations();
257 
261  std::list<Relation> getRelations(std::string entity);
262 
266  std::list<Relation> getRelations(Entity* entity);
267 
272  std::list<Relation> getRelationsMatching(std::string subject = "any",std::string verb = "any", std::string object = "any", std::string c_place = "any", std::string c_time = "any", std::string c_manner = "any" );
273 
279  std::list<Relation> getRelationsMatchingLoosly(std::string subject = "any", std::string verb = "any", std::string object = "any", std::string c_place = "any", std::string c_time = "any", std::string c_manner = "any");
280 
285  bool setLifeTime(int id, double lifeTime);
286 
293  Entity *getEntity(const std::string &name, bool forceUpdate = false);
294 
301  Entity *getEntity(int id, bool forceUpdate = false);
302 
308  bool removeEntity(const std::string &name);
309 
315  bool removeEntity(int id);
316 
321  void checkout(bool updateCache=true);
322 
326  void update();
327 
332  void update(Entity *e);
333 
337  void commit();
338 
343  void commit(Entity *e);
344 
348  std::list<Entity*> Entities(const yarp::os::Bottle &condition);
349 
353  std::list<Entity*> Entities(const std::string &prop, const std::string &op, const std::string &value);
354 
358  std::list<Entity*> EntitiesCache() const;
359 
363  std::list<std::shared_ptr<Entity>> EntitiesCacheCopy() const;
364 
368  std::string toString();
369 };
370 
371 }//Namespace
372 #endif
373 
std::list< Relation > getRelationsMatching(std::string subject="any", std::string verb="any", std::string object="any", std::string c_place="any", std::string c_time="any", std::string c_manner="any")
Returns a list of all existing relations between entities.
Definition: opcClient.cpp:617
Represent any entity that can be stored within the OPC.
Definition: entity.h:40
bool containsRelation(Entity *subject, Entity *verb, Entity *object=nullptr, Entity *complement_place=nullptr, Entity *complement_time=nullptr, Entity *complement_manner=nullptr)
Check if a relation exists.
Definition: opcClient.cpp:501
void close()
Close the client ports.
Definition: opcClient.cpp:69
Grants access to high level motor commands (grasp, touch, look, goto, etc) of the robot as well as it...
Definition: icubClient.h:66
std::list< Relation > getRelationsMatchingLoosly(std::string subject="any", std::string verb="any", std::string object="any", std::string c_place="any", std::string c_time="any", std::string c_manner="any")
Returns a list of all existing relations between entities.
Definition: opcClient.cpp:710
void commit()
Commit all the entities & relations stored locally, erasing the server copies.
Definition: opcClient.cpp:835
T * addOrRetrieveEntity(const std::string &name)
Obtains an entity with the given name.
Definition: opcClient.h:119
bool connect(const std::string &opcName)
Try to connect the client to an OPC server.
Definition: opcClient.h:157
bool changeName(Entity *e, const std::string &newName)
Change the name of an entity and send messages to relevant subsystems.
Definition: opcClient.cpp:969
bool removeRelation(const Relation &r)
Try to remove a relation between two entities.
Definition: opcClient.cpp:450
T * addEntity(const std::string &name)
Creates a new entity.
Definition: opcClient.h:92
OPCClient(const std::string &moduleName)
Create an OPC client.
Definition: opcClient.cpp:32
std::list< Entity * > EntitiesCache() const
Getter of the list of entities stored locally.
Definition: opcClient.cpp:905
bool write(yarp::os::Bottle &cmd, yarp::os::Bottle &reply, bool Verbose=false)
Send a command to the OPC and return the reply.
Definition: opcClient.cpp:39
std::list< std::shared_ptr< Entity > > EntitiesCacheCopy() const
Getter of the list of the copies of the entities stored locally.
Definition: opcClient.cpp:917
Entity * getEntity(const std::string &name, bool forceUpdate=false)
Gets an entity based on its name, but do no create it if it doesn&#39;t exist yet.
Definition: opcClient.cpp:134
std::string toString()
Returns a human readable description of the client content (Entities & Relations) ...
Definition: opcClient.cpp:954
std::list< Relation > getRelations()
Returns a list of all existing relations between entities.
Definition: opcClient.cpp:570
bool setEntityProperty(std::string sourceEntityName, std::string propertyName, std::string targetEntityName)
Try to assign a property from an entity to another entity, using a specific property name...
Definition: opcClient.cpp:122
An OPC client using the datastructures defined within the icub-client library.
Definition: opcClient.h:35
void clear()
Clear the OPC content.
Definition: opcClient.cpp:80
yarp::os::Port opc
Port which connects to /OPC/rpc.
Definition: opcClient.h:40
void interrupt()
Interrupt communications of the client ports.
Definition: opcClient.cpp:64
bool isConnected()
Check if the client is already connected to the OPC server.
Definition: opcClient.h:141
std::list< Entity * > Entities(const yarp::os::Bottle &condition)
Getter of the list of entities of matching a complex condition (e.g "(entity "==" agent) "&&" (isPres...
Definition: opcClient.cpp:865
Represent a relation between two entities.
Definition: relation.h:31
void checkout(bool updateCache=true)
Poll the OPC for all entities and relations and store them locally.
Definition: opcClient.cpp:754
void addEntity(Entity *e)
Send a the request to add a new entity to the OPC.
Definition: opcClient.cpp:95
std::map< int, Entity * > entitiesByID
Definition: opcClient.h:50
bool addRelation(const Relation &r, double lifeTime=-1)
Obtains a relation between two entities.
Definition: opcClient.cpp:390
std::string opcName
Definition: opcClient.h:73
bool setLifeTime(int id, double lifeTime)
Set the lifetime property of a relation in the OPC.
Definition: opcClient.cpp:524
void update()
Update the properties of all locally stored entities & relations.
Definition: opcClient.cpp:794
bool removeEntity(const std::string &name)
Removes an entity based on its name.
Definition: opcClient.cpp:246
int getRelationID(Entity *subject, Entity *verb, Entity *object=nullptr, Entity *complement_place=nullptr, Entity *complement_time=nullptr, Entity *complement_manner=nullptr)
Definition: opcClient.cpp:302