ProcessSamplingClassic.cc 3.71 KB
/**
 * ProcessSamplingClassic.cc
 *
 *  Created on: Dec 11, 2012
 *      Author: AKKA IS
 */
#include <stdlib.h>
#include <string>

#include "ProcessSamplingClassic.hh"
#include "Parameter.hh"
#include "ParameterManager.hh"
#include "ParameterCreatorFromExpression.hh"
#include "Resampling.hh"
#include "ParamMgr.hh"

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

namespace AMDA {
namespace Parameters {

ProcessSamplingClassic::ProcessSamplingClassic(Parameter &parameter) :
		SingleParamProcess_CRTP(parameter) {

}

ProcessSamplingClassic::ProcessSamplingClassic(const ProcessSamplingClassic& pProcess, Parameter &parameter)
: SingleParamProcess_CRTP(pProcess,parameter) {
}

ProcessSamplingClassic::~ProcessSamplingClassic() {
}

TimeStamp ProcessSamplingClassic::init() {
	//LOG4CXX_DEBUG(_logger, "BRE - ProcessSamplingClassic::init - 1");
	/// Init input parameter
	TimeStamp timeStamp = _parameterInput->init( this, _timeIntervalList);
	 /// Calibration information copy
	Parameter::InfoList lInfoList = _parameterInput->getInfoList();
	_parameter.getInfoList().insert(lInfoList.begin(), lInfoList.end());
	_paramInput = _parameterInput->getParamData(this).get();


	//GET Sampling
	if (_attributList.size() != 2) {
		BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_PROCESS_ERR) << AMDA::ex_msg(std::string("ProcessSamplingClassic required attributes: sampling and threshold")));
	}
	double sampling = atof(_attributList.begin()->c_str());
	if ( sampling == 0) {
		BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_PROCESS_ERR) << AMDA::ex_msg(std::string("Sampling must be different of 0")));
	}
	double threshold = atof((++(_attributList.begin()))->c_str());

	double newThreshold = threshold * _paramInput->getMinSampling() / sampling;
	double computedGapSize = _parameterInput->getParameterManager().getComputedGapSize(threshold, _paramInput->getMinSampling());

	if (computedGapSize < sampling) {
		//If computedGapSize < sampling, set computedGapSize to sampling value (else, each interval between data will be considered as a gap)
		newThreshold = 1;
		computedGapSize = sampling;
	}

	_parameter.setGapThreshold(newThreshold);

	// ProcessSamplingClassic _operation creation
	Resampling::CreateResampling lCreateResampling( *this, _timeIntervalList, sampling, computedGapSize, *_paramInput, _parameterInput->getDataWriterTemplate()->useNearestValue());
	_operation = lCreateResampling.getResampling();
	static_cast<Resampling::ResamplingAbstract*>(_operation)->init();

	/// Get result ParamData
	_paramData = ParamDataSPtr(_operation->getParamOutput());
	_paramData->setMinSampling(static_cast<Resampling::ResamplingAbstract*>(_operation)->getSampling());
	return timeStamp;
}

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

	SingleParamProcess::updateInfo(parameter);

	//Param info
	AMDA::Info::ParamInfoSPtr paramInfo = AMDA::Info::ParamMgr::getInstance()->getParamInfoFromId(parameter.getInfoId());

	if (paramInfo == nullptr)
		return;

	std::string processInfo = "Resampling of '";
	processInfo += _parameterInput->getId();
	processInfo += "' with sampling time '";
	processInfo += _attributList.begin()->c_str();
	processInfo += " s'";

	if (_parameterInput->getDataWriterTemplate()->useNearestValue())
		processInfo += ". Use nearest value.";

	paramInfo->setProcessInfo(processInfo);
}

double ProcessSamplingClassic::getMinSampling()
{
	if (_attributList.size() != 2)
		return 0;
	return atof(_attributList.begin()->c_str());
}

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