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

#include "Composite/TemporalAndExp.h"
#include "Composite/TemporalNotExp.h"
#include "TLPlan/WeakUntilExp.h"

namespace planning
{
  /// <summary>
  /// Represents a constraint expression which must be evaluated at the end of the trajectory.
  /// </summary>
  class SometimeBeforeExp : public WeakUntilExp
  {
  public:
    SometimeBeforeExp(const ITemporalExp* exp, const ITemporalExp* beforeExp)
      : WeakUntilExp(new TemporalAndExp(new TemporalNotExp(m_exp), new TemporalNotExp(m_beforeExp)),
                     new TemporalAndExp(m_beforeExp, new TemporalNotExp(m_exp))), 
        m_exp(exp), m_beforeExp(beforeExp)
    {
      assert(m_exp != NULL);
      assert(m_beforeExp != NULL);

      m_exp->acquire(this);
      m_beforeExp->acquire(this);

      INITIALIZE_HASH_CODE();
    }

    ~SometimeBeforeExp()
    {
      m_exp->release(this);
      m_beforeExp->release(this);
    }

  protected:
    virtual void _output(std::ostream& out) const;

    virtual size_t _calculateHashCode() const
    {
      return 19 * m_exp->getHashCode()
           + 7 * m_beforeExp->getHashCode()
           + 879314;
    }

    virtual const IExp* _internalApply(const Bindings* bindings) const;

    virtual int _internalCompareTo(const IExp& other) const;

  private:
    const ITemporalExp* m_exp;
    const ITemporalExp* m_beforeExp;
  };
}

#endif