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