experiments/include/neuroninterfaces.h

00001 /********************************************************************************
00002  *  FARSA Experimentes Library                                                  *
00003  *  Copyright (C) 2007-2012                                                     *
00004  *  Gianluca Massera <emmegian@yahoo.it>                                        *
00005  *  Stefano Nolfi <stefano.nolfi@istc.cnr.it>                                   *
00006  *  Tomassino Ferrauto <tomassino.ferrauto@istc.cnr.it>                         *
00007  *                                                                              *
00008  *  This program is free software; you can redistribute it and/or modify        *
00009  *  it under the terms of the GNU General Public License as published by        *
00010  *  the Free Software Foundation; either version 2 of the License, or           *
00011  *  (at your option) any later version.                                         *
00012  *                                                                              *
00013  *  This program is distributed in the hope that it will be useful,             *
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00016  *  GNU General Public License for more details.                                *
00017  *                                                                              *
00018  *  You should have received a copy of the GNU General Public License           *
00019  *  along with this program; if not, write to the Free Software                 *
00020  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00021  ********************************************************************************/
00022 
00023 #ifndef NEURONINTERFACES_H
00024 #define NEURONINTERFACES_H
00025 
00026 #include "experimentsconfig.h"
00027 #include "resourcesuser.h"
00028 #include "parametersettable.h"
00029 #include "baseexception.h"
00030 #include <QString>
00031 #include <QColor>
00032 #include <QMap>
00033 
00034 // All the suff below is to avoid warnings on Windows about the use of the
00035 // unsafe function sprintf and strcpy...
00036 #if defined(_MSC_VER)
00037     #pragma warning(push)
00038     #pragma warning(disable:4996)
00039 #endif
00040 
00041 namespace farsa {
00042 
00043 class Evonet;
00044 
00055 class FARSA_EXPERIMENTS_TEMPLATE NeuronsIterator : public Resource {
00056 public:
00058     virtual ~NeuronsIterator() { /*nothing to do*/ };
00068     virtual bool setCurrentBlock( QString blockName ) = 0;
00074     virtual bool nextNeuron() = 0;
00076     virtual void setInput( double value ) = 0;
00078     virtual double getOutput() = 0;
00085     virtual void setGraphicProperties( QString label, double minValue, double maxValue, QColor color ) = 0;
00086 };
00087 
00097 class FARSA_EXPERIMENTS_API Sensor : public ParameterSettableInConstructor, public ConcurrentResourcesUser {
00098 public:
00103     Sensor( ConfigurationParameters& params, QString prefix );
00105     ~Sensor();
00110     void save( ConfigurationParameters& params, QString prefix );
00112     static void describe( QString type );
00116     QString name();
00118     void setName( QString name );
00120     virtual void update() = 0;
00126     virtual int size() = 0;
00127 protected:
00131     void checkAllNeededResourcesExist();
00135     void resetNeededResourcesCheck();
00136 private:
00138     bool allNeededResourcesExist;
00140     QString sensorName;
00141 };
00142 
00152 class FARSA_EXPERIMENTS_API Motor : public ParameterSettableInConstructor, public ConcurrentResourcesUser {
00153 public:
00158     Motor( ConfigurationParameters& params, QString prefix );
00160     ~Motor();
00165     void save( ConfigurationParameters& params, QString prefix );
00167     static void describe( QString type );
00171     QString name();
00173     void setName( QString name );
00175     virtual void update() = 0;
00181     virtual int size() = 0;
00182 protected:
00186     void checkAllNeededResourcesExist();
00190     void resetNeededResourcesCheck();
00191 private:
00193     bool allNeededResourcesExist;
00195     QString motorName;
00196 };
00197 
00205 class FARSA_EXPERIMENTS_API EvonetIterator : public NeuronsIterator {
00206 public:
00208     EvonetIterator();
00210     virtual ~EvonetIterator();
00214     void setEvonet( Evonet* evonet );
00216     typedef enum { InputLayer, HiddenLayer, OutputLayer } layer_t;
00224     void defineBlock( QString name, layer_t layer, int startIndex, int size );
00235     bool setCurrentBlock( QString blockName );
00241     bool nextNeuron();
00243     void setInput( double value );
00245     double getOutput();
00252     void setGraphicProperties( QString label, double minValue, double maxValue, QColor color );
00253 private:
00259     void checkCurrentStatus( const QString& funcName = QString() ) const;
00261     class BlockInfo {
00262     public:
00263         EvonetIterator::layer_t layer;
00264         int startIndex;
00265         int endIndex;
00266     };
00268     QMap<QString, BlockInfo> blocks;
00270     layer_t currLayer;
00274     int currStartIndex;
00276     int currEndIndex;
00278     int currIndex;
00280     Evonet* evonet;
00281 };
00282 
00286 class FARSA_CONF_TEMPLATE EvonetIteratorInvalidStatusException : public BaseException
00287 {
00288 public:
00299     EvonetIteratorInvalidStatusException(const char* function, const char* reason) throw() :
00300         BaseException()
00301     {
00302         strncpy(m_function, function, 256);
00303         m_function[255] = '\0';
00304         strncpy(m_reason, reason, 256);
00305         m_reason[255] = '\0';
00306         sprintf(m_errorMessage, "Invalid status for EvonetItarator in function \"%s\", reason: %s", m_function, m_reason);
00307         m_errorMessage[1023] = '\0';
00308     }
00309 
00315     EvonetIteratorInvalidStatusException(const EvonetIteratorInvalidStatusException& other) throw() :
00316         BaseException(other)
00317     {
00318         strncpy(m_function, other.m_function, 256);
00319         m_function[255] = '\0';
00320         strncpy(m_reason, other.m_reason, 256);
00321         m_reason[255] = '\0';
00322         strncpy(m_errorMessage, other.m_errorMessage, 1024);
00323         m_errorMessage[1023] = '\0';
00324     }
00325 
00331     EvonetIteratorInvalidStatusException& operator=(const EvonetIteratorInvalidStatusException& other) throw()
00332     {
00333         if (&other == this) {
00334             return *this;
00335         }
00336 
00337         BaseException::operator=(other);
00338         strncpy(m_function, other.m_function, 256);
00339         m_function[255] = '\0';
00340         strncpy(m_reason, other.m_reason, 256);
00341         m_reason[255] = '\0';
00342         strncpy(m_errorMessage, other.m_errorMessage, 1024);
00343         m_errorMessage[1023] = '\0';
00344 
00345         return *this;
00346     }
00347 
00351     virtual ~EvonetIteratorInvalidStatusException() throw()
00352     {
00353     }
00354 
00360     virtual const char *what() const throw()
00361     {
00362         return m_errorMessage;
00363     }
00364 
00370     const char *function() const throw()
00371     {
00372         return m_function;
00373     }
00374 
00381     const char *reason() const throw()
00382     {
00383         return m_reason;
00384     }
00385 
00390     EXCEPTION_HELPER_FUNCTIONS(EvonetIteratorInvalidStatusException)
00391 
00392 private:
00396     char m_function[256];
00397 
00401     char m_reason[256];
00402 
00406     char m_errorMessage[1024];
00407 };
00408 
00409 } // end namespace farsa
00410 
00411 // All the suff below is to restore the warning state on Windows
00412 #if defined(_MSC_VER)
00413     #pragma warning(pop)
00414 #endif
00415 
00416 #endif