Fawkes API  Fawkes Development Version
skill_wrapper.cpp
1 
2 /***************************************************************************
3  * skill_wrapper.cpp - Wrap a skill as XABSL basic behavior
4  *
5  * Created: Sun Aug 10 10:22:22 2008
6  * Copyright 2006-2008 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "skill_wrapper.h"
24 
25 #include <core/exception.h>
26 #include <utils/misc/string_conversions.h>
27 
28 using std::string;
29 
30 /** @class XabslSkillWrapper "skill_wrapper.h"
31  * Xabsl Skill Wrapper.
32  * This wraps a Fawkes skill as a basic behavior for Xabsl.
33  * @author Tim Niemueller
34  */
35 
36 /** Constructor.
37  * @param name name of the skill
38  * @param error_handler Xabsl error handler
39  * @param params parameters of this skill
40  */
42  xabsl::ErrorHandler &error_handler,
43  ParameterList & params)
44 : xabsl::BasicBehavior(name, error_handler)
45 {
46  params_ = params;
47  execute_ = false;
48 }
49 
50 /** Destructor. */
52 {
53  std::map<std::string, ParameterValueBase *>::iterator i;
54  for (i = param_values_.begin(); i != param_values_.end(); ++i) {
55  delete i->second;
56  }
57  param_values_.clear();
58 }
59 
60 /** Get name of the skill.
61  * @return skill name
62  */
63 const char *
65 {
66  return n;
67 }
68 
69 /** Register parameters. */
70 void
72 {
73  for (ParameterList::iterator i = params_.begin(); i != params_.end(); ++i) {
74  if ((i->second == "float") || (i->second == "double") || (i->second == "int")
75  || (i->second == "unsigned int") || (i->second == "long int")
76  || (i->second == "unsigned long int")) {
77  ParameterValue<double> *pv = new ParameterValue<double>();
78  param_values_[i->first] = pv;
79  parameters->registerDecimal((string(n) + "." + i->first).c_str(), *(pv->get_value_ptr()));
80  } else if (i->second == "bool") {
81  ParameterValue<bool> *pv = new ParameterValue<bool>();
82  param_values_[i->first] = pv;
83  parameters->registerBoolean((string(n) + "." + i->first).c_str(), *(pv->get_value_ptr()));
84  } else {
85  throw fawkes::Exception("Unknown parameter type for field %s in skill %s",
86  i->first.c_str(),
87  n);
88  }
89  }
90 }
91 
92 /** Execute skill. */
93 void
95 {
96  execute_ = true;
97 }
98 
99 /** Get skill string for this string.
100  * If execution has been ordered with execute() this method will return a skill
101  * string generated based on the given skill name and the parameter list.
102  * @return skill string if executed, empty string otherwise
103  */
104 std::string
106 {
107  if (execute_) {
108  execute_ = false;
109 
110  std::string rv = std::string(n) + "{";
111  std::map<std::string, ParameterValueBase *>::iterator i;
112  bool is_first = true;
113  for (i = param_values_.begin(); i != param_values_.end(); ++i) {
114  if (is_first) {
115  is_first = false;
116  } else {
117  rv += ", ";
118  }
119  ParameterValue<double> *pvd;
120  ParameterValue<bool> * pvb;
121  if ((pvd = dynamic_cast<ParameterValue<double> *>(i->second)) != NULL) {
122  rv += i->first + "=" + fawkes::StringConversions::to_string(pvd->get_value());
123  } else if ((pvb = dynamic_cast<ParameterValue<bool> *>(i->second)) != NULL) {
124  rv += i->first + "=" + fawkes::StringConversions::to_string(pvb->get_value());
125  } else {
126  throw fawkes::Exception("Unknonw parameter value type");
127  }
128  }
129  rv += "}";
130  return rv;
131  } else {
132  return "";
133  }
134 }
std::string skill_string()
Get skill string for this string.
virtual void registerParameters()
Register parameters.
virtual void execute()
Execute skill.
XabslSkillWrapper(const char *name, xabsl::ErrorHandler &error_handler, ParameterList &params)
Constructor.
Base class for exceptions in Fawkes.
Definition: exception.h:35
~XabslSkillWrapper()
Destructor.
const char * name()
Get name of the skill.
std::list< std::pair< std::string, std::string > > ParameterList
Parameter list.
Definition: skill_wrapper.h:40
static std::string to_string(unsigned int i)
Convert unsigned int value to a string.