/*******************************************************************************
 * Copyright (c) 2015-2016 ONERA.
 * Ce logiciel est la propriété de l’ONERA (the French Aerospace Lab).
 * Tous droits réservés.
 *
 * Ce programme et les éléments qui l'accompagnent sont mis à disposition
 * aux conditions définies par le contrat de licence logiciel CeCILL-C soumise
 * au droit français et respectant les principes de diffusion des logiciels libres.
 * Vous pouvez utiliser, modifier et/ou redistribuer ce programme
 * sous les conditions de la licence CeCILL-C (http://www.cecill.info).

 * This software is property of ONERA (the French Aerospace Lab).
 * All rights reserved.
 *
 * This program and the accompanying materials are made available under
 * the terms of the CeCILL-C license under French law and
 * abiding by the rules of distribution of free software.
 * You can  use, modify and/ or redistribute the software under the terms of
 * the CeCILL-C license (http://www.cecill.info).
 *
 * Contributeurs/contributors:
 *     Guillaume Infantes (ONERA - Centre de Toulouse) - initial API and implementation
 *
 *******************************************************************************/

#include "Value.hh"
#include <limits.h>
#include <sstream>
#include "../domains/PredefinedDomain.hh"
#include <boost/functional/hash.hpp>
#include "ValueInt.hh"
#include "ValueBool.hh"
#include "ValueSymbol.hh"
#include "ValueReal.hh"

using namespace epoch::altarica;


bool Value::equals(const Value*v) const {
    //    return v->equals(this);
    if (v->isInt()) 
    	return equals((ValueInt*) v);
    if (v->isBool())
    	return equals((ValueBool*) v);
    if (v->isSymbol())
    	return equals((ValueSymbol*) v);
    if (v->isReal())
    	return equals((ValueReal*) v);
}


void Value::merge(const Value *v) {
    if (v->isBool()) 
	merge((ValueBool*)v);
    else if (v->isSymbol()) 
	merge((ValueSymbol*)v);
    else if (v->isInt())
	merge((ValueInt*)v);
    else
	merge((ValueReal*)v);
}



void Value::merge(const ValueInt*v) {
    BOOST_THROW_EXCEPTION(std::runtime_error("merging open INT to openNOTint"));
}


void Value::merge(const ValueBool*v) {
    BOOST_THROW_EXCEPTION(std::runtime_error("merging open BOOL to openNOTbool"));
}

void Value::merge(const ValueSymbol*v) {
    BOOST_THROW_EXCEPTION(std::runtime_error("merging openSymbol to opennotsymbol"));
}

void Value::merge(const ValueReal*v) {
    BOOST_THROW_EXCEPTION(std::runtime_error("merging openReal to opennotreal"));
}

void Value::intersectValue(const Value* v) {
    if (v->isInt())
	intersectValue((ValueInt*)v);
    else if (v->isBool())
	intersectValue((ValueBool*)v);
    else if (v->isSymbol())
	intersectValue((ValueSymbol*)v);    
    else 
	intersectValue((ValueReal*)v);    	
}

bool Value::contains(const Value *v) const {
    if (v->isBool())
	return contains((ValueBool*)v);
    else  if (v->isSymbol()) 
	return contains((ValueSymbol*)v);
    else if (v->isInt())
	return contains((ValueInt*)v);
    else
	return contains((ValueReal*)v);
}

bool Value::hasIntersection(const Value *v) const {
    if (v->isBool())
	return hasIntersection((ValueBool*)v);
    if (v->isSymbol()) 
	return hasIntersection((ValueSymbol*)v);
    if (v->isInt())
	return hasIntersection((ValueInt*)v);    
    return hasIntersection((ValueReal*)v);
}


bool Value::pDifferent(const Value *v) const{
    if (v->isBool()) 
	return pDifferent((const ValueBool*)v);
    if (v->isSymbol()) 
	return pDifferent((const ValueSymbol*)v);
    if (v->isInt())
	return pDifferent((const ValueInt*)v);
    return pDifferent((const ValueReal*)v);
}

bool Value::contains(const ValueInt*v) const {
    BOOST_THROW_EXCEPTION(std::runtime_error("contains fixed INT in openNOTint"));
}


bool Value::contains(const ValueBool*v) const {
    BOOST_THROW_EXCEPTION(std::runtime_error("contains fixed BOOL in openNOTbool"));
}

bool Value::contains(const ValueSymbol*v) const {
    BOOST_THROW_EXCEPTION(std::runtime_error("contains fixedSymbol in opennotsymbol"));
}

bool Value::contains(const ValueReal*v) const {
    BOOST_THROW_EXCEPTION(std::runtime_error("contains fixedreal in opennotreal"));
}




void Value::removeValue(const Value *v) {
    if (v == NULL)
	return;
    if (v->isInt())
	removeValue((ValueInt*)v);
    else if (v->isBool())
	removeValue((ValueBool*)v);
    else if (v->isSymbol())
	removeValue((ValueSymbol*)v);
    else
	removeValue((ValueReal*)v);	
}

bool Value::less(const Value*v) const {
    if (v->isInt())
	return this->less((ValueInt*)v);
    if (v->isBool())
	return this->less((ValueBool*)v);
    if (v->isSymbol())
	return this->less((ValueSymbol*)v);
    return this->less((ValueReal*)v);
}
