GetConstantNode.cc 5.73 KB
/*
 * GetConstantNode.cc
 *
 *  Created on: Sep 18, 2018
 *      Author: AKKA
 */

#include "ServicesServer.hh"
#include "Parameter.hh"
#include "ParamGet.hh"
#include "ParamGetConstant.hh"
#include "ParameterManager.hh"

using namespace AMDA::Parameters;

#include "Constant.hh"

#include "Config.hh"
#include "GetConstantNode.hh"
#include "ConstantParamData.hh"

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

using namespace AMDA::XMLConfigurator;

namespace AMDA {
namespace XMLParameterConfigurator {

class ConstantParamNode: public AMDA::XMLConfigurator::NodeGrpCfg {
public:
	ConstantParamNode() : AMDA::XMLConfigurator::NodeGrpCfg() {
	}

	void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext)
	{
		LOG4CXX_DEBUG(gLogger,	"ConstantParamNode::proceed: '" << pNode->name << "' node")

		// Context setting
		ServicesServer* lServicesServer = pContext.get<ServicesServer*>();
		Parameter* lParentParameter = pContext.get<Parameter*>();
		ParameterManager* lParameterManager =  pContext.get<ParameterManager*>();

		// Attributes list

		try {
			// Sampling
			xmlChar* lSampling = xmlGetProp(pNode, (const xmlChar*)"sampling");
			std::string samplingStr;
			double sampling = 0.;
			if (lSampling) {
				samplingStr = std::string((const char*) lSampling);
				boost::trim(samplingStr);
				xmlFree(lSampling);
				if (samplingStr.empty()) {
					ERROR_EXCEPTION(ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@sampling")
				}
				try {
					sampling = boost::lexical_cast<double>(samplingStr);
				} catch (const boost::bad_lexical_cast &e) {
					ERROR_EXCEPTION(ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@sampling")
				}
				if (sampling <= 0) {
					ERROR_EXCEPTION(ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@sampling")
				}
			}
			else {
				ERROR_EXCEPTION(ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@sampling")
			}

			// Type
			xmlChar* lType = xmlGetProp(pNode, (const xmlChar*)"type");
			std::string typeStr;
			AMDA::ConstantInterface::ConstantParamType type = AMDA::ConstantInterface::TYPE_FLOAT;
			if (lType) {
				typeStr = std::string((const char*) lType);
				boost::trim(typeStr);
				boost::algorithm::to_lower(typeStr);
				xmlFree(lType);
				if (typeStr.compare("float") == 0) {
					type = AMDA::ConstantInterface::TYPE_FLOAT;
				}
				else if (typeStr.compare("double") == 0) {
					type = AMDA::ConstantInterface::TYPE_DOUBLE;
				}
				else if (typeStr.compare("int") == 0) {
					type = AMDA::ConstantInterface::TYPE_INT;
				}
				else if (typeStr.compare("short") == 0) {
					type = AMDA::ConstantInterface::TYPE_SHORT;
				}
				else {
					ERROR_EXCEPTION(ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@type")
				}
			}
			else {
				ERROR_EXCEPTION(ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@type")
			}

			// Value
			xmlChar* lValue = xmlGetProp(pNode, (const xmlChar*)"value");
			std::string valueStr;
			double value = 0.;
			if (lValue) {
				valueStr = std::string((const char*) lValue);
				boost::trim(valueStr);
				xmlFree(lValue);
				if (valueStr.empty()) {
					ERROR_EXCEPTION(ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@value")
				}
				try {
					value = boost::lexical_cast<double>(valueStr);
				} catch (const boost::bad_lexical_cast &e) {
					ERROR_EXCEPTION(ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@value")
				}
			}
			else {
				ERROR_EXCEPTION(ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@value")
			}

			// Dim1
			xmlChar* lDim1 = xmlGetProp(pNode, (const xmlChar*)"dim1");
			std::string dim1Str("1");
			int dim1 = 1;
			if (lDim1) {
				dim1Str = std::string((const char*) lDim1);
				boost::trim(dim1Str);
				xmlFree(lDim1);
				try {
					dim1 = boost::lexical_cast<int>(dim1Str);
				} catch (const boost::bad_lexical_cast &e) {
					dim1Str = "1";
					dim1 = 1;
				}
			}

			// Dim 2
			xmlChar* lDim2 = xmlGetProp(pNode, (const xmlChar*)"dim2");
			std::string dim2Str("1");
			int dim2 = 1;
			if (lDim2) {
				dim2Str = std::string((const char*) lDim2);
				boost::trim(dim2Str);
				xmlFree(lDim2);
				try {
					dim2 = boost::lexical_cast<int>(dim2Str);
				} catch (const boost::bad_lexical_cast &e) {
					dim2Str = "1";
					dim2 = 1;
				}
			}

			//build unique param id
			std::string paramId = "constant_";
			paramId += samplingStr;
			paramId += "_";
			paramId += typeStr;
			paramId += "_";
			paramId += valueStr;
			paramId += "_";
			paramId += dim1Str;
			paramId += "_";
			paramId += dim2Str;


			lParameterManager->applyParamIdCorrection(paramId);

			ParameterSPtr lParameter;
			if ( lParameterManager->addParameter(lParentParameter,paramId,lParameter)) {
				const char *lSouceParamGet = "CONSTANT";
				AMDA::ConstantInterface::ParamGetConstantSPtr lParamGet(dynamic_cast<AMDA::ConstantInterface::ParamGetConstant*>(lServicesServer->getParamGet( lSouceParamGet, *lParameter)));

				DataWriterSPtr lDataWriter( lParamGet);
				std::string* xmlFileName = pContext.get<std::string*>();
				lDataWriter->setSignatureTrigger(*xmlFileName);
				lParamGet->setSampling(sampling);
				lParamGet->setType(type);
				lParamGet->setValue(value);
				lParamGet->setDim1(dim1);
				lParamGet->setDim2(dim2);
				lParameter->setDataWriter(lDataWriter);

				AMDA::Parameters::CfgContext lContext(pContext);
				lContext.push<AMDA::ConstantInterface::ParamGetConstant*>(lParamGet.get());
				NodeGrpCfg::proceed(pNode, lContext);
			}
		} catch (...) {
			throw;
		}
	}
};

GetConstantNode::GetConstantNode() : NodeGrpCfg()
{
	getChildList()["param"] = NodeCfgSPtr(new XMLParameterConfigurator::ConstantParamNode());
}

void GetConstantNode::proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext)
{
	NodeGrpCfg::proceed(pNode, pContext);
}

}/* namespace XMLParameterConfigurator */
} /* namespace AMDA */