ParamOutputDataMining.cc 16.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
/*
 * ParamOutputDataMining.cc
 *
 *  Created on: 23 août 2013
 *      Author: CS
 */

#include <string>
#include <iostream>
#include <sstream>

#include <iomanip>
#include <string.h>
#include <time.h>
#include <map>
#include <set>
#include <limits>

#include "ParamOutputDataMining.hh"

#include "DicError.hh"

// Parameters
#include "ParameterManager.hh"
#include "Parameter.hh"
#include "ParamData.hh"
#include "TimeUtil.hh"
#include "Helper.hh"
#include "Process.hh"

#include "Config.hh"
#include "DD_time.hh"

// TimeTable
#include "AbstractWriter.hh"
#include "AsciiWriter.hh"
#include "InternalXMLWriter.hh"
#include "VOTableWriter.hh"
#include "TimeTableCatalogUtil.hh"

// DD_Client_r_Lib
#include "TimeUtil.hh"

#include "ParamMgr.hh"

using namespace AMDA::Parameters;
using namespace TimeTableCatalog;

namespace AMDA {
	namespace ParamOutputImpl {
		namespace DataMining {


			const std::map<std::string, TimeTable::TIME_FORMAT> ParamOutputDataMining::TIME_FORMAT_MAP = {
				{"", TimeTable::TIME_FORMAT::UNKNOWN},
				{"ISO", TimeTable::TIME_FORMAT::YYYYMMDDThhmmss}
			};

			const std::map<std::string, std::string> ParamOutputDataMining::FILE_FORMAT_MAP = {
				{"ASCII", AsciiWriter::FORMAT},
				{"XML", InternalXMLWriter::FORMAT},
				{"VOT", VOTableWriter::FORMAT}
			};

			static const std::string ONE_FILE = "one-file";
			static const std::string ONE_FILE_PER_INTERVAL = "one-file-per-interval";

			ParamOutputDataMining::ParamOutputDataMining(ParameterManager& pParameterManager) :
					ParamOutput(pParameterManager),
					_fileFormat(FILE_FORMAT_MAP.find("ASCII")->second),
					_timeFormat(TIME_FORMAT_MAP.find("ISO")->second),
					_outputStructure(ONE_FILE),
					_fileName("timeTable_PARAMID_XXX"),
					_firstVisit(true),
					_taskPriority(PRIORITY::PARAM) {

			}

			ParamOutputDataMining::~ParamOutputDataMining() {
			}

			void ParamOutputDataMining::establishConnection() {
				// TODO check if _paramName is set otherwise throw an error
				LOG4CXX_DEBUG(_logger, "ParamOutputDataMining::establishConnection : " << "Retrieve parameter");
				_parameter = _parameterManager.getSampledParameter(_paramName, _samplingMode, _samplingValue, _gapThreshold, true);
				if(_parameter == NULL)  {
					LOG4CXX_ERROR(_logger,"ParamOutput::init parameter : \""<< _paramName <<"\" Not Exist" );
					BOOST_THROW_EXCEPTION( ParamOutput_exception());
				} else {
					_parameter->openConnection(this);
					_samplingValue = getSamplingInTreeParameter(_parameter);
					_gapThreshold = _parameter->getGapThreshold();
				}
			}

			void ParamOutputDataMining::init() {
				if(_timeIntervalList->size() != 0) {
					getParameter()->init(this, _timeIntervalList);
				} else {
					// No need to initialize parameter when there is no time interval to process.
				}

				AMDA::Info::ParamInfoSPtr paramInfo = AMDA::Info::ParamMgr::getInstance()->getParamInfoFromId(_parameter->getInfoId());
				if (paramInfo != nullptr) {
					_expression = paramInfo->getProcessDescription();
				}
				if (_expression.empty()) {
					_expression = "Unknown";
				}
			}

