/*
 *  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, G1K 7P4
 *  http://vision.gel.ulaval.ca
 *
 */

/*!
 *  \file   beagle/GP/PrimitiveInline.hpp
 *  \brief  Implementation of some inlined methods of GP::Primitive.
 *  \author Christian Gagne
 *  \author Marc Parizeau
 *  $Revision: 1.3 $
 *  $Date: 2004/04/25 23:12:20 $
 */

#ifndef Beagle_GP_PrimitiveInline_hpp
#define Beagle_GP_PrimitiveInline_hpp

#include "beagle/GP/Tree.hpp"
#include "beagle/GP/Context.hpp"
#include "beagle/GP/Primitive.hpp"


/*!
 *  \brief Get the value of the nth argument.
 *  \param inN Index of the argument to get.
 *  \param outResult Value of the nth argument.
 *  \param ioContext Evolutionary context.
 *  \throw Beagle::AssertException If Argument index is invalid.
 */
inline void Beagle::GP::Primitive::getArgument(unsigned int inN,
                                               Beagle::GP::Datum& outResult,
                                               Beagle::GP::Context& ioContext)
{
  using namespace Beagle;
  Beagle_BoundCheckAssertM(inN, 0, mNumberArguments-1);
  GP::Tree& lActualTree = ioContext.getGenotype();
  unsigned int lNodeIndex = ioContext.getCallStackTop() + 1;
  for(unsigned int i=0; i<inN; i++) lNodeIndex += lActualTree[lNodeIndex].mSubTreeSize;
  ioContext.pushCallStack(lNodeIndex);
  Beagle_LogDebugM(
    ioContext.getSystem().getLogger(),
    "primitive", "Beagle::GP::Primitive",
    std::string("Executing the primitive \"")+
    lActualTree[lNodeIndex].mPrimitive->getName()+std::string("\" of the ")+uint2ordinal(lNodeIndex+1)+
    std::string(" node of the ")+uint2ordinal(ioContext.getGenotypeIndex()+1)+" tree"
  );
  lActualTree[lNodeIndex].mPrimitive->execute(outResult, ioContext);
  ioContext.popCallStack();
}


/*!
 *  \brief Get the value of the arguments.
 *  \param outResults Value of the arguments. Array of the good size must be allocated.
 *  \param inSizeTDatum Size (in byte) of each datum of the array.
 *  \param ioContext Evolutionary context.
 *  \throw Beagle::AssertException If the size of the bag doesn't match the nb. of arguments.
 */
inline void Beagle::GP::Primitive::getArguments(Beagle::GP::Datum outResults[],
                                                size_t inSizeTDatum,
                                                Beagle::GP::Context& ioContext)
{
  using namespace Beagle;
  char* lCharResults = reinterpret_cast<char*>(outResults);
  GP::Tree& lActualTree = ioContext.getGenotype();
  unsigned int lNodeIndex = ioContext.getCallStackTop() + 1;
  ioContext.pushCallStack(lNodeIndex);
  lActualTree[lNodeIndex].mPrimitive->execute(*outResults, ioContext);
  ioContext.popCallStack();
  for(unsigned int i=1; i<mNumberArguments; i++) {
    lNodeIndex += lActualTree[lNodeIndex].mSubTreeSize;
    ioContext.pushCallStack(lNodeIndex);
    GP::Datum* lResultsI = reinterpret_cast<GP::Datum*>(lCharResults + (i*inSizeTDatum));
    Beagle_LogDebugM(
      ioContext.getSystem().getLogger(),
      "primitive", "Beagle::GP::Primitive",
      std::string("Executing the primitive \"")+
      lActualTree[lNodeIndex].mPrimitive->getName()+std::string("\" of the ")+
      uint2ordinal(lNodeIndex+1)+std::string(" node of the ")+
      uint2ordinal(ioContext.getGenotypeIndex()+1)+" tree"
    );
    lActualTree[lNodeIndex].mPrimitive->execute(*lResultsI, ioContext);
    ioContext.popCallStack();
  }
}


