-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathDataSpecUtils.cxx
More file actions
707 lines (652 loc) · 28.6 KB
/
DataSpecUtils.cxx
File metadata and controls
707 lines (652 loc) · 28.6 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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include "Framework/DataSpecUtils.h"
#include "Framework/DataDescriptorMatcher.h"
#include "Framework/DataMatcherWalker.h"
#include "Framework/VariantHelpers.h"
#include "Framework/Logger.h"
#include "Framework/RuntimeError.h"
#include "Headers/DataHeaderHelpers.h"
#include <cstring>
#include <cinttypes>
#include <regex>
namespace o2::framework
{
namespace
{
std::string join(ConcreteDataMatcher const& matcher, std::string const& sep = "_")
{
return matcher.origin.as<std::string>() + sep + matcher.description.as<std::string>() + sep + std::to_string(matcher.subSpec);
}
std::string join(ConcreteDataTypeMatcher const& matcher, std::string const& sep = "_")
{
return matcher.origin.as<std::string>() + sep + matcher.description.as<std::string>();
}
} // namespace
using namespace data_matcher;
bool DataSpecUtils::match(const InputSpec& spec,
const o2::header::DataOrigin& origin,
const o2::header::DataDescription& description,
const o2::header::DataHeader::SubSpecificationType& subSpec)
{
ConcreteDataMatcher target{origin, description, subSpec};
return match(spec, target);
}
bool DataSpecUtils::match(const OutputSpec& spec,
const o2::header::DataOrigin& origin,
const o2::header::DataDescription& description,
const o2::header::DataHeader::SubSpecificationType& subSpec)
{
ConcreteDataTypeMatcher dataType = DataSpecUtils::asConcreteDataTypeMatcher(spec);
return std::visit(overloaded{[&dataType, &origin, &description, &subSpec](ConcreteDataMatcher const& matcher) -> bool {
return dataType.origin == origin &&
dataType.description == description &&
matcher.subSpec == subSpec;
},
[&dataType, &origin, &description](ConcreteDataTypeMatcher const& matcher) {
return dataType.origin == origin &&
dataType.description == description;
}},
spec.matcher);
}
std::string DataSpecUtils::describe(InputSpec const& spec)
{
if (auto concrete = std::get_if<ConcreteDataMatcher>(&spec.matcher)) {
return join(*concrete, "/");
} else if (auto matcher = std::get_if<DataDescriptorMatcher>(&spec.matcher)) {
std::ostringstream ss;
ss << "<matcher query: " << *matcher << ">";
return ss.str();
}
throw runtime_error("Unhandled InputSpec kind.");
}
std::string DataSpecUtils::describe(OutputSpec const& spec)
{
return std::visit([](auto const& concrete) {
return join(concrete, "/");
},
spec.matcher);
}
void DataSpecUtils::describe(char* buffer, size_t size, InputSpec const& spec)
{
if (auto concrete = std::get_if<ConcreteDataMatcher>(&spec.matcher)) {
char origin[5];
origin[4] = 0;
char description[17];
description[16] = 0;
snprintf(buffer, size, "%s/%s/%" PRIu32, (strncpy(origin, concrete->origin.str, 4), origin),
(strncpy(description, concrete->description.str, 16), description), concrete->subSpec);
} else if (auto matcher = std::get_if<DataDescriptorMatcher>(&spec.matcher)) {
std::ostringstream ss;
ss << "<matcher query: " << *matcher << ">";
strncpy(buffer, ss.str().c_str(), size - 1);
} else {
throw runtime_error("Unsupported InputSpec");
}
}
void DataSpecUtils::describe(char* buffer, size_t size, OutputSpec const& spec)
{
if (auto concrete = std::get_if<ConcreteDataMatcher>(&spec.matcher)) {
char origin[5];
origin[4] = 0;
char description[17];
description[16] = 0;
snprintf(buffer, size, "%s/%s/%" PRIu32, (strncpy(origin, concrete->origin.str, 4), origin),
(strncpy(description, concrete->description.str, 16), description), concrete->subSpec);
} else if (auto concrete = std::get_if<ConcreteDataTypeMatcher>(&spec.matcher)) {
fmt::format_to(buffer, "<matcher query: {}/{}>", concrete->origin, concrete->description);
} else {
throw runtime_error("Unsupported OutputSpec");
}
}
std::string DataSpecUtils::label(InputSpec const& spec)
{
if (auto concrete = std::get_if<ConcreteDataMatcher>(&spec.matcher)) {
return join(*concrete, "_");
} else if (auto matcher = std::get_if<DataDescriptorMatcher>(&spec.matcher)) {
std::ostringstream ss;
ss << *matcher;
std::hash<std::string> hash_fn;
auto s = ss.str();
auto result = std::to_string(hash_fn(s));
return result;
}
throw runtime_error("Unsupported InputSpec");
}
std::string DataSpecUtils::label(OutputSpec const& spec)
{
// FIXME: unite with the InputSpec one...
return std::visit(overloaded{[](auto const& matcher) {
return join(matcher, "_");
}},
spec.matcher);
}
std::string DataSpecUtils::restEndpoint(InputSpec const& spec)
{
if (auto concrete = std::get_if<ConcreteDataMatcher>(&spec.matcher)) {
return std::string("/") + join(*concrete, "/");
} else {
throw runtime_error("Unsupported InputSpec kind");
}
}
void DataSpecUtils::updateMatchingSubspec(InputSpec& spec, header::DataHeader::SubSpecificationType subSpec)
{
if (auto concrete = std::get_if<ConcreteDataMatcher>(&spec.matcher)) {
concrete->subSpec = subSpec;
} else {
// FIXME: this will only work for the cases in which we do have a dataType defined.
auto dataType = DataSpecUtils::asConcreteDataTypeMatcher(spec);
spec.matcher = ConcreteDataMatcher(dataType.origin, dataType.description, subSpec);
}
}
void DataSpecUtils::updateMatchingSubspec(OutputSpec& spec, header::DataHeader::SubSpecificationType subSpec)
{
std::visit(overloaded{
[&subSpec](ConcreteDataMatcher& concrete) {
concrete.subSpec = subSpec;
},
[&spec, &subSpec](ConcreteDataTypeMatcher& dataType) {
spec.matcher = ConcreteDataMatcher{
dataType.origin,
dataType.description,
subSpec};
},
},
spec.matcher);
}
bool DataSpecUtils::validate(InputSpec const& spec)
{
using namespace header;
if (spec.binding.empty()) {
return false;
}
if (auto concrete = std::get_if<ConcreteDataMatcher>(&spec.matcher)) {
return (concrete->description != DataDescription("")) &&
(concrete->description != header::gDataDescriptionInvalid) &&
(concrete->origin != DataOrigin("")) &&
(concrete->origin != header::gDataOriginInvalid);
}
return true;
}
bool DataSpecUtils::validate(OutputSpec const& spec)
{
using namespace header;
auto dataType = DataSpecUtils::asConcreteDataTypeMatcher(spec);
return (dataType.description != DataDescription("")) &&
(dataType.description != header::gDataDescriptionInvalid) &&
(dataType.origin != DataOrigin("")) &&
(dataType.origin != header::gDataOriginInvalid);
}
bool DataSpecUtils::match(InputSpec const& spec, ConcreteDataTypeMatcher const& target)
{
return std::visit(overloaded{
[](ConcreteDataMatcher const& matcher) {
// We return false because the matcher is more
// qualified (has subSpec) than the target.
return false;
},
[&target](DataDescriptorMatcher const& matcher) {
// FIXME: to do it properly we should actually make sure that the
// matcher is invariant for changes of SubSpecification. Maybe it's
// enough to check that none of the nodes actually match on SubSpec.
ConcreteDataMatcher concreteExample{
target.origin,
target.description,
static_cast<header::DataHeader::SubSpecificationType>(0xffffffff)};
data_matcher::VariableContext context;
return matcher.match(concreteExample, context);
}},
spec.matcher);
}
bool DataSpecUtils::match(InputSpec const& spec, ConcreteDataMatcher const& target)
{
if (auto concrete = std::get_if<ConcreteDataMatcher>(&spec.matcher)) {
return *concrete == target;
} else if (auto matcher = std::get_if<DataDescriptorMatcher>(&spec.matcher)) {
data_matcher::VariableContext context;
return matcher->match(target, context);
} else {
throw runtime_error("Unsupported InputSpec");
}
}
bool DataSpecUtils::match(OutputSpec const& spec, ConcreteDataMatcher const& target)
{
return std::visit(overloaded{
[&target](ConcreteDataMatcher const& concrete) {
return concrete == target;
},
[&target](ConcreteDataTypeMatcher const& concrete) {
return concrete.origin == target.origin &&
concrete.description == target.description;
}},
spec.matcher);
}
bool DataSpecUtils::match(OutputSpec const& left, OutputSpec const& right)
{
if (auto leftConcrete = std::get_if<ConcreteDataMatcher>(&left.matcher)) {
return match(right, *leftConcrete);
} else if (auto rightConcrete = std::get_if<ConcreteDataMatcher>(&right.matcher)) {
return match(left, *rightConcrete);
} else {
// both sides are ConcreteDataTypeMatcher without subspecification, we simply specify 0
// this is ignored in the mathing since also left hand object is ConcreteDataTypeMatcher
ConcreteDataTypeMatcher dataType = DataSpecUtils::asConcreteDataTypeMatcher(right);
return match(left, dataType.origin, dataType.description, 0);
}
}
bool DataSpecUtils::match(InputSpec const& input, OutputSpec const& output)
{
return std::visit([&input](auto const& concrete) -> bool {
return DataSpecUtils::match(input, concrete);
},
output.matcher);
}
bool DataSpecUtils::partialMatch(OutputSpec const& output, header::DataOrigin const& origin)
{
auto dataType = DataSpecUtils::asConcreteDataTypeMatcher(output);
return dataType.origin == origin;
}
bool DataSpecUtils::partialMatch(InputSpec const& input, header::DataOrigin const& origin)
{
return DataSpecUtils::asConcreteOrigin(input) == origin;
}
bool DataSpecUtils::partialMatch(InputSpec const& input, header::DataDescription const& description)
{
try {
return DataSpecUtils::asConcreteDataDescription(input) == description;
} catch (...) {
return false;
}
}
bool DataSpecUtils::partialMatch(OutputSpec const& output, header::DataDescription const& description)
{
try {
return DataSpecUtils::asConcreteDataTypeMatcher(output).description == description;
} catch (...) {
return false;
}
}
struct MatcherInfo {
header::DataOrigin origin = header::gDataOriginInvalid; // Whether or not we found an origins (should be a bad query!)
header::DataDescription description = header::gDataDescriptionInvalid; // Whether or not we found a description
header::DataHeader::SubSpecificationType subSpec = 0; // Whether or not we found a description
bool hasOrigin = false;
bool hasDescription = false;
bool hasSubSpec = false;
bool hasUniqueOrigin = false; // Whether the matcher involves a unique origin
bool hasUniqueDescription = false; // Whether the matcher involves a unique origin
bool hasUniqueSubSpec = false;
bool hasError = false;
};
MatcherInfo extractMatcherInfo(DataDescriptorMatcher const& top)
{
using namespace data_matcher;
using ops = DataDescriptorMatcher::Op;
MatcherInfo state;
auto nodeWalker = overloaded{
[&state](EdgeActions::EnterNode action) {
if (state.hasError) {
return VisitNone;
}
// For now we do not support extracting a type from things
// which have an OR, so we reset all the uniqueness attributes.
if (action.node->getOp() == ops::Or || action.node->getOp() == ops::Xor) {
state.hasError = true;
return VisitNone;
}
if (action.node->getOp() == ops::Just) {
return VisitLeft;
}
return VisitBoth;
},
[](auto) { return VisitNone; }};
auto leafWalker = overloaded{
[&state](OriginValueMatcher const& valueMatcher) {
// FIXME: If we found already more than one data origin, it means
// we are ANDing two incompatible origins. Until we support OR,
// this is an error.
// In case we find a ContextRef, it means we have
// a wildcard, so there is not a unique origin.
if (state.hasOrigin) {
state.hasError = false;
return;
}
state.hasOrigin = true;
valueMatcher.visit(overloaded{
[&state](std::string const& s) {
strncpy(state.origin.str, s.data(), 4);
state.hasUniqueOrigin = true;
},
[&state](auto) { state.hasUniqueOrigin = false; }});
},
[&state](DescriptionValueMatcher const& valueMatcher) {
if (state.hasDescription) {
state.hasError = true;
return;
}
state.hasDescription = true;
valueMatcher.visit(overloaded{
[&state](std::string const& s) {
strncpy(state.description.str, s.data(), 16);
state.hasUniqueDescription = true;
},
[&state](auto) { state.hasUniqueDescription = false; }});
},
[&state](SubSpecificationTypeValueMatcher const& valueMatcher) {
if (state.hasSubSpec) {
state.hasError = true;
return;
}
state.hasSubSpec = true;
valueMatcher.visit(overloaded{
[&state](uint32_t const& data) {
state.subSpec = data;
state.hasUniqueSubSpec = true;
},
[&state](auto) { state.hasUniqueSubSpec = false; }});
},
[](auto t) {}};
DataMatcherWalker::walk(top, nodeWalker, leafWalker);
return state;
}
ConcreteDataMatcher DataSpecUtils::asConcreteDataMatcher(InputSpec const& spec)
{
return std::visit(overloaded{[](ConcreteDataMatcher const& concrete) {
return concrete;
},
[&binding = spec.binding](DataDescriptorMatcher const& matcher) {
auto info = extractMatcherInfo(matcher);
if (info.hasOrigin && info.hasUniqueOrigin &&
info.hasDescription && info.hasDescription &&
info.hasSubSpec && info.hasUniqueSubSpec) {
return ConcreteDataMatcher{info.origin, info.description, info.subSpec};
}
throw std::runtime_error("Cannot convert " + binding + " to ConcreteDataMatcher");
}},
spec.matcher);
}
ConcreteDataMatcher DataSpecUtils::asConcreteDataMatcher(OutputSpec const& spec)
{
return std::get<ConcreteDataMatcher>(spec.matcher);
}
ConcreteDataTypeMatcher DataSpecUtils::asConcreteDataTypeMatcher(OutputSpec const& spec)
{
return std::visit([](auto const& concrete) {
return ConcreteDataTypeMatcher{concrete.origin, concrete.description};
},
spec.matcher);
}
ConcreteDataTypeMatcher DataSpecUtils::asConcreteDataTypeMatcher(InputSpec const& spec)
{
return std::visit(overloaded{
[](auto const& concrete) {
return ConcreteDataTypeMatcher{concrete.origin, concrete.description};
},
[](DataDescriptorMatcher const& matcher) {
auto state = extractMatcherInfo(matcher);
if (state.hasUniqueOrigin && state.hasUniqueDescription) {
return ConcreteDataTypeMatcher{state.origin, state.description};
}
throw runtime_error("Could not extract data type from query");
}},
spec.matcher);
}
header::DataOrigin DataSpecUtils::asConcreteOrigin(InputSpec const& spec)
{
return std::visit(overloaded{
[](auto const& concrete) {
return concrete.origin;
},
[](DataDescriptorMatcher const& matcher) {
auto state = extractMatcherInfo(matcher);
if (state.hasUniqueOrigin) {
return state.origin;
}
throw runtime_error("Could not extract data type from query");
}},
spec.matcher);
}
header::DataDescription DataSpecUtils::asConcreteDataDescription(InputSpec const& spec)
{
return std::visit(overloaded{
[](auto const& concrete) {
return concrete.description;
},
[](DataDescriptorMatcher const& matcher) {
auto state = extractMatcherInfo(matcher);
if (state.hasUniqueDescription) {
return state.description;
}
throw runtime_error("Could not extract data type from query");
}},
spec.matcher);
}
OutputSpec DataSpecUtils::asOutputSpec(InputSpec const& spec)
{
return std::visit(overloaded{
[&spec](ConcreteDataMatcher const& concrete) {
return OutputSpec{{spec.binding}, concrete, spec.lifetime};
},
[&spec](DataDescriptorMatcher const& matcher) {
auto state = extractMatcherInfo(matcher);
if (state.hasUniqueOrigin && state.hasUniqueDescription && state.hasUniqueSubSpec) {
return OutputSpec{{spec.binding}, ConcreteDataMatcher{state.origin, state.description, state.subSpec}, spec.lifetime};
} else if (state.hasUniqueOrigin && state.hasUniqueDescription) {
return OutputSpec{{spec.binding}, ConcreteDataTypeMatcher{state.origin, state.description}, spec.lifetime};
}
throw runtime_error_f("Could not extract neither ConcreteDataMatcher nor ConcreteDataTypeMatcher from query %s", describe(spec).c_str());
}},
spec.matcher);
}
DataDescriptorMatcher DataSpecUtils::dataDescriptorMatcherFrom(ConcreteDataMatcher const& concrete)
{
DataDescriptorMatcher matchEverything{
DataDescriptorMatcher::Op::And,
OriginValueMatcher{concrete.origin.as<std::string>()},
std::make_unique<DataDescriptorMatcher>(
DataDescriptorMatcher::Op::And,
DescriptionValueMatcher{concrete.description.as<std::string>()},
std::make_unique<DataDescriptorMatcher>(
DataDescriptorMatcher::Op::And,
SubSpecificationTypeValueMatcher{concrete.subSpec},
std::make_unique<DataDescriptorMatcher>(DataDescriptorMatcher::Op::Just,
StartTimeValueMatcher{ContextRef{0}})))};
return std::move(matchEverything);
}
DataDescriptorMatcher DataSpecUtils::dataDescriptorMatcherFrom(ConcreteDataTypeMatcher const& dataType)
{
auto timeDescriptionMatcher = std::make_unique<DataDescriptorMatcher>(
DataDescriptorMatcher::Op::And,
DescriptionValueMatcher{dataType.description.as<std::string>()},
StartTimeValueMatcher(ContextRef{0}));
return std::move(DataDescriptorMatcher(
DataDescriptorMatcher::Op::And,
OriginValueMatcher{dataType.origin.as<std::string>()},
std::move(timeDescriptionMatcher)));
}
DataDescriptorMatcher DataSpecUtils::dataDescriptorMatcherFrom(header::DataOrigin const& origin)
{
char buf[5] = {0, 0, 0, 0, 0};
strncpy(buf, origin.str, 4);
DataDescriptorMatcher matchOnlyOrigin{
DataDescriptorMatcher::Op::And,
OriginValueMatcher{buf},
std::make_unique<DataDescriptorMatcher>(
DataDescriptorMatcher::Op::And,
DescriptionValueMatcher{ContextRef{1}},
std::make_unique<DataDescriptorMatcher>(
DataDescriptorMatcher::Op::And,
SubSpecificationTypeValueMatcher{ContextRef{2}},
std::make_unique<DataDescriptorMatcher>(DataDescriptorMatcher::Op::Just,
StartTimeValueMatcher{ContextRef{0}})))};
return std::move(matchOnlyOrigin);
}
DataDescriptorMatcher DataSpecUtils::dataDescriptorMatcherFrom(header::DataDescription const& description)
{
char buf[17] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
strncpy(buf, description.str, 16);
DataDescriptorMatcher matchOnlyOrigin{
DataDescriptorMatcher::Op::And,
OriginValueMatcher{ContextRef{1}},
std::make_unique<DataDescriptorMatcher>(
DataDescriptorMatcher::Op::And,
DescriptionValueMatcher{buf},
std::make_unique<DataDescriptorMatcher>(
DataDescriptorMatcher::Op::And,
SubSpecificationTypeValueMatcher{ContextRef{2}},
std::make_unique<DataDescriptorMatcher>(DataDescriptorMatcher::Op::Just,
StartTimeValueMatcher{ContextRef{0}})))};
return std::move(matchOnlyOrigin);
}
InputSpec DataSpecUtils::matchingInput(OutputSpec const& spec)
{
return std::visit(overloaded{
[&spec](ConcreteDataMatcher const& concrete) -> InputSpec {
return InputSpec{
spec.binding.value,
concrete.origin,
concrete.description,
concrete.subSpec,
spec.lifetime};
},
[&spec](ConcreteDataTypeMatcher const& dataType) -> InputSpec {
auto&& matcher = DataSpecUtils::dataDescriptorMatcherFrom(dataType);
return InputSpec{
spec.binding.value,
std::move(matcher),
spec.lifetime};
}},
spec.matcher);
}
InputSpec DataSpecUtils::fromMetadataString(std::string s)
{
std::regex word_regex("(\\w+)");
auto words = std::sregex_iterator(s.begin(), s.end(), word_regex);
if (std::distance(words, std::sregex_iterator()) != 3) {
throw runtime_error_f("Malformed input spec metadata: %s", s.c_str());
}
std::vector<std::string> data;
for (auto i = words; i != std::sregex_iterator(); ++i) {
data.emplace_back(i->str());
}
char origin[4];
char description[16];
std::memcpy(&origin, data[1].c_str(), 4);
std::memcpy(&description, data[2].c_str(), 16);
return InputSpec{data[0], header::DataOrigin{origin}, header::DataDescription{description}};
}
std::optional<header::DataOrigin> DataSpecUtils::getOptionalOrigin(InputSpec const& spec)
{
// FIXME: try to address at least a few cases.
return std::visit(overloaded{
[](ConcreteDataMatcher const& concrete) -> std::optional<header::DataOrigin> {
return std::make_optional(concrete.origin);
},
[](DataDescriptorMatcher const& matcher) -> std::optional<header::DataOrigin> {
auto state = extractMatcherInfo(matcher);
if (state.hasUniqueOrigin) {
return std::make_optional(state.origin);
} else if (state.hasError) {
throw runtime_error("Could not extract origin from query");
}
return {};
}},
spec.matcher);
}
std::optional<header::DataDescription> DataSpecUtils::getOptionalDescription(InputSpec const& spec)
{
// FIXME: try to address at least a few cases.
return std::visit(overloaded{
[](ConcreteDataMatcher const& concrete) -> std::optional<header::DataDescription> {
return std::make_optional(concrete.description);
},
[](DataDescriptorMatcher const& matcher) -> std::optional<header::DataDescription> {
auto state = extractMatcherInfo(matcher);
if (state.hasUniqueDescription) {
return std::make_optional(state.description);
} else if (state.hasError) {
throw runtime_error("Could not extract description from query");
}
return {};
}},
spec.matcher);
}
std::optional<header::DataHeader::SubSpecificationType> DataSpecUtils::getOptionalSubSpec(OutputSpec const& spec)
{
return std::visit(overloaded{
[](ConcreteDataMatcher const& concrete) -> std::optional<header::DataHeader::SubSpecificationType> {
return std::make_optional(concrete.subSpec);
},
[](ConcreteDataTypeMatcher const&) -> std::optional<header::DataHeader::SubSpecificationType> {
return {};
}},
spec.matcher);
}
std::optional<header::DataHeader::SubSpecificationType> DataSpecUtils::getOptionalSubSpec(InputSpec const& spec)
{
// FIXME: try to address at least a few cases.
return std::visit(overloaded{
[](ConcreteDataMatcher const& concrete) -> std::optional<header::DataHeader::SubSpecificationType> {
return std::make_optional(concrete.subSpec);
},
[](DataDescriptorMatcher const& matcher) -> std::optional<header::DataHeader::SubSpecificationType> {
auto state = extractMatcherInfo(matcher);
if (state.hasUniqueSubSpec) {
return std::make_optional(state.subSpec);
} else if (state.hasError) {
throw runtime_error("Could not extract subSpec from query");
}
return {};
}},
spec.matcher);
}
bool DataSpecUtils::includes(const InputSpec& left, const InputSpec& right)
{
return std::visit(
overloaded{
[&left](ConcreteDataMatcher const& rightMatcher) {
return match(left, rightMatcher);
},
[&left](DataDescriptorMatcher const& rightMatcher) {
auto rightInfo = extractMatcherInfo(rightMatcher);
return std::visit(
overloaded{
[&rightInfo](ConcreteDataMatcher const& leftMatcher) {
return rightInfo.hasUniqueOrigin && rightInfo.origin == leftMatcher.origin &&
rightInfo.hasUniqueDescription && rightInfo.description == leftMatcher.description &&
rightInfo.hasUniqueSubSpec && rightInfo.subSpec == leftMatcher.subSpec;
},
[&rightInfo](DataDescriptorMatcher const& leftMatcher) {
auto leftInfo = extractMatcherInfo(leftMatcher);
return (!leftInfo.hasOrigin || (rightInfo.hasOrigin && leftInfo.origin == rightInfo.origin)) &&
(!leftInfo.hasDescription || (rightInfo.hasDescription && leftInfo.description == rightInfo.description)) &&
(!leftInfo.hasSubSpec || (rightInfo.hasSubSpec && leftInfo.subSpec == rightInfo.subSpec));
}},
left.matcher);
}},
right.matcher);
}
void DataSpecUtils::updateInputList(std::vector<InputSpec>& list, InputSpec&& input)
{
auto locate = std::find_if(list.begin(), list.end(), [&](InputSpec& entry) { return entry.binding == input.binding; });
if (locate != list.end()) {
// amend entry
auto& entryMetadata = locate->metadata;
entryMetadata.insert(entryMetadata.end(), input.metadata.begin(), input.metadata.end());
std::sort(entryMetadata.begin(), entryMetadata.end(), [](ConfigParamSpec const& a, ConfigParamSpec const& b) { return a.name < b.name; });
auto new_end = std::unique(entryMetadata.begin(), entryMetadata.end(), [](ConfigParamSpec const& a, ConfigParamSpec const& b) { return a.name == b.name; });
entryMetadata.erase(new_end, entryMetadata.end());
} else {
// add entry
list.emplace_back(std::move(input));
}
}
} // namespace o2::framework