			/**
			 * @note Create instance of DataMiningType
			 */
			void ParamOutputDataMining::createDataMiningWriter() {
				// DataMiningTooSmall.
				DataMiningInvalidInterval lDataMiningTooSmall = DataMiningTooSmall(this);
				lDataMiningTooSmall.setWorkPath(_workPath);
				// Set the biggest time interval because there is no need to parse each time interval.
				if (!_timeIntervalListTooSmall->empty())
					lDataMiningTooSmall.setTimeInterval(*_currentTimeInterval);

				// Calculated digit number must be greater than minimum digit number constant.
				std::stringstream buf;
				buf << _timeIntervalListTooSmall->size();
				unsigned int lDigitNumber = buf.str().size();
				if(lDigitNumber < DataMiningType::MIN_DIGIT_NUMBER) {
					lDigitNumber = DataMiningType::MIN_DIGIT_NUMBER;
				}

				// DataMiningTrue
				DataMiningValidInterval lDataMiningTrue = DataMiningTrue(this);
				lDataMiningTrue.setWorkPath(_workPath);
				lDataMiningTrue.setDigitNumber(lDigitNumber);

				// DataMiningNoData
				DataMiningValidInterval lDataMiningNoData = DataMiningNoData(this);
				lDataMiningNoData.setWorkPath(_workPath);
				lDataMiningNoData.setDigitNumber(lDigitNumber);

				// Define first time interval.
//				if(_outputStructure == ONE_FILE) {
					// We must give start time of the first time interval and end time of the last time interval
//					lDataMiningTrue.setTimeInterval(TimeInterval(_timeIntervalList->front()._startTime, _timeIntervalList->back()._stopTime));
//					lDataMiningNoData.setTimeInterval(TimeInterval(_timeIntervalList->front()._startTime, _timeIntervalList->back()._stopTime));
//				} else {
					lDataMiningTrue.setTimeInterval(*_currentTimeInterval);
					lDataMiningNoData.setTimeInterval(*_currentTimeInterval);

//				}

				// Push in valid interval list
				_dataMiningValidIntervalList.push_back(lDataMiningTrue);
				_dataMiningValidIntervalList.push_back(lDataMiningNoData);

				// Push in invalid inteval list
				_dataMiningInvalidIntervalList.push_back(lDataMiningTooSmall);
			}

			/**
			 * @note Process data where time interval list is valid (greater or equal than sampling time).
			 */
			void ParamOutputDataMining::processInvalidInterval() {
				for (DataMiningInvalidIntervalList::iterator it = _dataMiningInvalidIntervalList.begin(); it != _dataMiningInvalidIntervalList.end(); ++it) {
					// Store invalid time interval in time table.
					(*it).createInterval(_timeIntervalListTooSmall.get());

					// Write information in time table.
					(*it).fillInformation(_timeFormat, _fileName, 0);
				}
			}

			/**
			 * @note Process data where time interval list is invalid (less than sampling time).
			 */
			void ParamOutputDataMining::processValidInterval() {
				// Index of created file
				int lFileIndex = 0;

				do {
					try {
						_paramDataIndexInfo = getParameter()->getAsync(this).get();
						getParameter()->getParamData(this)->accept(*this);

						if (_paramDataIndexInfo._timeIntToProcessChanged) {

							// Set next time interval.
							LOG4CXX_DEBUG(_logger, "ParamOutputDataMining => Next Time Interval");
							++_currentTimeInterval;

							if (_currentTimeInterval == _timeIntervalList->end()) {
								_paramDataIndexInfo._timeIntToProcessChanged = false;
								continue;
							}

							// Increase index for the filename.
							if(_outputStructure == ONE_FILE_PER_INTERVAL) {
								lFileIndex++;
							}

							// For each DataMiningValidInterval write time table content and change time interval.
							for(DataMiningValidIntervalList::iterator it = _dataMiningValidIntervalList.begin(); it != _dataMiningValidIntervalList.end(); ++it) {
								if(_outputStructure == ONE_FILE_PER_INTERVAL) {
									(*it).fillInformation(_timeFormat, _fileName, lFileIndex);
								}

								// Change time interval
								(*it).setTimeInterval(*_currentTimeInterval);
							}

						}
					} catch(AMDA::AMDA_exception & e) {
						e << AMDA::errno_code(AMDA_PARAM_OUTPUT_ERR);
						throw;
					}
				} while (!_paramDataIndexInfo._noMoreTimeInt);

				// Increase index for the filename.
				if(_outputStructure == ONE_FILE_PER_INTERVAL) {
					lFileIndex++;
				}

				// For each DataMiningValidInterval fill information of time table (description, history, filename, ...).
				for(DataMiningValidIntervalList::iterator it = _dataMiningValidIntervalList.begin(); it != _dataMiningValidIntervalList.end(); ++it) {
					(*it).fillInformation(_timeFormat, _fileName, lFileIndex);
				}
			}

			void ParamOutputDataMining::apply() {
				// Check if all needed parameter are set

				// If sampling value is set to 0 that's to say
				// time_resolution tag was not set in param xml file.
				if(_samplingValue == 0) {
					// Retrieve default sampling time for this param
					if(_parameter->getTimeResolution() == 0) {
						_samplingValue = _parameter->getParamData(this)->getMinSampling();
					} else {
						_samplingValue = _parameter->getTimeResolution();
					}
				}

				// Check if intervals creation must be canceled.
				if (!cancelIntervalCreation()) {
					// Create DataMiningTrue, DataMiningNoData, DataMiningTooSmall
					createDataMiningWriter();

					// Process invalid interval
					processInvalidInterval();

					// Process valid interval
					if(_timeIntervalList->size() != 0) {
						processValidInterval();
					} else {
						// Nothing to do.
					}

				} else {
					LOG4CXX_DEBUG(_logger, "ParamOutputDataMining => The output treatment with priority '" << _taskPriority << "' for param name '" << _paramName << "' is aborted because there is an other treatment with an higher priority");
					_parameter->closeConnection(this);
				}
			}

