ParamTableOperation.hh 9.19 KB
/**
 * ParamTableOperation.hh
 *
 *  Created on: 23 nov. 2022
 *      Author: AKKA
 */

#ifndef ParamTableOperation_HH_
#define ParamTableOperation_HH_

#include <list>
#include "ParamData.hh"
#include "DataTypeMath.hh"
#include "VisitorOfParamData.hh"
#include "Operation.hh"
#include "ParamTableProcess.hh"

#include "TimeInterval.hh"

namespace AMDA {
namespace Parameters {

namespace ParamTable {

namespace Base {
class TableDataTransform {
public:
	virtual ~TableDataTransform() {}
	virtual std::vector<double>  transformData(unsigned int /*index*/) = 0;
};
}

template<typename ParamInput>
class TableDataTransform : public Base::TableDataTransform {
public:

	/**
	 * @brief Constructor
	 */
	TableDataTransform(ParamInput&  pParamInput) : _paramInput(pParamInput) {}

private:
	/**
	 * @brief paramData Input
	 */
	ParamInput&  _paramInput;
};

template<typename ElType>
class TableDataTransform<ParamDataSpec<std::vector<ElType> >> : public Base::TableDataTransform {
public:
	typedef	ParamDataSpec<std::vector<ElType> > ParamInput;

	/**
	 * @brief Constructor
	 */
	TableDataTransform(ParamInput&  pParamInput) : _paramInput(pParamInput) {}

	/**
	 * @overload Base::TableDataTransform::transformData(unsigned int index)
	 */
	std::vector<double>  transformData(unsigned int index) {
		const std::vector<ElType>& lData = _paramInput.getDataList()[index];
		std::vector<double> transData;
		for (typename std::vector<ElType>::const_iterator it = lData.begin(); it != lData.end(); ++it) {
			transData.push_back((double)*it);
		}
		return transData;
	}

private:
	ParamInput&  _paramInput;
};
    
class ParamTableDataVisitor : public VisitorOfParamData {
public:
	/**
	 * @brief Constructor.
	 */
	ParamTableDataVisitor(ParamData& pInputParamData) : _dataTransform(NULL) {
		pInputParamData.accept(*this);
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataScalaireShort *)
	 */
	void visit(ParamDataScalaireShort *) { BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("ParamTableDataVisitor operation not supported")); }

	/**
	 * @overload VisitorOfParamData::visit(ParamDataScalaireFloat *)
	 */
	void visit(ParamDataScalaireFloat *) { BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("ParamTableDataVisitor operation not supported")); }

	/**
	 * @overload VisitorOfParamData::visit(ParamDataScalaireDouble *)
	 */
	void visit(ParamDataScalaireDouble *) { BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("ParamTableDataVisitor operation not supported")); }

	/**
	 * @overload VisitorOfParamData::visit(ParamDataScalaireLongDouble *)
	 */
	void visit(ParamDataScalaireLongDouble *) { BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("ParamTableDataVisitor operation not supported")); }

	/**
	 * @overload VisitorOfParamData::visit(ParamDataScalaireInt *)
	 */
	void visit(ParamDataScalaireInt *) { BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("ParamTableDataVisitor operation not supported")); }

	/**
	 * @overload VisitorOfParamData::visit(ParamDataLogicalData *)
	 */
	void visit(ParamDataLogicalData *) { BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("ParamTableDataVisitor operation not supported")); }

	/**
	 * @overload VisitorOfParamData::visit(ParamDataTab1DShort *)
	 */
	void visit(ParamDataTab1DShort *pInputParamData) {
		_dataTransform = new TableDataTransform<ParamDataTab1DShort>(*pInputParamData);
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataTab1DFloat *)
	 */
	void visit(ParamDataTab1DFloat *pInputParamData) {
		_dataTransform = new TableDataTransform<ParamDataTab1DFloat>(*pInputParamData);
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataTab1DDouble *)
	 */
	void visit(ParamDataTab1DDouble *pInputParamData) {
		_dataTransform = new TableDataTransform<ParamDataTab1DDouble>(*pInputParamData);
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataTab1DLongDouble *)
	 */
	void visit(ParamDataTab1DLongDouble *pInputParamData) {
		_dataTransform = new TableDataTransform<ParamDataTab1DLongDouble>(*pInputParamData);
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataTab1DInt *)
	 */
	void visit(ParamDataTab1DInt *pInputParamData) {
		_dataTransform = new TableDataTransform<ParamDataTab1DInt>(*pInputParamData);
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataTab1DLogicalData *)
	 */
	void visit(ParamDataTab1DLogicalData *) {
		BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("ParamTableDataVisitor operation not supported"));	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataTab2DShort *)
	 */
	void visit(ParamDataTab2DShort *) {
		BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("ParamTableDataVisitor operation not supported"));
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataTab2DFloat *)
	 */
	void visit(ParamDataTab2DFloat *) {
		BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("ParamTableDataVisitor operation not supported"));
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataTab2DDouble *)
	 */
	void visit(ParamDataTab2DDouble *) {
		BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("ParamTableDataVisitor operation not supported"));
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataTab2DLongDouble *)
	 */
	void visit(ParamDataTab2DLongDouble *) {
		BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("ParamTableDataVisitor operation not supported"));
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataTab2DInt *)
	 */
	void visit(ParamDataTab2DInt *) {
		BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("ParamTableDataVisitor operation not supported"));
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataTab2DLogicalData *)
	 */
	void visit(ParamDataTab2DLogicalData *) {
		BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("TableParamDataPusher operation not supported"));
	}

	/**
	 * @brief get the Deriv parameterized operation.
	 */
	std::vector<double> transformData(unsigned int index) {

		return _dataTransform->transformData(index);
	}

