SingleParamProcess.cc 6.17 KB
/*
 * SingleParamProcess.cc
 *
 *  Created on: Feb 6, 2013
 *      Author: f.casimir
 */

#include "Operation.hh"
#include "ParamData.hh"
#include "SingleParamProcess.hh"
#include "ParameterCreatorFromExpression.hh"
#include "ParamMgr.hh"

namespace AMDA {
	namespace Parameters {

		SingleParamProcess::SingleParamProcess(Parameter &parameter) : Process(parameter), _paramInput(NULL), _treatTerminated(false), _expressionParsed(false)
		{
		}

		SingleParamProcess::SingleParamProcess(const SingleParamProcess &pProcess, Parameter &parameter) :
				Process(pProcess,parameter),
				_parameterInput(pProcess._parameterInput),
				_paramInput(NULL), _treatTerminated(false), _expressionParsed(false) {
			  //Establish Connection Without Parse
			  _parameterInput->openConnection(this);
		}

		SingleParamProcess::~SingleParamProcess() {
		}

		void SingleParamProcess::parse() {
				ParameterCreatorFromExpression creator(_parameter.getParameterManager());
				_parameterInput = creator.getOneParameterFromExpression(_parameter,_expression, isUserProcess());
		}


		void SingleParamProcess::establishConnection() {
			if (!_expressionParsed)
				parse();
			_expressionParsed = true;
			_parameterInput->openConnection(this);
		}

		unsigned int SingleParamProcess::write() {
			int ret = 0;
			unsigned int nbDataBeforeCallProcess = _paramData->getDataNumber();
			ParamDataIndexInfo lParamDataIndexInfo;

			this->_treatTerminated = false;

			lParamDataIndexInfo =_parameterInput->getAsync(this).get();
			_operation->write(lParamDataIndexInfo);

			ret = _paramData->getDataNumber() - nbDataBeforeCallProcess;
                        bool updateDims = (nbDataBeforeCallProcess == 0) && (ret > 0);

			// Reset operation to prepare static data for the next TimeInterval.
			if (lParamDataIndexInfo._timeIntToProcessChanged) {
				_paramData->getIndexInfo()._endTimeIntIndexList.push_back(_paramData->getDataNumber());
				_operation->reset();
			}
			// There is no more time interval to process
			else if (lParamDataIndexInfo._noMoreTimeInt) {
				_paramData->getIndexInfo()._endTimeIntIndexList.push_back(_paramData->getDataNumber());
			} else {
				// Nothing to do.
			}

			// Pull up information on which time interval changed.
			_paramData->getIndexInfo()._timeIntToProcessChanged = lParamDataIndexInfo._timeIntToProcessChanged;
			_paramData->getIndexInfo()._noMoreTimeInt = lParamDataIndexInfo._noMoreTimeInt;
                        
                        if (updateDims)
                            _paramData->updateDims();
			
			return ret;
		}

		/*
		 * @brief @ to know the resampling strategy to use. If it's true, the nearest value will be use
		 */
		bool SingleParamProcess::useNearestValue()
		{
			return _parameterInput->getDataWriterTemplate()->useNearestValue();
		}

		/**
		 * @brief update parameter info in relation to the process
		 */
		void SingleParamProcess::updateInfo(Parameter & parameter)
		{
			LOG4CXX_DEBUG(_logger, "SingleParamProcess::updateInfo - " << parameter.getId());

			if (parameter.getInfoId().empty())
				parameter.setInfoId(parameter.getId());

			//just clone param info from input parameter to processed parameter
			AMDA::Info::ParamInfoSPtr paramInfo = AMDA::Info::ParamMgr::getInstance()->cloneParamInfoFromId(_parameterInput->getInfoId(), parameter.getInfoId());

			if (paramInfo == nullptr)
				return;

			//Derived parameter => no comes from a dataset
			paramInfo->setDatasetId("");

			std::string processInfo = "Single param process from '";
			processInfo += _parameterInput->getId();
			processInfo += "'";

			paramInfo->addLinkedParamId(_parameterInput->getId());
                        
                                                            // adding tableParams into linked params 
                                                            std::map<int, boost::shared_ptr<AMDA::Info::ParamTable>> tables =paramInfo->getTables();

                                                            if(! tables.empty()){
                                                                 for(auto table : tables){
                                                                     if (table.second != nullptr){
                                                                            if(table.second->isVariable(&parameter.getParameterManager())){
                                                                            std::map<std::string, std::string> tableParams = table.second->getTableParams(&parameter.getParameterManager());
                                                                            if(! tableParams.empty())
                                                                                for(auto tableParam : tableParams){
                                                                                    paramInfo->addLinkedParamId(tableParam.second);
                                                                                }
                                                                     
                                                                           }
                                                                     }
                                                               }
                                                            }

			paramInfo->setProcessInfo(processInfo);
		}

		/*
		 * @brief Get min sampling
		 */
		double SingleParamProcess::getMinSampling()
		{
			if (!_expressionParsed)
				parse();
			_expressionParsed = true;
			if (_parameterInput == nullptr)
				return 0;
			if (!_parameterInput->getReferenceParameter().empty()) {
				std::string paramRefId = _parameterInput->getReferenceParameter();
				ParameterSPtr refParam = _parameterInput->getParameterManager().getParameter(paramRefId);
				return refParam->getDataWriterTemplate()->getMinSampling();
			}
			else if (_parameterInput->getTimeResolution() > 0)
				return _parameterInput->getTimeResolution();
			if (_parameterInput->getDataWriterTemplate() == nullptr)
				return 0;
			return _parameterInput->getDataWriterTemplate()->getMinSampling();
		}
	} /* namespace Parameters */
} /* namespace AMDA */