			bool ParamOutputDataMining::cancelIntervalCreation() {
				// The output is aborted only if there is an other output on the same param
				// that have an higher priority
				bool lAbortTreatment = false;

				// If this output has the highest priority don't check other output
				if(_taskPriority == (PRIORITY::HIGHER_PRIORITY_VALUE - 1)) {
					// Nothing to do
				} else {
					// Search for other output that are of type ParamOutputDataMining which work on the same param.
					// If the found output has an higher priority than the current output, the current output task is aborted.

					ParamOutputList lParamOutputList = _parameterManager.getParamOutputList();
					for(std::list<ParamOutputSPtr>::iterator it=lParamOutputList.begin(); it != lParamOutputList.end(); ++it) {
						if (typeid((*(it->get()))) == typeid(ParamOutputDataMining)) {
							ParamOutputDataMining* lOtherOutput =  dynamic_cast<ParamOutputDataMining*>(it->get());

							// Check if the other output work on the same param and has an higher priority
							if(lOtherOutput->getParamName() == _paramName && lOtherOutput->getOutputPriority() > _taskPriority) {
								lAbortTreatment = true;
							}
						}
					}
				}

				return lAbortTreatment;
			}

			void ParamOutputDataMining::terminate() {
				std::vector<std::string> lFilenames;

				// For each DataMiningValidInterval write time table content.
				for(DataMiningValidIntervalList::iterator it = _dataMiningValidIntervalList.begin(); it != _dataMiningValidIntervalList.end(); ++it) {
					lFilenames = (*it).write(_fileFormat);
					_files.insert(_files.end(), lFilenames.begin(), lFilenames.end());
				}

				for(DataMiningInvalidIntervalList::iterator it = _dataMiningInvalidIntervalList.begin(); it != _dataMiningInvalidIntervalList.end(); ++it) {
					lFilenames = (*it).write(_fileFormat);
					_files.insert(_files.end(), lFilenames.begin(), lFilenames.end());
				}

				// Call post processing
				ParamOutput::terminate();
			}

			/**
			 * @overload VisitorOfParamData::visit(ParamDataScalaireShort *)
			 */
			void ParamOutputDataMining::visit(AMDA::Parameters::ParamDataScalaireShort * /*pParamData*/) {
				ERROR_EXCEPTION("Not Implemented!");
			}

			/**
			 * @overload VisitorOfParamData::visit(ParamDataScalaireFloat *)
			 */
			void ParamOutputDataMining::visit(AMDA::Parameters::ParamDataScalaireFloat * /*pParamData*/) {
				ERROR_EXCEPTION("Not Implemented!");
			}

			/**
			 * @overload VisitorOfParamData::visit(ParamDataScalaireDouble *)
			 */
			void ParamOutputDataMining::visit(AMDA::Parameters::ParamDataScalaireDouble * /*pParamData*/) {
				ERROR_EXCEPTION("Not Implemented!");
			}

			/**
			 * @overload VisitorOfParamData::visit(ParamDataScalaireLongDouble *)
			 */
			void ParamOutputDataMining::visit(AMDA::Parameters::ParamDataScalaireLongDouble * /*pParamData*/) {
				ERROR_EXCEPTION("Not Implemented!");
			}

			/**
			 * @overload VisitorOfParamData::visit(ParamDataScalaireInt *)
			 */
			void ParamOutputDataMining::visit(AMDA::Parameters::ParamDataScalaireInt * pParamData) {
				LOG4CXX_DEBUG(_logger, "ParamOutputDataMining => create output");
				for(DataMiningValidIntervalList::iterator it = _dataMiningValidIntervalList.begin(); it != _dataMiningValidIntervalList.end(); ++it) {
					(*it).createInterval<AMDA::Parameters::ParamDataScalaireInt>(pParamData, _paramDataIndexInfo);
				}
			}

			/**
			 * @overload VisitorOfParamData::visit(ParamDataLogicalData *)
			 */
			void ParamOutputDataMining::visit(AMDA::Parameters::ParamDataLogicalData * pParamData) {
				LOG4CXX_DEBUG(_logger, "ParamOutputDataMining => create output");
				for(DataMiningValidIntervalList::iterator it = _dataMiningValidIntervalList.begin(); it != _dataMiningValidIntervalList.end(); ++it) {
					(*it).createInterval<AMDA::Parameters::ParamDataLogicalData>(pParamData, _paramDataIndexInfo);
				}
			}

