Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * skill_wrapper.cpp - Wrap a skill as XABSL basic behavior 00004 * 00005 * Created: Sun Aug 10 10:22:22 2008 00006 * Copyright 2006-2008 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * This program is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU Library General Public License for more details. 00019 * 00020 * Read the full text in the LICENSE.GPL file in the doc directory. 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 /** @class XabslSkillWrapper "skill_wrapper.h" 00031 * Xabsl Skill Wrapper. 00032 * This wraps a Fawkes skill as a basic behavior for Xabsl. 00033 * @author Tim Niemueller 00034 */ 00035 00036 /** Constructor. 00037 * @param name name of the skill 00038 * @param error_handler Xabsl error handler 00039 * @param params parameters of this skill 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 /** Destructor. */ 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 /** Get name of the skill. 00063 * @return skill name 00064 */ 00065 const char * 00066 XabslSkillWrapper::name() 00067 { 00068 return n; 00069 } 00070 00071 00072 /** Register parameters. */ 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 /** Execute skill. */ 00096 void 00097 XabslSkillWrapper::execute() 00098 { 00099 __execute = true; 00100 } 00101 00102 00103 /** Get skill string for this string. 00104 * If execution has been ordered with execute() this method will return a skill 00105 * string generated based on the given skill name and the parameter list. 00106 * @return skill string if executed, empty string otherwise 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 }