ROSEndEffector
ROS End-Effector package: provides a ROS-based set of standard interfaces to command robotics end-effectors in an agnostic fashion.
YamlWorker.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 <copyright holder> <email>
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
20 
21 std::string ROSEE::YamlWorker::createYamlFile( const ROSEE::Action* action, std::string pathFolder) {
22 
23  ROSEE::Utils::create_directory ( pathFolder );
24  std::string output = emitYaml ( action );
25  ROSEE::Utils::out2file(pathFolder + action->getName() + ".yaml", output);
26  return (pathFolder + action->getName() + ".yaml");
27 
28 }
29 
30 std::string ROSEE::YamlWorker::createYamlFile( const ROSEE::ActionGeneric::Ptr action, std::string pathFolder) {
31 
32  ROSEE::Utils::create_directory ( pathFolder );
33  std::string output = emitYaml ( action );
34  ROSEE::Utils::out2file(pathFolder + action->getName() + ".yaml", output);
35  return (pathFolder + action->getName() + ".yaml");
36 
37 }
38 
40  const std::map < std::set <std::string> , ActionPrimitive* > mapOfActions,
41  const std::string actionName, std::string pathFolder) {
42 
43  ROSEE::Utils::create_directory ( pathFolder );
44  std::string output = emitYaml ( mapOfActions );
45  ROSEE::Utils::out2file( pathFolder + actionName + ".yaml", output);
46  return (pathFolder + actionName + ".yaml");
47 
48 
49 }
50 
51 
53  const std::map < std::set <std::string> , ActionPrimitive* > mapOfActions ) {
54 
55  YAML::Emitter out;
56  out << YAML::BeginMap;
57  for (const auto & mapEl : mapOfActions) {
58  mapEl.second->emitYaml(out);
59  out << YAML::Newline << YAML::Newline; //double to insert a blanck line between tips pair
60  }
61  out << YAML::EndMap;
62  return out.c_str();
63 
64 }
65 
66 std::string ROSEE::YamlWorker::emitYaml ( const ROSEE::Action* action ) {
67 
68  YAML::Emitter out;
69  action->emitYaml(out);
70  out << YAML::Newline << YAML::Newline; //double to insert a blanck line
71  return out.c_str();
72 
73 }
74 
76 
77  YAML::Emitter out;
78  action->emitYaml(out);
79  out << YAML::Newline << YAML::Newline; //double to insert a blanck line
80  return out.c_str();
81 
82 }
83 
85  std::string fileWithPath, ROSEE::ActionPrimitive::Type actionType) {
86 
87  std::map < std::set < std::string>, ROSEE::ActionPrimitive::Ptr > parsedMap;
88 
89  //TODO check elsewhere if file exist or not?
90  std::ifstream ifile(fileWithPath);
91  if (! ifile) {
92  std::cout << "[ERROR YAMLPARSER:: " << __func__ << "]: file " << fileWithPath << " not found. " << std::endl;
93  return parsedMap;
94  }
95 
96  YAML::Node node = YAML::LoadFile(fileWithPath);
97 
98  for(YAML::const_iterator it4Action = node.begin(); it4Action != node.end(); ++it4Action) {
100  switch (actionType) {
101  case ActionPrimitive::Type::PinchTight: {
102  ptr = std::make_shared <ActionPinchTight>();
103  break;
104  }
105  case ActionPrimitive::Type::PinchLoose: {
106  ptr = std::make_shared <ActionPinchLoose> ();
107  break;
108  }
109  case ActionPrimitive::Type::Trig: {
110  ptr = std::make_shared <ActionTrig>("trig", ActionPrimitive::Type::Trig);
111  break;
112  }
113  case ActionPrimitive::Type::TipFlex: {
114  ptr = std::make_shared <ActionTrig>("tipFlex", ActionPrimitive::Type::TipFlex);
115  break;
116  }
117  case ActionPrimitive::Type::FingFlex: {
118  ptr = std::make_shared <ActionTrig>("fingFlex", ActionPrimitive::Type::FingFlex);
119  break;
120  }
121  case ActionPrimitive::Type::SingleJointMultipleTips: {
122  ptr = std::make_shared <ActionSingleJointMultipleTips>();
123  break;
124  }
125  case ActionPrimitive::Type::MultiplePinchTight: {
126  ptr = std::make_shared <ActionMultiplePinchTight> ();
127  break;
128  }
129 
130  default : {
131  std::cout << "[ERROR YAMLPARSER]: " << actionType << " : type not found" << std::endl;
132  }
133  }
134 
135  ptr->fillFromYaml ( it4Action );
136 
137  parsedMap.insert ( std::make_pair ( ptr->getKeyElements(), ptr) );
138  }
139 
140  return parsedMap;
141 }
142 
143 
144 std::map < std::set < std::string>, ROSEE::ActionPrimitive::Ptr > ROSEE::YamlWorker::parseYamlPrimitive (std::string fileWithPath) {
145 
146  std::map < std::set < std::string>, ROSEE::ActionPrimitive::Ptr > parsedMap;
147 
148  //TODO check elsewhere if file exist or not?
149  std::ifstream ifile(fileWithPath);
150  if (! ifile) {
151  std::cout << "[ERROR YAMLPARSER:: " << __func__ << "]: file " << fileWithPath << " not found. " << std::endl;
152  return parsedMap;
153  }
154 
155  YAML::Node node = YAML::LoadFile(fileWithPath);
156 
157  for(YAML::const_iterator it4Action = node.begin(); it4Action != node.end(); ++it4Action) {
158 
159  std::string actionName;
160  ROSEE::ActionPrimitive::Type type = ROSEE::ActionPrimitive::Type::None;
161 
162  //now look for the type of the action and for the name
163  for ( YAML::const_iterator actionState = it4Action->second.begin(); actionState != it4Action->second.end(); ++actionState) {
164 
165  if (actionState->first.as<std::string>().compare("PrimitiveType") == 0) {
166  type = static_cast<ROSEE::ActionPrimitive::Type> ( actionState->second.as < int > () );
167  break; //we only need the type
168  }
169  }
170 
171  //now use emit of specific action
173  switch (type) {
174  case ActionPrimitive::Type::PinchTight: {
175  ptr = std::make_shared <ActionPinchTight>();
176  break;
177  }
178  case ActionPrimitive::Type::PinchLoose: {
179  ptr = std::make_shared <ActionPinchLoose> ();
180  break;
181  }
182  case ActionPrimitive::Type::Trig: {
183  ptr = std::make_shared <ActionTrig>("trig", ActionPrimitive::Type::Trig);
184  break;
185  }
186  case ActionPrimitive::Type::TipFlex: {
187  ptr = std::make_shared <ActionTrig>("tipFlex", ActionPrimitive::Type::TipFlex);
188  break;
189  }
190  case ActionPrimitive::Type::FingFlex: {
191  ptr = std::make_shared <ActionTrig>("fingFlex", ActionPrimitive::Type::FingFlex);
192  break;
193  }
194  case ActionPrimitive::Type::SingleJointMultipleTips: {
195  ptr = std::make_shared <ActionSingleJointMultipleTips>();
196  break;
197  }
198  case ActionPrimitive::Type::MultiplePinchTight: {
199  ptr = std::make_shared <ActionMultiplePinchTight> ();
200  break;
201  }
202 
203  default : {
204  std::cout << "[ERROR YAMLPARSER]: " << type << " : type not found" << std::endl;
205  }
206  }
207 
208  ptr->fillFromYaml ( it4Action );
209 
210  parsedMap.insert ( std::make_pair ( ptr->getKeyElements(), ptr) );
211  }
212 
213  return parsedMap;
214 
215 }
216 
217 
219 
220  ROSEE::ActionComposed parsedAction;
221 
222  //TODO check elsewhere if file exist or not?
223  std::ifstream ifile(fileWithPath);
224  if (! ifile) {
225  std::cout << "[ERROR YAMLPARSER:: " << __func__ << "]: file " << fileWithPath << " not found. " << std::endl;
226  return parsedAction;
227  }
228 
229  YAML::Node node = YAML::LoadFile(fileWithPath);
230  YAML::const_iterator yamlIt = node.begin();
231 
232  parsedAction.fillFromYaml ( yamlIt );
233 
234  return parsedAction;
235 
236 }
237 
239 
240  ActionGeneric::Ptr ptrAction;
241 
242  //TODO check elsewhere if file exist or not?
243  std::ifstream ifile(fileWithPath);
244  if (! ifile) {
245  std::cout << "[ERROR YAMLPARSER:: " << __func__ << "]: file " << fileWithPath << " not found. " << std::endl;
246  return ptrAction;
247  }
248 
249  YAML::Node node = YAML::LoadFile(fileWithPath);
250 
251  YAML::const_iterator it4Action = node.begin(); //for not primitive, only one action is inside the file
252 
253  ROSEE::Action::Type type = ROSEE::Action::Type::None;
254 
255  //now look for the type of the action
256  for ( YAML::const_iterator el = it4Action->second.begin(); el != it4Action->second.end(); ++el) {
257 
258  if (el->first.as<std::string>().compare("Type") == 0) {
259  type = static_cast<ROSEE::Action::Type> ( el->second.as < int > () );
260  break; //we only need the type
261  }
262  }
263 
264  switch (type) {
265  case Action::Type::Primitive : {
266  std::cout << "[ERROR YAMLPARSER:: " << __func__ << "]: file " << fileWithPath << " contains a primitive action, "
267  << " please use parseYamlPrimitive to parse it " << std::endl;
268  return ptrAction;
269  break;
270  }
271  case Action::Type::Generic : {
272  ptrAction = std::make_shared <ActionGeneric> ();
273  break;
274  }
275  case Action::Type::Composed : {
276  ptrAction = std::make_shared <ActionComposed> ();
277  break;
278  }
279  case Action::Type::Timed : {
280  std::cout << "[ERROR YAMLPARSER:: " << __func__ << "]: file " << fileWithPath << " contains a timed action, "
281  << " please use parseYamlTimed to parse it " << std::endl;
282  return ptrAction;
283  break;
284  }
285  default : {
286  std::cout << "[ERROR YAMLPARSER:: " << __func__ << "]: file " << fileWithPath << " contains an action of not know type "
287  << type << std::endl;
288  return ptrAction;
289  }
290  }
291 
292  ptrAction->fillFromYaml(it4Action);
293 
294  return ptrAction;
295 
296 
297 }
298 
299 
300 std::shared_ptr < ROSEE::ActionTimed > ROSEE::YamlWorker::parseYamlTimed (std::string fileWithPath){
301 
302  std::shared_ptr < ROSEE::ActionTimed > ptrAction;
303 
304  //TODO check elsewhere if file exist or not?
305  std::ifstream ifile(fileWithPath);
306  if (! ifile) {
307  std::cout << "[ERROR YAMLPARSER:: " << __func__ << "]: file " << fileWithPath << " not found. " << std::endl;
308  return nullptr;
309  }
310 
311  YAML::Node node = YAML::LoadFile(fileWithPath);
312  YAML::const_iterator yamlIt = node.begin();
313  ptrAction = std::make_shared <ActionTimed> ();
314 
315  ptrAction->fillFromYaml ( yamlIt );
316 
317  return ptrAction;
318 
319 }
std::string getName() const
Get the name of the action.
Definition: Action.cpp:116
Type
Enum useful to discriminate each action when, for example, we want to parse a file if you change thi...
Definition: Action.h:82
Virtual class, Base of all the primitive actions.
std::map< std::set< std::string >, ROSEE::ActionPrimitive::Ptr > parseYamlPrimitive(std::string fileWithPath)
Parse a yaml file and return the map with all the actions present in the file.
Definition: YamlWorker.cpp:144
std::shared_ptr< ROSEE::ActionTimed > parseYamlTimed(std::string fileWithPath)
Parse a timed Action.
Definition: YamlWorker.cpp:300
ROSEE::ActionGeneric::Ptr parseYamlGeneric(std::string fileWithPath)
Definition: YamlWorker.cpp:238
The pure virtual class representing an Action.
Definition: Action.h:71
A ActionComposed, which is formed by one or more Primitives (or even other composed).
static bool create_directory(std::string pathDirectory)
Definition: Utils.h:39
std::shared_ptr< ActionGeneric > Ptr
Definition: ActionGeneric.h:35
std::shared_ptr< ActionPrimitive > Ptr
virtual void emitYaml(YAML::Emitter &out) const =0
Function to fill the argument passed with info about the action.
static void out2file(std::string pathFile, std::string output)
Definition: Utils.h:44
Type
Enum useful to discriminate each primitive action when, for example, we want to parse a file if you ...
std::string emitYaml(const std::map< std::set< std::string >, ActionPrimitive * >)
support functions for createYamlFile
Definition: YamlWorker.cpp:52
YamlWorker()
Costructor default.
Definition: YamlWorker.cpp:19
virtual bool fillFromYaml(YAML::const_iterator yamlIt) override
Fill the internal data with infos taken from yaml file.
ROSEE::ActionComposed parseYamlComposed(std::string fileWithPath)
Parse a composed Action.
Definition: YamlWorker.cpp:218
std::string createYamlFile(const std::map< std::set< std::string >, ActionPrimitive * > mapOfActions, const std::string actionName, std::string pathFolder)
Create/overwrite yaml file and emit info on it about each ActionPrimitive inside the given mapOfActio...
Definition: YamlWorker.cpp:39