private:
	Base::TableDataTransform* _dataTransform;
};


/**
 * Define ParamTableOperation.
 */
class ParamTableOperation: public Operation {
public:
	/**
	 * @brief constructor
	 */
	ParamTableOperation(ParamTableProcess& pProcess) :
			Operation(pProcess), _processParamTable(pProcess), _paramOutput(new ParamDataTab1DDouble()){
        _paramDataOutput = _paramOutput;
	}
	/**
	 * @brief destructor.
	 */
	virtual ~ParamTableOperation() {
        for (std::map<std::string, ParamTableDataVisitor*>::iterator it = _paramTableDataVisitor.begin(); it != _paramTableDataVisitor.end(); ++it)
			delete it->second;
		_paramTableDataVisitor.clear();
	}

    /**
     * @brief write data in  _paramDataOutput
     * @detail pParamDataIndexInfo contain input description data
     */
    void  write(ParamDataIndexInfo &pParamDataIndexInfo) {
        std::map<std::string, std::string> tableParams = _processParamTable.getParamTable()->getTableParams(&_processParamTable.getParameterManager());
        
        for (unsigned int index = pParamDataIndexInfo._startIndex;
				index
						< pParamDataIndexInfo._startIndex
								+ pParamDataIndexInfo._nbDataToProcess;
				index++) {
                    double time = _processParamTable.getParameterList().begin()->second.first->getParamData(&_process)->getTime(index);
                    std::map<std::string, std::vector<double>> paramsTableData;
                    for (std::map<std::string, std::string>::iterator it = tableParams.begin(); it != tableParams.end(); ++it) {
                        ParameterSPtr p = _processParamTable.getParameterManager().getParameter(it->second);

                        if (_paramTableDataVisitor.find(it->first) == _paramTableDataVisitor.end()) {
					        _paramTableDataVisitor[it->first] = new ParamTableDataVisitor(*p->getParamData(&_processParamTable).get());
				        }

                        paramsTableData[it->first] = _paramTableDataVisitor[it->first]->transformData(index);
                    }
					int size = paramsTableData.begin()->second.size();
                    std::vector<double> tab1DData(size);
                    for (int i = 0; i < size; ++i) {
                        AMDA::Info::t_TableBound bound = _processParamTable.getParamTable()->getBound(&_processParamTable.getParameterManager(), i, &paramsTableData);
                        tab1DData[i] = bound.center;
                    }
                    _paramOutput->pushTime(time);
                    _paramOutput->push(tab1DData);
        }
        
    }

private:
    AMDA::Parameters::ParamTableProcess&  _processParamTable;

    ParamDataTab1DDouble *_paramOutput;

    std::map<std::string, ParamTableDataVisitor*> _paramTableDataVisitor;

};

} /* ParamTable */
} /* AMDA */
} /* Parameters */

#endif /* ParamTableOperation_HH_ */