//
// 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 _I_LOGICAL_EXP_H
#define _I_LOGICAL_EXP_H

#include "ITemporalExp.h"
#include "../World/PartialWorld.h"

#include <boost/unordered_set.hpp>


namespace planning
{
  class Bindings;
  class IDurativeClosedWorld;
  /// <summary>
  /// A logical expression yields a boolean value when evaluated.
  /// Evaluation in a closed world returns true, false, or undefined.
  /// Evaluation in an open world returns true, false, undefined, or unknown.
  /// </summary>
  class ILogicalExp : public ITemporalExp
  {
  public:
    typedef boost::unordered_set<PartialWorld*, ExpHash<PartialWorld>, ExpEqual<PartialWorld>> PartialWorldSet;
    typedef bool evaluated_type;

    /// <summary>
    /// Evaluates this logical expression in the specified closed world.
    /// The bindings should not be modified by this call.
    /// </summary>
    /// <param name="world">The evaluation world.</param>
    /// <param name="bindings">A set of variable bindings.</param>
    /// <returns>True, false, or undefined.</returns>
    /// <exception cref="PDDLParser.Exception.BindingException">A BindingException is thrown if an attempt
    /// is made to evaluate an unbound variable.</exception>
    virtual bool evaluate(const IClosedWorld* world, const Bindings* bindings) const = 0;

    /// <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>
    virtual void enumerateSatisfyingWorlds(PartialWorldSet& worlds) const = 0;
  
    //
    // Other functions
    //
    const ILogicalExp* apply(const Bindings* bindings) const
    {
      return dynamic_cast<const ILogicalExp*>(IExp::apply(bindings));
    }

    virtual ProgressionValue progress(const IDurativeClosedWorld* world, const Bindings* bindings) const;

    virtual bool evaluateIdle(const IDurativeClosedWorld* world, const Bindings* bindings) const;

    /// <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>
    //HashSet<PartialWorld> EnumerateAllSatisfyingWorlds();

    /// <summary>
    /// Evaluates this logical expression in the specified open world.
    /// The bindings should not be modified by this call.
    /// </summary>
    /// <param name="world">The evaluation world.</param>
    /// <param name="bindings">A set of variable bindings.</param>
    /// <returns>True, false, undefined, or unknown.</returns>
    /// <exception cref="PDDLParser.Exception.BindingException">A BindingException is thrown if an attempt
    /// is made to evaluate an unbound variable.</exception>
    //FuzzyBool Evaluate(IReadOnlyOpenWorld world, ILocalBindings bindings);

    /// <summary>
    /// Simplifies this logical expression by evaluating its known expression parts.
    /// The bindings should not be modified by this call.
    /// The resulting expression should not contain any unbound variables, since
    /// they are substituted according to the bindings supplied.
    /// </summary>
    /// <param name="world">The evaluation world.</param>
    /// <param name="bindings">A set of variable bindings.</param>
    /// <returns>True, false, undefined, or the simplified expression.</returns>
    /// <exception cref="PDDLParser.Exception.BindingException">A BindingException is thrown if an attempt
    /// is made to evaluate an unbound variable.</exception>
    //LogicalValue Simplify(IReadOnlyOpenWorld world, ILocalBindings bindings);
  };
}

#endif