neuroninterfaces.h
1 /********************************************************************************
2  * FARSA Experimentes Library *
3  * Copyright (C) 2007-2012 *
4  * Gianluca Massera <emmegian@yahoo.it> *
5  * Stefano Nolfi <stefano.nolfi@istc.cnr.it> *
6  * Tomassino Ferrauto <tomassino.ferrauto@istc.cnr.it> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
21  ********************************************************************************/
22 
23 #ifndef NEURONINTERFACES_H
24 #define NEURONINTERFACES_H
25 
26 #include "experimentsconfig.h"
27 #include "resourcesuser.h"
28 #include "parametersettable.h"
29 #include "baseexception.h"
30 #include <QString>
31 #include <QColor>
32 #include <QMap>
33 
34 // All the suff below is to avoid warnings on Windows about the use of the
35 // unsafe function sprintf and strcpy...
36 #if defined(_MSC_VER)
37  #pragma warning(push)
38  #pragma warning(disable:4996)
39 #endif
40 
41 namespace farsa {
42 
43 class Evonet;
44 
55 class FARSA_EXPERIMENTS_TEMPLATE NeuronsIterator : public Resource {
56 public:
58  virtual ~NeuronsIterator() { /*nothing to do*/ };
68  virtual bool setCurrentBlock( QString blockName ) = 0;
74  virtual bool nextNeuron() = 0;
76  virtual void setInput( double value ) = 0;
78  virtual double getOutput() = 0;
85  virtual void setGraphicProperties( QString label, double minValue, double maxValue, QColor color ) = 0;
86 };
87 
97 class FARSA_EXPERIMENTS_API Sensor : public ParameterSettableInConstructor, public ConcurrentResourcesUser {
98 public:
103  Sensor( ConfigurationParameters& params, QString prefix );
105  ~Sensor();
110  void save( ConfigurationParameters& params, QString prefix );
112  static void describe( QString type );
116  QString name();
118  void setName( QString name );
120  virtual void update() = 0;
126  virtual int size() = 0;
127 protected:
131  void checkAllNeededResourcesExist();
135  void resetNeededResourcesCheck();
136 private:
138  bool allNeededResourcesExist;
140  QString sensorName;
141 };
142 
152 class FARSA_EXPERIMENTS_API Motor : public ParameterSettableInConstructor, public ConcurrentResourcesUser {
153 public:
158  Motor( ConfigurationParameters& params, QString prefix );
160  ~Motor();
165  void save( ConfigurationParameters& params, QString prefix );
167  static void describe( QString type );
171  QString name();
173  void setName( QString name );
175  virtual void update() = 0;
181  virtual int size() = 0;
182 protected:
186  void checkAllNeededResourcesExist();
190  void resetNeededResourcesCheck();
191 private:
193  bool allNeededResourcesExist;
195  QString motorName;
196 };
197 
205 class FARSA_EXPERIMENTS_API EvonetIterator : public NeuronsIterator {
206 public:
208  EvonetIterator();
210  virtual ~EvonetIterator();
214  void setEvonet( Evonet* evonet );
216  typedef enum { InputLayer, HiddenLayer, OutputLayer } layer_t;
224  void defineBlock( QString name, layer_t layer, int startIndex, int size );
235  bool setCurrentBlock( QString blockName );
241  bool nextNeuron();
243  void setInput( double value );
245  double getOutput();
252  void setGraphicProperties( QString label, double minValue, double maxValue, QColor color );
253 private:
259  void checkCurrentStatus( const QString& funcName = QString() ) const;
261  class BlockInfo {
262  public:
264  int startIndex;
265  int endIndex;
266  };
268  QMap<QString, BlockInfo> blocks;
270  layer_t currLayer;
274  int currStartIndex;
276  int currEndIndex;
278  int currIndex;
280  Evonet* evonet;
281 };
282 
287 {
288 public:
299  EvonetIteratorInvalidStatusException(const char* function, const char* reason) throw() :
300  BaseException()
301  {
302  strncpy(m_function, function, 256);
303  m_function[255] = '\0';
304  strncpy(m_reason, reason, 256);
305  m_reason[255] = '\0';
306  sprintf(m_errorMessage, "Invalid status for EvonetItarator in function \"%s\", reason: %s", m_function, m_reason);
307  m_errorMessage[1023] = '\0';
308  }
309 
316  BaseException(other)
317  {
318  strncpy(m_function, other.m_function, 256);
319  m_function[255] = '\0';
320  strncpy(m_reason, other.m_reason, 256);
321  m_reason[255] = '\0';
322  strncpy(m_errorMessage, other.m_errorMessage, 1024);
323  m_errorMessage[1023] = '\0';
324  }
325 
332  {
333  if (&other == this) {
334  return *this;
335  }
336 
337  BaseException::operator=(other);
338  strncpy(m_function, other.m_function, 256);
339  m_function[255] = '\0';
340  strncpy(m_reason, other.m_reason, 256);
341  m_reason[255] = '\0';
342  strncpy(m_errorMessage, other.m_errorMessage, 1024);
343  m_errorMessage[1023] = '\0';
344 
345  return *this;
346  }
347 
352  {
353  }
354 
360  virtual const char *what() const throw()
361  {
362  return m_errorMessage;
363  }
364 
370  const char *function() const throw()
371  {
372  return m_function;
373  }
374 
381  const char *reason() const throw()
382  {
383  return m_reason;
384  }
385 
390  EXCEPTION_HELPER_FUNCTIONS(EvonetIteratorInvalidStatusException)
391 
392 private:
396  char m_function[256];
397 
401  char m_reason[256];
402 
406  char m_errorMessage[1024];
407 };
408 
409 } // end namespace farsa
410 
411 // All the suff below is to restore the warning state on Windows
412 #if defined(_MSC_VER)
413  #pragma warning(pop)
414 #endif
415 
416 #endif