ProcessSumIntoTableRange.cc 5.52 KB
/*
 * ProcessSumIntoTableRange.cc
 *
 *  Created on: Oct 6, 2016
 *      Author: AKKA IS
 */
#include <stdlib.h>
#include <string>

#include <boost/lexical_cast.hpp>

#include "DicError.hh"
#include "AMDA_exception.hh"

#include "Operation.hh"
#include "ParameterManager.hh"
#include "ProcessSumIntoTableRange.hh"
#include "SumIntoTableRangeCreator.hh"
#include "ParamMgr.hh"
#include "ParameterCreatorFromExpression.hh"

using namespace std;
using namespace boost;
using namespace log4cxx;

namespace AMDA {
namespace Parameters {

  ProcessSumIntoTableRange::ProcessSumIntoTableRange(Parameter &parameter) : MultiParamProcess_CRTP(parameter), _dim1Table(NULL), _dim2Table (NULL), _outputParamTable(NULL), _mainParamId(""), _tablesInit(false) {
  }

  ProcessSumIntoTableRange::ProcessSumIntoTableRange(const ProcessSumIntoTableRange& pProcess, Parameter &parameter)
    : MultiParamProcess_CRTP(pProcess,parameter), _dim1Table(pProcess._dim1Table), _dim2Table (pProcess._dim2Table), _outputParamTable(pProcess._outputParamTable), _mainParamId(pProcess._mainParamId), _tablesInit(pProcess._tablesInit) {
  }

  ProcessSumIntoTableRange::~ProcessSumIntoTableRange() {
  }

  void ProcessSumIntoTableRange::establishConnection() {
	  ParameterCreatorFromExpression creator(_parameter.getParameterManager());
	  ParameterSPtr lParameter = creator.getOneParameterFromExpression(_parameter,_expression, isUserProcess());
	  _mainParamId = lParameter->getId();
	  _paramNameList[lParameter->getId()].first = lParameter;

	  AMDA::Info::ParamInfoSPtr paramInfo = AMDA::Info::ParamMgr::getInstance()->getParamInfoFromId(lParameter->getInfoId());

	  if (paramInfo != nullptr)
	  {
		  std::map<int, boost::shared_ptr<AMDA::Info::ParamTable>>& paramTables = paramInfo->getTables();
		  if (paramTables.find(0) != paramTables.end())
			  _dim1Table = paramInfo->getTable(0).get();
		  else
			  _dim1Table = NULL;
		  if (paramTables.find(1) != paramTables.end())
			  _dim2Table = paramInfo->getTable(1).get();
		  else
			  _dim2Table = NULL;

		  if (_dim1Table != NULL) {
			  if (_dim1Table->isVariable(&_parameter.getParameterManager())) {
				  for (std::map<std::string, std::string>::iterator it = _dim1Table->getTableParams(&_parameter.getParameterManager()).begin(); it != _dim1Table->getTableParams(&_parameter.getParameterManager()).end(); ++it) {
					  _paramNameList[it->second].first = _parameter.getParameterManager().getParameter(it->second);
				  }
			  }
		  }

		  if (_dim2Table != NULL) {
			  if (_dim2Table->isVariable(&_parameter.getParameterManager())) {
				  for (std::map<std::string, std::string>::iterator it = _dim2Table->getTableParams(&_parameter.getParameterManager()).begin(); it != _dim2Table->getTableParams(&_parameter.getParameterManager()).end(); ++it) {
					  _paramNameList[it->second].first = _parameter.getParameterManager().getParameter(it->second);
				  }
			  }
		  }
                  _tablesInit = true;
	  }

	  MultiParamProcess::establishConnection();
  }

  TimeStamp ProcessSumIntoTableRange::init() {
	if (_attributList.size() < 3) {
	  BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_PROCESS_ERR) << AMDA::ex_msg(std::string("ProcessSumIntoTableRange::init required at least 3 attributes'")));
	}

	TimeStamp timeStamp = MultiParamProcess::init();

	if (!_tablesInit) {
		AMDA::Info::ParamInfoSPtr paramInfo = AMDA::Info::ParamMgr::getInstance()->getParamInfoFromId(_paramNameList[_mainParamId].first->getInfoId());
		if (paramInfo != nullptr)
		{
			std::map<int, boost::shared_ptr<AMDA::Info::ParamTable>>& paramTables = paramInfo->getTables();
			if (paramTables.find(0) != paramTables.end())
				_dim1Table = paramInfo->getTable(0).get();
			else
				_dim1Table = NULL;
			if (paramTables.find(1) != paramTables.end())
				_dim2Table = paramInfo->getTable(1).get();
			else
				_dim2Table = NULL;
			_tablesInit = true;
			
		}
	}

	int    relatedDim = atoi(_attributList[0].c_str());
	double minRange1  = atof(_attributList[1].c_str());
	double maxRange1  = atof(_attributList[2].c_str());

	double minRange2 = -1;
	double maxRange2 = -1;
	bool twoRanges = false; 
	if (_attributList.size() == 5) {
		minRange2 = atof(_attributList[3].c_str());
		maxRange2 = atof(_attributList[4].c_str());
		twoRanges = true;
	}

	AMDA::Info::ParamTable* table1Ptr = NULL;
	AMDA::Info::ParamTable* table2Ptr = NULL;
	switch (relatedDim) {
	case 0:
		table1Ptr = _dim1Table;
		if (twoRanges)
			table2Ptr = _dim2Table;
		else
			_outputParamTable = _dim2Table;
		break;
	case 1:
		table1Ptr = _dim2Table;
		if (twoRanges)
			table2Ptr = _dim1Table;
		else
			_outputParamTable = _dim1Table;
		break;
	default:
		table1Ptr = NULL;
		table2Ptr = NULL;
	}

	if (table1Ptr == NULL) {
	  BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_PROCESS_ERR) << AMDA::ex_msg(std::string("ProcessSumIntoTableRange::init Cannot retrieve related table")));
	}
	if (twoRanges && (table2Ptr == NULL)) {
	  BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_PROCESS_ERR) << AMDA::ex_msg(std::string("ProcessSumIntoTableRange::init Cannot retrieve related table")));
	}

	ParamData* lmainParamInput = _paramNameList[_mainParamId].first->getParamData(this).get();

	SumIntoTableRangeCreator lSumIntoTableRangeCreator(*this, *lmainParamInput, minRange1, maxRange1, table1Ptr, relatedDim, minRange2, maxRange2, table2Ptr);
	_operation = lSumIntoTableRangeCreator.getOperation();
	_paramData = ParamDataSPtr(_operation->getParamOutput());
	_paramData->setMinSampling(lmainParamInput->getMinSampling());

	return timeStamp;
}

} /* namespace Parameters */
} /* namespace AMDA */