//
// Copyright (c) 2009 Froduald Kabanza and the Université de Sherbrooke.
// Use of this software is permitted for non-commercial research purposes, and
// it may be copied or applied only for that use. All copies must include this
// copyright message.
// 
// This is a research prototype that has not gone through intensive tests and
// is delivered as is. It may still contain bugs. Froduald Kabanza and the
// Université de Sherbrooke disclaim any responsibility for damage that may be
// caused by using it.
//
// Please note that some files were inspired in part by the PDDL4J library:
// http://www.math-info.univ-paris5.fr/~pellier/software/software.php 
//
// Implementation: Simon Chamberland
// Project Manager: Froduald Kabanza
//

#ifndef _AND_EFFECT_H
#define _AND_EFFECT_H

#include "../FlatListExp.h"
#include "../IEffect.h"
#include "NoEffect.h"

#include <vector>

namespace planning
{
  /// <summary>
  /// This class represents a conjunction of effects.
  /// </summary>
  class AndEffect : public FlatListExp<IEffect>, public virtual IEffect
  {
  public:
    /// <summary>
    /// Creates a new conjunction of effects.
    /// </summary>
    /// <param name="effects">The effects associated with the conjunctive expression.</param>
    template <class Iterator>
    AndEffect(size_t n, Iterator effectsBegin, Iterator effectsEnd)
      : FlatListExp<IEffect>(n, s_image, effectsBegin, effectsEnd)
    {
      INITIALIZE_HASH_CODE();
    }

    AndEffect(const IEffect* effect1, const IEffect* effect2)
      : FlatListExp<IEffect>(s_image, effect1, effect2)
    {
      INITIALIZE_HASH_CODE();
    }

    template <class Iterator>
    static const IEffect* instantiate(size_t n, Iterator elementsBegin, Iterator elementsEnd)
    {
      if (n == 0)
      {
        assert(elementsBegin == elementsEnd);
        return NoEffect::getInstance();
      }
      else if (n == 1)
      {
        assert(++Iterator(elementsBegin) == elementsEnd);
        return *elementsBegin;
      }
      else
      {
        return new AndEffect(n, elementsBegin, elementsEnd);
      }
    }

    /// <summary>
    /// Updates the specified world with this effect.
    /// A conjunction of effects sequentially updates the world with each internal effect.
    /// </summary>
    /// <param name="evaluationWorld">The world to evaluate conditions against.</param>
    /// <param name="updateWorld">The world to update.</param>
    /// <param name="bindings">A set of variable bindings.</param>
    /// <param name="actionContext">The action evaluation context.</param>
    virtual void update(const IClosedWorld* evaluationWorld, 
                        IDurativeClosedWorld* updateWorld, 
                        const Bindings* bindings, 
                        const ActionContext* actionContext) const;

  protected:
    virtual size_t _calculateHashCode() const
    {
      return CALCULATE_HASH_CODE(FlatListExp)
           + 188578601;
    }

    virtual const IExp* _internalApply(const Bindings* bindings) const;

  private:
    static const std::string s_image;

    ///// <summary>
    ///// Retrieves all the described formulas modified by this effect.
    ///// </summary>
    ///// <returns>All the described formulas modified by this effect.</returns>
    //public HashSet<DescribedFormula> GetModifiedDescribedFormulas()
    //{
    //  HashSet<DescribedFormula> formulas = new HashSet<DescribedFormula>();
    //  foreach (IEffect effect in this.m_expressions)
    //  {
    //    formulas.UnionWith(effect.GetModifiedDescribedFormulas());
    //  }
    //  return formulas;
    //}
  };
}

#endif