ParameterCreatorFromExpression.cc 3.62 KB
/*
 * ParameterCreatorFromExpression.cc
 *
 *  Created on: Dec 14, 2012
 *      Author: f.casimir
 */

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

#include "DicError.hh"

#include "Process.hh"

#include "ParameterManager.hh"
#include "ServicesServer.hh"

#include "InternLib_Config.hh"
#include "ParameterCreatorFromExpression.hh"

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

namespace AMDA {
	namespace Parameters {

		ParameterCreatorFromExpression::ParameterCreatorFromExpression(ParameterManager& pParameterManager) :
				_parameterManager(pParameterManager) {


		}

		ParameterCreatorFromExpression::~ParameterCreatorFromExpression() {
		}

		ParameterSPtr ParameterCreatorFromExpression::getOneParameterFromExpression(Parameter& pParameter, const std::string& pExpression, bool isUserProcess) {
			ParameterSPtr lParameter;
			_parser.process(pExpression);

			///Case of one parameter return this parameter
			if (_parser.getListParameterName().size() == 1
					&& _parser.getListProcessName().size() == 0
					&& _parser.getListFctName().size() == 0 
					&& _parser.isParamOnly()) {
				lParameter = _parameterManager.getParameter(*_parser.getListParameterName().begin());
				// Here we have a broken link in parameter chaining so create link.
				// Child is already creating before linking it to its parent parameter.
				pParameter.addParameter(lParameter);
			} else {
				///Case of one process return a Parameter with this Process
				if (_parser.getListParameterName().size() == 0
							&& _parser.getListProcessName().size() == 1
							&& _parser.getListFctName().size() == 0
							&& _parser.isProcessOnly()) {
					const Parser::ListProcessName& processNameList = _parser.getListProcessName();
					auto lProcessIt = processNameList.begin();

					if ( _parameterManager.addParameter(&pParameter,lProcessIt->first,lParameter)) {
							Process* lProcess = ServicesServer::getInstance()->getProcess(lProcessIt->second.name,*lParameter.get());
							if ( lProcess) {
								lProcess->setExpression(lProcessIt->second.expression);
								lProcess->setAttributList(lProcessIt->second.attribut);
                                                                lProcess->setIsUserProcess(isUserProcess);
								DataWriterSPtr dataWriter(lProcess);
								lParameter->setDataWriter(dataWriter);
							} else {
								stringstream lError;  lError << "Process: '" << lProcessIt->second.name << "' not found";
								LOG4CXX_ERROR(AMDA::InternLib::gLogger, lError.str());
								BOOST_THROW_EXCEPTION(AMDA::InternLib::exception() << AMDA::errno_code(AMDA_PROCESS_ERR) << AMDA::ex_msg(lError.str()));
							}
						}
				} else {
				    boost::hash<std::string> string_hash;
				     std::stringstream key;
				     key << string_hash(pExpression);
					///Else return a Parameter with a Standard Process
					if ( _parameterManager.addParameter(&pParameter,key.str(),lParameter)) {
						Process *lProcess = ServicesServer::getInstance()->getProcess("standard", *lParameter.get());
						if ( lProcess) {
							lProcess->setExpression(pExpression);
                                                        lProcess->setIsUserProcess(isUserProcess);
							DataWriterSPtr dataWriter(lProcess);
							lParameter->setDataWriter(dataWriter);
						} else {
							stringstream lError;  lError << "Process: 'standard' not found";
							LOG4CXX_ERROR(AMDA::InternLib::gLogger, lError.str());
							BOOST_THROW_EXCEPTION(AMDA::InternLib::exception() << AMDA::errno_code(AMDA_PROCESS_ERR) << AMDA::ex_msg(lError.str()));
						}
					}
				}
			}

			return lParameter;
		}

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