icub-client
agent.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 
19 
20 #include <algorithm>
22 
23 using namespace std;
24 using namespace yarp::os;
25 using namespace yarp::sig;
26 using namespace icubclient;
27 
28 Agent::Agent():Object()
29 {
31  m_ego_position[0] = 0.0;
32 }
33 
35 {
36  this->m_belief = b.m_belief;
38  this->m_body = b.m_body;
39 }
40 
41 Bottle Agent::asBottle() const
42 {
43  //Get the Object bottle
44  Bottle b = this->Object::asBottle();
45  Bottle bSub;
46  bSub.addString("belief");
47  Bottle& bSubIds = bSub.addList();
48  for(auto& m_belief_item : m_belief)
49  {
50  Bottle& bSub2 = bSubIds.addList();
51  bSub2 = m_belief_item.asBottle();
52  }
53  b.addList() = bSub;
54 
55  Bottle bSubEmotions;
56  bSubEmotions.addString("emotions");
57  Bottle& bSubEmoList = bSubEmotions.addList();
58  for(auto & m_emotion_intrinsic : m_emotions_intrinsic)
59  {
60  Bottle& bSubEmo = bSubEmoList.addList();
61  bSubEmo.addString(m_emotion_intrinsic.first.c_str());
62  bSubEmo.addDouble(m_emotion_intrinsic.second);
63  }
64  b.addList() = bSubEmotions;
65 
66  Bottle bSubBody;
67  bSubBody.addString("body");
68  bSubBody.addList() = m_body.asBottle();
69  b.addList() = bSubBody;
70 
71  return b;
72 }
73 
74 bool Agent::fromBottle(const Bottle &b)
75 {
76  if (!this->Object::fromBottle(b))
77  return false;
78 
79  if (!b.check("belief")||!b.check("emotions"))
80  return false;
81 
82  m_belief.clear();
83  Bottle* beliefs = b.find("belief").asList();
84  for(unsigned int i=0; i<beliefs->size() ; i++)
85  {
86  Bottle* bRelation = beliefs->get(i).asList();
87  Relation r(*bRelation);
88  m_belief.push_back(r);
89  }
90 
91  m_emotions_intrinsic.clear();
92  Bottle* emotions = b.find("emotions").asList();
93  for(unsigned int i=0; i<emotions->size() ; i++)
94  {
95  Bottle* bEmo = emotions->get(i).asList();
96  string emotionName = bEmo->get(0).asString().c_str();
97  double emotionValue = bEmo->get(1).asDouble();
98  m_emotions_intrinsic[emotionName.c_str()] = emotionValue;
99  }
100 
101  Bottle* bodyProperty = b.find("body").asList();
102  m_body.fromBottle(*bodyProperty);
103 
104  return true;
105 }
106 
107 string Agent::toString() const
108 {
109  std::ostringstream oss;
110  oss<< this->Object::toString();
111 
112  oss<<"Believes that : \n";
113  for(const auto& it : m_belief)
114  {
115  oss<< it.toString() <<endl;
116  }
117 
118  oss<<"Emotions:"<<endl;
119  for(const auto& it : m_emotions_intrinsic)
120  {
121  oss<< '\t'<<it.first <<" : "<<it.second<<endl;
122  }
123 
124  return oss.str();
125 }
126 
128 {
129  //Check if this relation is already present
130  list<Relation>::const_iterator it = find(m_belief.begin(),m_belief.end(),r);
131  if (it != m_belief.end())
132  {
133  //cout<<"Agent "+name()+" already believes that " + r.toString();
134  return false;
135  }
136 
137  m_belief.push_back(r);
138  //cout<<"Agent "+name()+" now believes that " + r.toString();
139  return true;
140 }
141 
143 {
144  //Check if this relation is already present
145  list<Relation>::iterator it = find(m_belief.begin(),m_belief.end(),r);
146  if (it != m_belief.end())
147  {
148  m_belief.erase(it);
149  //cout<<"Agent "+name()+" do not believes anymore that " + r.toString();
150  return true;
151  }
152  //cout<<"Agent "+name()+" didn not believe that " + r.toString();
153  return false;
154 }
155 
156 bool Agent::checkBelief(const Relation& r) const
157 {
158  return (find(m_belief.begin(),m_belief.end(),r) != m_belief.end());
159 }
160 
161 const std::list<Relation> &Agent::beliefs() const
162 {
163  return m_belief;
164 }
std::map< std::string, double > m_emotions_intrinsic
Definition: agent.h:102
bool checkBelief(const Relation &r) const
Check if some relation is believed by an agent.
Definition: agent.cpp:156
STL namespace.
Represent any physical entity (including objects and agents) that can be stored within the OPC...
Definition: object.h:35
yarp::sig::VectorOf< double > m_ego_position
Position of the Object, in the initial ego-centered reference frame of the agent mainting the OPC (in...
Definition: object.h:46
bool removeBelief(const Relation &r)
Remove the belief of a relation from the agent.
Definition: agent.cpp:142
Represent an agent.
Definition: agent.h:93
bool addBelief(const Relation &r)
Add the belief of a relation to the agent.
Definition: agent.cpp:127
virtual bool fromBottle(const yarp::os::Bottle &b)
Fill entity fields from a bottle representation.
Definition: object.cpp:169
bool fromBottle(const yarp::os::Bottle &b)
Definition: agent.h:73
yarp::os::Bottle asBottle() const
Definition: agent.h:56
virtual std::string toString() const
Return a human readable description of the entity.
Definition: object.cpp:213
Represent a relation between two entities.
Definition: relation.h:31
virtual std::string toString() const
Return a human readable description of the entity.
Definition: agent.cpp:107
virtual yarp::os::Bottle asBottle() const
Return the entity as a bottle.
Definition: object.cpp:78
#define ICUBCLIENT_OPC_ENTITY_AGENT
Definition: tags.h:40
const std::list< Relation > & beliefs() const
Get a read-only copy of the agent believes.
Definition: agent.cpp:161
virtual yarp::os::Bottle asBottle() const
Return the entity as a bottle.
Definition: agent.cpp:41
std::string m_entity_type
Definition: entity.h:55
virtual bool fromBottle(const yarp::os::Bottle &b)
Fill entity fields from a bottle representation.
Definition: agent.cpp:74