DataMiningValidInterval.hh 3.37 KB
/*
 * DataMiningValidInterval.hh
 *
 *  Created on: 3 oct. 2013
 *      Author: guillaume
 */

#ifndef DATAMININGVALIDINTERVAL_HH_
#define DATAMININGVALIDINTERVAL_HH_

#include "DataMiningType.hh"

#include <utility>
#include <vector>
#include <memory>

#include "TimeInterval.hh"
#include "AbstractWriter.hh"


using namespace AMDA::Parameters;

namespace AMDA {
	namespace ParamOutputImpl {
		namespace DataMining {

			class ParamOutputDataMining;

			typedef boost::shared_ptr<TimeTable> TimeTableSPtr;

			class DataMiningValidInterval : public DataMiningType {

			public:
				DataMiningValidInterval(std::string pPrefixTTFileName, bool pIsGapFile, ParamOutputDataMining* pParamOutputDataMining);

				/**
				 * @brief Change time interval on which to work.
				 * @note It also reset static data to begin with proper value and not with the old time interval.
				 */
				void setTimeInterval(const TimeInterval& pTimeInterval);

				/**
				 * @brief Create interval in which value corresponds to the given value in constructor parameter.
				 */
				template <typename TParamData>
				void createInterval(TParamData* pParamData, ParamDataIndexInfo pParamDataIndexInfo) {
					for (unsigned int index = pParamDataIndexInfo._startIndex; index < pParamDataIndexInfo._nbDataToProcess + pParamDataIndexInfo._startIndex; ++index) {
						double lTime = pParamData->getTime(index);
						typename TParamData::ElementType lData = pParamData->get(index);
						typename TParamData::ElementType nullElem;
						nullElem << ElemNull();

						bool isValid = _isGapFile ? isNAN(lData) : (lData != nullElem) && !isNAN(lData);

						// Found beginning of a time interval
						if(!_valCurrent && isValid) {
							_valCurrent = true;
							_beginTimeInterval = lTime;
							_endTimeInterval = lTime;
						}
						// Increase the end of the time interval
						else if(_valCurrent && isValid) {
							_endTimeInterval = lTime;
						}
						// Found ending of the time interval
						else if(_valCurrent && !isValid) {
							_valCurrent = false;

							// Check time interval bounds.
							checkTimeIntervalBounds();

							_timeTable->addInterval(TimeInterval(_beginTimeInterval, _endTimeInterval));
						}
					}
				}


				/**
				 * @brief Fill information (history, description, filename, ...).
				 */
				void fillInformation(TimeTable::TIME_FORMAT pTimeFormat, std::string pFileName, int pIndex = 0);

			protected:
				/**
				 * @brief _valCurrent read, true if current value is True (see LogicalData)
				*/
				bool _valCurrent;

				/**
				 * @brief _beginTimeInterval identify the beginning value of an interval
				 */
				double _beginTimeInterval;

				/**
				 * @brief _beginTimeInterval identify the ending value of an interval
				 */
				double _endTimeInterval;

				static log4cxx::LoggerPtr _logger;

			private:
				bool _isGapFile;

				void checkTimeIntervalBounds();

				void finalize();
			};

			struct DataMiningTrue : public DataMiningValidInterval {
				DataMiningTrue (ParamOutputDataMining* pParamOutputDataMining) :
					DataMiningValidInterval("", false,  pParamOutputDataMining) {
				}
			};

			struct DataMiningNoData : public DataMiningValidInterval {
				DataMiningNoData (ParamOutputDataMining* pParamOutputDataMining) :
					DataMiningValidInterval("Gaps_", true,  pParamOutputDataMining) {
				}
			};

		} // DataMining
	} // ParamOutputImpl
} // AMDA
#endif /* DATAMININGVALIDINTERVAL_HH_ */