ParameterManager.hh 5.81 KB
/**
 * ParamterManager.hh
 *
 *  Created on: 15 oct. 2012
 *      Author: AKKA IS
 */

#ifndef PARAMTERMANAGER_HH_
#define PARAMTERMANAGER_HH_

#include <map>
#include <list>
#include <string>
#include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp>

#include "ParamGet.hh"

#include "log4cxx/logger.h"

#include "TimeInterval.hh"


namespace AMDA
{
  namespace Parameters
  {


    class ParameterManager;
    typedef boost::shared_ptr<ParameterManager> ParameterManagerSPtr;
    class Parameter;
    typedef boost::shared_ptr<Parameter> ParameterSPtr;
    class ParamOutput;
    typedef boost::shared_ptr<ParamOutput> ParamOutputSPtr;
    typedef std::list<ParamOutputSPtr> ParamOutputList;

//    typedef std::list<TimeTableCatalog::TimeInterval> TimeIntervalList;
//    typedef boost::shared_ptr<TimeIntervalList> TimeIntervalListSPtr;

    struct ParameterManager_exception: virtual AMDA_exception { };

    class ParameterManager
    {
    public:
        typedef std::map<std::string,ParameterSPtr> ParameterList;

        ParameterManager();
        virtual ~ParameterManager();

      /*
       * @brief Mutex used to protect multi-thread access to CDF lib (CDF lib is not thread-safe!)
       * This mutex is a part of the ParameterManager to be shared between by FileWriterCDF (DownloadOutput plugin)
       * and class and FileReaderCDF (ParamGetLocalFile)
       */
      static boost::mutex mutexCDFLib;

      /*
       * @brief Mutex used to protect multi-thread access to netCDF lib (netCDF lib is not thread-safe!)
       * This mutex is a part of the ParameterManager to be shared between by FileWriterNetCDF (DownloadOutput plugin)
       * and class and FileReaderNetCDF (ParamGetLocalFile)
       */
      static boost::mutex mutexNetCDFLib;

      /**
       * @return no return, do a getParameter
       */
      void createParameter( const std::string& pIDParam);

      /**
       * get the parameter or if sampling information is asked, create the sampling parameter
       */
      ParameterSPtr  getSampledParameter(const std::string& pIDParam, const std::string &samplingMode = "", float samplingValue = 0.0, float gapThreshold = 0.0, bool isUserProcess = true);

      /**
       * get a resampled parameter under a reference parameter
       */
      ParameterSPtr getSampledParameterUnderRefParam(const std::string& pIDParam, const std::string& pIDRefParam, bool isUserProcess);

      /**
       * get a parameter from an expression
       */
      ParameterSPtr getParameterFromExpression(const std::string& pExpression, double gapThreshold = NAN, bool isUserProcess = true);

      /**
       * get a parameter identify by parameterName
       */
      ParameterSPtr  getParameter(const std::string& pIDParam);

  	// Others methods
	 /**
	  * Create a new Parameter.
	  * @return false if already exist otherwise true.
	  */
     bool addParameter(Parameter* pParentParameter,  const std::string& pIDParam, ParameterSPtr& pParamResult);

	ParamOutputList& getParamOutputList() {
		return _paramOutputList;
	}

	void execute(std::string workingPath);

	/**
	 * @brief Retrieve list of time interval
	 */
	TimeIntervalListSPtr getInputIntervals();

	/**
	 * @brief add time interval to the list.
	 */
	void addInputInterval(double pStartTime, double pStopTime, int pIndex, std::string& pttPath, std::string& pttName, int pttTotalIntervals);

	/**
	 * @brief add time interval to the list.
	 */
	void addInputInterval(const TimeTableCatalog::TimeInterval& pTimeInterval);

	/*
	 * @brief add time interval data to the list
	 */
	void addInputIntervalDataList(const int pIndex, const std::string& pDataKey, const std::vector<std::string>& dataList) ;

	/*
	 * get a time interval data specified by the data key
	 */
	std::vector<std::string>& getInputIntervalDataList(const int pIndex, const std::string& pDataKey);

	/**
	 * get the computed gap size
	 */
	double getComputedGapSize(double gapThreshold, double minSampling);

	/*
	 * @brief get the default value of the gap threshold
	 */
	double getDefaultGapThreshold()
	{
		return _defaultGapThreshold;
	}

	/*
	 * @brief set the default value of the gap threshold
	 */
	void setDefaultGapThreshold(double defaultGapThreshold)
	{
		_defaultGapThreshold = defaultGapThreshold;
	}

	/*
	 * @brief Apply some correction in a parameter ID
	 */
	void applyParamIdCorrection(std::string& paramId);

	void addParamInParameterList(ParameterSPtr& pParam);

	ParameterSPtr findParamInParameterList(const std::string& paramId);

	void addParamTimeRestriction(const std::string& paramId, double restriction) {
		_paramsTimeRestrictions[paramId] = restriction;
	}

	std::map<std::string, double> getParamsTimeRestrictions() {
		return _paramsTimeRestrictions;
	}

	/**
	 * @return The real parameter if the process is empty.
	 */
	ParameterSPtr& checkIfIsANeededParameter(ParameterSPtr& pParamResult);

private:

	/**
	 * logger
	 */
	static log4cxx::LoggerPtr _logger;

      /**
       * Store created parameters
       */
	 ParameterList  _parameterList;

	 /**
	  * List of request paramOutput
	  */
	 ParamOutputList _paramOutputList;

	 /**
	  * @brief _timeIntervalList List of input interval contained in timetable defined in the XML request file.
	  */
	 TimeIntervalListSPtr _timeIntervalList;

	 /*
	  * @brief additional data associated to each time interval of the _timeIntervalList
	  */
	 std::map<int, std::map<std::string, std::vector<std::string>>> _timeIntervalDataList;

	/*
	 * @brief store paramId correction to compute it only once and improve execution time
	 */
	std::map<std::string,std::string> _paramIdCorrectionMap;

	/*
	  * @brief default value for the gap threshold of all parameters
	  */
	 double _defaultGapThreshold;

	/*
	 * @bried define some parameters time restrictions
	 */
	std::map<std::string, double> _paramsTimeRestrictions;
    };

  } /* namespace Parameters */
} /* namespace AMDA */
#endif /* PARAMTERMANAGER_HH_ */