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();
140  QString actualResourceNameForMultirobot(QString resourceName) const;
141 private:
143  bool allNeededResourcesExist;
145  QString sensorName;
148  QString resourcePrefix;
149 };
150 
160 class FARSA_EXPERIMENTS_API Motor : public ParameterSettableInConstructor, public ConcurrentResourcesUser {
161 public:
166  Motor( ConfigurationParameters& params, QString prefix );
168  ~Motor();
173  void save( ConfigurationParameters& params, QString prefix );
175  static void describe( QString type );
179  QString name();
181  void setName( QString name );
183  virtual void update() = 0;
189  virtual int size() = 0;
190 protected:
194  void checkAllNeededResourcesExist();
198  void resetNeededResourcesCheck();
203  QString actualResourceNameForMultirobot(QString resourceName) const;
204 private:
206  bool allNeededResourcesExist;
208  QString motorName;
211  QString resourcePrefix;
212 };
213 
221 class FARSA_EXPERIMENTS_API EvonetIterator : public NeuronsIterator {
222 public:
224  EvonetIterator();
226  virtual ~EvonetIterator();
230  void setEvonet( Evonet* evonet );
232  Evonet* getEvonet();
234  typedef enum { InputLayer, HiddenLayer, OutputLayer } layer_t;
242  void defineBlock( QString name, layer_t layer, int startIndex, int size );
253  bool setCurrentBlock( QString blockName );
259  bool nextNeuron();
261  void setInput( double value );
263  double getOutput();
270  void setGraphicProperties( QString label, double minValue, double maxValue, QColor color );
271 private:
277  void checkCurrentStatus( const QString& funcName = QString() ) const;
279  class BlockInfo {
280  public:
282  int startIndex;
283  int endIndex;
284  };
286  QMap<QString, BlockInfo> blocks;
288  layer_t currLayer;
292  int currStartIndex;
294  int currEndIndex;
296  int currIndex;
298  Evonet* evonet;
299 };
300 
305 {
306 public:
317  EvonetIteratorInvalidStatusException(const char* function, const char* reason) throw() :
318  BaseException()
319  {
320  strncpy(m_function, function, 256);
321  m_function[255] = '\0';
322  strncpy(m_reason, reason, 256);
323  m_reason[255] = '\0';
324  sprintf(m_errorMessage, "Invalid status for EvonetItarator in function \"%s\", reason: %s", m_function, m_reason);
325  m_errorMessage[1023] = '\0';
326  }
327 
334  BaseException(other)
335  {
336  strncpy(m_function, other.m_function, 256);
337  m_function[255] = '\0';
338  strncpy(m_reason, other.m_reason, 256);
339  m_reason[255] = '\0';
340  strncpy(m_errorMessage, other.m_errorMessage, 1024);
341  m_errorMessage[1023] = '\0';
342  }
343 
350  {
351  if (&other == this) {
352  return *this;
353  }
354 
355  BaseException::operator=(other);
356  strncpy(m_function, other.m_function, 256);
357  m_function[255] = '\0';
358  strncpy(m_reason, other.m_reason, 256);
359  m_reason[255] = '\0';
360  strncpy(m_errorMessage, other.m_errorMessage, 1024);
361  m_errorMessage[1023] = '\0';
362 
363  return *this;
364  }
365 
370  {
371  }
372 
378  virtual const char *what() const throw()
379  {
380  return m_errorMessage;
381  }
382 
388  const char *function() const throw()
389  {
390  return m_function;
391  }
392 
399  const char *reason() const throw()
400  {
401  return m_reason;
402  }
403 
408  EXCEPTION_HELPER_FUNCTIONS(EvonetIteratorInvalidStatusException)
409 
410 private:
414  char m_function[256];
415 
419  char m_reason[256];
420 
424  char m_errorMessage[1024];
425 };
426 
427 } // end namespace farsa
428 
429 // All the suff below is to restore the warning state on Windows
430 #if defined(_MSC_VER)
431  #pragma warning(pop)
432 #endif
433 
434 #endif