/*******************************************************************************
 * 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 "EventLaw.hh"
#include <stdlib.h>
#include "MemberAccess.hh"
#include <sstream>
#include "values/Value.hh"
#include "values/ValueInt.hh"
#include "values/ValueReal.hh"
#include "expressions/Expression.hh"


namespace epoch {
    namespace altarica {
    




	EventLaw * EventLaw::clone() const {
	    EventLaw * el;
	    if (ma_ != NULL)
		el = new EventLaw(ma_->clone(), type_, param1_, param2_);
	    else
		el = new EventLaw(NULL, type_, param1_, param2_);
	    return el;
	}
	
	EventLaw::~EventLaw() {
	    delete ma_;
      //     delete param_;      
	}

      void EventLaw::deleteExpr() 
      {
        delete param1_;        
      }
      

	bool EventLaw::isDisabled() const {
	    return (type_ == ALTARICA_DISCRETE && param1_ == 0);
	}

	bool EventLaw::isDirac() const {
	    return type_ == ALTARICA_DIRAC_0;
	}

	bool EventLaw::isDiscrete() const {
	    return type_ == ALTARICA_DISCRETE;
	}

	bool EventLaw::isCont() const {
	    return type_ == ALTARICA_EXP;
	}

	bool EventLaw::isTimed() const {
	    return type_ == ALTARICA_TIMED;
	}

	double EventLaw::evalParam(const AltaricaModel *am, AltaricaState *s) const {
	    Value *v = param1_->evaluate(am, s, n_, true);
	    double ret;
	    if (v->isInt())
		ret = ((ValueInt*)v)->toInt();
	    else
		ret = ((ValueReal*)v)->toDouble();
	    Value::deleteIfPossible(v);
	    return ret;
	}

	std::string EventLaw::toString() const {
	    std::stringstream ss;
	    switch(type_) {
	    case ALTARICA_TIMED:
		ss<<"timed(" << param1_->toString() <<", " << param2_->toString() << ")";
		break;
	    case   ALTARICA_DISCRETE:
		ss <<  "discrete(" << param1_->toString() <<")";
		break; 
	    case ALTARICA_EXP:
		ss <<  "exp(" << param1_->toString() <<")";
		break;
	    case ALTARICA_DIRAC_0:
		ss<<"dirac(0)";
		break;
	    }
	    return ss.str();
	}

    }
}


