ROSEndEffector
ROS End-Effector package: provides a ROS-based set of standard interfaces to command robotics end-effectors in an agnostic fashion.
Public Member Functions | Private Member Functions | List of all members
ROSEE::YamlWorker Class Reference

#include <YamlWorker.h>

+ Collaboration diagram for ROSEE::YamlWorker:

Public Member Functions

 YamlWorker ()
 Costructor default. More...
 
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 mapOfActions. More...
 
std::string createYamlFile (const Action *action, std::string pathFolder)
 Create/overwrite yaml file and emit info on it about the given ActionComposed action. More...
 
std::string createYamlFile (const ActionGeneric::Ptr, std::string pathFolder)
 
std::map< std::set< std::string >, ROSEE::ActionPrimitive::PtrparseYamlPrimitive (std::string fileWithPath)
 Parse a yaml file and return the map with all the actions present in the file. More...
 
std::map< std::set< std::string >, ROSEE::ActionPrimitive::PtrparseYamlPrimitive (std::string fileWithPath, ROSEE::ActionPrimitive::Type)
 
ROSEE::ActionComposed parseYamlComposed (std::string fileWithPath)
 Parse a composed Action. More...
 
std::shared_ptr< ROSEE::ActionTimedparseYamlTimed (std::string fileWithPath)
 Parse a timed Action. More...
 
ROSEE::ActionGeneric::Ptr parseYamlGeneric (std::string fileWithPath)
 

Private Member Functions

std::string emitYaml (const std::map< std::set< std::string >, ActionPrimitive * >)
 support functions for createYamlFile More...
 
std::string emitYaml (const Action *action)
 support functions for createYamlFile More...
 
std::string emitYaml (const ActionGeneric::Ptr action)
 

Detailed Description

Todo:
PUT this in Parser?

Definition at line 47 of file YamlWorker.h.

Constructor & Destructor Documentation

ROSEE::YamlWorker::YamlWorker ( )

Costructor default.

Definition at line 19 of file YamlWorker.cpp.

19 {}

Member Function Documentation

std::string ROSEE::YamlWorker::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 mapOfActions.

Parameters
mapOfActionscontainer of actions which infos will be emitted on file
actionNamethe name of the action inside the map (that will have all same name)
Returns
std::string the filename (with the path) of the file created/overwritten

Definition at line 39 of file YamlWorker.cpp.

41  {
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 }
static bool create_directory(std::string pathDirectory)
Definition: Utils.h:39
static void out2file(std::string pathFile, std::string output)
Definition: Utils.h:44
std::string emitYaml(const std::map< std::set< std::string >, ActionPrimitive * >)
support functions for createYamlFile
Definition: YamlWorker.cpp:52
std::string ROSEE::YamlWorker::createYamlFile ( const Action action,
std::string  pathFolder 
)

Create/overwrite yaml file and emit info on it about the given ActionComposed action.

Parameters
action[in] pointer to ActionComposed
pathFolderfolder where to store the file. It will be created if does not exist
Returns
std::string the filename (with the path) of the file created/overwritten

Definition at line 21 of file YamlWorker.cpp.

21  {
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 }
static bool create_directory(std::string pathDirectory)
Definition: Utils.h:39
static void out2file(std::string pathFile, std::string output)
Definition: Utils.h:44
std::string emitYaml(const std::map< std::set< std::string >, ActionPrimitive * >)
support functions for createYamlFile
Definition: YamlWorker.cpp:52
std::string ROSEE::YamlWorker::createYamlFile ( const ActionGeneric::Ptr  action,
std::string  pathFolder 
)

Definition at line 30 of file YamlWorker.cpp.

30  {
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 }
static bool create_directory(std::string pathDirectory)
Definition: Utils.h:39
static void out2file(std::string pathFile, std::string output)
Definition: Utils.h:44
std::string emitYaml(const std::map< std::set< std::string >, ActionPrimitive * >)
support functions for createYamlFile
Definition: YamlWorker.cpp:52
std::string ROSEE::YamlWorker::emitYaml ( const std::map< std::set< std::string >, ActionPrimitive * >  mapOfActions)
private

support functions for createYamlFile

Parameters
mapOfActionsthe map where take info from
Returns
std::string a string formatted as yaml file, ready to be put in the file

Definition at line 52 of file YamlWorker.cpp.

53  {
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 }
std::string ROSEE::YamlWorker::emitYaml ( const Action action)
private

support functions for createYamlFile

Parameters
actionthe action which infos must be emitted
Returns
std::string a string formatted as yaml file, ready to be put in the file

Definition at line 66 of file YamlWorker.cpp.

66  {
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 }
std::string ROSEE::YamlWorker::emitYaml ( const ActionGeneric::Ptr  action)
private

Definition at line 75 of file YamlWorker.cpp.

75  {
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 }
ROSEE::ActionComposed ROSEE::YamlWorker::parseYamlComposed ( std::string  fileWithPath)

Parse a composed Action.

Parameters
fileWithPaththe path of the file to be parsed
Returns
the ActionComposed parsed

Definition at line 218 of file YamlWorker.cpp.

218  {
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 }
A ActionComposed, which is formed by one or more Primitives (or even other composed).
virtual bool fillFromYaml(YAML::const_iterator yamlIt) override
Fill the internal data with infos taken from yaml file.
ROSEE::ActionGeneric::Ptr ROSEE::YamlWorker::parseYamlGeneric ( std::string  fileWithPath)

Definition at line 238 of file YamlWorker.cpp.

238  {
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 }
Type
Enum useful to discriminate each action when, for example, we want to parse a file if you change thi...
Definition: Action.h:82
std::shared_ptr< ActionGeneric > Ptr
Definition: ActionGeneric.h:35
std::map< std::set< std::string >, ROSEE::ActionPrimitive::Ptr > ROSEE::YamlWorker::parseYamlPrimitive ( std::string  fileWithPath)

Parse a yaml file and return the map with all the actions present in the file.

For the moment, a actionType argument must be passed to create the right Action object

Parameters
fileWithPaththe path of the file to be parsed

Definition at line 144 of file YamlWorker.cpp.

144  {
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 }
std::shared_ptr< ActionPrimitive > Ptr
Type
Enum useful to discriminate each primitive action when, for example, we want to parse a file if you ...
std::map< std::set< std::string >, ROSEE::ActionPrimitive::Ptr > ROSEE::YamlWorker::parseYamlPrimitive ( std::string  fileWithPath,
ROSEE::ActionPrimitive::Type  actionType 
)

Definition at line 84 of file YamlWorker.cpp.

85  {
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 }
std::shared_ptr< ActionPrimitive > Ptr
std::shared_ptr< ROSEE::ActionTimed > ROSEE::YamlWorker::parseYamlTimed ( std::string  fileWithPath)

Parse a timed Action.

Parameters
fileWithPaththe path of the file to be parsed
Returns
the ActionTimed parsed

Definition at line 300 of file YamlWorker.cpp.

300  {
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 }

The documentation for this class was generated from the following files: