//
// 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_OPERATION_ASSIGN_EFFECT_H
#define _NUMERIC_OPERATION_ASSIGN_EFFECT_H

#include "NumericAssignEffect.h"

#include <functional>

namespace planning
{
  template <class functional>
  class ArithmeticFunctional : public ArithmeticFunction
  {
  public:
    virtual double operator() (double d1, double d2) const
    {
      return m_functional(d1, d2);
    }

  private:
    functional m_functional;
  };

  /// <summary>
  /// This class represents a numeric fluent assignment.
  /// </summary>
  template <class functional>
  class NumericOperationAssignEffect : public NumericAssignEffect
  {
  public:
    NumericOperationAssignEffect(const std::string& image, 
                                 const NumericFluentApplication* head, 
                                 const INumericExp* body)
      : NumericAssignEffect(image, head, body)
    {
    }

    /// <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>
    virtual void update(const IClosedWorld* evaluationWorld, 
                        IDurativeClosedWorld* updateWorld, 
                        const Bindings* bindings, 
                        const ActionContext* actionContext) const
    {
      updateWorld->setNumericFluent(m_head->evaluateArguments(evaluationWorld, bindings), 
                                    s_functional,
                                    m_body->evaluate(evaluationWorld, bindings));
    }

  private:
    static ArithmeticFunctional<functional> s_functional;
  };
}

#endif