CCA
Collector.h
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 #pragma once
00028 
00029 #include <vector>
00030 
00031 #include <string>
00032 #include <iostream>
00033 #include <sstream>
00034 
00035 #include <rsc/logging/Logger.h>
00036 
00037 #include <nemo/Vector.h>
00038 
00039 #include "cca/Node.h"
00040 #include "cca/processing/Moderate.h"
00041 
00042 namespace cca {
00043 
00053 template<class DTOTYPE>
00054 class Collector: public Node {
00055 public:
00056 
00065     Collector(std::string nodename, unsigned int number) :
00066             Node(nodename), dim(number),
00067                     collectConfig(nemo::IntVector(nemo::dim(number), 1)), ips(),
00068                     op(),
00069                     logger(rsc::logging::Logger::getLogger("cca.collector")) {
00070 
00071         for (unsigned int port = 0; port < dim; ++port) {
00072             ips.push_back(InputPort<DTOTYPE>::create());
00073             std::stringstream oss;
00074             oss << "in" << port;
00075             registerPort(oss.str(), ips[port]);
00076         }
00077 
00078         op = OutputPort<DTOTYPE>::create();
00079         registerPort("out", op);
00080 
00081         // Define default strategy - moderate
00082         this->setProcessingStrategy(Moderate::timeout());
00083     }
00084 
00095     Collector(std::string nodename, nemo::IntVector config) :
00096             Node(nodename), dim(config.dimension()), collectConfig(config),
00097                     ips(), op(),
00098                     logger(rsc::logging::Logger::getLogger("cca.collector")) {
00099 
00100         unsigned int count = 0;
00101         for (unsigned int port = 0; port < collectConfig.dimension(); ++port) {
00102             ips.push_back(InputPort<DTOTYPE>::create());
00103             std::stringstream oss;
00104             oss << "in" << port;
00105             registerPort(oss.str(), ips[port]);
00106             count += collectConfig[port];
00107         }
00108         this->dim = count;
00109 
00110         op = OutputPort<DTOTYPE>::create();
00111         registerPort("out", op);
00112 
00113         // Define default strategy - moderate
00114         this->setProcessingStrategy(Moderate::timeout());
00115     }
00116 
00117     ~Collector() {
00118     }
00119 
00124     void onProcess() {
00125         std::vector<boost::shared_ptr<DTOTYPE> > collected = std::vector<
00126                 boost::shared_ptr<DTOTYPE> >();
00127 
00128         // Collect values from input - none of the inputs can be empty
00129         for (unsigned int port = 0; port < collectConfig.dimension(); ++port) {
00130             if (!ips[port]->empty()) {
00131                 collected.push_back(ips[port]->get());
00132             } else {
00133                 logger->error(
00134                         "Collector " + this->getName()
00135                                 + "onProcess() Not all ports received data yet, can`t collect and merge.");
00136                 return;
00137             }
00138         }
00139 
00140         // Preparing and sending collected DTO
00141         typename OutputPort<DTOTYPE>::DataPtr merged = DTOTYPE::create(
00142                 this->dim);
00143         merged->merge(collected);
00144         op->publish(merged);
00145     }
00146 
00154     static NodePtr create(std::string nodename, unsigned int number) {
00155         return NodePtr(new Collector<DTOTYPE>(nodename, number));
00156     }
00157 
00168     static NodePtr create(std::string nodename, nemo::IntVector config) {
00169         return NodePtr(new Collector<DTOTYPE>(nodename, config));
00170     }
00171 
00185     static NodePtr create(std::string nodename, int first, int second) {
00186         return NodePtr(
00187                 new Collector<DTOTYPE>(nodename,
00188                         nemo::IntVector(first, second)));
00189     }
00190 
00206     static NodePtr create(std::string nodename, int first, int second,
00207             int third) {
00208         return NodePtr(
00209                 new Collector<DTOTYPE>(nodename,
00210                         nemo::IntVector(first, second, third)));
00211     }
00212 
00220     static NodePtr create(unsigned int number) {
00221         return NodePtr(new Collector<DTOTYPE>("Collector", number));
00222     }
00223 
00230     friend std::ostream & operator<<(std::ostream& os,
00231             const Collector<DTOTYPE> &node) {
00232         os.precision(3); // Precision when printing double values
00233         os << std::endl << "<Collector '" << node.getName() << "'>"
00234                 << std::endl;
00235         return os;
00236     }
00237 
00248     virtual void configureInputPortByIndex(unsigned int index,
00249             PortConfigurationPtr portcfg) {
00250         std::stringstream portname;
00251         portname << "in" << index;
00252         this->configureInputPort(portname.str(), portcfg);
00253     }
00254 
00263     virtual void configureInputPortByIndex(unsigned int index,
00264             const std::string &scope) {
00265         std::stringstream portname;
00266         portname << "in" << index;
00267         this->configureInputPort(portname.str(), scope);
00268     }
00269 
00270 private:
00271     unsigned int dim;
00272     nemo::IntVector collectConfig;
00273     std::vector<typename InputPort<DTOTYPE>::Ptr> ips;
00274     typename OutputPort<DTOTYPE>::Ptr op;
00275     rsc::logging::LoggerPtr logger;
00276 };
00277 
00278 }