ProcessSumIntoTableRange.cc 3.79 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), _mainParamId("") {
  }

  ProcessSumIntoTableRange::ProcessSumIntoTableRange(const ProcessSumIntoTableRange& pProcess, Parameter &parameter)
    : MultiParamProcess_CRTP(pProcess,parameter), _dim1Table(NULL), _dim2Table (NULL) {
  }

  ProcessSumIntoTableRange::~ProcessSumIntoTableRange() {
  }

  void ProcessSumIntoTableRange::establishConnection() {
	  ParameterCreatorFromExpression creator(_parameter.getParameterManager());
	  ParameterSPtr lParameter = creator.getOneParameterFromExpression(_parameter,_expression);
	  _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()) {
				  for (std::map<std::string, std::string>::iterator it = _dim1Table->getTableParams().begin(); it != _dim1Table->getTableParams().end(); ++it) {
					  _paramNameList[it->second].first = _parameter.getParameterManager().getParameter(it->second);
				  }
			  }
		  }

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

	  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'")));
	}

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

	AMDA::Info::ParamTable* tablePtr = NULL;
	switch (relatedDim) {
	case 0:
		tablePtr = _dim1Table;
		break;
	case 1:
		tablePtr = _dim2Table;
		break;
	default:
		tablePtr = NULL;
	}

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

	TimeStamp timeStamp = MultiParamProcess::init();

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

	SumIntoTableRangeCreator lSumIntoTableRangeCreator(*this, *lmainParamInput, minRange, maxRange, tablePtr);
	_operation = lSumIntoTableRangeCreator.getOperation();
	_paramData = ParamDataSPtr(_operation->getParamOutput());
	_paramData->setMinSampling(lmainParamInput->getMinSampling());

	return timeStamp;
}

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