CCA
Info.cpp
Go to the documentation of this file.
00001 /* ============================================================
00002  *
00003  * This file is a part of CCA project
00004  *
00005  * Copyright (C) 2011 by Arne Nordmann <anordman at cor-lab dot uni-bielefeld dot de>
00006  *
00007  * This file may be licensed under the terms of the
00008  * GNU Lesser General Public License Version 3 (the ``LGPL''),
00009  * or (at your option) any later version.
00010  *
00011  * Software distributed under the License is distributed
00012  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
00013  * express or implied. See the LGPL for the specific language
00014  * governing rights and limitations.
00015  *
00016  * You should have received a copy of the LGPL along with this
00017  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
00018  * or write to the Free Software Foundation, Inc.,
00019  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  * The development of this software was supported by:
00022  *   CoR-Lab, Research Institute for Cognition and Robotics
00023  *     Bielefeld University
00024  *
00025  * ============================================================ */
00026 
00027 #include "Info.h"
00028 
00029 #include "State.h"
00030 
00031 #include <sstream>
00032 
00033 using namespace std;
00034 using namespace rsc;
00035 using namespace rsc::runtime;
00036 
00037 namespace cca {
00038 
00039 ComponentInfo::ComponentInfo(const std::string &n) :
00040         DataTransferObject(), name(n), strategyCache(), dataPortsCache(),
00041                 statePortsCache(), beatPortsCache() {
00042 }
00043 
00044 ComponentInfo::~ComponentInfo() {
00045 }
00046 
00047 std::string ComponentInfo::print() const {
00048     ostringstream outstream(ostringstream::out);
00049 
00050     outstream << line(name, 0, true);
00051 
00052     if (!stateCache.empty()) {
00053         outstream << this->stateCache;
00054     }
00055     if (!strategyCache.empty()) {
00056         outstream << this->strategyCache;
00057     }
00058     if (!beatPortsCache.empty()) {
00059         outstream << this->beatPortsCache;
00060     }
00061     if (!statePortsCache.empty()) {
00062         outstream << this->statePortsCache;
00063     }
00064     if (!dataPortsCache.empty()) {
00065         outstream << this->dataPortsCache;
00066     }
00067 
00068     outstream << line("", 0, true);
00069 
00070     if (!propertiesCache.empty()) {
00071         outstream << this->propertiesCache;
00072     } else {
00073         outstream << line("...", 1);
00074     }
00075 
00076     outstream << line("", 0, true);
00077     return outstream.str();
00078 }
00079 
00080 std::string ComponentInfo::line(std::string str, unsigned int place,
00081         bool horizontal) const {
00082     unsigned int linelen = 80;
00083     unsigned int cursor = 0;
00084 
00085     ostringstream outstream(ostringstream::out);
00086 
00087     // Start the line
00088     outstream << " ";
00089     if (horizontal) {
00090         outstream << " +----";
00091         cursor += 6;
00092     } else {
00093         outstream << " |  ";
00094         cursor += 4;
00095     }
00096 
00097     // Insert tabs
00098     for (unsigned int tabs = 0; tabs < place; ++tabs) {
00099         if (!horizontal) {
00100             outstream << "    ";
00101             cursor += 4;
00102         }
00103     }
00104 
00105     // Insert string
00106     if (!str.empty()) {
00107         outstream << " " << str << " ";
00108         cursor += str.length() + 2;
00109     }
00110 
00111     // Fill up the line
00112     for (; cursor < linelen; ++cursor) {
00113         if (horizontal) {
00114             outstream << "-";
00115         } else {
00116             outstream << " ";
00117         }
00118     }
00119 
00120     // Finish the line
00121     if (horizontal) {
00122         outstream << "+";
00123     } else {
00124         outstream << "|";
00125     }
00126 
00127     outstream << std::endl;
00128     return outstream.str();
00129 }
00130 
00131 void ComponentInfo::displayDataPorts(NamedInputPorts ip, NamedOutputPorts op) {
00132     ostringstream outstream(ostringstream::out);
00133 
00134     if (ip.empty()) {
00135         outstream << line("No data input ports.");
00136     } else {
00137         outstream << line("Data input ports:");
00138         for (NamedInputPorts::const_iterator it = ip.begin(); it != ip.end();
00139                 ++it) {
00140             ostringstream port(ostringstream::out);
00141             port << "\"" << it->first << "\"" << it->second->print();
00142             outstream << line(port.str(), 1);
00143         }
00144     }
00145     if (op.empty()) {
00146         outstream << line("No data output ports.");
00147     } else {
00148         outstream << line("Data output ports:");
00149         for (NamedOutputPorts::const_iterator it = op.begin(); it != op.end();
00150                 ++it) {
00151             ostringstream port(ostringstream::out);
00152             port << "\"" << it->first << "\"" << it->second->print();
00153             outstream << line(port.str(), 1);
00154         }
00155     }
00156 
00157     this->dataPortsCache = outstream.str();
00158 }
00159 
00160 void ComponentInfo::displayStatePorts(InputPort<ComponentState>::Ptr ip,
00161         OutputPort<ComponentState>::Ptr op) {
00162     ostringstream outstream(ostringstream::out);
00163 
00164     outstream << line("Component State Ports:");
00165     outstream << line("in:  " + ip->print(), 1);
00166     outstream << line("out: " + op->print(), 1);
00167 
00168     this->statePortsCache = outstream.str();
00169 }
00170 
00171 void ComponentInfo::displayBeatPort(InputPort<cca::timing::Tick>::Ptr ip) {
00172     ostringstream outstream(ostringstream::out);
00173 
00174     outstream << line("Beat Port:");
00175     outstream << line(ip->print(), 1);
00176 
00177     this->beatPortsCache = outstream.str();
00178 }
00179 
00180 void ComponentInfo::displayStrategy(StrategyPtr strat) {
00181     ostringstream outstream(ostringstream::out);
00182 
00183     if (strat) {
00184         outstream << line("Processing strategy:");
00185         outstream << line(strat->print(), 1);
00186     } else {
00187         outstream << line("Processing strategy:");
00188         outstream << line("<unconfigured>", 1);
00189     }
00190 
00191     this->strategyCache = outstream.str();
00192 }
00193 
00194 void ComponentInfo::displayComponentState(ComponentStatePtr state) {
00195     ostringstream outstream(ostringstream::out);
00196 
00197     if (state) {
00198         outstream << line("Component State:");
00199         outstream << line(state->print(), 1);
00200     }
00201 
00202     this->stateCache = outstream.str();
00203 }
00204 
00205 void ComponentInfo::displayProperties(const rsc::runtime::Properties& d,
00206         const rsc::runtime::Properties& p) {
00207     ostringstream outstream(ostringstream::out);
00208     ostringstream properties(ostringstream::out);
00209 
00210     outstream << line("Properties:", 1);
00211 
00212     for (Properties::const_iterator it = p.begin(); it != p.end(); ++it) {
00213         ostringstream property(ostringstream::out);
00214 
00215         property << it->second.type() << " '" << it->first << "': ";
00216         if (it->second.type() == typeid(std::string)) {
00217             property << "\"" << boost::any_cast<std::string>(it->second)
00218                     << "\"";
00219         } else if (it->second.type() == typeid(bool)) {
00220             property << boost::any_cast<bool>(it->second);
00221         } else if (it->second.type() == typeid(int)) {
00222             property << boost::any_cast<int>(it->second);
00223         } else if (it->second.type() == typeid(unsigned int)) {
00224             property << boost::any_cast<unsigned int>(it->second);
00225         } else if (it->second.type() == typeid(double)) {
00226             property << boost::any_cast<double>(it->second);
00227         }
00228 
00229         if (d.count(it->first)) {
00230             property << "  (default: ";
00231             if (d.at(it->first).type() == typeid(std::string)) {
00232                 property << "\""
00233                         << boost::any_cast<std::string>(d.at(it->first))
00234                         << "\"";
00235             } else if (d.at(it->first).type() == typeid(bool)) {
00236                 property << boost::any_cast<bool>(d.at(it->first));
00237             } else if (d.at(it->first).type() == typeid(int)) {
00238                 property << boost::any_cast<int>(d.at(it->first));
00239             } else if (d.at(it->first).type() == typeid(unsigned int)) {
00240                 property << boost::any_cast<unsigned int>(d.at(it->first));
00241             } else if (d.at(it->first).type() == typeid(double)) {
00242                 property << boost::any_cast<double>(d.at(it->first));
00243             }
00244             property << ")";
00245         }
00246 
00247         outstream << line(property.str(), 2);
00248     }
00249 
00250     this->propertiesCache = outstream.str();
00251 }
00252 
00253 }