CCA
Node.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 <string>
00030 #include <vector>
00031 #include <map>
00032 
00033 #include <boost/format.hpp>
00034 
00035 #include <rsb/Factory.h>
00036 #include <rsb/Informer.h>
00037 #include <rsb/Listener.h>
00038 #include <rsb/Event.h>
00039 
00040 #include <rsc/runtime/Properties.h>
00041 
00042 #include "cca/buffer/Buffer.h"
00043 #include "cca/buffer/SingleItemBuffer.h"
00044 #include "cca/port/InputPort.h"
00045 #include "cca/port/OutputPort.h"
00046 #include "cca/port/PortConfiguration.h"
00047 #include "cca/ProcessingStrategy.h"
00048 #include "cca/component/Info.h"
00049 #include "cca/Exception.h"
00050 
00051 namespace cca {
00052 
00053 using namespace boost;
00054 
00055 class Node;
00056 typedef ::boost::shared_ptr<Node> NodePtr;
00057 
00061 class Node {
00062 
00063 public:
00064 
00071     Node(const std::string &name);
00072     virtual ~Node();
00073 
00084     template<typename T>
00085     void registerProperty(const std::string &name, const T& defaultvalue) {
00086         RSCTRACE(logger,
00087                 this->getName() + " registerProperty() property " + name);
00088         RSCTRACE(logger,
00089                 str( format("%1% registerProperty() property"
00090                 "'%2%' with default value '%3%'") % this->getName() % name % defaultvalue));
00091         this->properties[name] = defaultvalue;
00092         this->defaultProperties[name] = defaultvalue;
00093     }
00094 
00101     template<typename T>
00102     T getProperty(const std::string& name) const {
00103         RSCTRACE(logger,
00104                 str( format("%1% getProperty() property "
00105                 "'%2%' is '%3%'") % this->getName() % name % this->properties.get < T > (name)));
00106         return this->properties.get<T>(name);
00107     }
00108 
00120     template<typename T>
00121     void setProperty(const std::string& name, const T& value) throw () {
00122         RSCTRACE(logger, str(format("%1% setProperty() property "
00123         "'%2%' to '%3%'") % this->getName() % name % value));
00124 
00125         this->properties[name] = value;
00126     }
00127 
00133     virtual std::string getName() const;
00134 
00144     virtual void configureInputPort(const std::string &name,
00145             PortConfigurationPtr portcfg);
00146 
00155     virtual void configureInputPort(const std::string &name,
00156             const std::string &scope);
00157 
00168     virtual void configureOutputPort(const std::string &name,
00169             PortConfigurationPtr portcfg);
00178     virtual void configureOutputPort(const std::string &name,
00179             const std::string &scope);
00180 
00189     virtual void setProcessingStrategy(StrategyPtr strategy);
00190 
00194     virtual bool isReady() const;
00195 
00199     virtual void onCreate();
00200 
00207     virtual void onStop();
00208 
00216     virtual void onResume();
00217 
00224     virtual void onReset();
00225 
00234     virtual void onLoad();
00235 
00243     virtual void onPersist();
00244 
00248     virtual void onTick();
00249 
00254     virtual void onInitialize();
00255 
00264     virtual bool initialize();
00265 
00272     virtual bool reset();
00273 
00279     void tick();
00280 
00284     virtual std::string print() const;
00285 
00289     virtual void onProcess() = 0;
00290 
00300     virtual bool isConfigured();
00301 
00302 protected:
00303 
00312     void registerPort(const std::string &name, InputPortPtr port,
00313             bool optional = false);
00314 
00315     void registerPort(const std::string &name, OutputPortPtr port,
00316             bool optional = false);
00317 
00326     bool newValueAt(const std::string &name) const;
00327 
00331     virtual void process();
00332 
00338     virtual void create();
00339 
00348     virtual bool stop();
00349 
00359     virtual bool resume();
00360 
00371     virtual bool load();
00372 
00379     virtual bool persist();
00380 
00389     virtual void inputCallback(rsb::EventPtr event, const std::string& name);
00390 
00391     typedef std::pair<std::string, InputPortPtr> NamedInputPort;
00392     typedef std::pair<std::string, OutputPortPtr> NamedOutputPort;
00393     typedef std::map<std::string, InputPortPtr> NamedInputPorts;
00394     typedef std::map<std::string, OutputPortPtr> NamedOutputPorts;
00395 
00399     NamedInputPorts inputPorts;
00400 
00404     NamedOutputPorts outputPorts;
00405 
00410     std::map<std::string, std::string> config;
00411 
00416     std::map<std::string, std::string> userconfig;
00417 
00418     virtual StrategyPtr getProcessingStrategy() const;
00419 
00421     rsc::runtime::Properties properties;
00422 
00424     rsc::runtime::Properties defaultProperties;
00425 
00426 private:
00431     Node(Node &node);
00432 
00437     void operator=(const Node&);
00438 
00440     std::string name;
00441 
00442     bool ready, loaded, created, configured, processing;
00443 
00445     StrategyPtr strategy;
00446 
00447     rsc::logging::LoggerPtr logger;
00448 };
00449 
00450 }
00451 
00460 std::ostream & operator<<(std::ostream& os, const cca::Node& node);