/***************************************************************************
 *   Copyright (C) 2005 by Robot Group Leipzig                             *
 *    martius@informatik.uni-leipzig.de                                    *
 *    fhesse@informatik.uni-leipzig.de                                     *
 *    der@informatik.uni-leipzig.de                                        *
 *    frankguettler@gmx.de                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 *                                                                         *
 *   $Log: abstractcontrolleradapter.h,v $
 *   Revision 1.4  2009/08/05 22:58:13  martius
 *   use copy constructor for abstractcontroller -> config and inspection works
 *   configurable interface passthrough
 *
 *   Revision 1.3  2009/03/25 11:46:58  robot1
 *   bugfix
 *
 *   Revision 1.2  2008/04/17 14:54:44  martius
 *   randomGen added, which is a random generator with long period and an
 *    internal state. Each Agent has an instance and passed it to the controller
 *    and the wiring. This is good for
 *   a) repeatability on agent basis,
 *   b) parallel execution as done in ode_robots
 *
 *   Revision 1.1  2007/03/22 08:05:03  robot3
 *   this is an adapter class which implements all needed things. This class is
 *   used for example by the DiscreteControllerAdapter.
 *

 *                                                                         *
 ***************************************************************************/
#ifndef __ABSTRACTCONTROLLERADAPTER_H
#define __ABSTRACTCONTROLLERADAPTER_H

#include "abstractcontroller.h"

/**
 * Abstract adapter class (interface) for robot controller.
 * The controller gets a number of input sensor values each timestep
 *  and has to generate a number of output motor values.
 *
 * Interface assumes the following usage:
 *  - init() is called first to initialise the dimension of sensor- and motor space
 *  - each time step
 *     either step() or stepNoLearning() is called to ask the controller for motor values.
 *
 * This is an abstract adapter class, it's useful for implementing adapters such as the
 * DescreteController, which can be used with all Controllers.
 * 
 * Note that the configureable and inspectable classes hold internal lists
 *  that are copied here. If a new parameter is added to either interfaces
 *  after the call of the contructor then this will not be visible througt the adapter 
 *  (at least partially. get and set will work but not the printing (also used by store)) 
 */
class AbstractControllerAdapter : public AbstractController {
public:

  AbstractControllerAdapter(AbstractController* controller)
    : AbstractController(*controller), controller(controller)
  { // Note that we use the copy constructor of AbtractController here to 
    //  copy the inspectable and configureable lists
  }

  /// contructor (hint: use $ID$ for revision) 
  AbstractControllerAdapter(AbstractController* controller, 
			    const std::string& name, const std::string& revision)
    : AbstractController(*controller), controller(controller) 
  { // Note that we use the copy constructor of AbtractController here to 
    //  copy the inspectable and configureable lists
  }


  virtual ~AbstractControllerAdapter() {}

  /****************************************************************************/
  /*	AbstractControllerAdapter must implement the following classes:        	*/
  /*	AbstractController, Configurable, Inspectable, Storeable	            */
  /****************************************************************************/
  
  
  /****************************************************************************/
  /*	BEGIN methods of AbstractController                                 	*/
  /****************************************************************************/

  /** initialisation of the controller with the given sensor/ motornumber
   * Must NORMALLY be called before use. For all ControllerAdapters
   * call first AbstractControllerAdapter::init(sensornumber,motornumber)
   * if you overwrite this method
   */
  virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0){
    //		this->sensorNumber=sensornumber;
    //		this->motorNumber=motornumber;
    controller->init(sensornumber,motornumber);
  }

  /** @return Number of sensors the controller
      was initialised with or 0 if not initialised */
  virtual int getSensorNumber() const { return controller->getSensorNumber();}
  
  /** @return Number of motors the controller
      was initialised with or 0 if not initialised */
  virtual int getMotorNumber() const {  return controller->getMotorNumber();}
  
  /** performs one step (includes learning).
      Calculates motor commands from sensor inputs.
      @param sensors sensors inputs scaled to [-1,1]
      @param sensornumber length of the sensor array
      @param motors motors outputs. MUST have enough space for motor values!
      @param motornumber length of the provided motor array
  */
  virtual void step(const sensor* sensors, int sensornumber,
		    motor* motors, int motornumber) { 
    controller->step(sensors, sensornumber, motors,  motornumber); 
  }
  
  /** performs one step without learning.
      @see step
  */
  virtual void stepNoLearning(const sensor* sensors , int sensornumber,
			      motor* motors, int motornumber) { 
    controller->stepNoLearning(sensors,sensornumber,motors,motornumber); 
  }
  
  /****************************************************************************/
  /*	END methods of AbstractController                                 	    */
  /****************************************************************************/
  
  /****************************************************************************/
  /*	BEGIN methods of Configurable                                       	*/
  /****************************************************************************/
  
  virtual paramval getParam(const paramkey& key) const {
    return controller->getParam(key);
  }
  
  virtual bool setParam(const paramkey& key, paramval val){
    return controller->setParam(key,val);
  }
  
  virtual paramlist getParamList() const {
    return controller->getParamList();
  }  

  /****************************************************************************/
  /*	END methods of Configurable                                      	    */
  /****************************************************************************/
  
  /****************************************************************************/
  /*	BEGIN methods of Inspectable                                          	*/
  /****************************************************************************/
  
  /** The list of the names of all internal parameters given by getInternalParams().
      The naming convention is "v[i]" for vectors
      and "A[i][j]" for matrices, where i, j start at 0.
      @return: list of keys
  */
  virtual iparamkeylist getInternalParamNames() const { return controller->getInternalParamNames();}
  
  /** @return: list of values
   */
  virtual iparamvallist getInternalParams() const { return controller->getInternalParams(); }
  
  /****************************************************************************/
  /*	END methods of Inspectable                                       	    */
  /****************************************************************************/
  
  /****************************************************************************/
  /*	BEGIN methods of Storable                                       	*/
  /****************************************************************************/
  
  /********* STORABLE INTERFACE ******/
  /// @see Storable
  virtual bool store(FILE* f) const { 
    return controller->store(f);
  }
  
  /// @see Storable
  virtual bool restore(FILE* f) { 
    return controller->restore(f);
  }
  
  
  /****************************************************************************/
  /*	END methods of Storable                                        	    */
  /****************************************************************************/
  
  
protected:
  AbstractController* controller; // the controller for the adapter to handle
  //	int sensorNumber; // number of sensors the controller has
  //	int motorNumber; // number of motors the controller has
  
};

#endif
