//
// 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 this file was inspired in part by the PDDL4J library:
// http://www.math-info.univ-paris5.fr/~pellier/software/software.php 
//
// Implementation: Daniel Castonguay
// Project Manager: Froduald Kabanza
//

#include "stdafx.h"
#include "AlwaysExp.h"
#include "Composite/TemporalAndExp.h"
#include "../../World/IDurativeClosedWorld.h"

using namespace planning;

AlwaysExp::AlwaysExp(const ITemporalExp* exp)
  : m_exp(exp)
{
  m_exp->acquire(this);

  INITIALIZE_HASH_CODE();
}

AlwaysExp::~AlwaysExp()
{
  m_exp->release(this);
}

ProgressionValue AlwaysExp::progress(const IDurativeClosedWorld* world, const Bindings* bindings) const
{
  //return (this.Exp.Progress(world, bindings) && (new ProgressionValue(this, ProgressionValue.NoTimestamp)));

  ProgressionValue value = m_exp->progress(world, bindings);
  if (value.isFalse())
  {
    return value;
  }
  else if (value.isTrue())
  {
    return this;
  }
  else
  {
    return ProgressionValue(new TemporalAndExp(value.getExp(), this->apply(bindings)));
  }
}

bool AlwaysExp::evaluateIdle(const IDurativeClosedWorld* world, const Bindings* bindings) const
{
  return m_exp->evaluateIdle(world, bindings);
}

void AlwaysExp::_output(std::ostream& out) const
{
  out << "(always " << *m_exp << ")";
}

const IExp* AlwaysExp::_internalApply(const Bindings* bindings) const
{
  return new AlwaysExp(m_exp->apply(bindings));
}

int AlwaysExp::_internalCompareTo(const IExp& other) const
{
  const AlwaysExp& exp = dynamic_cast<const AlwaysExp&>(other);

  COMPARE(m_exp, exp.m_exp);

  return 0;
}
