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

#include "DescribedFormula.h"
#include "AtomicFormulaApplication.h"

namespace planning
{
  /// <summary>
  /// An atomic formula is a boolean described formula (a described predicate).
  /// </summary>
  class AtomicFormula : public DescribedFormula
  {
  public:
    /// <summary>
    /// Creates a new atomic formula with the specified name and arguments.
    /// </summary>
    /// <param name="name">The name of this atomic formula.</param>
    /// <param name="arguments">The arguments of this atomic formula.</param>
    /// <param name="attributes">The new atomic formula's attributes.</param>
    template <class Iterator>
    AtomicFormula(const std::string& name, Iterator argumentsBegin, Iterator argumentsEnd, 
                  Attributes attributes)
      : DescribedFormula(name, argumentsBegin, argumentsEnd, attributes)
    {
    }

    AtomicFormula(const std::string& name, ObjectParameterVariable* argument, Attributes attributes)
      : DescribedFormula(name, &argument, &argument + 1, attributes)
    {
    }

    template <class Iterator>
    AtomicFormulaApplication* getFormulaApplication(Iterator argumentsBegin, 
                                                          Iterator argumentsEnd) const
    {
      return dynamic_cast<AtomicFormulaApplication*>(
        RootFormula::getFormulaApplication(argumentsBegin, argumentsEnd));
    }

    template <class Iterator>
    AtomicFormulaApplication* evaluateArguments(Iterator argumentsBegin,
                                                      Iterator argumentsEnd,
                                                      const IClosedWorld* world, 
                                                      const Bindings* bindings) const
    {
      return dynamic_cast<const AtomicFormulaApplication*>(
        RootFormula::evaluateArguments(argumentsBegin, argumentsEnd, world, bindings));
    }

    static const TypeId UniqueTypeId;

  protected:
    template <class TermIterator>
    AtomicFormulaApplication* instantiate(TermIterator begin, TermIterator end) const
    {
      return dynamic_cast<AtomicFormulaApplication*>(DescribedFormula::instantiate(begin, end));
    }

    /// <summary>
    /// Instantiates a formula application associated with this atomic formula.
    /// </summary>
    /// <param name="arguments">Arguments of the formula application to instantiate.</param>
    /// <returns>A new atomic formula application associated with this atomic formula.</returns>
    virtual AtomicFormulaApplication* instantiate(const ITerm** arguments) const;
  };
}

#endif