DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
oks_merge.cxx
Go to the documentation of this file.
1//
2// DUNE DAQ modification notice:
3// This file has been modified from the original ATLAS oks_utils source for the DUNE DAQ project.
4// Fork baseline commit: c2e7dfc7 (2022-03-30).
5// Renamed since fork: yes (from src/bin/oks_merge.cpp to apps/oks_merge.cxx).
6//
7
16
17
18#include "oks/kernel.hpp"
19
20using namespace dunedaq;
21using namespace dunedaq::oks;
22
24
25#include "ers/ers.hpp"
26
28 oks_merge,
29 BadCommandLine,
30 "Bad command line: \"" << what << '\"',
31 ((const char*)what)
32)
33
35 oks_merge,
37 "Failed to parse query \"" << query << "\" in class \"" << class_name << '\"',
38 ((const char*)query)
39 ((const char*)class_name)
40)
41
43 oks_merge,
44 BadClass,
45 "Cannot find class \"" << class_name << '\"',
46 ((const char*)class_name)
47)
48
49
50
52 oks_merge,
53 CaughtException,
54 "Caught " << what << " exception: \'" << text << '\'',
55 ((const char *)what)
56 ((const char *)text)
57)
58
59
60
61std::string appTitle;
62
63void printUsage(const char *appName, std::ostream& s)
64{
65 s << appTitle << "\n"
66 "Usage: " << appName << "\n"
67 " [--class name-of-class [--query query [--print-references recursion-depth [class-name*] [--]]]\n"
68 " [--version]\n"
69 " [--help]\n"
70 " [--out-data-file data_file] [--out-data-file schema_file] inputfiles*\n"
71 "\n"
72 " Options:\n"
73 " -o | --out-data-file datafilename the out filename to store oks objects from input files\n"
74 " -s | --out-schema-file schemafilename the out filename to store oks classes from input files\n"
75 " -c | --class class_name dump given class (all objects or matching some query)\n"
76 " -q | --query query print objects matching query (can only be used with class)\n"
77 " -r | --print-references N C1 C2 ... CX print objects referenced by found objects (can only be used with query), where:\n"
78 " * the parameter N defines recursion depth for referenced objects (> 0)\n"
79 " * the optional set of names {C1 .. CX} defines [sub-]classes for above objects\n"
80 " -v | --version print version\n"
81 " -h | --help print this text\n"
82 "\n"
83 " Description:\n"
84 " Merges several oks files into single schema and data files.\n"
85 "\n";
86}
87
88enum __OksMergeExitStatus__ {
89 __Success__ = 0,
90 __NoDataFilesLoaded__,
91 __NoSchemaFilesLoaded__,
93 __NoOutputSchemaFile__,
97};
98
99
100static void
101no_param(const char * s)
102{
103 Oks::error_msg("oks_merge") << "no parameter(s) for command line argument \'" << s << "\' provided\n\n";
104 exit(__BadCommandLine__);
105}
106
107int
108main(int argc, char *argv[])
109{
110 appTitle = "OKS merge. OKS kernel version ";
112
113 const char * appName = "oks_merge";
114
115 std::string out_data_file;
116 std::string out_schema_file;
117
118 const char * class_name = nullptr;
119 const char * query = nullptr;
120 long recursion_depth = 0;
121 std::vector<std::string> ref_classes;
122
123
124 if(argc == 1) {
125 printUsage(appName, std::cerr);
126 return __BadCommandLine__;
127 }
128
129 OksKernel kernel;
131
132 try {
133
134 for(int i = 1; i < argc; i++) {
135 const char * cp = argv[i];
136
137 if(!strcmp(cp, "-h") || !strcmp(cp, "--help")) {
138 printUsage(appName, std::cout);
139 return __Success__;
140 }
141 else if(!strcmp(cp, "-v") || !strcmp(cp, "--version")) {
142 std::cout << appTitle << std::endl;
143 return __Success__;
144 }
145 else if(!strcmp(cp, "-o") || !strcmp(cp, "--out-data-file")) {
146 if(++i == argc) { no_param(cp); } else { out_data_file = argv[i]; }
147 }
148 else if(!strcmp(cp, "-s") || !strcmp(cp, "--out-schema-file")) {
149 if(++i == argc) { no_param(cp); } else { out_schema_file = argv[i]; }
150 }
151 else if(!strcmp(cp, "-c") || !strcmp(cp, "--class")) {
152 if(++i == argc) { no_param(cp); } else { class_name = argv[i]; }
153 }
154 else if(!strcmp(cp, "-q") || !strcmp(cp, "--query")) {
155 if(++i == argc) { no_param(cp); } else { query = argv[i]; }
156 }
157 else if(!strcmp(cp, "-r") || !strcmp(cp, "--print-references")) {
158 if(++i == argc) { no_param(cp); } else { recursion_depth = atol(argv[i]); }
159 int j = 0;
160 for(; j < argc - i - 1; ++j) {
161 if(argv[i+1+j][0] != '-') { ref_classes.push_back(argv[i+1+j]); } else { break; }
162 }
163 i += j;
164 }
165 else if(strcmp(cp, "--")) {
166 kernel.load_file(cp);
167 }
168 }
169
170 if(!out_data_file.empty() && kernel.data_files().empty()) {
171 ers::fatal(oks_merge::BadCommandLine(ERS_HERE,"There were no data files loaded"));
172 return __NoDataFilesLoaded__;
173 }
174
175 if(!out_schema_file.empty() && kernel.schema_files().empty()) {
176 ers::fatal(oks_merge::BadCommandLine(ERS_HERE,"There were no schema files loaded"));
177 return __NoSchemaFilesLoaded__;
178 }
179
180 if(query && !class_name) {
181 ers::fatal(oks_merge::BadCommandLine(ERS_HERE,"Query can only be executed when class name is provided (use -c option)"));
182 return __BadCommandLine__;
183 }
184
185
186 OksFile * data_file_h = 0;
187 OksFile * schema_file_h = 0;
188
189 if(!out_data_file.empty()) {
190 data_file_h = kernel.new_data(out_data_file);
191 kernel.set_active_data(data_file_h);
192 }
193
194 if(!out_schema_file.empty()) {
195 schema_file_h = kernel.new_schema(out_schema_file);
196 kernel.set_active_schema(schema_file_h);
197 }
198
199 if(!data_file_h && !schema_file_h) {
200 ers::fatal(oks_merge::BadCommandLine(ERS_HERE,"There is no out schema file name defined"));
201 return __NoOutputSchemaFile__;
202 }
203
204 if(schema_file_h) {
205 for(OksClass::Map::const_iterator j = kernel.classes().begin(); j != kernel.classes().end(); ++j) {
206 j->second->set_file(schema_file_h, false);
207 }
208
209 kernel.save_schema(schema_file_h);
210 }
211
212 if(data_file_h) {
213 if(schema_file_h) {
214 data_file_h->add_include_file(out_schema_file);
215 }
216
217 if(class_name && *class_name) {
218 if(OksClass * c = kernel.find_class(class_name)) {
219 if(query && *query) {
220 OksQuery * q = new OksQuery(c, query);
221 if(q->good()) {
222 OksObject::List * objs = c->execute_query(q);
223 size_t num = (objs ? objs->size() : 0);
224 std::cout << "Found " << num << " matching query \"" << query << "\" in class \"" << class_name << "\"";
225 if(num) {
226 OksObject::FSet refs;
227 std::cout << ':' << std::endl;
228 while(!objs->empty()) {
229 OksObject * o = objs->front();
230 objs->pop_front();
231 if(recursion_depth > 0) {
232 oks::ClassSet all_ref_classes;
233 kernel.get_all_classes(ref_classes, all_ref_classes);
234 o->references(refs, recursion_depth, true, &all_ref_classes);
235 std::cout << " - " << o << " references " << refs.size() << " objects" << std::endl;
236 }
237 else {
238 refs.insert(o);
239 }
240 }
241 delete objs;
242 for(OksObject::FSet::iterator i = refs.begin(); i != refs.end(); ++i) {
243 const_cast<OksObject *>(*i)->set_file(data_file_h, false);
244 }
245 }
246 else {
247 std::cout << std::endl;
248 }
249 }
250 else {
251 ers::fatal(oks_merge::BadQuery(ERS_HERE, query, class_name));
252 return __BadQuery__;
253 }
254 delete q;
255 }
256 else {
257 std::cout << *c << std::endl;
258 }
259 }
260 else {
261 ers::fatal(oks_merge::BadClass(ERS_HERE, class_name));
262 return __NoSuchClass__;
263 }
264 }
265 else {
266 for(OksObject::Set::const_iterator j = kernel.objects().begin(); j != kernel.objects().end(); ++j) {
267 (*j)->set_file(data_file_h, false);
268 }
269 }
270
271 kernel.save_data(data_file_h);
272 }
273
274 }
275
276 catch (oks::exception & ex) {
277 ers::fatal(oks_merge::CaughtException(ERS_HERE, "OKS", ex.what()));
278 return __ExceptionCaught__;
279 }
280
281 catch (std::exception & ex) {
282 ers::fatal(oks_merge::CaughtException(ERS_HERE, "Standard C++", ex.what()));
283 return __ExceptionCaught__;
284 }
285
286 catch (...) {
287 ers::fatal(oks_merge::CaughtException(ERS_HERE, "unknown", ""));
288 return __ExceptionCaught__;
289 }
290
291 return __Success__;
292}
#define ERS_HERE
The OKS class.
Definition class.hpp:205
Provides interface to the OKS XML schema and data files.
Definition file.hpp:345
void add_include_file(const std::string &name)
Add include file.
Definition file.cpp:1199
Provides interface to the OKS kernel.
Definition kernel.hpp:582
OksFile * load_file(const std::string &name, bool bind=true)
Load OKS database file.
Definition kernel.cpp:1793
const OksFile::Map & data_files() const
Get all data files.
Definition kernel.hpp:1498
OksFile * new_data(const std::string &name, const std::string &logical_name="", const std::string &type="")
Create OKS data file.
Definition kernel.cpp:3723
void set_active_schema(OksFile *file_h)
Set active OKS schema file.
Definition kernel.cpp:2908
const OksObject::Set & objects() const
Get objects.
Definition kernel.hpp:1795
static void set_use_strict_repository_paths(bool flag)
Set flag to use strict-repository-paths check.
Definition kernel.hpp:1727
const OksClass::Map & classes() const
Get classes.
Definition kernel.hpp:1772
void save_schema(OksFile *file_h, bool force=false, OksFile *true_file_h=0)
Save OKS schema file.
Definition kernel.cpp:2543
void save_data(OksFile *file_h, bool ignore_bad_objects=false, OksFile *true_file_h=nullptr, bool force_defaults=false)
Save OKS data file.
Definition kernel.cpp:3799
OksFile * new_schema(const std::string &name)
Create OKS schema file.
Definition kernel.cpp:2490
static const char * GetVersion()
Get OKS version. The method returns string containing CVS tag and date of OKS build.
Definition kernel.cpp:302
OksClass * find_class(const std::string &class_name) const
Find class by name (C++ string).
Definition kernel.hpp:1819
void set_active_data(OksFile *file_h)
Set active OKS data file.
Definition kernel.cpp:4283
const OksFile::Map & schema_files() const
Get all schema files.
Definition kernel.hpp:1228
void get_all_classes(const std::vector< std::string > &names_in, ClassSet &classes_out) const
The method searches all classes including subclasses for given names.
Definition kernel.cpp:4680
OksObject describes instance of OksClass.
Definition object.hpp:841
std::list< OksObject * > List
Definition object.hpp:880
void references(OksObject::FSet &refs, unsigned long recursion_depth, bool add_self=false, oks::ClassSet *classes=0) const
Definition object.cpp:3115
std::unordered_set< OksObject *, oks::hash_obj_ptr, oks::equal_obj_ptr > FSet
Definition object.hpp:871
void set_file(OksFile *file, bool update_owner=true)
Move object to different file.
Definition object.cpp:1489
OKS query class.
Definition query.hpp:41
bool good() const
Definition query.hpp:56
static std::ostream & error_msg(const char *)
Definition kernel.cpp:561
virtual const char * what() const noexcept
int main(int argc, char *argv[])
#define ERS_DECLARE_ISSUE( namespace_name, class_name, message_, attributes)
Definition macro.hpp:65
std::unordered_set< const OksClass *, oks::hash_class_ptr, oks::equal_class_ptr > ClassSet
Definition object.hpp:825
Including Qt Headers.
Definition module.cpp:16
void fatal(const Issue &issue)
Definition ers.hpp:99
static void printUsage(const char *appName, std::ostream &s)
std::string appTitle
@ __BadCommandLine__
@ __Success__
@ __ExceptionCaught__
Definition oks_dump.cxx:36
@ __BadQuery__
Definition oks_dump.cxx:33
@ __NoSuchClass__
Definition oks_dump.cxx:34
static void no_param(const char *s)
BadQuery
Definition oks_merge.cxx:36