//
// 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 _CONDITIONAL_EFFECT_H
#define _CONDITIONAL_EFFECT_H

#include "../IEffect.h"
#include "../ILogicalExp.h"

namespace planning
{
  /// <summary>
  /// This class represents a conjunction of effects.
  /// </summary>
  class ConditionalEffect : public virtual IEffect
  {
  public:
    ConditionalEffect(const ILogicalExp* condition, const IEffect* effect)
      : m_condition(condition), m_effect(effect)
    {
      assert(m_condition != NULL);
      assert(m_effect != NULL);

      m_condition->acquire(this);
      m_effect->acquire(this);

      INITIALIZE_HASH_CODE();
    }

    ~ConditionalEffect()
    {
      m_condition->release(this);
      m_effect->release(this);
    }

    virtual void update(const IClosedWorld* evaluationWorld, 
                        IDurativeClosedWorld* updateWorld, 
                        const Bindings* bindings, 
                        const ActionContext* actionContext) const;

  protected:
    virtual void _output(std::ostream& out) const;

    virtual size_t _calculateHashCode() const
    {
      return 23 * m_condition->getHashCode()
           + 5 * m_effect->getHashCode()
           + 5804385;
    }

    virtual int _internalCompareTo(const IExp& other) const
    {
      const ConditionalEffect& effect = dynamic_cast<const ConditionalEffect&>(other);

      COMPARE(m_condition, effect.m_condition);

      COMPARE(m_effect, effect.m_effect);

      return 0;
    }

    virtual const IExp* _internalApply(const Bindings* bindings) const
    {
      return new ConditionalEffect(m_condition->apply(bindings), m_effect->apply(bindings));
    }

  private:
    const ILogicalExp* m_condition;
    const IEffect* m_effect;
  };
}

#endif