//
// 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 "OrExp.h"
#include "../UnaryFunctions.h"

#include <assert.h>

using namespace planning;

const std::string OrExp::s_image = "or";

/// <summary>
/// Evaluates this logical expression in the specified closed world.
/// </summary>
/// <param name="world">The evaluation world.</param>
/// <param name="bindings">A set of variable bindings.</param>
/// <returns>True, false, or undefined.</returns>
bool OrExp::evaluate(const IClosedWorld* world, const Bindings* bindings) const
{
  for (const_iterator it = m_elements.begin(); 
       it != m_elements.end();
       ++it)
  {
    if ((*it)->evaluate(world, bindings))
      return true;
  }
  return false;
}

/// <summary>
/// Enumerates all the worlds within which this logical expression evaluates to true.
/// This method is used to support the goal modality expressions.
/// </summary>
/// <returns>All the worlds satisfying this logical expression.</returns>
void OrExp::enumerateSatisfyingWorlds(PartialWorldSet& worlds) const
{
  assert(worlds.size() == 0);

  for (const_iterator it = m_elements.begin(); 
       it != m_elements.end();
       ++it)
  {
    PartialWorldSet otherWorlds;
    (*it)->enumerateSatisfyingWorlds(otherWorlds);
    worlds.insert(otherWorlds.begin(), otherWorlds.end());
  }

  //  HashSet<PartialWorld> contexts = new HashSet<PartialWorld>();
  //  foreach (ILogicalExp exp in this.m_expressions)
  //  {
  //    contexts.UnionWith(exp.EnumerateAllSatisfyingWorlds());
  //  }
  //  return contexts;
}

const IExp* OrExp::_internalApply(const Bindings* bindings) const
{
  return new OrExp(m_elements.size(),
                   boost::transform_iterator<ApplyBindings<ILogicalExp>, const_iterator>(m_elements.begin(), bindings), 
                   boost::transform_iterator<ApplyBindings<ILogicalExp>, const_iterator>(m_elements.end(), bindings));
}