DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
gtool.cpp
Go to the documentation of this file.
1// DUNE DAQ modification notice:
2// This file has been modified from the original ATLAS dbe source for the DUNE DAQ project.
3// Fork baseline commit: dbe-02-12-17 (2022-05-12).
4// Renamed since fork: yes (from src/graphtool/gtool.cpp to apps/graphtool/gtool.cpp).
5
6/************************************************************
7 * gtool.cpp
8 *
9 * Created on: Jun 10, 2016
10 * Author: Leonidas Georgopoulos
11 ************************************************************/
12
13#include "dbe/gtool.hpp"
14#include "dbe/confaccessor.hpp"
15#include "dbe/config_api.hpp"
18#include "dbe/graphtool.hpp"
19#include "dbe/messenger.hpp"
20
21#include <QFileInfo>
22
23#include <boost/graph/graphviz.hpp>
24
25#include <algorithm>
26#include <string>
27#include <vector>
28
29namespace dbe {
30namespace tool {
31namespace graph {
32
33//------------------------------------------------------------------------------------------
34gtool::gtool(std::string const& rl, dbinfo dbtype)
35 : this_src(rl)
36{
37 QFileInfo DatabaseFile(QString::fromStdString(rl));
38
39 if (DatabaseFile.exists()) {
40 QString path_to_database = QString(DatabaseFile.absoluteFilePath());
41 {
42 confaccessor::setdbinfo(path_to_database, dbtype);
43 INFO("Database location set", "Program execution control success");
44
45 if (confaccessor::load()) {
46 INFO("Database initialized", "User request");
47 } else {
48 ERROR("Could not load database", "OKS error ", " Actions : check environment variables e.g. DUNEDAQ_DB_PATH");
49 throw std::string("Could not load database");
50 }
51 }
52 } else {
53 ERROR("Cannot open database", "File error", "for file", rl);
54 }
55}
56
57gtool::t_graph const&
59{
60 return this_graph;
61}
62
63void
65{
66 INFO("Start load objects into internal cache", "Program execution control");
67
68 std::vector<std::string> all_classes = { config::api::info::onclass::allnames<std::vector<std::string>>() };
69
70 for (auto const& x : all_classes) {
72 }
73
74 INFO("All objects loaded into internal cache",
75 "Program execution control success",
76 "#objects:",
77 std::to_string(this_all_objects.size()));
78}
79
80void
81gtool::load_all_class_objects(std::string const& cname)
82{
83 std::vector<dbe::tref> all_objects = config::api::info::onclass::objects<false>(cname, false);
84
85 INFO("Class to load: ", "Program execution control", cname);
86
87 for (dbe::tref const& x : all_objects) {
88 this_all_objects.push_back(x);
89 }
90
91 INFO("Class load success: ", "Program execution control", cname, "#objects", std::to_string(all_objects.size()));
92}
93
94void
96{
97 INFO("Database graph generation initiated", "Program execution control");
98 already_processed.clear();
99
100 for (auto const& x : this_all_objects) {
102 }
103
104 INFO("Graph generation completed successfully", "Program execution control success");
105}
106
108gtool::add_object(gtool::t_graph& g, tref const& x, bool uniqueness)
109{
110 vertex_label xl{ x.UID(), x.class_name(), x.full_name() };
111
112 if (not uniqueness) {
113 // Multiple vertices with the same properties can be added to this graph
114 return boost::add_vertex(xl, g);
115 } else {
116 bool result;
117 boost::graph_traits<t_graph>::vertex_iterator viter;
118 std::tie(result, viter) = lookup(g, xl);
119
120 if (result) {
121 return *viter;
122 } else {
123 return boost::add_vertex(xl, g);
124 }
125 }
126}
127
130{
131 t_registry::iterator vat = registry.find(x.full_name());
132
133 if (std::end(registry) == vat) {
134 FULLDEBUG("Adding object", "Program execution control", x.full_name());
135 // not in the registry implies it is not in the graph , i.e. its user responsibility
136 t_registry::iterator v;
137 std::tie(v, std::ignore) = registry.emplace(x.full_name(), add_object(g, x, false));
138 return v->second;
139 } else {
140 return vat->second;
141 }
142}
143
146{
147 DEBUG("Processing object", "Program execution control", o.full_name());
148 t_vertex ov = add_object(g, o, registry);
149
150 std::vector<tref> friends{ config::api::graph::linked::by::object<tref>(o) }; // get all neighbors
151 DEBUG(
152 "Processing object", "Program execution control", o.full_name(), " # friends: ", std::to_string(friends.size()));
153
154 for (tref const& x : friends) {
155 t_vertex xv = add_object(g, x, registry);
156 boost::add_edge(ov, xv, g);
157 FULLDEBUG("Add edge ", "Program excecution control", o.full_name(), "->", x.full_name());
158 }
159
160 return ov;
161}
162
165{
166 auto const& vertices = boost::vertices(g);
167
168 for (auto v = vertices.first; v != vertices.second; ++v) {
169 if (l.label == boost::get(&vertex_label::label, g, *v)) {
170 return t_lookup_ret(true, v);
171 }
172 }
173
174 return t_lookup_ret(false, vertices.second);
175}
176//------------------------------------------------------------------------------------------
177
178//------------------------------------------------------------------------------------------
179writegraph::writegraph(std::string const& s)
180 : this_dest(s)
181{
182}
183
184int
186{
187 INFO("Saving result", "Program execution control", this_dest);
188 write(x.getgraph());
189 INFO("Result sent to output", "Program execution control success", this_dest);
190 return EXIT_SUCCESS;
191}
192
193void
195{
197}
198//------------------------------------------------------------------------------------------
199
200//------------------------------------------------------------------------------------------
201void
202write(gtool::t_graph const& g, std::string const& ofn)
203{
204 if (ofn.empty()) {
205 write_to_cout(g);
206 } else {
207 write_to_file(g, ofn);
208 }
209}
210
211void
213{
214 INFO("Sending output to stdout", "Program execution control")
215 boost::write_graphviz(std::cout, g, boost::make_label_writer(boost::get(&gtool::vertex_label::label, g)));
216}
217
218void
219write_to_file(gtool::t_graph const& g, std::string const& ofn)
220{
221 std::ofstream of;
222 of.open(ofn);
223
224 if (of.is_open()) {
225 INFO("Sending output to file", "Program execution control", "File name:", ofn);
226 boost::write_graphviz(of, g, boost::make_label_writer(boost::get(&gtool::vertex_label::label, g)));
227
228 of.close();
229
230 if (of.fail()) {
231 ERROR("Could not close file", "Program execution control", "File name:", ofn);
232 } else {
233 NOTE("Output written to file", "Program execution control success", "File name:", ofn);
234 }
235 } else {
236 ERROR("Output could not be written file", "Stream could not be opened", "File name:", ofn);
237 write_to_cout(g);
238 }
239}
240//------------------------------------------------------------------------------------------
241
242} /* namespace graph */
243} /* namespace tool */
244} /* namespace dbe */
static bool load(bool subscribeToChanges=true)
static void setdbinfo(QString const &location, dbinfo const itype=dbinfo::oks)
static std::vector< dbe::inner::configobject::tref > objects(std::string const &cname, bool const keep_inherited=true)
boost::graph_traits< t_graph >::vertex_descriptor t_vertex
Definition gtool.hpp:53
t_objects this_all_objects
Definition gtool.hpp:128
std::map< std::string, t_vertex > t_registry
Definition gtool.hpp:55
gtool(std::string const &rl, dbinfo dbtype)
Definition gtool.cpp:34
boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS, vertex_label > t_graph
Definition gtool.hpp:50
static t_lookup_ret lookup(t_graph const &g, vertex_label const &l)
Definition gtool.cpp:164
std::pair< bool, boost::graph_traits< gtool::t_graph >::vertex_iterator > t_lookup_ret
Definition gtool.hpp:110
std::string this_src
Definition gtool.hpp:131
t_registry already_processed
Definition gtool.hpp:129
static t_vertex add_object(t_graph &g, tref const &o, bool uniqueness=true)
Definition gtool.cpp:108
void load_all_class_objects(std::string const &cname)
Definition gtool.cpp:81
static t_vertex add_object_and_friends(t_graph &, tref const &, t_registry &registry)
Definition gtool.cpp:145
t_graph const & getgraph() const
Definition gtool.cpp:58
void write(gtool::t_graph const &) const
Definition gtool.cpp:194
int operator()(gtool const &) const
Definition gtool.cpp:185
writegraph(std::string const &)
Definition gtool.cpp:179
#define ERROR(...)
Definition messenger.hpp:90
#define FULLDEBUG(...)
#define NOTE(...)
#define DEBUG(...)
#define INFO(...)
Definition messenger.hpp:98
std::vector< T > object(tref const &item)
void write(gtool::t_graph const &, std::string const &)
Definition gtool.cpp:202
void write_to_cout(gtool::t_graph const &)
Definition gtool.cpp:212
void write_to_file(gtool::t_graph const &, std::string const &)
Definition gtool.cpp:219
Include QT Headers.
inner::configobject::tref tref
Definition tref.hpp:36
dbinfo
Definition dbinfo.hpp:19