//
// 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 "DerivedPredicateApplication.h"
#include "DerivedPredicate.h"

#include <stdexcept>

using namespace planning;

DerivedPredicateApplication::DerivedPredicateApplication(const DerivedPredicate* derivedPredicate, const ITerm** arguments)
  : DefinedFormulaApplication(derivedPredicate, arguments)
{
  INITIALIZE_HASH_CODE();
}

bool DerivedPredicateApplication::evaluate(const IClosedWorld* world, const Bindings* bindings) const
{
  const DerivedPredicateApplication* groundFormula = evaluateArguments(world, bindings);

  return dynamic_cast<const DerivedPredicate*>(m_rootFormula)->evaluate(world, (const Object**)groundFormula->m_arguments);
}

/// <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 DerivedPredicateApplication::enumerateSatisfyingWorlds(PartialWorldSet& worlds) const
{
  throw std::runtime_error("DerivedPredicateApplication::enumerateSatisfyingWorlds() is not supported");
}

const DerivedPredicateApplication* DerivedPredicateApplication::evaluateArguments(const IClosedWorld* world, 
                                                                                  const Bindings* bindings) const
{
  return dynamic_cast<const DerivedPredicateApplication*>(FormulaApplication::evaluateArguments(world, bindings));
}

const DerivedPredicate* DerivedPredicateApplication::getDerivedPredicate() const
{
  return static_cast<const DerivedPredicate*>(m_rootFormula);
}