Jpp 20.0.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JHistory.hh
Go to the documentation of this file.
1#ifndef __JRECONSTRUCTION__JHISTORY__
2#define __JRECONSTRUCTION__JHISTORY__
3
4#include <istream>
5#include <ostream>
6#include <iomanip>
7#include <vector>
8#include <algorithm>
9#include <random>
10#include <limits>
11
12#include <TObject.h>
13
15
16#include "JLang/Jpp.hh"
17#include "JLang/JUUID.hh"
18#include "JLang/JPredicate.hh"
19#include "JLang/JEquals.hh"
21
22
23/**
24 * \author mdejong
25 */
26
27namespace JRECONSTRUCTION {}
28namespace JPP { using namespace JRECONSTRUCTION; }
29
30namespace JFIT {
31
32 using JLANG::JEquals;
33 using JLANG::JUUID;
34
35 /**
36 * Auxiliary class for historical event.
37 */
38 struct JEvent :
39 public JEquals<JEvent>
40 {
41 /**
42 * Default constructor.
43 */
45 type(-1),
46 uuid()
47 {}
48
49
50 /**
51 * Constructor.
52 *
53 * \param type application type
54 */
55 JEvent(const int type)
56 {
57 using namespace std;
58 using namespace JPP;
59
60 this->type = type;
61 this->git = getGITVersion();
62 this->date = getDateAndTime();
63
64 // UUID
65
66 random_device device;
67
68 uniform_int_distribution<uint32_t> generator(1, numeric_limits<uint32_t>::max());
69
70 uint32_t* value = reinterpret_cast<uint32_t*>(&(this->uuid.uuid));
71
72 for (int i = 0; i != 3; ++i) {
73 value[i] = generator(device);
74 }
75 }
76
77
78 /**
79 * Virtual destructor.
80 */
81 virtual ~JEvent()
82 {}
83
84
85 /**
86 * Check equality.
87 *
88 * \param event event
89 * \return true if events are equal; else false
90 */
91 bool equals(const JEvent& event) const
92 {
93 return (this->type == event.type &&
94 this->uuid == event.uuid &&
95 this->git == event.git &&
96 this->date == event.date);
97 }
98
99
100 /**
101 * Make this event unique.
102 *
103 * \return event
104 */
106 {
107 uint32_t* value = reinterpret_cast<uint32_t*>(&(this->uuid.uuid));
108
109 value[3] = ++counter;
110
111 return *this;
112 }
113
114
115 /**
116 * Write event to output stream.
117 *
118 * \param out output stream
119 * \param event event
120 * \return output stream
121 */
122 friend std::ostream& operator<<(std::ostream& out, const JEvent& event)
123 {
124 using namespace std;
125
126 out << setw(3) << right << event.type << ' '
127 << setw(36) << left << event.uuid << ' '
128 << setw(20) << left << event.git << ' '
129 << setw(20) << left << event.date << right;
130
131 return out;
132 }
133
135
136 int type; ///< application type
137 JUUID uuid; ///< UUID
138 std::string git; ///< GIT revision
139 std::string date; ///< date
140
141 protected:
142 uint32_t counter = 0;//!< internal counter
143 };
144
145
146 /**
147 * Container for historical events.
148 */
149 struct JHistory :
150 public std::vector<JEvent>
151 {
152 /**
153 * Auxiliary class to test history.
154 */
155 struct is_event :
156 public std::vector<int>
157 {
158 /**
159 * Constructor.
160 *
161 * \param history history
162 */
163 is_event(const JHistory& history)
164 {
165 for (JHistory::const_iterator i = history.begin(); i != history.end(); ++i) {
166 this->push_back(i->type);
167 }
168 }
169
170
171 /**
172 * Test history.
173 *
174 * \param history history
175 * \return true if given history same as this history; else false
176 */
177 bool operator()(const JHistory& history) const
178 {
179 return history.getStatus(*this);
180 }
181 };
182
183
184 /**
185 * Auxiliary class to test history.
186 */
188 {
189 /**
190 * Constructor.
191 *
192 * \param type application type
193 */
195 type(type)
196 {}
197
198
199 /**
200 * Test history.
201 *
202 * \param history history
203 * \return true if given history ends with this application type; else false
204 */
205 bool operator()(const JHistory& history) const
206 {
207 return !history.empty() && history.rbegin()->type == this->type;
208 }
209
210 int type;
211 };
212
213
214 /**
215 * Auxiliary class to test history.
216 */
218 /**
219 * Constructor.
220 *
221 * \param type application type
222 */
224 {
225 this->type = type;
226 }
227
228
229 /**
230 * Test history.
231 *
232 * \param history history
233 * \return true if given history does not contain specified event type; else false
234 */
235 bool operator()(const JHistory& history) const
236 {
237 using namespace std;
238 using namespace JPP;
239
240 return count_if(history.begin(), history.end(), make_predicate(&JEvent::type, this->type)) == 0;
241 }
242
243 int type;
244 };
245
246
247 /**
248 * Default constructor.
249 */
251 std::vector<JEvent>()
252 {}
253
254
255 /**
256 * Constructor.
257 *
258 * \param event event
259 */
260 JHistory(const JEvent& event) :
261 std::vector<JEvent>(1, event)
262 {}
263
264
265 /**
266 * Constructor.
267 *
268 * \param type application type
269 */
270 JHistory(const int type) :
271 std::vector<JEvent>(1, JEvent(type))
272 {}
273
274
275 /**
276 * Constructor.
277 *
278 * \param history history
279 * \param event event
280 */
281 JHistory(const JHistory& history,
282 const JEvent& event) :
283 std::vector<JEvent>(history)
284 {
285 push_back(event);
286 }
287
288
289 /**
290 * Virtual destructor.
291 */
292 virtual ~JHistory()
293 {}
294
295
296 /**
297 * Get history.
298 *
299 * \return histtory
300 */
301 const JHistory& getHistory() const
302 {
303 return static_cast<const JHistory&>(*this);
304 }
305
306
307 /**
308 * Get history.
309 *
310 * \return histtory
311 */
313 {
314 return static_cast<JHistory&>(*this);
315 }
316
317
318 /**
319 * Has event in history.
320 *
321 * \param type application type
322 * \return true if given event in history; else false
323 */
324 bool has(const int type) const
325 {
326 for (const_iterator i = this->begin(); i != this->end(); ++i) {
327 if (i->type == type) {
328 return true;
329 }
330 }
331
332 return false;
333 }
334
335
336 /**
337 * Check if given history is part of this history.
338 *
339 * \param history history
340 * \return true if history matches; else false
341 */
342 bool match(const JHistory& history) const
343 {
344 if (!history.empty()) {
345
346 const_iterator i = history.begin();
347 const_iterator p = this->begin();
348
349 while (p != this->end() && *p != *i) {
350 ++p;
351 }
352
353 while (i != history.end() && p != this->end() && *i == *p) {
354 ++i;
355 ++p;
356 }
357
358 return (i == history.end());
359 }
360
361 return false;
362 }
363
364
365 /**
366 * Get status.
367 *
368 * \param types application types
369 * \return true if history matches appplication types; else false
370 */
371 bool getStatus(const std::vector<int>& types) const
372 {
373 if (this->size() == types.size()) {
374
375 for (size_t i = 0; i != this->size(); ++i) {
376 if ((*this)[i].type != types[i]) {
377 return false;
378 }
379 }
380
381 return true;
382
383 } else {
384
385 return false;
386 }
387 }
388
389
390 /**
391 * Add event to history.
392 *
393 * \param type application type
394 * \return this history
395 */
396 JHistory& add(const int type)
397 {
398 push_back(JEvent(type));
399
400 return *this;
401 }
402
403
404 /**
405 * Has parent UUID.
406 */
407 bool hasParentUUID() const
408 {
409 return this->size() >= 2u;
410 }
411
412
413 /**
414 * Get UUID.
415 */
416 const JUUID& getUUID() const
417 {
418 return this->at(this->size() - 1).uuid;
419 }
420
421
422 /**
423 * Get parent UUID.
424 */
425 const JUUID& getParentUUID() const
426 {
427 return this->at(this->size() - 2).uuid;
428 }
429
430
431 /**
432 * Write history to output stream.
433 *
434 * \param out output stream
435 * \param history history
436 * \return output stream
437 */
438 friend std::ostream& operator<<(std::ostream& out, const JHistory& history)
439 {
440 using namespace std;
441
442 for (const_iterator i = history.begin(); i != history.end(); ++i) {
443 out << *i << endl;
444 }
445
446 return out;
447 }
448
450 };
451}
452
453namespace JRECONSTRUCTION {
456}
457
458#endif
Date and time functions.
Jpp environment information.
Auxiliary classes and methods for linear and iterative data regression.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
JFIT::JHistory JHistory
Definition JHistory.hh:455
JFIT::JEvent JEvent
Definition JHistory.hh:454
Description of Monte Carlo event generation applications.
Definition JHead.hh:469
Auxiliary class for historical event.
Definition JHistory.hh:40
virtual ~JEvent()
Virtual destructor.
Definition JHistory.hh:81
JEvent(const int type)
Constructor.
Definition JHistory.hh:55
uint32_t counter
internal counter
Definition JHistory.hh:142
int type
application type
Definition JHistory.hh:136
JEvent()
Default constructor.
Definition JHistory.hh:44
ClassDef(JEvent, 2)
friend std::ostream & operator<<(std::ostream &out, const JEvent &event)
Write event to output stream.
Definition JHistory.hh:122
JUUID uuid
UUID.
Definition JHistory.hh:137
std::string date
date
Definition JHistory.hh:139
const JEvent & operator()()
Make this event unique.
Definition JHistory.hh:105
bool equals(const JEvent &event) const
Check equality.
Definition JHistory.hh:91
std::string git
GIT revision.
Definition JHistory.hh:138
Auxiliary class to test history.
Definition JHistory.hh:188
is_application(int type)
Constructor.
Definition JHistory.hh:194
bool operator()(const JHistory &history) const
Test history.
Definition JHistory.hh:205
Auxiliary class to test history.
Definition JHistory.hh:157
bool operator()(const JHistory &history) const
Test history.
Definition JHistory.hh:177
is_event(const JHistory &history)
Constructor.
Definition JHistory.hh:163
Auxiliary class to test history.
Definition JHistory.hh:217
bool operator()(const JHistory &history) const
Test history.
Definition JHistory.hh:235
is_not_event(int type)
Constructor.
Definition JHistory.hh:223
Container for historical events.
Definition JHistory.hh:151
JHistory(const JEvent &event)
Constructor.
Definition JHistory.hh:260
const JUUID & getUUID() const
Get UUID.
Definition JHistory.hh:416
bool getStatus(const std::vector< int > &types) const
Get status.
Definition JHistory.hh:371
bool hasParentUUID() const
Has parent UUID.
Definition JHistory.hh:407
JHistory()
Default constructor.
Definition JHistory.hh:250
const JUUID & getParentUUID() const
Get parent UUID.
Definition JHistory.hh:425
const JHistory & getHistory() const
Get history.
Definition JHistory.hh:301
ClassDef(JHistory, 2)
JHistory & getHistory()
Get history.
Definition JHistory.hh:312
JHistory(const int type)
Constructor.
Definition JHistory.hh:270
JHistory(const JHistory &history, const JEvent &event)
Constructor.
Definition JHistory.hh:281
virtual ~JHistory()
Virtual destructor.
Definition JHistory.hh:292
bool match(const JHistory &history) const
Check if given history is part of this history.
Definition JHistory.hh:342
friend std::ostream & operator<<(std::ostream &out, const JHistory &history)
Write history to output stream.
Definition JHistory.hh:438
JHistory & add(const int type)
Add event to history.
Definition JHistory.hh:396
bool has(const int type) const
Has event in history.
Definition JHistory.hh:324
Template definition of auxiliary base class for comparison of data structures.
Definition JEquals.hh:84
Simple wrapper for UUID.
Definition JUUID.hh:24
uuid_t uuid
Definition JUUID.hh:172