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