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

#include "NumericAssignEffect.h"
#include "../../IInitEl.h"

namespace planning
{
  /// <summary>
  /// This class represents a numeric fluent direct assignment.
  /// </summary>
  class NumericAssign : public NumericAssignEffect, public IInitEl
  {
  public:
    /// <summary>
    /// Creates a new numeric fluent direct assignment.
    /// </summary>
    /// <param name="head">The numeric fluent to assign a value to.</param>
    /// <param name="body">The value to assign to the numeric fluent.</param>
    NumericAssign(const NumericFluentApplication* head, 
                        const INumericExp* body)
      : NumericAssignEffect(s_image, head, body)
    {
    }

    /// <summary>
    /// Updates the specified world with this effect.
    /// </summary>
    /// <param name="evaluationWorld">The world to evaluate conditions against. Note that this is 
    /// usually the un-modified version of the world to update.</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
    {
      updateWorld->setNumericFluent(m_head->evaluateArguments(evaluationWorld, bindings), 
                            m_body->evaluate(evaluationWorld, bindings));
    }

  protected:
    virtual const IExp* _internalApply(const Bindings* bindings) const
    {
      return new NumericAssign(m_head->apply(bindings), m_body->apply(bindings));
    }

  private:
    static const std::string s_image;



    ///// <summary>
    ///// Updates the world with this fluent assignment.
    ///// </summary>
    ///// <param name="head">The evaluated fluent application to update.</param>
    ///// <param name="updateWorld">The world to update.</param>
    ///// <param name="bindings">A set of variable bindings.</param>
    //protected override void UpdateWorldWithAssignEffect(FluentApplication head, IDurativeOpenWorld updateWorld,
    //                                                    LocalBindings bindings)
    //{
    //  FuzzyDouble bodyValue = this.Body.Evaluate(updateWorld, bindings);
    //  if (bodyValue.Status != FuzzyDouble.State.Defined)
    //  {
    //    throw new UndefinedExpException(this.ToString() +
    //      " failed since the second operand evaluates to undefined or unknown.");
    //  }
    //  else
    //  {
    //    updateWorld.SetNumericFluent((NumericFluentApplication)head, bodyValue.Value);
    //  }
    //}
  };
}

#endif