00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <opspace/Skill.hpp>
00027 #include <opspace/task_library.hpp>
00028
00029 using boost::shared_ptr;
00030
00031
00032 namespace opspace {
00033
00034
00035 Skill::
00036 Skill(std::string const & name)
00037 : ParameterReflection("skill", name)
00038 {
00039 }
00040
00041
00042 Skill::
00043 ~Skill()
00044 {
00045 }
00046
00047
00048 Status Skill::
00049 init(Model const & model)
00050 {
00051 bool ok(true);
00052
00053 {
00054 std::ostringstream msg;
00055 msg << "missing non-optional task instances:\n";
00056 for (slot_map_t::const_iterator is(slot_map_.begin()); is != slot_map_.end(); ++is) {
00057 if ((0 == is->second->getNInstances())
00058 && ( ! is->second->isOptional())) {
00059 ok = false;
00060 msg << " slot `" << is->first << "' task `" << is->first << "'\n";
00061 }
00062 }
00063 if ( ! ok) {
00064 return Status(false, msg.str());
00065 }
00066 }
00067
00068 {
00069 std::ostringstream msg;
00070 msg << "failed task initializations:\n";
00071 for (slot_map_t::const_iterator is(slot_map_.begin()); is != slot_map_.end(); ++is) {
00072 for (size_t it(0); it < is->second->getNInstances(); ++it) {
00073 shared_ptr<Task> task(is->second->getInstance(it));
00074 Status const st(task->init(model));
00075 if ( ! st) {
00076 ok = false;
00077 msg << " slot `" << is->first << "' task[" << it << "] `" << task->getName()
00078 << "': " << st.errstr << "\n";
00079 }
00080 }
00081 }
00082 if ( ! ok) {
00083 return Status(false, msg.str());
00084 }
00085 }
00086
00087 return Status();
00088 }
00089
00090
00091 boost::shared_ptr<TaskSlotAPI> Skill::
00092 lookupSlot(std::string const & name)
00093 {
00094 shared_ptr<TaskSlotAPI> slot;
00095 slot_map_t::iterator ism(slot_map_.find(name));
00096 if (ism != slot_map_.end()) {
00097 slot = ism->second;
00098 }
00099 return slot;
00100 }
00101
00102
00103 void Skill::
00104 dump(std::ostream & os,
00105 std::string const & title,
00106 std::string const & prefix) const
00107 {
00108 if ( ! title.empty()) {
00109 os << title << "\n";
00110 }
00111 os << prefix << "skill " << instance_name_ << "\n";
00112 ParameterReflection::dump(os, prefix + " parameters:", prefix + " ");
00113 os << prefix << " slots:\n";
00114 for (slot_map_t::const_iterator is(slot_map_.begin()); is != slot_map_.end(); ++is) {
00115 os << prefix << " " << is->first;
00116 if (0 == is->second->getNInstances()) {
00117 os << " (EMPTY)\n";
00118 }
00119 else if (1 == is->second->getNInstances()) {
00120 os << ": " << is->second->getInstance(0)->getName() << "\n";
00121 }
00122 else {
00123 os << ":\n";
00124 for (size_t it(0); it < is->second->getNInstances(); ++it) {
00125 os << prefix << " [" << it << "]: "
00126 << is->second->getInstance(it)->getName() << "\n";
00127 }
00128 }
00129 }
00130 }
00131
00132
00133 void Skill::
00134 dbg(std::ostream & os,
00135 std::string const & title,
00136 std::string const & prefix) const
00137 {
00138 if ( ! title.empty()) {
00139 os << title << "\n";
00140 }
00141 os << prefix << "skill " << instance_name_ << "\n";
00142 ParameterReflection::dump(os, prefix + " parameters", prefix + " ");
00143 for (slot_map_t::const_iterator is(slot_map_.begin()); is != slot_map_.end(); ++is) {
00144 for (size_t it(0); it < is->second->getNInstances(); ++it) {
00145 Task const * task(is->second->getInstance(it).get());
00146 task->dbg(os, " " + is->first + "/" + task->getName(), prefix + " ");
00147 }
00148 }
00149 }
00150
00151 }