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

#include "../ILogicalExp.h"

#include <stdexcept>
#include <assert.h>

namespace planning
{
  /// <summary>
  /// This class represents a comparison between two expressions.
  /// </summary>
  /// <typeparam name="ArgumentType">The type of argument to compare.</typeparam>
  /// <typeparam name="Comparator">The comparator used to perform the comparison.</typeparam>
  template <class ArgumentType, class Comparator>
  class ComparisonExp : public ILogicalExp
  {
  public:
    /// <summary>
    /// Creates a new comparison expression with the two specified arguments.
    /// </summary>
    /// <param name="op">A string representation of this operator.</param>
    /// <param name="arg1">The first argument.</param>
    /// <param name="arg2">The second argument.</param>
    ComparisonExp(const std::string&op, const ArgumentType* arg1, const ArgumentType* arg2)
      : m_op(op), m_arg1(arg1), m_arg2(arg2), m_comparator()
    {
      assert(m_arg1 != NULL);
      assert(m_arg2 != NULL);

      m_arg1->acquire(this);
      m_arg2->acquire(this);

      INITIALIZE_HASH_CODE();
    }

    ~ComparisonExp()
    {
      m_arg1->release(this);
      m_arg2->release(this);
    }

    /// <summary>
    /// Evaluates this logical expression in the specified closed world.
    /// </summary>
    /// <param name="world">The evaluation world.</param>
    /// <param name="bindings">A set of variable bindings.</param>
    /// <returns>True, false, or undefined.</returns>
    virtual bool evaluate(const IClosedWorld* world, const Bindings* bindings) const
    {
      return m_comparator(m_arg1->evaluate(world, bindings), m_arg2->evaluate(world, bindings));
    }

    /// <summary>
    /// Enumerates all the worlds within which this logical expression evaluates to true.
    /// This method is used to support the goal modality expressions.
    /// </summary>
    /// <returns>All the worlds satisfying this logical expression.</returns>
    virtual void enumerateSatisfyingWorlds(PartialWorldSet& worlds) const
    {
      unused(worlds);
      throw std::runtime_error("ComparisonExp::enumerateSatisfyingWorlds() is not supported.");
    }

  protected:
    virtual void _output(std::ostream& out) const
    {
      out << "(" << m_op << " " << *m_arg1 << " " << *m_arg2 << ")";
    }

    virtual size_t _calculateHashCode() const
    {
      return 3 * m_arg1->getHashCode()
           + 7 * m_arg2->getHashCode();
    }

    virtual int _internalCompareTo(const IExp& other) const
    {
      const ComparisonExp<ArgumentType, Comparator>& exp = 
        dynamic_cast<const ComparisonExp<ArgumentType, Comparator>&>(other);

      COMPARE(m_arg1, exp.m_arg1);

      COMPARE(m_arg2, exp.m_arg2);

      return 0;
    }

    /// <summary>
    /// A string representation of the operator.
    /// </summary>
    const std::string &m_op;

    /// <summary>
    /// The first argument of the comparison operator.
    /// </summary>
    const ArgumentType* m_arg1;

    /// <summary>
    /// The second argument of the comparison operator.
    /// </summary>
    const ArgumentType* m_arg2;

    const Comparator m_comparator;

    ///// <summary>
    ///// This function is not supported.
    ///// </summary>
    ///// <exception cref="NotSupportedException">A NotSupportedException is always thrown since
    ///// this function is not supported.</exception>
    //public override HashSet<PartialWorld> EnumerateAllSatisfyingWorlds()
    //{
    //  throw new NotSupportedException();
    //}

    ///// <summary>
    ///// Evaluates the progression of this constraint expression in the next worlds.
    ///// </summary>
    ///// <param name="world">The current world.</param>
    ///// <param name="bindings">A set of variable bindings.</param>
    ///// <returns>True, false, or undefined.</returns>
    //public override ProgressionValue Progress(IReadOnlyDurativeClosedWorld world, LocalBindings bindings)
    //{
    //  return new ProgressionValue(Evaluate(world, bindings));
    //}

    ///// <summary>
    ///// Evaluates this constraint expression in an idle world, i.e. a world which
    ///// won't be modified by further updates.
    ///// </summary>
    ///// <param name="idleWorld">The (idle) evaluation world.</param>
    ///// <param name="bindings">A set of variable bindings.</param>
    ///// <returns>True, false, or undefined.</returns>
    //public override Bool EvaluateIdle(IReadOnlyDurativeClosedWorld idleWorld, LocalBindings bindings)
    //{
    //  return Evaluate(idleWorld, bindings);
    //}

    ///// <summary>
    ///// Substitutes all occurrences of the variables that occur in this
    ///// expression by their corresponding bindings.
    ///// The base implementation is to simply clone the object.
    ///// </summary>
    ///// <param name="bindings">The bindings.</param>
    ///// <returns>A substituted copy of this expression.</returns>
    //public override IExp Apply(ParameterBindings bindings)
    //{
    //  ComparisonExp<T> other = (ComparisonExp<T>)base.Apply(bindings);
    //  other.m_arg1 = (T)this.m_arg1.Apply(bindings);
    //  other.m_arg2 = (T)this.m_arg2.Apply(bindings);