			/**
			 * @overload VisitorOfParamData::visit(ParamDataTab1DShort *)
			 */
			void ParamOutputDataMining::visit(AMDA::Parameters::ParamDataTab1DShort * /*pParamData*/) {
				ERROR_EXCEPTION("Not Implemented!");
			}

			/**
			 * @overload VisitorOfParamData::visit(ParamDataTab1DFloat *)
			 */
			void ParamOutputDataMining::visit(AMDA::Parameters::ParamDataTab1DFloat * /*pParamData*/) {
				ERROR_EXCEPTION("Not Implemented!");
			}

			/**
			 * @overload VisitorOfParamData::visit(ParamDataTab1DDouble *)
			 */
			void ParamOutputDataMining::visit(AMDA::Parameters::ParamDataTab1DDouble * /*pParamData*/) {
				ERROR_EXCEPTION("Not Implemented!");
			}

			/**
			 * @overload VisitorOfParamData::visit(ParamDataTab1DLongDouble *)
			 */
			void ParamOutputDataMining::visit(AMDA::Parameters::ParamDataTab1DLongDouble * /*pParamData*/) {
				ERROR_EXCEPTION("Not Implemented!");
			}

			/**
			 * @overload VisitorOfParamData::visit(ParamDataTab1DInt *)
			 */
			void ParamOutputDataMining::visit(AMDA::Parameters::ParamDataTab1DInt * /*pParamData*/) {
				ERROR_EXCEPTION("Not Implemented!");
			}

			/**
			 * @overload VisitorOfParamData::visit(ParamDataTab1DLogicalData *)
			 */
			void ParamOutputDataMining::visit(AMDA::Parameters::ParamDataTab1DLogicalData * /*pParamData*/) {
				ERROR_EXCEPTION("Not Implemented!");
			}

			/**
			 * @overload VisitorOfParamData::visit(ParamDataTab2DShort *)
			 */
			void ParamOutputDataMining::visit(AMDA::Parameters::ParamDataTab2DShort *) {
				ERROR_EXCEPTION("Not Implemented!");
			}

			/**
			 * @overload VisitorOfParamData::visit(ParamDataTab2DFloat *)
			 */
			void ParamOutputDataMining::visit(AMDA::Parameters::ParamDataTab2DFloat *) {
				ERROR_EXCEPTION("Not Implemented!");
			}

			/**
			 * @overload VisitorOfParamData::visit(ParamDataTab2DDouble *)
			 */
			void ParamOutputDataMining::visit(AMDA::Parameters::ParamDataTab2DDouble *) {
				ERROR_EXCEPTION("Not Implemented!");
			}

			/**
			 * @overload VisitorOfParamData::visit(ParamDataTab2DLongDouble *)
			 */
			void ParamOutputDataMining::visit(AMDA::Parameters::ParamDataTab2DLongDouble *) {
				ERROR_EXCEPTION("Not Implemented!");
			}

			/**
			 * @overload VisitorOfParamData::visit(ParamDataTab2DInt *)
			 */
			void ParamOutputDataMining::visit(AMDA::Parameters::ParamDataTab2DInt *) {
				ERROR_EXCEPTION("Not Implemented!");
			}

			/**
			 * @overload VisitorOfParamData::visit(ParamDataTab2DLogicalData *)
			 */
			void ParamOutputDataMining::visit(AMDA::Parameters::ParamDataTab2DLogicalData *) {
				ERROR_EXCEPTION("Not Implemented!");
			}

			// Access method

			/**
			 * @brief Set the file format value. This function do a mapping between value in the output request XML file
			 * and Time Table writer format.
			 */
			void ParamOutputDataMining::setFileFormat(const std::string& pFileFormat) {
				std::map<std::string, std::string>::const_iterator it;
				it = FILE_FORMAT_MAP.find(pFileFormat);

				std::string lFileFormatMapValue;
				if(it != FILE_FORMAT_MAP.end()) {
					lFileFormatMapValue = it->second;
				} else {
					lFileFormatMapValue = FILE_FORMAT_MAP.find("ASCII")->second;
				}


				_fileFormat = lFileFormatMapValue;
			}

			/**
			 * @brief Set the time format value. This function do a mapping between value in the output request XML file
			 * and Time Table time format.
			 */
			void ParamOutputDataMining::setTimeFormat(std::string pTimeFormat) {
				std::map<std::string, TimeTable::TIME_FORMAT>::const_iterator it;
				it = TIME_FORMAT_MAP.find(pTimeFormat);

				TimeTable::TIME_FORMAT lTimeFormatMapValue;
				if(it != TIME_FORMAT_MAP.end()) {
					lTimeFormatMapValue = it->second;
				} else {
					lTimeFormatMapValue = TIME_FORMAT_MAP.find("")->second;
				}

				_timeFormat = lTimeFormatMapValue;
			}

		} // namespace DataMining
	} // namespace ParamOutputImpl
} /* namespace AMDA */