/*!
 *  \brief Get the value of the first argument.
 *  \param outResult Value of the first argument.
 *  \param ioContext Evolutionary context.
 *  \throw Beagle::AssertException If there is no argument to the primitive.
 */
inline void Beagle::GP::Primitive::get1stArgument(Beagle::GP::Datum& outResult,
                                                  Beagle::GP::Context& ioContext)
{
  using namespace Beagle;
  Beagle_AssertM(getNumberArguments() >= 1);
  unsigned int lNodeIndex = ioContext.getCallStackTop() + 1;
  ioContext.pushCallStack(lNodeIndex);
  Beagle_LogDebugM(
    ioContext.getSystem().getLogger(),
    "primitive", "Beagle::GP::Primitive",
    std::string("Executing the primitive \"")+
    ioContext.getGenotype()[lNodeIndex].mPrimitive->getName()+std::string("\" of the ")+
    uint2ordinal(lNodeIndex+1)+std::string(" node of the ")+
    uint2ordinal(ioContext.getGenotypeIndex()+1)+" tree"
  );
  ioContext.getGenotype()[lNodeIndex].mPrimitive->execute(outResult, ioContext);
  ioContext.popCallStack();
}


/*!
 *  \brief Get the value of the second argument.
 *  \param outResult Value of the second argument.
 *  \param ioContext Evolutionary context.
 *  \throw Beagle::AssertException If there is no third argument to the primitive.
 */
inline void Beagle::GP::Primitive::get2ndArgument(Beagle::GP::Datum& outResult,
                                                  Beagle::GP::Context& ioContext)
{
  using namespace Beagle;
  Beagle_AssertM(getNumberArguments() >= 2);
  GP::Tree& lActualTree = ioContext.getGenotype();
  unsigned int lNodeIndex = ioContext.getCallStackTop() + 1;
  lNodeIndex += lActualTree[lNodeIndex].mSubTreeSize;
  ioContext.pushCallStack(lNodeIndex);
  Beagle_LogDebugM(
    ioContext.getSystem().getLogger(),
    "primitive", "Beagle::GP::Primitive",
    std::string("Executing the primitive \"")+
    lActualTree[lNodeIndex].mPrimitive->getName()+std::string("\" of the ")+
    uint2ordinal(lNodeIndex+1)+std::string(" node of the ")+
    uint2ordinal(ioContext.getGenotypeIndex()+1)+" tree"
  );
  lActualTree[lNodeIndex].mPrimitive->execute(outResult, ioContext);
  ioContext.popCallStack();
}


/*!
 *  \brief Get the value of the third argument.
 *  \param outResult Value of the third argument.
 *  \param ioContext Evolutionary context.
 *  \throw Beagle::AssertException If there is no second argument to the primitive.
 */
inline void Beagle::GP::Primitive::get3rdArgument(Beagle::GP::Datum& outResult,
                                                  Beagle::GP::Context& ioContext)
{
  using namespace Beagle;
  Beagle_AssertM(getNumberArguments() >= 3);
  GP::Tree& lActualTree = ioContext.getGenotype();
  unsigned int lNodeIndex = ioContext.getCallStackTop() + 1;
  lNodeIndex += lActualTree[lNodeIndex].mSubTreeSize;
  lNodeIndex += lActualTree[lNodeIndex].mSubTreeSize;
  ioContext.pushCallStack(lNodeIndex);
  Beagle_LogDebugM(
    ioContext.getSystem().getLogger(),
    "primitive", "Beagle::GP::Primitive",
    std::string("Executing the primitive \"")+
    lActualTree[lNodeIndex].mPrimitive->getName()+std::string("\" of the ")+
    uint2ordinal(lNodeIndex+1)+std::string(" node of the ")+
    uint2ordinal(ioContext.getGenotypeIndex()+1)+" tree"
  );
  lActualTree[lNodeIndex].mPrimitive->execute(outResult, ioContext);
  ioContext.popCallStack();
}


#endif // Beagle_GP_PrimitiveInline_hpp

