//
// 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 this file was inspired in part by the PDDL4J library:
// http://www.math-info.univ-paris5.fr/~pellier/software/software.php 
//
// Implementation: Daniel Castonguay
// Project Manager: Froduald Kabanza
//

#include "stdafx.h"
#include "TemporalExistsExp.h"
#include "TemporalOrExp.h"
#include "../../BindingsIterator.h"
#include "../../../World/IDurativeClosedWorld.h"

using namespace planning;

const std::string TemporalExistsExp::s_image = "exists";

ProgressionValue TemporalExistsExp::progress(const IDurativeClosedWorld* world, const Bindings* bindings) const
{
  //BindingsIterator bindingsIt(m_variables, bindings);
  //std::vector<const ITemporalExp*> exps;
  //while (bindingsIt.nextBinding())
  //{
  //  const Bindings& currentBindings = bindingsIt.getCurrentBindings();
  //  ProgressionValue suffix = m_body->progress(world, &currentBindings);
  //  if (suffix.isTrue())
  //    return suffix;
  //  else if (!suffix.isFalse())
  //    exps.push_back(suffix.getExp());
  //}
  //if (exps.size() == 0)
  //  return ProgressionValue(false);
  //else if (exps.size() == 1)
  //  return ProgressionValue(exps[0]);
  //else
  //  return ProgressionValue(new TemporalOrExp(exps.size(), exps.begin(), exps.end()));

  return _getGroundExp()->progress(world, bindings);
}

bool TemporalExistsExp::evaluateIdle(const IDurativeClosedWorld* world, const Bindings* bindings) const
{
  //BindingsIterator bindingsIt(m_variables, bindings);
  //while (bindingsIt.nextBinding())
  //{
  //  const Bindings& currentBindings = bindingsIt.getCurrentBindings();
  //  if (m_body->evaluateIdle(world, &currentBindings))
  //    return true;
  //}
  //return false;

  return _getGroundExp()->evaluateIdle(world, bindings);
}

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

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

