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