ParameterInfo.cc 5.64 KB
/*
 * ParameterInfo.cc
 *
 *  Created on: Mar 18 2016
 *      Author: b.renard
 */

#include "ParameterInfo.hh"
#include "TimeInterval.hh"

#include <boost/algorithm/string.hpp>
#include <string>

namespace AMDA {
namespace Parameters {

ParameterInfo::ParameterInfo(ParameterManager& pParameterManager) :
							ParamOutput(pParameterManager),
		_parameter(NULL) {
}

ParameterInfo::~ParameterInfo() {
}

void ParameterInfo::establishConnection() {
	_parameter = _parameterManager.getSampledParameter(_paramName, _samplingMode, _samplingValue, _gapThreshold).get();
	if(_parameter == NULL)  {
		LOG4CXX_ERROR(_logger,"ParamOutput::init parameter : \""<< _paramName <<"\" Not Exist" );
		BOOST_THROW_EXCEPTION( ParamOutput_exception());
	}
	_parameter->openConnection(this);
}


void ParameterInfo::init() {
	_parameter->init(this, _timeIntervalList);
}

void ParameterInfo::writeInfoFile() {
        AMDA::ParameterInfo::ParameterInfoFileWriter fileWriter;

        std::stringstream filePath;
	filePath << "info_" << _paramName.c_str() << ".xml";
	fileWriter.initWriter(filePath.str().c_str());
	fileWriter.addAttribute("id", _parameter->getId().c_str());

	unsigned int dim1 = _parameter->getDataWriterTemplate()->getParamData()->getDim1();
	unsigned int dim2 = _parameter->getDataWriterTemplate()->getParamData()->getDim2();

	//Write dimensions info
	fileWriter.startElement("dimensions"); /* dimensions */
	std::string dim_1 = std::to_string(dim1);
	std::string dim_2 = std::to_string(dim2);
	fileWriter.addAttribute("dim_1",dim_1.c_str());
	fileWriter.addAttribute("dim_2",dim_2.c_str());
	fileWriter.endElement(); /* dimensions */

	//Write components info
	std::vector<std::string> components = this->getComponents();
	if (!components.empty()) {
		fileWriter.startElement("components"); /* components */

		for (int i = 0; i < (int)dim1; ++i) {
			for (int j = 0; j < (int)dim2; ++j) {
				fileWriter.startElement("component"); /* component */
				std::string index_1 = std::to_string(i);
				std::string index_2 = std::to_string(j);

				fileWriter.addAttribute("index_1",index_1.c_str());
				if (dim2 > 1)
					fileWriter.addAttribute("index_2",index_2.c_str());
				fileWriter.addAttribute("name",(components[i*(int)dim2+j]).c_str());
				fileWriter.endElement(); /* component */
			}
		}

		fileWriter.endElement(); /* components */
	}

	// Write tables info
	AMDA::Info::ParamInfoSPtr paramInfo = AMDA::Info::ParamMgr::getInstance()->getParamInfoFromId(_parameter->getInfoId());

	fileWriter.startElement("tables"); /* tables */
	boost::shared_ptr<AMDA::Info::ParamTable> table1SPtr = paramInfo->getTable(0);
	if (table1SPtr != nullptr) {
		this->writeTable(table1SPtr, fileWriter, "dim_1");
	}

	boost::shared_ptr<AMDA::Info::ParamTable> table2SPtr = paramInfo->getTable(1);
	if (table2SPtr != nullptr) {
		this->writeTable(table2SPtr, fileWriter, "dim_2");
	}

	fileWriter.endElement(); /* tables */

	fileWriter.closeWriter();
}

std::vector<std::string> ParameterInfo::getComponents() {
	unsigned int dim1 = _parameter->getDataWriterTemplate()->getParamData()->getDim1();
	unsigned int dim2 = _parameter->getDataWriterTemplate()->getParamData()->getDim2();

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

	std::vector<std::string> components;

	if ( dim1 * dim2 <= 1 ) //scalar => no components
		return components;

	std::string componentsStr = paramInfo->getComponents();

	//Split labels
	boost::split(components,componentsStr,boost::is_any_of(","));

	if (components.size() != dim1 * dim2) {
		//If labels not defined (or misdefined)
		components.clear();
		for (int i = 0; i < (int)dim1; ++i) {
			for (int j = 0; j < (int)dim2; ++j) {
				std::string crtComp;
				crtComp += (!paramInfo->getShortName().empty() ? paramInfo->getShortName() : paramInfo->getId());
				crtComp += "[";
				if (dim1 > 1)
					crtComp += std::to_string(i);
				if (dim1 > 1 && dim2 > 1)
					crtComp += ",";
				if (dim2 > 1)
					crtComp += std::to_string(j);
				crtComp += "]";
				components.push_back(crtComp);
			}
		}
	}

	return components;
}

void ParameterInfo::writeTable(boost::shared_ptr<AMDA::Info::ParamTable> tableSPtr, AMDA::ParameterInfo::ParameterInfoFileWriter& fileWriter, std::string relatedDim) {
	if (tableSPtr == nullptr)
		return;
	fileWriter.startElement("table"); /* table */
	fileWriter.addAttribute("relatedDim", relatedDim.c_str() );
	fileWriter.addAttribute("name",tableSPtr->getName(&_parameter->getParameterManager()).c_str());
	fileWriter.addAttribute("units",tableSPtr->getUnits(&_parameter->getParameterManager()).c_str());
	if (tableSPtr->isVariable(&_parameter->getParameterManager())) {
		fileWriter.addAttribute("variable","true");
	}
	else {
		for (int i = 0; i < tableSPtr->getSize(&_parameter->getParameterManager()); ++i) {
			AMDA::Info::t_TableBound bound = tableSPtr->getBound(&_parameter->getParameterManager(),i);
			fileWriter.startElement("channel"); /* channel */
			std::string min = std::to_string(bound.min);
			std::string max = std::to_string(bound.max);
			fileWriter.addAttribute("min",min.c_str());
			fileWriter.addAttribute("max",max.c_str());
			fileWriter.endElement(); /* channel */
		}
	}
	fileWriter.endElement(); /* table */
}

void ParameterInfo::apply() {
    //Intervals loop
    while (_currentTimeInterval != _timeIntervalList->end())
    {
        if (_currentTimeInterval->_stopTime - _currentTimeInterval->_startTime == 0) {
            ++_currentTimeInterval;
            continue;
        }
    
        _parameterManager.getParameter(_parameter->getId())->getAsync(this).get();
                
        ++_currentTimeInterval;
    }
    
    writeInfoFile();
}

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