/*******************************************************************************
 * 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 "ExpressionMEMBER.hh"
#include "../MemberAccess.hh"
#include "../Constant.hh"
#include "../AltaricaModel.hh"

using namespace epoch::altarica;



std::string ExpressionMEMBER::toString() const {
    return ma_->toString();
}


std::string ExpressionMEMBER::toString(const Node *n, const std::pair<std::string,std::string> *f, const AltaricaModel& am) const {

	initSymbol(n, am);

	if ( n != NULL && isConst() )
		return  toString();
	if (n != NULL && n->isSymbol(getSymbol()))
		setIsSymbol(0);
	if (n->findStateVariableByName(s_))
		return ( getIsSymbol() ? (f->second + std::string("&") + n->toFiacre() + std::string("_") + ma_->toString()) : ma_->toString() );
	return ( getIsSymbol() ? (f->first + std::string("&") + n->toFiacre() + std::string("_") + ma_->toString()) : ma_->toString() );
}



std::string ExpressionMEMBER::toString(const Node *n, const AltaricaModel& am) const {
	initSymbol(n, am);
	if ( n != NULL && isConst() )
		return toString();
	if (n != NULL && n->isSymbol(getSymbol()))
	      setIsSymbol(0);
    return ( getIsSymbol() ? (n->toFiacre() + std::string("_") + ma_->toString()) : ma_->toString() );
}


ExpressionMEMBER * ExpressionMEMBER::clone() const {
    return new ExpressionMEMBER(ma_->clone());
}

ExpressionMEMBER::ExpressionMEMBER(MemberAccess *ma) { 
    ma_ = ma;
    s_ = ma_->toString();
    symbol_ = -1;
    //    osCache_ = NULL;
    fs = NULL;
}

ExpressionMEMBER::~ExpressionMEMBER() {
    delete ma_;
    ma_ = NULL;
    // if (osCache_ != NULL)
    // 	Value::release(osCache_);
    if (fs != NULL)
	Value::release(fs);
}

Variable * ExpressionMEMBER::getVariable(const Node*n, const AltaricaModel *am) const {
  if (varsCache_.size() != 0) {      
    Variable * v = varsCache_[n->getIndex()];
    if (v != NULL)
      return v;
  } else 
    initCache(n, am);
  
    if (symbol_ == 1)
    return n->findStateVariableByName(ma_);

  return n->findFlowVariableByName(ma_);
}

void ExpressionMEMBER::initSymbol(const Node *node_, const AltaricaModel &amodel_)  const
{
  if (this->getIsSymbol() != -1)
    return;

  Variable *v;

  if (node_ != NULL && node_->isSymbol(getSymbol())) {
    setIsSymbol(0);
    setSymbol();
  } else {
    if (node_ != NULL)
      v = node_->findStateVariableByName(getMA());
    
    if (v != NULL)
      setIsSymbol(1);
     else {
      if (node_ != NULL)
        v = node_->findFlowVariableByName(getMA());
      if (v != NULL)
        setIsSymbol(2);
      else {
        Constant *c = amodel_.findConstantByName(getMA()->toString());
        if (c != NULL) {
          setIsSymbol(3);
        } else {
          // in this case, symbol that is not in n->symbolCache ( -> symbol of subnode....)
          setIsSymbol(0);
          setSymbol();
        }
      }
    }
  }
}


void ExpressionMEMBER::initCache(const Node *node_, const AltaricaModel *amodel_)  const
{       
  if (this->getIsSymbol() != -1)
    return;

  Variable *v;
  
  if (node_ != NULL && node_->isSymbol(getSymbol())) {
    setIsSymbol(0);
    setSymbol();
  } else {
    if (node_ != NULL) {
      v = node_->findStateVariableByName(getMA());
      resizeCache(amodel_->getNNodes());
    }
    if (v != NULL) {
      setIsSymbol(1);
      setCache(node_->getIndex(), v);
    } else {
      if (node_ != NULL)
        v = node_->findFlowVariableByName(getMA());
      if (v != NULL) {
        setIsSymbol(2);
        setCache(node_->getIndex(), v);
      } else {
        Constant *c = amodel_->findConstantByName(getMA()->toString());
        if (c != NULL) {
          setIsSymbol(3);
        } else {
          // in this case, symbol that is not in n->symbolCache ( -> symbol of subnode....)
          setIsSymbol(0);
          setSymbol();
        }
      }
    }
  }     
}


void ExpressionMEMBER::setSymbol() const {
    fs = new ValueSymbol(s_);
    fs->own();
}

void ExpressionMEMBER::resizeCache(unsigned int i) const {
     varsCache_.resize(i,NULL);
}

void ExpressionMEMBER::setCache(unsigned int i, Variable *v) const {
    	varsCache_[i] = v;
}

