/*
 *  Open BEAGLE
 *  Copyright (C) 2001-2004 by Christian Gagne and Marc Parizeau
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  Contact:
 *  Laboratoire de Vision et Systemes Numeriques
 *  Departement de genie electrique et de genie informatique
 *  Universite Laval, Quebec, Canada, G1V 4J7
 *  http://vision.gel.ulaval.ca
 *
 */

/*!
 *  \file   beagle/GP/Primitive.hpp
 *  \brief  Definition of the type GP::Primitive.
 *  \author Christian Gagne
 *  \author Marc Parizeau
 *  $Revision: 1.2 $
 *  $Date: 2004/01/15 21:08:56 $
 */

/*!
 *  \defgroup Primit GP Primitives
 *  \ingroup GPF
 *  \brief Genetic programming primitives mechanism and library, part of the GP framework.
 */
 
#ifndef Beagle_GP_Primitive_hpp
#define Beagle_GP_Primitive_hpp

#include <string>

#include "beagle/config.hpp"
#include "beagle/macros.hpp"
#include "beagle/Object.hpp"
#include "beagle/Allocator.hpp"
#include "beagle/AbstractAllocT.hpp"
#include "beagle/Pointer.hpp"
#include "beagle/Container.hpp"
#include "beagle/System.hpp"
#include "beagle/GP/Datum.hpp"

namespace Beagle {
namespace GP {


// Forward declaration.
class Context;
class System;


/*!
 *  \class Primitive beagle/GP/Primitive.hpp "beagle/GP/Primitive.hpp"
 *  \brief The GP primitive class.
 *  \ingroup GPF
 *  \ingroup Primit
 */
class Primitive : public Object {

public:

  //! GP::Primitive allocator type.
  typedef AbstractAllocT<Primitive,Object::Alloc>
          Alloc;
  //! GP::Primitive handle type.
  typedef PointerT<Primitive,Object::Handle>
          Handle;
  //! GP::Primitive bag type.
  typedef ContainerT<Primitive,Object::Bag>
          Bag;

  explicit Primitive(unsigned int inNumberArguments=0, const char* inName="");
  explicit Primitive(unsigned int inNumberArguments, std::string inName);
  virtual ~Primitive() { }

  virtual std::string  getArgType(unsigned int inN) const;
          unsigned int getChildrenNodeIndex(unsigned int inN, GP::Context& ioContext) const;
  virtual std::string  getReturnType() const;
  virtual void         getValue(Object& outValue);
  virtual Handle       giveReference(GP::Context& ioContext);
  virtual bool         isEqual(const Object& inRightObj) const;
  virtual void         initialize(GP::System& ioSystem);
  virtual void         read(XMLNode::Handle& inNode);
  virtual void         setValue(const Object& inValue);
  virtual bool         validate(GP::Context& ioContext) const;
  virtual void         write(XMLStreamer& ioStreamer) const;
  virtual void         writeContent(XMLStreamer& ioStreamer) const;
    
  /*!
   *  \brief Execute the caracteristic primitive operation.
   *  \param outDatum Result of the execution.
   *  \param ioContext Evolutionary context.
   */
  virtual void execute(GP::Datum& outDatum, GP::Context& ioContext) =0;

  /*!
   *  \brief Get the name of the primitive.
   *  \return Name of the primitive.
   */
  inline std::string getName() const
  {
    return mName;
  }

  /*!
   *  \brief Get the number of arguments of the primitive.
   *  \return Number of arguments of the primitive.
   */
  inline unsigned int getNumberArguments() const
  {
    return mNumberArguments;
  }

protected:

  /*!
   *  \brief Set the name of the primitive.
   *  \param inName Name of the primitive.
   */
  inline void setName(std::string inName)
  {
    mName = inName;
  }

  /*!
   *  \brief Set the number of arguments of the primitive.
   *  \param inNumberArguments Number of arguments of the primitive.
   */
  inline void setNumberArguments(unsigned int inNumberArguments)
  {
    mNumberArguments = inNumberArguments;
  }

  inline void getArgument(unsigned int inN, GP::Datum& outResult, GP::Context& ioContext);
  inline void getArguments(GP::Datum outResults[], size_t inSizeTDatum, GP::Context& ioContext);
  inline void get1stArgument(GP::Datum& outResult, GP::Context& ioContext);
  inline void get2ndArgument(GP::Datum& outResult, GP::Context& ioContext);
  inline void get3rdArgument(GP::Datum& outResult, GP::Context& ioContext);

private:
  std::string  mName;             //!< Name of the primitive.
  unsigned int mNumberArguments;  //!< Number of arguments of the primitive.

};

}
}

#endif // Beagle_GP_Primitive_hpp


