icub-client
action.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 WYSIWYD Consortium, European Commission FP7 Project ICT-612139
3  * Authors: Ilaria Gori and Maxime Petit
4  * email: ilaria.gori@iit.it, maxime.petit@inserm.fr
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/functions.h"
22 
23 using namespace std;
24 using namespace yarp::os;
25 using namespace icubclient;
26 
27 Action::Action():Entity()
28 {
30 }
31 
33 {
34  this->initialDescription = b.initialDescription;
35  this->subActions = b.subActions;
37 }
38 
39 Bottle Action::asBottle() const
40 {
41  Bottle b = this->Entity::asBottle();
42  Bottle bSub;
43  bSub.addString("description");
44  bSub.addList() = initialDescription.asBottle();
45  b.addList() = bSub;
46  bSub.clear();
47  bSub.addString("subactions");
48  Bottle& subs = bSub.addList();
49  for(auto& subAction : subActions)
50  {
51  subs.addList()=subAction.asBottle();
52  }
53  b.addList() = bSub;
54 
55  bSub.clear();
56  bSub.addString("estimatedDriveEffects");
57  Bottle &subss = bSub.addList();
58  for(auto& estimatedDriveEffect : estimatedDriveEffects)
59  {
60  Bottle &ss = subss.addList();
61  ss.addString(estimatedDriveEffect.first.c_str());
62  ss.addDouble(estimatedDriveEffect.second);
63  }
64  b.addList() = bSub;
65  return b;
66 }
67 
68 bool Action::fromBottle(const Bottle &b)
69 {
70  if (!this->Entity::fromBottle(b))
71  return false;
72 
73  if (!b.check("description")||!b.check("subactions"))
74  return false;
75 
76  Bottle* bDesc = b.find("description").asList();
77  initialDescription.fromBottle(*bDesc);
78 
79  this->subActions.clear();
80  Bottle* bSub = b.find("subactions").asList();
81  for(unsigned int i=0; i<bSub->size(); i++)
82  {
83  Action a;
84  a.fromBottle(*bSub->get(i).asList());
85  this->subActions.push_back(a);
86  }
87 
88  this->estimatedDriveEffects.clear();
89  bSub = b.find("estimatedDriveEffects").asList();
90  for(unsigned int i=0; i<bSub->size(); i++)
91  {
92  string driveName = bSub->get(i).asList()->get(0).asString().c_str();
93  double driveEffect = bSub->get(i).asList()->get(1).asDouble();
94  this->estimatedDriveEffects[driveName] = driveEffect;
95  }
96  return true;
97 }
98 
100 {
101  this->initialDescription.fromBottle(r.asBottle());
102 }
103 
105 {
106  Action a;
107  string initial = this->asBottle().toString().c_str();
108  string final = initial.c_str();
109  replace_all(final,this->initialDescription.subject().c_str(),"___subject");
110  replace_all(final,this->initialDescription.object().c_str(),"___object");
111  replace_all(final,this->initialDescription.complement_time().c_str(),"___compTime");
112  replace_all(final,this->initialDescription.complement_place().c_str(),"___compPlace");
113  replace_all(final,this->initialDescription.complement_manner().c_str(),"___compManner");
114  replace_all(final,"___subject", r.subject());
115  replace_all(final,"___object", r.object());
116  replace_all(final,"___compTime", r.complement_time());
117  replace_all(final,"___compPlace", r.complement_place());
118  replace_all(final,"___compManner", r.complement_manner());
119  Bottle b(final.c_str());
120  a.fromBottle(b);
121  return a;
122 }
123 
125 {
126  Relation r;
127  r.fromBottle(this->initialDescription.asBottle());
128  return r;
129 }
130 
132 {
133  subActions.push_back(a);
134 }
135 
136 list<Action> Action::asPlan() const
137 {
138  list<Action> unrolled;
139  if (this->subActions.size() == 0) {
140  unrolled.push_back(*this);
141  } else {
142  for(const auto& it : subActions)
143  {
144  list<Action> subUnrolled = it.asPlan();
145  unrolled.splice(unrolled.end(), subUnrolled);
146  }
147  }
148  return unrolled;
149 }
150 
151 list<Action> Action::asPlan(const Relation &newDescription) const
152 {
153  Action expressed = this->express(newDescription);
154  list<Action> unrolled = expressed.asPlan();
155  return unrolled;
156 }
157 
158 void Action::getPlanDrivesEffect(map<string,double> &driveEffects)
159 {
160  //Added the effect of the current plan
161  for(map<string, double>::iterator sIt = estimatedDriveEffects.begin() ; sIt != estimatedDriveEffects.end(); sIt++)
162  {
163  driveEffects[sIt->first] += sIt->second;
164  }
165 
166  //Added recursively the effects of subplans
167  for(list<Action>::iterator it = subActions.begin(); it != subActions.end() ; it++)
168  {
169  it->getPlanDrivesEffect(driveEffects);
170  }
171 }
172 string Action::toString() const
173 {
174  return toString(this->initialDescription);
175 }
176 
177 string Action::toString(const Relation& newRelation) const
178 {
179  std::ostringstream oss;
180  oss<<"Unrolling: "<<newRelation.toString()<<endl;
181  list<Action> unrolled = this->asPlan(newRelation);
182  int count=0;
183  for(list<Action>::iterator it = unrolled.begin() ; it!=unrolled.end(); it++,count++)
184  {
185  oss<<'\t'<<count<<")"<<it->description().toString()<<endl;
186  }
187  return oss.str();
188 }
void fromBottle(const yarp::os::Bottle &b)
Create a Relation from a Bottle.
Definition: relation.cpp:179
std::string complement_manner() const
Get the complement of manner of the relation.
Definition: relation.cpp:236
virtual bool fromBottle(const yarp::os::Bottle &b)
Fill entity fields from a bottle representation.
Definition: entity.cpp:95
virtual yarp::os::Bottle asBottle() const
Number of subactions composing this one.
Definition: action.cpp:39
yarp::os::Bottle asBottle(bool ignoreID=false) const
Definition: relation.cpp:90
virtual void getPlanDrivesEffect(std::map< std::string, double > &driveEffects)
Estimate the effect of this plan on a specific drive, by summing all the effects of subplans to this ...
Definition: action.cpp:158
Represent any entity that can be stored within the OPC.
Definition: entity.h:40
void append(Action &a)
Append a subaction to create a composite one.
Definition: action.cpp:131
virtual std::string toString() const
Return string representation of this Relation.
Definition: relation.cpp:190
std::string subject() const
Get the name of the relation&#39;s subject.
Definition: relation.cpp:211
std::string complement_time() const
Get the complement of time of the relation.
Definition: relation.cpp:231
virtual std::list< Action > asPlan() const
Get an unrolled plan description.
Definition: action.cpp:136
STL namespace.
void replace_all(std::string &in, const std::string &plain, const std::string &tok)
Simple search and replace function for strings;.
Definition: functions.cpp:42
Represents an action, whether composite or not.
Definition: action.h:33
Relation description() const
Return initialDescription as Relation.
Definition: action.cpp:124
std::string complement_place() const
Get the complement of place of the relation.
Definition: relation.cpp:226
std::string object() const
Get the name of the relation&#39;s object.
Definition: relation.cpp:216
virtual bool fromBottle(const yarp::os::Bottle &b)
Fill entity fields from a bottle representation.
Definition: action.cpp:68
void setInitialDescription(const Relation &r)
Definition: action.cpp:99
Represent a relation between two entities.
Definition: relation.h:31
virtual yarp::os::Bottle asBottle() const
Return the entity as a bottle.
Definition: entity.cpp:45
#define ICUBCLIENT_OPC_ENTITY_ACTION
Definition: tags.h:41
std::map< std::string, double > estimatedDriveEffects
Estimated effects on the drive.
Definition: action.h:44
std::string m_entity_type
Definition: entity.h:55
virtual std::string toString() const
Return a human readable description of the entity.
Definition: action.cpp:172
Action express(const Relation &r) const
Definition: action.cpp:104