/*******************************************************************************
 * Copyright (c) 2015-2017 ONERA and IRT AESE (IRT Saint Exupéry).
 * Ce logiciel est la propriété de l’ONERA (the French Aerospace Lab) et de
 * l'IRT AESE (IRT Saint Exupéry).
 * 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) and of
 * the IRT AESE (IRT Saint Exupéry).
 * 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
 *     Alexandre Albore (IRT Saint-Exupéry) - Altarica/Fiacre translation
 *
 *******************************************************************************/

#include "StructuredDomain.hh"
#include "Variable.hh"

namespace epoch {
    namespace altarica {
    

	StructuredDomain::StructuredDomain() {
	    arity_ = 1;
	}
	
	void StructuredDomain::addDomains(std::vector<std::string> *domainNames, Domain* domain) {
	    for (std::vector<std::string>::iterator idit = domainNames->begin(); idit!=domainNames->end(); ++idit)
		addDomain(*idit, domain);
	}

	void StructuredDomain::addDomain(std::string domainName, Domain* domain) {
	    if (sdomain_.find(domainName) != sdomain_.end())
		BOOST_THROW_EXCEPTION(std::runtime_error("bad identifier for domain name"));
	    sdomain_[domainName] = domain;
	    arity_ *= domain->getArity();
	}

	void StructuredDomain::addMyDomainsTo(StructuredDomain * sd) {
	    for (std::map<std::string, Domain*>::iterator dit = sdomain_.begin();
		 dit != sdomain_.end(); ++dit)
		sd->addDomain((*dit).first,(*dit).second);
	}


	unsigned int StructuredDomain::index(std::string arg) const {
	    BOOST_THROW_EXCEPTION(std::runtime_error(std::string("index asked on a Structured domain")));
	    return 0;
	}

	std::string  StructuredDomain::text(unsigned int index) const {
	    BOOST_THROW_EXCEPTION(std::runtime_error(std::string("text asked on a Structured domain")));
	    return "text associated to a structured domain";
	}

	Value * StructuredDomain::toValue() const {
	    BOOST_THROW_EXCEPTION(std::runtime_error(std::string("toValue asked on a Structured domain")));
	    return NULL;	   	    
	}

	int StructuredDomain::getMin() const {
	    BOOST_THROW_EXCEPTION(std::runtime_error(std::string("getMin asked on a Structured domain")));
	    return -1;	   	    
	}

	int StructuredDomain::getMax() const {
	    BOOST_THROW_EXCEPTION(std::runtime_error(std::string("getMax asked on a Structured domain")));
	    return -1;	   	    
	}


	// Value * StructuredDomain::getFixedValue(int i) {
	//     BOOST_THROW_EXCEPTION(std::runtime_error(std::string("getFixedValue(i) asked on a structDomain")));
	//     return NULL;	   	    
	// }

	Value * StructuredDomain::getValue(int i) {
	    BOOST_THROW_EXCEPTION(std::runtime_error(std::string("getFixedValue(i) asked on a structDomain")));
	    return NULL;	   	    	    
	}

	std::vector<Variable*> * StructuredDomain::flatten(Variable* v) const {
	    std::vector<Variable*> * nvv = new std::vector<Variable*>();	

	    for (std::map<std::string, Domain*>::const_iterator i = sdomain_.begin(); i != sdomain_.end(); ++i) {
		std::vector<Variable*> * fv = (*i).second->flatten(v);
		for (std::vector<Variable*>::iterator fvi= fv->begin(); fvi != fv->end(); ++fvi) {		
		    Variable * nv = (*fvi)->clone();
		    nv->addToMASecond((*i).first);
		    nvv->push_back(nv);
		}
		delete(fv);
	    }		
	    return nvv;
	}

	std::string StructuredDomain::toString() const {
	    return "STRUCTURED";
	}

	StructuredDomain::~StructuredDomain() {
	    sdomain_.clear();
	}


    }
}


