/*
 *  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/PrimitiveSet.hpp
 *  \brief  Definition of the type GP::PrimitiveSet.
 *  \author Christian Gagne
 *  \author Marc Parizeau
 *  $Revision: 1.4 $
 *  $Date: 2004/07/01 23:59:00 $
 */

#ifndef Beagle_PrimitiveSet_hpp
#define Beagle_PrimitiveSet_hpp

#include <string>

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


namespace Beagle {
namespace GP {


// Forward declaration
class System;


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

public:

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

  explicit PrimitiveSet(std::string inRootType="");
  virtual ~PrimitiveSet() { }

  /*!
   *  \brief Set the associated tree's root type.
   *  \param inRootType Root type.
   */
  inline void setRootType(std::string inRootType)
  {
    mRootType = inRootType;
  }

  virtual std::string       getRootType() const;
          Primitive::Handle getPrimitiveByName(std::string inName) const;
  virtual void              initialize(GP::System& ioSystem);
  virtual void              insert(Primitive::Handle inPrimitive, double inWeight=1.0);
  virtual Primitive::Handle selectPrimitive(GP::System& ioSystem);
  virtual Primitive::Handle selectPrimitiveWithArguments(GP::System& ioSystem);
  virtual Primitive::Handle selectPrimitiveWithKArguments(unsigned int inK, GP::System& ioSystem);
  virtual Primitive::Handle selectPrimitiveWithoutArgument(GP::System& ioSystem);
  virtual void              read(XMLNode::Handle& inNode);
  virtual void              write(XMLStreamer& ioStreamer) const;

protected:

  typedef std::vector< std::pair<double,Primitive::Handle> > Roulette;

  void              computeRoulette(Roulette& outRoulette, unsigned int inNbArgs) const;
  Primitive::Handle selectWithRoulette(const Roulette& inRoulette, GP::System& ioSystem) const;
  
  Roulette                        mWeights;      //!< Weigths of primitives.
  Map                             mNames;        //!< Look-up table (by name) of the primitives.
  std::map<unsigned int,Roulette> mMap;          //!< Nb. args -> weigths map.
  std::string                     mRootType;     //!< Associated root type. Used only with STGP.

};

}
}

#endif // Beagle_PrimitiveSet_hpp

