TimeTable.cc
14.1 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
/*
* TimeTable.cpp
*
* Created on: 5 août 2013
* Author: CS
*/
#include "TimeTable.hh"
#include "TimeUtil.hh"
#include "TimeTableCatalogUtil.hh"
#include <sstream>
#include <algorithm>
#include <boost/algorithm/string.hpp>
#define CURL_STATICLIB
#include <stdio.h>
#include <curl/curl.h>
#include <curl/easy.h>
#include "SpaceData.hh"
namespace TimeTableCatalog {
log4cxx::LoggerPtr TimeTable::_logger(
log4cxx::Logger::getLogger("AMDA-Kernel.TimeTable"));
TimeTable::TimeTable() :
_creationDate(0), _timeFormat(TimeTable::TIME_FORMAT::UNKNOWN), _extTimeFormat(AMDA::OutputFormatTime::FORMAT_OUTPUT_TIME_ISO), _modificationDate(0),
_listStartDate(0), _listStopDate(0), _contact("None"), _contactID("None"),_eventTableVersion(SpaceData::EVENT_TABLE_VERSION_DEFAULT), _listID(SpaceData::LIST_ID_DEFAULT){
}
TimeTable::TimeTable(TIME_FORMAT pFormat) :
_creationDate(0), _timeFormat(pFormat), _extTimeFormat(AMDA::OutputFormatTime::FORMAT_OUTPUT_TIME_ISO), _modificationDate(0),
_listStartDate(0), _listStopDate(0), _contact("None"), _contactID("None"),_eventTableVersion(SpaceData::EVENT_TABLE_VERSION_DEFAULT), _listID(SpaceData::LIST_ID_DEFAULT) {
}
TimeTable::TimeTable(AMDA::OutputFormatTime pFormat) :
_creationDate(0), _timeFormat(TimeTable::TIME_FORMAT::UNKNOWN), _extTimeFormat(pFormat),_modificationDate(0),
_listStartDate(0), _listStopDate(0), _contact("None"), _contactID("None"),_eventTableVersion(SpaceData::EVENT_TABLE_VERSION_DEFAULT), _listID(SpaceData::LIST_ID_DEFAULT) {
}
TimeTable::~TimeTable() {
}
// ------------------------------- PUBLIC ------------------------------
int TimeTable::getIntervalNumber() const {
return _intervals.size();
}
std::unique_ptr<TimeTable> TimeTable::merge(
const std::vector<TimeTable>& pTimeTableList) {
std::unique_ptr<TimeTable> ptt(new TimeTable);
// --------------------- METADATA ---------------------
// -- name
std::vector<std::string> names;
for (auto tt : pTimeTableList) {
names.push_back(tt._name);
}
ptt->_name = join(names, "_u_");
// -- no historic
// -- creation date
time_t ptr;
ptt->_creationDate = time(&ptr);
// -- description
std::string description = "Union between ";
description += join(names, ", ");
ptt->_description.push_back(description);
// ------------------- INTERVALS -------------------
// add all intervals into the same TT
ptt->group(pTimeTableList);
// sort the interval according to their start date
ptt->sort();
/*for (TimeInterval interval : ptt->_intervals) {
std::cout << interval;
}*/
std::unique_ptr<std::vector<TimeInterval>> mergedIntervals = merge(
ptt->_intervals);
// fill new TT intervals with merged intervals
ptt->_intervals.clear();
ptt->_intervals.insert(ptt->_intervals.end(), mergedIntervals->begin(),
mergedIntervals->end());
return ptt;
}
std::unique_ptr<TimeTable> TimeTable::intersect(
const std::vector<TimeTable>& pTimeTableList) {
std::unique_ptr<TimeTable> ptt(new TimeTable);
// ---------------- METADATA ---------------------
// -- name
std::vector<std::string> names;
for (auto tt : pTimeTableList) {
names.push_back(tt._name);
}
ptt->_name = join(names, "_i_");
// -- no historic
// -- creation date
time_t ptr;
ptt->_creationDate = time(&ptr);
// -- description
std::string description = "Intersection between ";
description += join(names, ", ");
ptt->_description.push_back(description);
// ---------------- INTERVALS -------------------
if (pTimeTableList.empty()) {
return ptt;
}
// one interval list is empty, no intersection available
for (auto tt : pTimeTableList) {
if (tt.getIntervalNumber() == 0) {
return ptt;
}
}
// create tmp container for intersected time intervals
std::vector<TimeInterval> intersetedIntervals;
auto tmpTT = pTimeTableList[0];
for (size_t i = 1; i < pTimeTableList.size(); ++i) {
tmpTT.sort();
intersetedIntervals.clear();
intersect(tmpTT, pTimeTableList[i], intersetedIntervals);
tmpTT._intervals.clear();
tmpTT._intervals.insert(tmpTT._intervals.end(),
intersetedIntervals.begin(), intersetedIntervals.end());
}
// fill new TT intervals with merged intervals
ptt->_intervals.clear();
ptt->_intervals.insert(ptt->_intervals.end(), intersetedIntervals.begin(),
intersetedIntervals.end());
return ptt;
}
std::unique_ptr<TimeTable> TimeTable::antiintersect(
const std::vector<TimeTable>& pTimeTableList) {
std::unique_ptr<TimeTable> ptt(new TimeTable);
// ---------------- METADATA ---------------------
// -- name
std::vector<std::string> names;
for (auto tt : pTimeTableList) {
names.push_back(tt._name);
}
ptt->_name = join(names, "_ai_");
// -- no historic
// -- creation date
time_t ptr;
ptt->_creationDate = time(&ptr);
// -- description
std::string description = "Anti'intersection between ";
description += join(names, ", ");
ptt->_description.push_back(description);
// ---------------- INTERVALS -------------------
if (pTimeTableList.empty()) {
return ptt;
}
// create tmp container for anti'intersected time intervals
// do anti'intersect for each tt with all other tt in list
// Means, we have in list tt1, tt2 and tt3
// add into the resulting interval list
// all non intersection between tt1/tt2, tt1/tt3 and tt2/tt3
// then make union between intervals
std::vector<TimeInterval> antiIntersetedIntervals;
TimeTable tt1, tt2;
for (size_t i = 0; i < pTimeTableList.size(); ++i) {
tt1._intervals.clear();
tt1._intervals.insert(tt1._intervals.end(),
pTimeTableList[i]._intervals.begin(),
pTimeTableList[i]._intervals.end());
tt1.sort();
for (size_t j = i + 1; j < pTimeTableList.size(); ++j) {
tt2._intervals.clear();
tt2._intervals.insert(tt2._intervals.end(),
pTimeTableList[j]._intervals.begin(),
pTimeTableList[j]._intervals.end());
tt2.sort();
antiintersect(tt1, tt2, antiIntersetedIntervals);
}
}
sort(antiIntersetedIntervals);
// merge intervals
std::unique_ptr<std::vector<TimeInterval>> mergedIntervals = merge(
antiIntersetedIntervals);
// fill new TT intervals with merged intervals
ptt->_intervals.clear();
ptt->_intervals.insert(ptt->_intervals.end(), mergedIntervals->begin(),
mergedIntervals->end());
return ptt;
}
void TimeTable::group(const std::vector<TimeTable>& pTimeTableList) {
for (TimeTable tt : pTimeTableList) {
_intervals.insert(_intervals.end(), tt._intervals.begin(),
tt._intervals.end());
}
}
void TimeTable::sort() {
sort(_intervals);
}
void TimeTable::addInterval(const TimeInterval& pInterval) {
_intervals.push_back(pInterval);
}
/**
* Unset current TT.
*/
void TimeTable::clear() {
_intervals.clear();
_name = std::string();
_description.clear();
_creationDate = -1;
_history = std::string();
_timeFormat = TIME_FORMAT::UNKNOWN;
_extTimeFormat = AMDA::OutputFormatTime::FORMAT_OUTPUT_TIME_ISO;
}
/**
* Downloads timetable file in tmp directory.
*/
std::string TimeTable::download(const std::string& pPath) {
std::string localPath;
std::string tmpPath(pPath);
std::transform(tmpPath.begin(), tmpPath.end(), tmpPath.begin(), ::tolower);
if (!boost::starts_with(tmpPath, "http:")
&& !boost::starts_with(tmpPath, "https:")) {
return pPath;
}
// download file
CURL *pCurl;
CURLcode codes;
const char *url = pPath.c_str();
// get tt name to create temp file as tmp_<ttdistantfilename>
size_t endOfPath = pPath.find_last_of("/");
if (endOfPath == std::string::npos) {
endOfPath = pPath.find_last_of("=/\\");
}
std::string tmpFile = "./tmp_" + pPath.substr(endOfPath + 1);
// do download
pCurl = curl_easy_init();
if (pCurl) {
FILE *fptr = fopen(tmpFile.c_str(), "wb");
if (fptr) {
curl_easy_setopt(pCurl, CURLOPT_URL, url);
curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYHOST, false);
curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, fptr);
// create buffer to get potential error string
std::vector<char> errBuf(1024);
curl_easy_setopt(pCurl, CURLOPT_ERRORBUFFER, &errBuf[0]);
codes = curl_easy_perform(pCurl);
curl_easy_cleanup(pCurl);
fclose(fptr);
if (codes == CURLE_OK) {
localPath = tmpFile;
} else {
LOG4CXX_ERROR(_logger,
"Unable to download " + pPath + " : " + errBuf[0]);
}
} else {
LOG4CXX_ERROR(_logger,
"Unable to download " + pPath + " : not found.");
}
} else {
LOG4CXX_ERROR(_logger,
"Unable to download " + pPath
+ " : cUrl cannot be initialized.");
}
// else, do nothing
// return local file or empty string if not downloaded
return localPath;
}
// ------------------------------- PRIVATE ------------------------------
/**
* Fills an interval list with all intersections between the two given time tables.
*/
void TimeTable::intersect(const TimeTable& ptt1, const TimeTable& ptt2,
std::vector<TimeInterval>& pIntersetedIntervals) {
size_t indexInTT2 = 0;
size_t indexInTT1 = 0;
for (; indexInTT1 < ptt1._intervals.size(); ++indexInTT1) {
auto interval1 = ptt1._intervals[indexInTT1];
for (; indexInTT2 < ptt2._intervals.size(); ++indexInTT2) {
auto interval2 = ptt2._intervals[indexInTT2];
// suppose two intervals
// B1 E1 in tt1
// B2 E2 in tt2
// if B1 < E2 or B2 < E1, we have an intersection
// all possible intersections
// tt1 | ____ | ____ | _____ | ___
// tt2 | ____ | ___ | ___ | _____
if ((std::max(interval2._startTime, interval1._startTime)
< std::min(interval1._stopTime, interval2._stopTime))) {
// here we have an intersection
TimeInterval intersectedInterval(
std::max(interval1._startTime, interval2._startTime),
std::min(interval1._stopTime, interval2._stopTime));
pIntersetedIntervals.push_back(intersectedInterval);
// keep interval1 if larger to find potential other intersection
if (interval1._stopTime > interval2._stopTime) {
--indexInTT1;
++indexInTT2;
}
break;
} else if (interval1._stopTime < interval2._startTime) {
// interval1 is before interval2
// go on to next tt1 interval
break;
}
// otherwise, continue to loop on tt2
}
}
}
/**
* Fills an interval list with all non intersections between the two given time tables.
*/
void TimeTable::antiintersect(const TimeTable& ptt1, const TimeTable& ptt2,
std::vector<TimeInterval>& pAntiIntersectedIntervals) {
size_t indexInTT1 = 0;
size_t indexInSecondTT = 0;
for (; indexInTT1 < ptt1._intervals.size(); ++indexInTT1) {
auto interval1 = ptt1._intervals[indexInTT1];
// no intervals remaining in tt2, break the loop and
// just save the rest of tt1
if (indexInSecondTT == ptt2._intervals.size()) {
break;
}
for (; indexInSecondTT < ptt2._intervals.size(); ++indexInSecondTT) {
auto interval2 = ptt2._intervals[indexInSecondTT];
if ((std::max(interval2._startTime, interval1._startTime)
<= std::min(interval1._stopTime, interval2._stopTime))) {
// here we have an intersection
TimeInterval intersectedInterval1(
std::min(interval1._startTime, interval2._startTime),
std::max(interval1._startTime, interval2._startTime));
pAntiIntersectedIntervals.push_back(intersectedInterval1);
TimeInterval intersectedInterval2(
std::min(interval1._stopTime, interval2._stopTime),
std::max(interval1._stopTime, interval2._stopTime));
pAntiIntersectedIntervals.push_back(intersectedInterval2);
indexInSecondTT++;
break;
} else {
// no intersection between the two intervals,
// save the first interval and
// search for a possible intersection with the second one
if (interval1._startTime <= interval2._startTime) {
pAntiIntersectedIntervals.push_back(interval1);
// .. go to next interval in tt1
break;
} else {
pAntiIntersectedIntervals.push_back(interval2);
// .. back to current tt1 interval
indexInTT1--;
}
}
}
}
// here, all intervals of tt1 or tt2 have been handled
// save the rest of tt1
if (indexInTT1 < ptt1._intervals.size()) {
for (; indexInTT1 < ptt1._intervals.size(); ++indexInTT1) {
auto interval1 = ptt1._intervals[indexInTT1];
pAntiIntersectedIntervals.push_back(interval1);
}
} else {
// or save the rest of tt2
for (; indexInSecondTT < ptt2._intervals.size(); ++indexInSecondTT) {
auto interval2 = ptt2._intervals[indexInSecondTT];
pAntiIntersectedIntervals.push_back(interval2);
}
}
}
/**
* Returns a merged interval list.
*/
std::unique_ptr<std::vector<TimeInterval>> TimeTable::merge(
const std::vector<TimeInterval>& pTimeIntervalsList) {
// create tmp container for merged time intervals
std::unique_ptr<std::vector<TimeInterval>> mergedIntervals(
new std::vector<TimeInterval>);
if (!pTimeIntervalsList.empty()) {
auto currentInterval = pTimeIntervalsList.front();
for (auto interval : pTimeIntervalsList) {
// merge current interval with previous one if start date
// is inside previous interval
if (interval._startTime <= currentInterval._stopTime) {
currentInterval._stopTime = std::max(interval._stopTime,
currentInterval._stopTime);
} else {
// intervals are merged, next interval is outside
// add interval to TT
mergedIntervals->push_back(currentInterval);
currentInterval = interval;
}
}
// write last interval
mergedIntervals->push_back(currentInterval);
}
return mergedIntervals;
}
void TimeTable::sort(std::vector<TimeInterval>& pIntervals) {
std::sort(pIntervals.begin(), pIntervals.end());
}
// ------------------------------- EXTERN ------------------------------
std::string join(const std::vector<std::string>& pvalues,
const std::string& pseparator) {
std::string res;
for (size_t i = 0; i < pvalues.size(); ++i) {
res += pvalues[i];
if (i < pvalues.size() - 1) {
res += pseparator;
}
}
return res;
}
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written;
written = fwrite(ptr, size, nmemb, stream);
return written;
}
std::ostream& operator <<(std::ostream& os, const TimeInterval& interval) {
writeISOTime(interval._startTime,
TimeTable::TIME_FORMAT::YYYYMMDDThhmmssmsk, os);
os << " ";
writeISOTime(interval._stopTime, TimeTable::TIME_FORMAT::YYYYMMDDThhmmssmsk,
os);
os << std::endl;
return os;
}
} /* namespace TimeTableCatalog */