Line data Source code
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"
16 : #include "dbe/config_api_info.hpp"
17 : #include "dbe/config_reference.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 :
29 : namespace dbe {
30 : namespace tool {
31 : namespace graph {
32 :
33 : //------------------------------------------------------------------------------------------
34 0 : gtool::gtool(std::string const& rl, dbinfo dbtype)
35 0 : : this_src(rl)
36 : {
37 0 : QFileInfo DatabaseFile(QString::fromStdString(rl));
38 :
39 0 : if (DatabaseFile.exists()) {
40 0 : QString path_to_database = QString(DatabaseFile.absoluteFilePath());
41 0 : {
42 0 : confaccessor::setdbinfo(path_to_database, dbtype);
43 0 : INFO("Database location set", "Program execution control success");
44 :
45 0 : if (confaccessor::load()) {
46 0 : INFO("Database initialized", "User request");
47 : } else {
48 0 : ERROR("Could not load database", "OKS error ", " Actions : check environment variables e.g. DUNEDAQ_DB_PATH");
49 0 : throw std::string("Could not load database");
50 : }
51 : }
52 0 : } else {
53 0 : ERROR("Cannot open database", "File error", "for file", rl);
54 : }
55 0 : }
56 :
57 : gtool::t_graph const&
58 0 : gtool::getgraph() const
59 : {
60 0 : return this_graph;
61 : }
62 :
63 : void
64 0 : gtool::load_all()
65 : {
66 0 : INFO("Start load objects into internal cache", "Program execution control");
67 :
68 0 : std::vector<std::string> all_classes = { config::api::info::onclass::allnames<std::vector<std::string>>() };
69 :
70 0 : for (auto const& x : all_classes) {
71 0 : load_all_class_objects(x);
72 : }
73 :
74 0 : INFO("All objects loaded into internal cache",
75 : "Program execution control success",
76 : "#objects:",
77 0 : std::to_string(this_all_objects.size()));
78 0 : }
79 :
80 : void
81 0 : gtool::load_all_class_objects(std::string const& cname)
82 : {
83 0 : std::vector<dbe::tref> all_objects = config::api::info::onclass::objects<false>(cname, false);
84 :
85 0 : INFO("Class to load: ", "Program execution control", cname);
86 :
87 0 : for (dbe::tref const& x : all_objects) {
88 0 : this_all_objects.push_back(x);
89 : }
90 :
91 0 : INFO("Class load success: ", "Program execution control", cname, "#objects", std::to_string(all_objects.size()));
92 0 : }
93 :
94 : void
95 0 : gtool::create_graph()
96 : {
97 0 : INFO("Database graph generation initiated", "Program execution control");
98 0 : already_processed.clear();
99 :
100 0 : for (auto const& x : this_all_objects) {
101 0 : add_object_and_friends(this_graph, x, already_processed);
102 : }
103 :
104 0 : INFO("Graph generation completed successfully", "Program execution control success");
105 0 : }
106 :
107 : gtool::t_vertex
108 0 : gtool::add_object(gtool::t_graph& g, tref const& x, bool uniqueness)
109 : {
110 0 : vertex_label xl{ x.UID(), x.class_name(), x.full_name() };
111 :
112 0 : if (not uniqueness) {
113 : // Multiple vertices with the same properties can be added to this graph
114 0 : return boost::add_vertex(xl, g);
115 : } else {
116 0 : bool result;
117 0 : boost::graph_traits<t_graph>::vertex_iterator viter;
118 0 : std::tie(result, viter) = lookup(g, xl);
119 :
120 0 : if (result) {
121 0 : return *viter;
122 : } else {
123 0 : return boost::add_vertex(xl, g);
124 : }
125 : }
126 0 : }
127 :
128 : gtool::t_vertex
129 0 : gtool::add_object(gtool::t_graph& g, tref const& x, t_registry& registry)
130 : {
131 0 : t_registry::iterator vat = registry.find(x.full_name());
132 :
133 0 : if (std::end(registry) == vat) {
134 0 : 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 0 : t_registry::iterator v;
137 0 : std::tie(v, std::ignore) = registry.emplace(x.full_name(), add_object(g, x, false));
138 0 : return v->second;
139 : } else {
140 0 : return vat->second;
141 : }
142 : }
143 :
144 : gtool::t_vertex
145 0 : gtool::add_object_and_friends(t_graph& g, tref const& o, t_registry& registry)
146 : {
147 0 : DEBUG("Processing object", "Program execution control", o.full_name());
148 0 : t_vertex ov = add_object(g, o, registry);
149 :
150 0 : std::vector<tref> friends{ config::api::graph::linked::by::object<tref>(o) }; // get all neighbors
151 : DEBUG(
152 0 : "Processing object", "Program execution control", o.full_name(), " # friends: ", std::to_string(friends.size()));
153 :
154 0 : for (tref const& x : friends) {
155 0 : t_vertex xv = add_object(g, x, registry);
156 0 : boost::add_edge(ov, xv, g);
157 0 : FULLDEBUG("Add edge ", "Program excecution control", o.full_name(), "->", x.full_name());
158 : }
159 :
160 0 : return ov;
161 0 : }
162 :
163 : gtool::t_lookup_ret
164 0 : gtool::lookup(t_graph const& g, vertex_label const& l)
165 : {
166 0 : auto const& vertices = boost::vertices(g);
167 :
168 0 : for (auto v = vertices.first; v != vertices.second; ++v) {
169 0 : if (l.label == boost::get(&vertex_label::label, g, *v)) {
170 0 : return t_lookup_ret(true, v);
171 : }
172 : }
173 :
174 0 : return t_lookup_ret(false, vertices.second);
175 : }
176 : //------------------------------------------------------------------------------------------
177 :
178 : //------------------------------------------------------------------------------------------
179 0 : writegraph::writegraph(std::string const& s)
180 0 : : this_dest(s)
181 : {
182 0 : }
183 :
184 : int
185 0 : writegraph::operator()(gtool const& x) const
186 : {
187 0 : INFO("Saving result", "Program execution control", this_dest);
188 0 : write(x.getgraph());
189 0 : INFO("Result sent to output", "Program execution control success", this_dest);
190 0 : return EXIT_SUCCESS;
191 : }
192 :
193 : void
194 0 : writegraph::write(gtool::t_graph const& g) const
195 : {
196 0 : graph::write(g, this_dest);
197 0 : }
198 : //------------------------------------------------------------------------------------------
199 :
200 : //------------------------------------------------------------------------------------------
201 : void
202 0 : write(gtool::t_graph const& g, std::string const& ofn)
203 : {
204 0 : if (ofn.empty()) {
205 0 : write_to_cout(g);
206 : } else {
207 0 : write_to_file(g, ofn);
208 : }
209 0 : }
210 :
211 : void
212 0 : write_to_cout(gtool::t_graph const& g)
213 : {
214 0 : INFO("Sending output to stdout", "Program execution control")
215 0 : boost::write_graphviz(std::cout, g, boost::make_label_writer(boost::get(>ool::vertex_label::label, g)));
216 0 : }
217 :
218 : void
219 0 : write_to_file(gtool::t_graph const& g, std::string const& ofn)
220 : {
221 0 : std::ofstream of;
222 0 : of.open(ofn);
223 :
224 0 : if (of.is_open()) {
225 0 : INFO("Sending output to file", "Program execution control", "File name:", ofn);
226 0 : boost::write_graphviz(of, g, boost::make_label_writer(boost::get(>ool::vertex_label::label, g)));
227 :
228 0 : of.close();
229 :
230 0 : if (of.fail()) {
231 0 : ERROR("Could not close file", "Program execution control", "File name:", ofn);
232 : } else {
233 0 : NOTE("Output written to file", "Program execution control success", "File name:", ofn);
234 : }
235 : } else {
236 0 : ERROR("Output could not be written file", "Stream could not be opened", "File name:", ofn);
237 0 : write_to_cout(g);
238 : }
239 0 : }
240 : //------------------------------------------------------------------------------------------
241 :
242 : } /* namespace graph */
243 : } /* namespace tool */
244 : } /* namespace dbe */
|