opspace/src/Skill.cpp

Go to the documentation of this file.
00001 /*
00002  * Shared copyright notice and LGPLv3 license statement.
00003  *
00004  * Copyright (C) 2011 The Board of Trustees of The Leland Stanford Junior University. All rights reserved.
00005  * Copyright (C) 2011 University of Texas at Austin. All rights reserved.
00006  *
00007  * Authors: Roland Philippsen (Stanford) and Luis Sentis (UT Austin)
00008  *          http://cs.stanford.edu/group/manips/
00009  *          http://www.me.utexas.edu/~hcrl/
00010  *
00011  * This program is free software: you can redistribute it and/or
00012  * modify it under the terms of the GNU Lesser General Public License
00013  * as published by the Free Software Foundation, either version 3 of
00014  * the License, or (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful, but
00017  * WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  * Lesser General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Lesser General Public
00022  * License along with this program.  If not, see
00023  * <http://www.gnu.org/licenses/>
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 }

Generated on Fri Aug 26 01:31:17 2011 for Stanford Whole-Body Control Framework by  doxygen 1.5.4