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 
51 /** Destructor. */
53 {
54  std::map<std::string, ParameterValueBase *>::iterator i;
55  for (i = __param_values.begin(); i != __param_values.end(); ++i) {
56  delete i->second;
57  }
58  __param_values.clear();
59 }
60 
61 
62 /** Get name of the skill.
63  * @return skill name
64  */
65 const char *
67 {
68  return n;
69 }
70 
71 
72 /** Register parameters. */
73 void
75 {
76  for (ParameterList::iterator i = __params.begin(); i != __params.end(); ++i) {
77  if ( (i->second == "float") || (i->second == "double") ||
78  (i->second == "int") || (i->second == "unsigned int") ||
79  (i->second == "long int") || (i->second == "unsigned long int") ) {
80  ParameterValue<double> *pv = new ParameterValue<double>();
81  __param_values[i->first] = pv;
82  parameters->registerDecimal((string(n) + "." + i->first).c_str(), *(pv->get_value_ptr()));
83  } else if ( i->second == "bool" ) {
84  ParameterValue<bool> *pv = new ParameterValue<bool>();
85  __param_values[i->first] = pv;
86  parameters->registerBoolean((string(n) + "." + i->first).c_str(), *(pv->get_value_ptr()));
87  } else {
88  throw fawkes::Exception("Unknown parameter type for field %s in skill %s",
89  i->first.c_str(), n);
90  }
91  }
92 }
93 
94 
95 /** Execute skill. */
96 void
98 {
99  __execute = true;
100 }
101 
102 
103 /** Get skill string for this string.
104  * If execution has been ordered with execute() this method will return a skill
105  * string generated based on the given skill name and the parameter list.
106  * @return skill string if executed, empty string otherwise
107  */
108 std::string
110 {
111  if ( __execute ) {
112  __execute = false;
113 
114  std::string rv = std::string(n) + "{";
115  std::map<std::string, ParameterValueBase *>::iterator i;
116  bool is_first = true;
117  for (i = __param_values.begin(); i != __param_values.end(); ++i) {
118  if ( is_first ) {
119  is_first = false;
120  } else {
121  rv += ", ";
122  }
123  ParameterValue<double> *pvd;
124  ParameterValue<bool> *pvb;
125  if ( (pvd = dynamic_cast<ParameterValue<double> *>(i->second)) != NULL) {
126  rv += i->first + "=" + fawkes::StringConversions::toString(pvd->get_value());
127  } else if ( (pvb = dynamic_cast<ParameterValue<bool> *>(i->second)) != NULL) {
128  rv += i->first + "=" + fawkes::StringConversions::toString(pvb->get_value());
129  } else {
130  throw fawkes::Exception("Unknonw parameter value type");
131  }
132  }
133  rv += "}";
134  return rv;
135  } else {
136  return "";
137  }
138 }
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:36
~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