/*
 *  Open BEAGLE
 *  Copyright (C) 2001-2005 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/ReplacementStrategyOp.hpp
 *  \brief  Definition of the class ReplacementStrategyOp.
 *  \author Christian Gagne
 *  \author Marc Parizeau
 *  $Revision: 1.7 $
 *  $Date: 2005/09/30 15:04:54 $
 */

#ifndef Beagle_ReplacementStrategyOp_hpp
#define Beagle_ReplacementStrategyOp_hpp

#include "beagle/config.hpp"
#include "beagle/macros.hpp"
#include "beagle/Object.hpp"
#include "beagle/Pointer.hpp"
#include "beagle/PointerT.hpp"
#include "beagle/Allocator.hpp"
#include "beagle/AllocatorT.hpp"
#include "beagle/AbstractAllocT.hpp"
#include "beagle/ContainerAllocator.hpp"
#include "beagle/Container.hpp"
#include "beagle/ContainerT.hpp"
#include "beagle/Operator.hpp"
#include "beagle/BreederNode.hpp"
#include "beagle/BreederOp.hpp"

namespace Beagle {


/*!
 *  \class ReplacementStrategyOp beagle/ReplacementStrategyOp.hpp "beagle/ReplacementStrategyOp.hpp"
 *  \brief Replacement strategy operator abstract class.
 *  \ingroup ECF
 *  \ingroup Op
 *
 *  The ReplacementStrategyOp class is the entry point the a breeding tree, and have a
 *  breeder root node. Each breeder are hierachically organized into a tree. A breeder receive a
 *  breeding pool to process, and must return from it one bred individual.
 */
class ReplacementStrategyOp : public Operator {

public:

  //! ReplacementStrategyOp allocator type.
  typedef AbstractAllocT<ReplacementStrategyOp,Operator::Alloc>
          Alloc;
  //! ReplacementStrategyOp handle type.
  typedef PointerT<ReplacementStrategyOp,Operator::Handle>
          Handle;
  //! ReplacementStrategyOp bag type.
  typedef ContainerT<ReplacementStrategyOp,Operator::Bag>
          Bag;

           ReplacementStrategyOp(string inName="Unnamed");
  virtual ~ReplacementStrategyOp() { }

  /*!
   *  \brief Apply the operation on a deme in the given context.
   *  \param ioDeme Reference to the deme on which the operation takes place.
   *  \param ioContext Evolutionary context of the operation.
   */
  virtual void operate(Deme& ioDeme, Context& ioContext) = 0;

  virtual void initialize(System& ioSystem);
  virtual void postInit(System& ioSystem);
  virtual void readWithMap(PACC::XML::ConstIterator inIter, OperatorMap& inOpMap);
  virtual void write(PACC::XML::Streamer& ioStreamer, bool inIndent=true) const;

  /*!
   *  \return Breeder root node.
   */
  inline BreederNode::Handle getRootNode() const
  {
    Beagle_StackTraceBeginM();
    return mBreederRoot;
    Beagle_StackTraceEndM("BreederNode::Handle ReplacementStrategyOp::getRootNode() const");
  }

  /*!
   *  \brief Set breeder root node.
   *  \param inRootNode Breeder root node.
   */
  inline void setRootNode(BreederNode::Handle inRootNode)
  {
    Beagle_StackTraceBeginM();
    mBreederRoot = inRootNode;
    Beagle_StackTraceEndM("void ReplacementStrategyOp::setRootNode(BreederNode::Handle inRootNode)");
  }

protected:

  void buildRoulette(RouletteT<unsigned int>& outRoulette, Context& ioContext) const;

private:

  BreederNode::Handle mBreederRoot;    //!< Breeder root node.

};

}

#endif // Beagle_ReplacementStrategyOp_hpp