    //  return other;
    //}

    ///// <summary>
    ///// Standardizes all occurrences of the variables that occur in this
    ///// expression. The IDictionary argument is used to store the variable already
    ///// standardized. Remember that free variables are existentially quantified.
    ///// The base implementation is to simply clone the object.
    ///// </summary>
    ///// <param name="images">The object that maps old variable images to the standardize
    ///// image.</param>
    ///// <returns>A standardized copy of this expression.</returns>
    //public override IExp Standardize(IDictionary<string, string> images)
    //{
    //  ComparisonExp<T> other = (ComparisonExp<T>)base.Standardize(images);
    //  other.m_arg1 = (T)this.m_arg1.Standardize(images);
    //  other.m_arg2 = (T)this.m_arg2.Standardize(images);

    //  return other;
    //}

    ///// <summary>
    ///// Returns the free variables in this expression.
    ///// </summary>
    ///// <returns>The free variables in this expression.</returns>
    //public override HashSet<Variable> GetFreeVariables()
    //{
    //  HashSet<Variable> vars = new HashSet<Variable>();
    //  vars.UnionWith(this.m_arg1.GetFreeVariables());
    //  vars.UnionWith(this.m_arg2.GetFreeVariables());
    //  return vars;
    //}

    ///// <summary>
    ///// Returns true if the expression is ground, i.e. it does not contain any variables.
    ///// </summary>
    ///// <returns>Whether the expression is ground.</returns>
    //public override bool IsGround()
    //{
    //  return this.m_arg1.IsGround() && this.m_arg2.IsGround();
    //}

    ///// <summary>
    ///// Returns a clone of this expression.
    ///// </summary>
    ///// <returns>A clone of this expression.</returns>
    //public override object Clone()
    //{
    //  ComparisonExp<T> other = (ComparisonExp<T>)base.Clone();
    //  other.m_arg1 = (T)this.m_arg1.Clone();
    //  other.m_arg2 = (T)this.m_arg2.Clone();
    //  return other;
    //}

    ///// <summary>
    ///// Returns true if this expression is equal to a specified object.
    ///// </summary>
    ///// <param name="obj">Object to test for equality.</param>
    ///// <returns>True if this expression is equal to the specified objet.</returns>
    //public override bool Equals(Object obj)
    //{
    //  if (obj == this)
    //  {
    //    return true;
    //  }
    //  else if (obj.GetType() == this.GetType())
    //  {
    //    ComparisonExp<T> other = (ComparisonExp<T>)obj;
    //    return this.m_arg1.Equals(other.m_arg1)
    //        && this.m_arg2.Equals(other.m_arg2);
    //  }
    //  else
    //  {
    //    return false;
    //  }
    //}

    ///// <summary>
    ///// Returns the hash code of this expression.
    ///// </summary>
    ///// <returns>The hash code of this expression.</returns>
    //public override int GetHashCode()
    //{
    //  return this.m_arg1.GetHashCode()
    //       + this.m_arg2.GetHashCode();
    //}

    ///// <summary>
    ///// Returns a string representation of this expression.
    ///// </summary>
    ///// <returns>A string representation of this expression.</returns>
    //public override string ToString()
    //{
    //  StringBuilder str = new StringBuilder();
    //  str.Append("(");
    //  str.Append(this.m_op.ToString().ToLower());
    //  str.Append(" ");
    //  str.Append(this.m_arg1.ToString());
    //  str.Append(" ");
    //  str.Append(this.m_arg2.ToString());
    //  str.Append(")");
    //  return str.ToString();
    //}

    ///// <summary>
    ///// Returns a typed string representation of this expression.
    ///// </summary>
    ///// <returns>A typed string representation of this expression.</returns>
    //public override string ToTypedString()
    //{
    //  StringBuilder str = new StringBuilder();
    //  str.Append("(");
    //  str.Append(this.m_op.ToString().ToLower());
    //  str.Append(" ");
    //  str.Append(this.m_arg1.ToTypedString());
    //  str.Append(" ");
    //  str.Append(this.m_arg2.ToTypedString());
    //  str.Append(")");
    //  return str.ToString();
    //}

    // #region IComparable<IExp> Interface

    ///// <summary>
    ///// Compares this comparison expression with another expression.
    ///// </summary>
    ///// <param name="other">The other expression to compare this expression to.</param>
    ///// <returns>An integer representing the total order relation between the two expressions.
    ///// </returns>
    //public override int CompareTo(IExp other)
    //{
    //  int value = base.CompareTo(other);
    //  if (value != 0)
    //    return value;

    //  ComparisonExp<T> otherExp = (ComparisonExp<T>)other;

    //  value = this.m_arg1.CompareTo(otherExp.m_arg1);
    //  if (value != 0)
    //    return value;

    //  return this.m_arg2.CompareTo(otherExp.m_arg2);
    //}

    //#endregion

private :
  // const member prenvent the assgment operator
  ComparisonExp& operator= (const ComparisonExp& );
  };
}

#endif