//
// 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 "CompositeType.h"
#include "../Object.h"

using namespace planning;

CompositeType::CompositeType(const std::set<PrimitiveType*>& primitiveTypes)
  : m_primitiveTypes(primitiveTypes)
{
  if (m_primitiveTypes.size() < 2)
    throw TypingException("An attempt was made to create a composite type with less than 2 primitive types.");
}

CompositeType::~CompositeType()
{
}

bool CompositeType::isSubsetOf(const Type* other) const
{
  // s1 is included in s2 <--> primitive subsets of s1 are included in s2

  bool subset = true;
  for (std::set<PrimitiveType*>::const_iterator it = m_primitiveTypes.begin();
       subset && it != m_primitiveTypes.end();
       ++it)
  {
    subset = (*it)->isSubsetOf(other);
  }
  return subset;
}

bool CompositeType::isSupersetOf(const PrimitiveType* other) const
{
  // s1 includes s2 <--> one of s1's primitive subsets includes s2
  bool superset = false;
  for (std::set<PrimitiveType*>::const_iterator it = m_primitiveTypes.begin();
       !superset && it != m_primitiveTypes.end();
       ++it)
  {
    superset = (*it)->isSupersetOf(other);
  }
  return superset;
}

bool CompositeType::intersectsWith(const PrimitiveType* other) const
{
  bool intersects = false;
  for (std::set<PrimitiveType*>::const_iterator it = m_primitiveTypes.begin();
       !intersects && it != m_primitiveTypes.end();
       ++it)
  {
    intersects = (*it)->intersectsWith(other);
  }
  return intersects;
}

bool CompositeType::intersectsWith(const Type* other) const
{
  bool intersects = false;
  for (std::set<PrimitiveType*>::const_iterator it = m_primitiveTypes.begin();
       !intersects && it != m_primitiveTypes.end();
       ++it)
  {
    intersects = (*it)->intersectsWith(other);
  }
  return intersects;
}

void CompositeType::addSubType(PrimitiveType *type)
{
  for (std::set<PrimitiveType*>::const_iterator it = m_primitiveTypes.begin();
       it != m_primitiveTypes.end();
       ++it)
  {
    (*it)->addSubType(type);
  }
}

void CompositeType::retrieveDomain(stdext::hash_set<const Object*>& domain)
{
  for (std::set<PrimitiveType*>::const_iterator it = m_primitiveTypes.begin();
       it != m_primitiveTypes.end();
       ++it)
  {
    (*it)->cacheDomain();

    domain.insert((*it)->domainBegin(), (*it)->domainEnd());
  }
}

void CompositeType::_output(std::ostream& out) const
{
  out << "(either";
  for (std::set<PrimitiveType*>::const_iterator it = m_primitiveTypes.begin();
       it != m_primitiveTypes.end();
       ++it)
  {
    out << " " << **it;
  }
  out << ")";
}
