CCA
Moderate.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 "Moderate.h"
00028 
00029 #include <vector>
00030 #include <numeric>
00031 #include <sstream>
00032 #include <iostream>
00033 
00034 using namespace std;
00035 using namespace boost;
00036 
00037 namespace cca {
00038 
00039 StrategyPtr Moderate::timeout(unsigned int timeout) {
00040     return StrategyPtr(new Moderate(timeout));
00041 }
00042 
00043 StrategyPtr Moderate::noTimeout() {
00044     return StrategyPtr(new Moderate(0));
00045 }
00046 
00047 Moderate::Moderate(unsigned int tout) :
00048         ProcessingStrategy("Moderate", true, true), ticks(0), tout(tout),
00049                 logger(
00050                         rsc::logging::Logger::getLogger(
00051                                 "cca.processing.moderate")) {
00052 
00053 }
00054 
00055 bool Moderate::processOnTick() const {
00056     RSCTRACE(logger, "Moderate::processOnTick()");
00057     if (this->tout > 0) {
00058         ++this->ticks;
00059         if (this->ticks == this->tout) {
00060             this->ticks = 0;
00061             RSCDEBUG(logger, "Moderate::processOnTick() yes, do process");
00062             return true;
00063         }
00064     }
00065     return false;
00066 }
00067 
00068 bool Moderate::processOnInputs(
00069         ProcessingStrategy::NamedInputPorts inputs) const {
00070     RSCTRACE(logger, "Moderate::processOnInputs()");
00071     if ((this->tout > 0) && (this->ticks >= this->tout)) {
00072         this->ticks = 0;
00073         RSCDEBUG(logger, "Moderate::processOnInputs() yes, do process");
00074         return true;
00075     }
00076 
00077     for (ProcessingStrategy::NamedInputPorts::const_iterator i = inputs.begin();
00078             i != inputs.end(); ++i) {
00079         if (i->second->empty() || !i->second->newItem()) {
00080             return false;
00081         } else {
00082             RSCTRACE(logger,
00083                     "Moderate::processOnInputs() New item in "+i->first);
00084         }
00085     }
00086 
00087     this->ticks = 0;
00088     RSCDEBUG(logger, "Moderate::processOnInputs() yes, do process");
00089     return true;
00090 }
00091 
00092 string Moderate::print() const {
00093     ostringstream outstream(ostringstream::out);
00094     outstream.precision(3); // Precision when printing double values
00095     outstream << "<" << this->name;
00096     if (this->tout > 0) {
00097         outstream << " (timeout after " << this->tout << " ticks)";
00098     } else {
00099         outstream << " (no timeout)";
00100     }
00101     outstream << ">";
00102     return outstream.str();
00103 }
00104 
00105 }