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