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