icub-client
entity.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
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 
20 
21 using namespace std;
22 using namespace yarp::os;
23 using namespace icubclient;
24 
25 Entity::Entity(yarp::os::Bottle &b)
26 {
27  this->m_opc_id = -1;
28  this->fromBottle(b);
29 }
30 
31 Entity::Entity()
32 {
33  this->m_opc_id = -1;
34  this->m_entity_type = "unknown";
35  this->m_name = "";
36 }
37 
38 Entity::Entity(const Entity &b)
39 {
40  this->m_name = b.m_name;
41  this->m_entity_type = b.m_entity_type;
42  this->m_opc_id = b.m_opc_id;
43 }
44 
45 Bottle Entity::asBottle() const
46 {
47  Bottle b;
48 
49  Bottle bName;
50  bName.addString("name");
51  bName.addString(m_name.c_str());
52  b.addList() = bName;
53 
54  Bottle bEntity;
55  bEntity.addString("entity");
56  bEntity.addString(m_entity_type.c_str());
57  b.addList() = bEntity;
58 
59  Bottle bProperties;
60  bProperties.addString("intrinsic_properties");
61  Bottle& bPropertiesGlob = bProperties.addList();
62  for(auto& m_property : m_properties)
63  {
64  Bottle bPropertiesValues;
65  bPropertiesValues.addString(m_property.first.c_str());
66  bPropertiesValues.addString(m_property.second.c_str());
67  bPropertiesGlob.addList() = bPropertiesValues;
68  }
69  b.addList() = bProperties;
70  return b;
71 }
72 
73 Bottle Entity::asBottleOnlyModifiedProperties() const
74 {
75  Bottle current_entity = this->asBottle();
76 
77  Bottle new_entity;
78  for(unsigned int p=0; p<current_entity.size(); p++)
79  {
80  string currentTag = current_entity.get(p).asList()->get(0).asString();
81  Value &currentValue = current_entity.find(currentTag.c_str());
82  Value &originalValue = m_original_entity.find(currentTag.c_str());
83 
84  if (currentValue.toString() != originalValue.toString())
85  {
86  Bottle &bSub=new_entity.addList();
87  bSub.addString(currentTag);
88  bSub.add(currentValue);
89  }
90  }
91 
92  return new_entity;
93 }
94 
95 bool Entity::fromBottle(const Bottle &b)
96 {
97  if (!b.check("name") || !b.check("entity"))
98  return false;
99 
100 // m_opc_id = b->find("id").asInt();
101  m_name = b.find("name").asString().c_str();
102  m_entity_type = b.find("entity").asString().c_str();
103 
104  Bottle* bProperties = b.find("intrinsic_properties").asList();
105 
106  for(unsigned int i=0; i< bProperties->size() ; i++)
107  {
108  std::string pTag = bProperties->get(i).asList()->get(0).asString().c_str();
109  std::string pVal = bProperties->get(i).asList()->get(1).asString().c_str();
110  m_properties[pTag] = pVal;
111  }
112 
113  return true;
114 }
115 
116 string Entity::toString() const
117 {
118  std::ostringstream oss;
119  oss << "**************"<<endl;
120  oss <<"Name : " << m_name<<endl;
121  oss <<"ID : " << m_opc_id<<endl;
122  oss <<"Type : " << m_entity_type<<endl;
123  oss <<"Intrinsic properties: "<<endl;
124  for(const auto& prop : m_properties) {
125  oss<<'\t'<<prop.first<<" : "<<prop.second<<endl;
126  }
127  return oss.str();
128 }
129 
130 
131 bool Entity::operator==(const Entity &b) const
132 {
133  return (this->m_opc_id == b.m_opc_id);
134 }
135 
136 
137 bool Entity::operator<(const Entity &b) const
138 {
139  return (this->m_opc_id < b.m_opc_id );
140 }
141 
142 
143 bool Entity::operator>(const Entity &b) const
144 {
145  return (this->m_opc_id > b.m_opc_id );
146 }
147 
148 
149 void Entity::changeName(std::string sName)
150 {
151  m_name = sName;
152 }
Represent any entity that can be stored within the OPC.
Definition: entity.h:40
STL namespace.
std::string m_entity_type
Definition: entity.h:55