//
// 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
//

#include "stdafx.h"
#include "ForallEffect.h"
#include "AndEffect.h"
#include "../BindingsIterator.h"
#include "../../World/IDurativeClosedWorld.h"

using namespace planning;

const std::string ForallEffect::s_image = "forall";

/// <summary>
/// Updates the specified world with this effect.
/// A universal effect updates the world for all substitutions of its quantified variables.
/// </summary>
/// <param name="evaluationWorld">The world to evaluate conditions against.</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>
void ForallEffect::update(const IClosedWorld* evaluationWorld, 
                          IDurativeClosedWorld* updateWorld, 
                          const Bindings* bindings, 
                          const ActionContext* actionContext) const
{
  //BindingsIterator bindingsIt(m_variables, bindings);
  //while (bindingsIt.nextBinding())
  //{
  //  const Bindings& currentBindings = bindingsIt.getCurrentBindings();
  //  m_body->update(evaluationWorld, updateWorld, &currentBindings, actionContext);
  //}

  return _getGroundExp()->update(evaluationWorld, updateWorld, bindings, actionContext);
}

IEffect* ForallEffect::_generateGroundExp() const
{
  std::vector<const IEffect*> effects;
  BindingsIterator bindingsIt(m_variables);
  while (bindingsIt.nextBinding())
  {
    const Bindings& currentBindings = bindingsIt.getCurrentBindings();
    effects.push_back(m_body->apply(&currentBindings));
  }
  return new AndEffect(effects.size(), effects.begin(), effects.end());
}

void ForallEffect::_output(std::ostream& out) const
{
  QuantifiedExp<IEffect>::_output(out); 
}

const IExp* ForallEffect::_internalApply(const Bindings* bindings) const
{
  return new ForallEffect(m_variables.begin(), m_variables.end(), m_body->apply(bindings));
}