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

#include "../../ITemporalExp.h"

namespace planning
{
  /// <summary>
  /// Represents the "next" operator of the LTL language. This is not part of PDDL; it was rather added
  /// for TLPlan.
  /// </summary>
  /// <remarks>
  /// The next operator specifies that a given constraint must be satisfied in the next world generated.
  /// </remarks>
  class NextExp : public ITemporalExp
  {
    // TODO: not suitable for domains with concurrency, or events...

  public:
    NextExp(const ITemporalExp* exp);

    ~NextExp();

    virtual ProgressionValue progress(const IDurativeClosedWorld* world, const Bindings* bindings) const;

    virtual bool evaluateIdle(const IDurativeClosedWorld* world, const Bindings* bindings) const;

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

    virtual size_t _calculateHashCode() const
    {
      return 73 * m_exp->getHashCode() 
           + 9842034;
    }

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

    virtual int _internalCompareTo(const IExp& other) const;

    const ITemporalExp* m_exp;

    ///// <summary>
    ///// Creates a new "next" constraint expression.
    ///// </summary>
    ///// <param name="arg">The body of the constraint expression.</param>
    //public NextExp(IConstraintExp arg)
    //  : base(arg)
    //{
    //  System.Diagnostics.Debug.Assert(arg != null);
    //}

    ///// <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, undefined, or a progressed expression.</returns>
    ///// <exception cref="PDDLParser.Exception.BindingException">A BindingException is thrown if an attempt
    ///// is made to evaluate an unbound variable.</exception>
    ///// <seealso cref="IConstraintExp.Progress"/>
    //public override ProgressionValue Progress(IReadOnlyDurativeClosedWorld world, LocalBindings bindings)
    //{
    //  return new ProgressionValue(this.Exp, ProgressionValue.NoTimestamp);
    //}

    ///// <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>
    ///// <exception cref="PDDLParser.Exception.BindingException">A BindingException is thrown if an attempt
    ///// is made to evaluate an unbound variable.</exception>
    ///// <seealso cref="IConstraintExp.EvaluateIdle"/>
    //public override Bool EvaluateIdle(IReadOnlyDurativeClosedWorld idleWorld, LocalBindings bindings)
    //{
    //  return this.Exp.EvaluateIdle(idleWorld, bindings);
    //}

    ///// <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("(next ");
    //  str.Append(this.Exp.ToString());
    //  str.Append(")");
    //  return str.ToString();
    //}

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

#endif