00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "skill_wrapper.h"
00024
00025 #include <core/exception.h>
00026 #include <utils/misc/string_conversions.h>
00027
00028 using std::string;
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 XabslSkillWrapper::XabslSkillWrapper(const char *name,
00042 xabsl::ErrorHandler &error_handler,
00043 ParameterList ¶ms)
00044 : xabsl::BasicBehavior(name, error_handler)
00045 {
00046 __params = params;
00047 __execute = false;
00048 }
00049
00050
00051
00052 XabslSkillWrapper::~XabslSkillWrapper()
00053 {
00054 std::map<std::string, ParameterValueBase *>::iterator i;
00055 for (i = __param_values.begin(); i != __param_values.end(); ++i) {
00056 delete i->second;
00057 }
00058 __param_values.clear();
00059 }
00060
00061
00062
00063
00064
00065 const char *
00066 XabslSkillWrapper::name()
00067 {
00068 return n;
00069 }
00070
00071
00072
00073 void
00074 XabslSkillWrapper::registerParameters()
00075 {
00076 for (ParameterList::iterator i = __params.begin(); i != __params.end(); ++i) {
00077 if ( (i->second == "float") || (i->second == "double") ||
00078 (i->second == "int") || (i->second == "unsigned int") ||
00079 (i->second == "long int") || (i->second == "unsigned long int") ) {
00080 ParameterValue<double> *pv = new ParameterValue<double>();
00081 __param_values[i->first] = pv;
00082 parameters->registerDecimal((string(n) + "." + i->first).c_str(), *(pv->get_value_ptr()));
00083 } else if ( i->second == "bool" ) {
00084 ParameterValue<bool> *pv = new ParameterValue<bool>();
00085 __param_values[i->first] = pv;
00086 parameters->registerBoolean((string(n) + "." + i->first).c_str(), *(pv->get_value_ptr()));
00087 } else {
00088 throw fawkes::Exception("Unknown parameter type for field %s in skill %s",
00089 i->first.c_str(), n);
00090 }
00091 }
00092 }
00093
00094
00095
00096 void
00097 XabslSkillWrapper::execute()
00098 {
00099 __execute = true;
00100 }
00101
00102
00103
00104
00105
00106
00107
00108 std::string
00109 XabslSkillWrapper::skill_string()
00110 {
00111 if ( __execute ) {
00112 __execute = false;
00113
00114 std::string rv = std::string(n) + "{";
00115 std::map<std::string, ParameterValueBase *>::iterator i;
00116 bool is_first = true;
00117 for (i = __param_values.begin(); i != __param_values.end(); ++i) {
00118 if ( is_first ) {
00119 is_first = false;
00120 } else {
00121 rv += ", ";
00122 }
00123 ParameterValue<double> *pvd;
00124 ParameterValue<bool> *pvb;
00125 if ( (pvd = dynamic_cast<ParameterValue<double> *>(i->second)) != NULL) {
00126 rv += i->first + "=" + fawkes::StringConversions::toString(pvd->get_value());
00127 } else if ( (pvb = dynamic_cast<ParameterValue<bool> *>(i->second)) != NULL) {
00128 rv += i->first + "=" + fawkes::StringConversions::toString(pvb->get_value());
00129 } else {
00130 throw fawkes::Exception("Unknonw parameter value type");
00131 }
00132 }
00133 rv += "}";
00134 return rv;
00135 } else {
00136 return "";
00137 }
00138 }