LCOV - code coverage report
Current view: top level - daqconf/apps - GraphBuilder.cpp (source / functions) Coverage Total Hit
Test: code.result Lines: 0.0 % 428 0
Test Date: 2026-07-12 15:23:06 Functions: 0.0 % 25 0

            Line data    Source code
       1              : /************************************************************
       2              :  *
       3              :  * GraphBuilder.cpp
       4              :  *
       5              :  * JCF, Sep-11-2024
       6              :  *
       7              :  * Implementation of GraphBuilder::construct_graph and GraphBuilder::write_graph
       8              :  *
       9              :  * This is part of the DUNE DAQ Application Framework, copyright 2020.
      10              :  * Licensing/copyright details are in the COPYING file that you should have
      11              :  * received with this code.
      12              :  *
      13              :  *************************************************************/
      14              : 
      15              : #include "GraphBuilder.hpp"
      16              : 
      17              : #include "appmodel/DFApplication.hpp"
      18              : #include "appmodel/DFOApplication.hpp"
      19              : #include "appmodel/MLTApplication.hpp"
      20              : #include "appmodel/ReadoutApplication.hpp"
      21              : #include "appmodel/SmartDaqApplication.hpp"
      22              : #include "appmodel/TPStreamWriterApplication.hpp"
      23              : #include "appmodel/TriggerApplication.hpp"
      24              : #include "appmodel/appmodelIssues.hpp"
      25              : 
      26              : #include "appmodel/DataHandlerModule.hpp"
      27              : #include "appmodel/DataReaderModule.hpp"
      28              : #include "appmodel/DataMoveCallbackConf.hpp"
      29              : 
      30              : #include "conffwk/Configuration.hpp"
      31              : #include "conffwk/Schema.hpp"
      32              : #include "confmodel/Connection.hpp"
      33              : #include "confmodel/NetworkConnection.hpp"
      34              : #include "confmodel/DaqModule.hpp"
      35              : #include "confmodel/Session.hpp"
      36              : #include "ers/ers.hpp"
      37              : 
      38              : #include "boost/graph/graphviz.hpp"
      39              : 
      40              : #include <algorithm>
      41              : #include <cassert>
      42              : #include <fstream>
      43              : #include <iostream>
      44              : #include <map>
      45              : #include <ranges>
      46              : #include <regex>
      47              : #include <sstream>
      48              : #include <string>
      49              : #include <unordered_map>
      50              : #include <vector>
      51              : 
      52              : namespace daqconf {
      53              : 
      54            0 : GraphBuilder::GraphBuilder(const std::string& oksfilename, const std::string& sessionname)
      55            0 :   : m_oksfilename{ oksfilename }
      56            0 :   , m_confdb{ nullptr }
      57            0 :   , m_included_classes{ { ObjectKind::kSession, { "Session", "Segment", "Application" } },
      58            0 :                         { ObjectKind::kSegment, { "Segment", "Application" } },
      59            0 :                         { ObjectKind::kApplication, { "Application", "Module" } },
      60            0 :                         { ObjectKind::kModule, { "Module" } } }
      61            0 :   , m_root_object_kind{ ObjectKind::kUndefined }
      62            0 :   , m_session{ nullptr }
      63            0 :   , m_session_name{ sessionname }
      64              : {
      65              : 
      66              :   // Open the database represented by the OKS XML file
      67              : 
      68            0 :   try {
      69            0 :     m_confdb = new dunedaq::conffwk::Configuration("oksconflibs:" + m_oksfilename);
      70            0 :   } catch (dunedaq::conffwk::Generic& exc) {
      71            0 :     TLOG() << "Failed to load OKS database: " << exc << "\n";
      72            0 :     throw exc;
      73            0 :   }
      74              : 
      75              :   // Get the session in the database
      76            0 :   std::vector<ConfigObject> session_objects{};
      77              : 
      78            0 :   m_confdb->get("Session", session_objects);
      79              : 
      80            0 :   if (m_session_name == "") { // If no session name given, use the one-and-only session expected in the database
      81            0 :     if (session_objects.size() == 1) {
      82            0 :       m_session_name = session_objects[0].UID();
      83              :     } else {
      84            0 :       std::stringstream errmsg;
      85            0 :       errmsg << "No Session instance name was provided, and since " << session_objects.size()
      86            0 :              << " session instances were found in \"" << m_oksfilename << "\" this is an error";
      87              : 
      88            0 :       throw daqconf::GeneralGraphToolError(ERS_HERE, errmsg.str());
      89            0 :     }
      90              :   } else { // session name provided by the user, let's make sure it's there
      91            0 :     auto it =
      92            0 :       std::ranges::find_if(session_objects, [&](const ConfigObject& obj) { return obj.UID() == m_session_name; });
      93              : 
      94            0 :     if (it == session_objects.end()) {
      95            0 :       std::stringstream errmsg;
      96            0 :       errmsg << "Did not find Session instance \"" << m_session_name << "\" in \"" << m_oksfilename
      97            0 :              << "\" and its includes";
      98            0 :       throw daqconf::GeneralGraphToolError(ERS_HERE, errmsg.str());
      99            0 :     }
     100              :   }
     101              : 
     102              :   // The following not-brief section of code is dedicated to
     103              :   // determining which applications in the configuration are
     104              :   // disabled
     105              : 
     106              :   // First, we need the session object to check if an application
     107              :   // has been disabled
     108              : 
     109              :   // Note the "const_cast" is needed since "m_confdb->get"
     110              :   // returns a const pointer, but since m_session is a member needed
     111              :   // by multiple functions and can't be determined until after we've
     112              :   // opened the database and found the session, we need to change
     113              :   // its initial value here. Once this is done, it shouldn't be
     114              :   // changed again.
     115              : 
     116            0 :   m_session = const_cast<dunedaq::confmodel::Session*>( // NOLINT
     117            0 :     m_confdb->get<dunedaq::confmodel::Session>(m_session_name));
     118              : 
     119            0 :   if (!m_session) {
     120            0 :     std::stringstream errmsg;
     121            0 :     errmsg << "Unable to get session with UID \"" << m_session_name << "\"";
     122            0 :     throw daqconf::GeneralGraphToolError(ERS_HERE, errmsg.str());
     123            0 :   }
     124              : 
     125            0 :   std::vector<ConfigObject> every_object_deriving_from_class{}; // Includes objects of the class itself
     126            0 :   std::vector<ConfigObject> objects_of_class{};                 // A subset of every_object_deriving_from_class
     127              : 
     128              :   // m_confdb->superclasses() returns a conffwk::fmap; see the conffwk package for details
     129              : 
     130            0 :   auto classnames = m_confdb->superclasses() | std::views::keys |
     131            0 :                     std::views::transform([](const auto& ptr_to_class_name) { return *ptr_to_class_name; });
     132              : 
     133            0 :   for (const auto& classname : classnames) {
     134              : 
     135            0 :     every_object_deriving_from_class.clear();
     136            0 :     objects_of_class.clear();
     137              : 
     138            0 :     m_confdb->get(classname, every_object_deriving_from_class);
     139              : 
     140            0 :     std::ranges::copy_if(every_object_deriving_from_class,
     141              :                          std::back_inserter(objects_of_class),
     142            0 :                          [&classname](const ConfigObject& obj) { return obj.class_name() == classname; });
     143              : 
     144            0 :     std::ranges::copy(objects_of_class, std::back_inserter(m_all_objects));
     145              : 
     146            0 :     if (classname.find("Application") != std::string::npos) { // DFApplication, ReadoutApplication, etc.
     147            0 :       for (const auto& appobj : objects_of_class) {
     148              : 
     149            0 :         auto daqapp = m_confdb->get<dunedaq::appmodel::SmartDaqApplication>(appobj.UID());
     150              : 
     151            0 :         if (daqapp) {
     152              : 
     153            0 :           auto res = daqapp->cast<dunedaq::confmodel::Resource>();
     154              : 
     155            0 :           if (res && res->is_disabled(*m_session)) {
     156            0 :             m_ignored_application_uids.push_back(appobj.UID());
     157            0 :             TLOG() << "Skipping disabled application " << appobj.UID() << "@" << daqapp->class_name();
     158            0 :             continue;
     159            0 :           }
     160              :         } else {
     161            0 :           TLOG(TLVL_DEBUG) << "Skipping non-SmartDaqApplication " << appobj.UID() << "@" << appobj.class_name();
     162            0 :           m_ignored_application_uids.push_back(appobj.UID());
     163              :         }
     164              :       }
     165              :     }
     166            0 :   }
     167            0 : }
     168              : 
     169              : void
     170            0 : GraphBuilder::find_candidate_objects()
     171              : {
     172              : 
     173            0 :   m_candidate_objects.clear();
     174              : 
     175            0 :   for (const auto& obj : m_all_objects) {
     176            0 :     for (const auto& classname : this->m_included_classes.at(m_root_object_kind)) {
     177              : 
     178            0 :       if (obj.class_name().find(classname) != std::string::npos &&
     179            0 :           std::ranges::find(m_ignored_application_uids, obj.UID()) == m_ignored_application_uids.end()) {
     180            0 :         m_candidate_objects.emplace_back(obj);
     181              :       }
     182              :     }
     183              :   }
     184            0 : }
     185              : 
     186              : void
     187            0 : GraphBuilder::calculate_graph(const std::string& root_obj_uid)
     188              : {
     189              : 
     190              :   // To start, get the session / segments / applications in the
     191              :   // session by setting up a temporary graph with the session as its
     192              :   // root. This way we can check to see if the actual requested root
     193              :   // object lies within the session in question.
     194              : 
     195            0 :   auto true_root_object_kind = m_root_object_kind;
     196            0 :   m_root_object_kind = ObjectKind::kSession;
     197            0 :   find_candidate_objects();
     198              : 
     199            0 :   auto it_session =
     200            0 :     std::ranges::find_if(m_all_objects, [&](const ConfigObject& obj) { return obj.UID() == m_session_name; });
     201              : 
     202            0 :   find_objects_and_connections(*it_session);
     203              : 
     204            0 :   if (!m_objects_for_graph.contains(root_obj_uid)) {
     205            0 :     std::stringstream errmsg;
     206            0 :     errmsg << "Unable to find requested object \"" << root_obj_uid << "\" in session \"" << m_session_name << "\"";
     207            0 :     throw daqconf::GeneralGraphToolError(ERS_HERE, errmsg.str());
     208            0 :   }
     209              : 
     210              :   // Since we used our first call to find_objects_and_connections
     211              :   // only as a fact-finding mission, reset the containers it filled
     212              : 
     213            0 :   m_objects_for_graph.clear();
     214            0 :   m_incoming_connections.clear();
     215            0 :   m_outgoing_connections.clear();
     216              : 
     217            0 :   m_candidate_objects.clear();
     218              : 
     219            0 :   m_root_object_kind = true_root_object_kind;
     220            0 :   find_candidate_objects();
     221              : 
     222            0 :   bool found = false;
     223            0 :   for (auto& obj : m_candidate_objects) {
     224            0 :     if (obj.UID() == root_obj_uid) {
     225            0 :       found = true;
     226            0 :       find_objects_and_connections(obj); // Put differently, "find what will make up the vertices and edges"
     227              :       break;
     228              :     }
     229              :   }
     230              : 
     231            0 :   assert(found);
     232              : 
     233            0 :   calculate_network_connections(); // Put differently, "find the edges between the vertices"
     234            0 : }
     235              : 
     236              : void
     237            0 : GraphBuilder::calculate_network_connections()
     238              : {
     239              : 
     240              :   // Will use "incoming_matched" and "outgoing_matched" to keep
     241              :   // track of incoming and outgoing connections which don't get
     242              :   // matched, i.e. would terminate external to the graph
     243              : 
     244            0 :   std::vector<std::string> incoming_matched;
     245            0 :   std::vector<std::string> outgoing_matched;
     246              : 
     247            0 :   for (auto& incoming : m_incoming_connections) {
     248              : 
     249            0 :     std::regex incoming_pattern(incoming.first);
     250              : 
     251            0 :     for (auto& outgoing : m_outgoing_connections) {
     252              : 
     253            0 :       std::regex outgoing_pattern(outgoing.first);
     254              : 
     255            0 :       bool match = false;
     256              : 
     257            0 :       if (incoming.first == outgoing.first) {
     258              :         match = true;
     259            0 :       } else if (incoming.first.find(".*") != std::string::npos) {
     260            0 :         if (std::regex_match(outgoing.first, incoming_pattern)) {
     261              :           match = true;
     262              :         }
     263            0 :       } else if (outgoing.first.find(".*") != std::string::npos) {
     264            0 :         if (std::regex_match(incoming.first, outgoing_pattern)) {
     265              :           match = true;
     266              :         }
     267              :       }
     268              : 
     269              :       if (match) {
     270              : 
     271            0 :         bool low_level_plot =
     272            0 :           m_root_object_kind == ObjectKind::kApplication || m_root_object_kind == ObjectKind::kModule;
     273              : 
     274            0 :         for (auto& receiver : incoming.second) {
     275            0 :           for (auto& sender : outgoing.second) {
     276              : 
     277              :             // We just want to plot applications sending to other
     278              :             // applications and queues sending to other
     279              :             // queues. Showing, e.g., a queue directly sending to
     280              :             // some other application via a network connection makes
     281              :             // the plot too busy.
     282              : 
     283            0 :             if (!m_objects_for_graph.contains(sender) || !m_objects_for_graph.contains(receiver)) {
     284            0 :               continue;
     285            0 :             } else if (m_objects_for_graph.at(sender).kind != m_objects_for_graph.at(receiver).kind) {
     286            0 :               continue;
     287            0 :             } else if (low_level_plot && incoming.first.find("NetworkConnection") != std::string::npos) {
     288              : 
     289              :               // Don't want to directly link modules in an
     290              :               // application if the data is transferred over network
     291            0 :               continue;
     292              :             }
     293              : 
     294            0 :             if (!low_level_plot || incoming.first.find(".*") == std::string::npos) {
     295            0 :               if (std::ranges::find(incoming_matched, incoming.first) == incoming_matched.end()) {
     296            0 :                 incoming_matched.push_back(incoming.first);
     297              :               }
     298              :             }
     299              : 
     300            0 :             if (!low_level_plot || outgoing.first.find(".*") == std::string::npos) {
     301            0 :               if (std::ranges::find(outgoing_matched, outgoing.first) == outgoing_matched.end()) {
     302            0 :                 outgoing_matched.push_back(outgoing.first);
     303              :               }
     304              :             }
     305              : 
     306            0 :             const EnhancedObject::ReceivingInfo receiving_info{ incoming.first, receiver };
     307              : 
     308            0 :             auto res = std::ranges::find(m_objects_for_graph.at(sender).receiving_object_infos, receiving_info);
     309            0 :             if (res == m_objects_for_graph.at(sender).receiving_object_infos.end()) {
     310            0 :               m_objects_for_graph.at(sender).receiving_object_infos.push_back(receiving_info);
     311              :             }
     312            0 :           }
     313              :         }
     314              :       }
     315            0 :     }
     316            0 :   }
     317              : 
     318            0 :   auto incoming_unmatched =
     319            0 :     m_incoming_connections | std::views::keys | std::views::filter([&incoming_matched](auto& connection) {
     320            0 :       return std::ranges::find(incoming_matched, connection) == incoming_matched.end();
     321            0 :     });
     322              : 
     323            0 :   auto included_classes = m_included_classes.at(m_root_object_kind);
     324              : 
     325            0 :   for (auto& incoming_conn : incoming_unmatched) {
     326              : 
     327            0 :     EnhancedObject external_obj{ ConfigObject{}, ObjectKind::kIncomingExternal };
     328            0 :     const std::string incoming_vertex_name = incoming_conn;
     329              : 
     330              :     // Find the connections appropriate to the granularity level of this graph
     331            0 :     for (auto& receiving_object_name : m_incoming_connections[incoming_conn]) {
     332              : 
     333            0 :       if (!m_objects_for_graph.contains(receiving_object_name)) {
     334            0 :         continue;
     335              :       }
     336              : 
     337            0 :       if (std::ranges::find(included_classes, "Module") != included_classes.end()) {
     338            0 :         if (m_objects_for_graph.at(receiving_object_name).kind == ObjectKind::kModule) {
     339            0 :           external_obj.receiving_object_infos.push_back({ incoming_conn, receiving_object_name });
     340              :         }
     341            0 :       } else if (std::ranges::find(included_classes, "Application") != included_classes.end()) {
     342            0 :         if (m_objects_for_graph.at(receiving_object_name).kind == ObjectKind::kApplication) {
     343            0 :           external_obj.receiving_object_infos.push_back({ incoming_conn, receiving_object_name });
     344              :         }
     345              :       }
     346              :     }
     347              : 
     348            0 :     m_objects_for_graph.insert({ incoming_vertex_name, external_obj });
     349            0 :   }
     350              : 
     351            0 :   auto outgoing_unmatched =
     352            0 :     m_outgoing_connections | std::views::keys | std::views::filter([&outgoing_matched](auto& connection) {
     353            0 :       return std::ranges::find(outgoing_matched, connection) == outgoing_matched.end();
     354            0 :     });
     355              : 
     356            0 :   for (auto& outgoing_conn : outgoing_unmatched) {
     357              : 
     358            0 :     EnhancedObject external_obj{ ConfigObject{}, ObjectKind::kOutgoingExternal };
     359            0 :     const std::string outgoing_vertex_name = outgoing_conn;
     360              : 
     361              :     // Find the connections appropriate to the granularity level of this graph
     362            0 :     for (auto& sending_object_name : m_outgoing_connections[outgoing_conn]) {
     363              : 
     364            0 :       if (!m_objects_for_graph.contains(sending_object_name)) {
     365            0 :         continue;
     366              :       }
     367              : 
     368            0 :       if (std::ranges::find(included_classes, "Module") != included_classes.end()) {
     369            0 :         if (m_objects_for_graph.at(sending_object_name).kind == ObjectKind::kModule) {
     370            0 :           m_objects_for_graph.at(sending_object_name)
     371            0 :             .receiving_object_infos.push_back({ outgoing_conn, outgoing_vertex_name });
     372              :         }
     373            0 :       } else if (std::ranges::find(included_classes, "Application") != included_classes.end()) {
     374            0 :         if (m_objects_for_graph.at(sending_object_name).kind == ObjectKind::kApplication) {
     375            0 :           m_objects_for_graph.at(sending_object_name)
     376            0 :             .receiving_object_infos.push_back({ outgoing_conn, outgoing_vertex_name });
     377              :         }
     378              :       }
     379              :     }
     380              : 
     381            0 :     m_objects_for_graph.insert({ outgoing_vertex_name, external_obj });
     382            0 :   }
     383            0 : }
     384              : 
     385              : void
     386            0 : GraphBuilder::find_objects_and_connections(const ConfigObject& object)
     387              : {
     388              : 
     389            0 :   EnhancedObject starting_object{ object, get_object_kind(object.class_name()) };
     390              : 
     391              :   // If we've got a session or a segment, look at its OKS-relations,
     392              :   // and recursively process those relation objects which are on the
     393              :   // candidates list and haven't already been processed
     394              : 
     395            0 :   if (starting_object.kind == ObjectKind::kSession || starting_object.kind == ObjectKind::kSegment) {
     396              : 
     397            0 :     for (auto& child_object : find_child_objects(starting_object.config_object)) {
     398              : 
     399            0 :       if (std::ranges::find(m_candidate_objects, child_object) != m_candidate_objects.end()) {
     400            0 :         find_objects_and_connections(child_object);
     401            0 :         starting_object.child_object_names.push_back(child_object.UID());
     402              :       }
     403            0 :     }
     404            0 :   } else if (starting_object.kind == ObjectKind::kApplication) {
     405              : 
     406              :     // If we've got an application object, try to determine what
     407              :     // modules are in it and what their connections are. Recursively
     408              :     // process the modules, and then add connection info to class-wide
     409              :     // member maps to calculate edges corresponding to the connections
     410              :     // for the plotted graph later
     411              : 
     412            0 :     dunedaq::conffwk::Configuration* local_database{ nullptr };
     413              : 
     414            0 :     try {
     415            0 :       local_database = new dunedaq::conffwk::Configuration("oksconflibs:" + m_oksfilename);
     416            0 :     } catch (dunedaq::conffwk::Generic& exc) {
     417            0 :       TLOG() << "Failed to load OKS database: " << exc << "\n";
     418            0 :       throw exc;
     419            0 :     }
     420              : 
     421            0 :     auto daqapp = local_database->get<dunedaq::appmodel::SmartDaqApplication>(object.UID());
     422            0 :     if (daqapp) {
     423            0 :       auto local_session = const_cast<dunedaq::confmodel::Session*>( // NOLINT
     424            0 :         local_database->get<dunedaq::confmodel::Session>(m_session_name));
     425              : 
     426            0 :       auto helper = std::make_shared<dunedaq::appmodel::ConfigurationHelper>(local_session);
     427            0 :       daqapp->generate_modules(helper);
     428            0 :       auto modules = daqapp->get_modules();
     429              : 
     430            0 :       std::vector<std::string> allowed_conns{};
     431              : 
     432            0 :       if (m_root_object_kind == ObjectKind::kSession || m_root_object_kind == ObjectKind::kSegment) {
     433            0 :         allowed_conns = { "NetworkConnection" };
     434            0 :       } else if (m_root_object_kind == ObjectKind::kApplication || m_root_object_kind == ObjectKind::kModule) {
     435            0 :         allowed_conns = { "NetworkConnection", "Queue", "QueueWithSourceId", "DataMoveCallbackConf" };
     436              :       }
     437              : 
     438            0 :       for (const auto& module : modules) {
     439              : 
     440            0 :         for (auto in : module->get_inputs()) {
     441              : 
     442              :           // Elsewhere in the code it'll be useful to know if the
     443              :           // connection is a network or a queue, so include the
     444              :           // class name in the std::string key
     445              : 
     446            0 :           std::string key = in->config_object().UID() + "@" + in->config_object().class_name();
     447              : 
     448            0 :           if (in->config_object().class_name() == "NetworkConnection") {
     449            0 :             auto innc = in->cast<dunedaq::confmodel::NetworkConnection>();
     450            0 :             key += "@" +  innc->get_connection_type();
     451              :           }
     452              : 
     453            0 :           if (std::ranges::find(allowed_conns, in->config_object().class_name()) != allowed_conns.end()) {
     454            0 :             m_incoming_connections[key].push_back(object.UID());
     455            0 :             m_incoming_connections[key].push_back(module->UID());
     456              :           }
     457            0 :         }
     458              : 
     459            0 :         for (auto out : module->get_outputs()) {
     460              : 
     461            0 :           std::string key = out->config_object().UID() + "@" + out->config_object().class_name();
     462              : 
     463            0 :           if (out->config_object().class_name() == "NetworkConnection") {
     464            0 :             auto outnc = out->cast<dunedaq::confmodel::NetworkConnection>();
     465            0 :             key += "@" + outnc->get_connection_type();
     466              :           }
     467              : 
     468            0 :           if (std::ranges::find(allowed_conns, out->config_object().class_name()) != allowed_conns.end()) {
     469            0 :             m_outgoing_connections[key].push_back(object.UID());
     470            0 :             m_outgoing_connections[key].push_back(module->UID());
     471              :           }
     472            0 :         }
     473              : 
     474              :         // Look for DataMoveCallbackConfs
     475            0 :         auto datareader = module->cast<dunedaq::appmodel::DataReaderModule>();
     476            0 :         auto datahandler = module->cast<dunedaq::appmodel::DataHandlerModule>();
     477              : 
     478            0 :         if (datareader != nullptr) {
     479            0 :           for (auto& out : datareader->get_raw_data_callbacks()) {
     480            0 :             const std::string key = out->config_object().UID() + "@" + out->config_object().class_name();
     481            0 :             if (std::ranges::find(allowed_conns, out->config_object().class_name()) != allowed_conns.end()) {
     482            0 :               m_outgoing_connections[key].push_back(object.UID());
     483            0 :               m_outgoing_connections[key].push_back(module->UID());
     484              :             }
     485            0 :           }
     486              :         }
     487            0 :         if (datahandler != nullptr) {
     488            0 :           auto in = datahandler->get_raw_data_callback();
     489            0 :           if (in != nullptr) {
     490            0 :             const std::string key = in->config_object().UID() + "@" + in->config_object().class_name();
     491              : 
     492            0 :             if (std::ranges::find(allowed_conns, in->config_object().class_name()) != allowed_conns.end()) {
     493            0 :               m_incoming_connections[key].push_back(object.UID());
     494            0 :               m_incoming_connections[key].push_back(module->UID());
     495              :             }
     496            0 :           }
     497              :         }
     498              : 
     499            0 :         if (std::ranges::find(m_included_classes.at(m_root_object_kind), "Module") !=
     500            0 :             m_included_classes.at(m_root_object_kind).end()) {
     501            0 :           find_objects_and_connections(module->config_object());
     502            0 :           starting_object.child_object_names.push_back(module->UID());
     503              :         }
     504              :       }
     505            0 :     }
     506              :   }
     507              : 
     508            0 :   assert(!m_objects_for_graph.contains(object.UID()));
     509              : 
     510            0 :   m_objects_for_graph.insert({ object.UID(), starting_object });
     511            0 : }
     512              : 
     513              : void
     514            0 : GraphBuilder::construct_graph(std::string root_obj_uid)
     515              : {
     516              : 
     517            0 :   if (root_obj_uid == "") {
     518            0 :     root_obj_uid = m_session_name;
     519              :   }
     520              : 
     521              :   // Next several lines just mean "tell me the class type of the root object in the config plot's graph"
     522              : 
     523            0 :   auto class_names_view =
     524            0 :     m_all_objects | std::views::filter([root_obj_uid](const ConfigObject& obj) { return obj.UID() == root_obj_uid; }) |
     525            0 :     std::views::transform([](const ConfigObject& obj) { return obj.class_name(); });
     526              : 
     527            0 :   if (std::ranges::distance(class_names_view) != 1) {
     528            0 :     std::stringstream errmsg;
     529            0 :     errmsg << "Failed to find instance of desired root object \"" << root_obj_uid << "\"";
     530            0 :     throw daqconf::GeneralGraphToolError(ERS_HERE, errmsg.str());
     531            0 :   }
     532              : 
     533            0 :   const std::string& root_obj_class_name = *class_names_view.begin();
     534              : 
     535            0 :   m_root_object_kind = get_object_kind(root_obj_class_name);
     536              : 
     537            0 :   calculate_graph(root_obj_uid);
     538              : 
     539            0 :   for (auto& enhanced_object : m_objects_for_graph | std::views::values) {
     540              : 
     541            0 :     if (enhanced_object.kind == ObjectKind::kIncomingExternal) {
     542            0 :       enhanced_object.vertex_in_graph = boost::add_vertex(VertexLabel("O", ""), m_graph);
     543            0 :     } else if (enhanced_object.kind == ObjectKind::kOutgoingExternal) {
     544            0 :       enhanced_object.vertex_in_graph = boost::add_vertex(VertexLabel("X", ""), m_graph);
     545              :     } else {
     546            0 :       auto& obj = enhanced_object.config_object;
     547            0 :       enhanced_object.vertex_in_graph = boost::add_vertex(VertexLabel(obj.UID(), obj.class_name()), m_graph);
     548              :     }
     549              :   }
     550              : 
     551            0 :   for (auto& parent_obj : m_objects_for_graph | std::views::values) {
     552            0 :     for (auto& child_obj_name : parent_obj.child_object_names) {
     553            0 :       boost::add_edge(parent_obj.vertex_in_graph,
     554            0 :                       m_objects_for_graph.at(child_obj_name).vertex_in_graph,
     555            0 :                       { "" }, // No label for an edge which just describes "ownership" rather than dataflow
     556              :                       m_graph);
     557              :     }
     558              :   }
     559              : 
     560            0 :   for (auto& possible_sender_object : m_objects_for_graph | std::views::values) {
     561            0 :     for (auto& receiver_info : possible_sender_object.receiving_object_infos) {
     562              : 
     563              :       // If we're plotting at the level of a session or segment,
     564              :       // show the connections as between applications; if we're
     565              :       // doing this for a single application, show them entering and
     566              :       // exiting the individual modules
     567              : 
     568            0 :       if (m_root_object_kind == ObjectKind::kSession || m_root_object_kind == ObjectKind::kSegment) {
     569            0 :         if (m_objects_for_graph.at(receiver_info.receiver_label).kind == ObjectKind::kModule) {
     570            0 :           continue;
     571              :         }
     572              :       }
     573              : 
     574            0 :       if (m_root_object_kind == ObjectKind::kApplication || m_root_object_kind == ObjectKind::kModule) {
     575            0 :         if (m_objects_for_graph.at(receiver_info.receiver_label).kind == ObjectKind::kApplication) {
     576            0 :           continue;
     577              :         }
     578              :       }
     579              : 
     580            0 :       boost::add_edge(possible_sender_object.vertex_in_graph,
     581            0 :                       m_objects_for_graph.at(receiver_info.receiver_label).vertex_in_graph,
     582            0 :                       { receiver_info.connection_name },
     583              :                       m_graph)
     584              :         .first;
     585              :     }
     586              :   }
     587            0 : }
     588              : 
     589              : std::vector<dunedaq::conffwk::ConfigObject>
     590            0 : GraphBuilder::find_child_objects(const ConfigObject& parent_obj)
     591              : {
     592              : 
     593            0 :   std::vector<ConfigObject> connected_objects{};
     594              : 
     595            0 :   dunedaq::conffwk::class_t classdef = m_confdb->get_class_info(parent_obj.class_name(), false);
     596              : 
     597            0 :   for (const dunedaq::conffwk::relationship_t& relationship : classdef.p_relationships) {
     598              : 
     599              :     // The ConfigObject::get(...) function doesn't have a
     600              :     // const-qualifier on it for no apparent good reason; we need
     601              :     // this cast in order to call it
     602              : 
     603            0 :     auto parent_obj_casted = const_cast<ConfigObject&>(parent_obj); // NOLINT
     604              : 
     605            0 :     if (relationship.p_cardinality == dunedaq::conffwk::only_one ||
     606              :         relationship.p_cardinality == dunedaq::conffwk::zero_or_one) {
     607            0 :       ConfigObject connected_obj{};
     608            0 :       parent_obj_casted.get(relationship.p_name, connected_obj);
     609            0 :       connected_objects.push_back(connected_obj);
     610            0 :     } else {
     611            0 :       std::vector<ConfigObject> connected_objects_in_relationship{};
     612            0 :       parent_obj_casted.get(relationship.p_name, connected_objects_in_relationship);
     613            0 :       connected_objects.insert(
     614            0 :         connected_objects.end(), connected_objects_in_relationship.begin(), connected_objects_in_relationship.end());
     615            0 :     }
     616            0 :   }
     617              : 
     618            0 :   return connected_objects;
     619            0 : }
     620              : 
     621              : void
     622            0 : GraphBuilder::write_graph(const std::string& outputfilename) const
     623              : {
     624              : 
     625            0 :   std::stringstream outputstream;
     626              : 
     627            0 :   boost::write_graphviz(outputstream,
     628              :                         m_graph,
     629              :                         boost::make_label_writer(boost::get(&GraphBuilder::VertexLabel::displaylabel, m_graph)),
     630            0 :                         boost::make_label_writer(boost::get(&GraphBuilder::EdgeLabel::displaylabel, m_graph)));
     631              : 
     632              :   // It's arguably hacky to edit the DOT code generated by
     633              :   // boost::write_graphviz after the fact to give vertices colors,
     634              :   // but the fact is that the color-assigning logic in Boost's graph
     635              :   // library is so messy and clumsy that this is a worthwhile
     636              :   // tradeoff
     637              : 
     638            0 :   struct VertexStyle
     639              :   {
     640              :     const std::string shape;
     641              :     const std::string color;
     642              :   };
     643              : 
     644            0 :   const std::unordered_map<ObjectKind, VertexStyle> vertex_styles{ { ObjectKind::kSession, { "octagon", "black" } },
     645            0 :                                                                    { ObjectKind::kSegment, { "hexagon", "brown" } },
     646            0 :                                                                    { ObjectKind::kApplication, { "pentagon", "blue" } },
     647            0 :                                                                    { ObjectKind::kModule, { "rectangle", "red" } } };
     648              : 
     649            0 :   std::string dotfile_slurped = outputstream.str();
     650            0 :   std::vector<std::string> legend_entries{
     651              :     "legendGA [label=<<font color=\"black\"><b><i>&#10230; Network Connection</i></b></font>>, shape=plaintext];",
     652              :     "legendGB [label=<<font color=\"blue\"><b><i>&#10230; Pub/Sub Network</i></b></font>>, shape=plaintext];"
     653            0 :   };
     654            0 :   std::vector<std::string> internal_legend_entries{
     655              :     "legendGC [label=<<font color=\"green\"><b><i>&#10230; Data Move Callback</i></b></font>>, shape=plaintext];",
     656              :     "legendGD [label=<<font color=\"red\"><b><i>&#10230; Queue</i></b></font>>, shape=plaintext];",
     657              :     "legendGE [label=<<font color=\"orange\"><b><i>&#10230; Queue w/ Source ID</i></b></font>>, shape=plaintext];"
     658            0 :   };
     659            0 :   bool internal_legend_added = false;
     660            0 :   std::vector<std::string> legend_ordering_code{};
     661              : 
     662            0 :   for (auto& eo : m_objects_for_graph | std::views::values) {
     663              : 
     664            0 :     std::stringstream vertexstr{};
     665            0 :     std::stringstream legendstr{};
     666            0 :     std::stringstream labelstringstr{};
     667            0 :     size_t insertion_location{ 0 };
     668              : 
     669            0 :     auto calculate_insertion_location = [&]() {
     670            0 :       labelstringstr << "label=\"" << eo.config_object.UID() << "\n";
     671            0 :       insertion_location = dotfile_slurped.find(labelstringstr.str());
     672            0 :       assert(insertion_location != std::string::npos);
     673            0 :       return insertion_location;
     674            0 :     };
     675              : 
     676              :     // TODO: John Freeman (jcfree@fnal.gov), Sep-17-2024
     677              : 
     678              :     // Switch to std::format for line construction rather than
     679              :     // std::stringstream when we switch to a gcc version which
     680              :     // supports it
     681              : 
     682            0 :     auto add_vertex_info = [&]() {
     683            0 :       vertexstr << "shape=" << vertex_styles.at(eo.kind).shape << ", color=" << vertex_styles.at(eo.kind).color
     684            0 :                 << ", fontcolor=" << vertex_styles.at(eo.kind).color << ", ";
     685            0 :       dotfile_slurped.insert(calculate_insertion_location(), vertexstr.str());
     686            0 :     };
     687              : 
     688            0 :     auto add_legend_entry = [&](char letter, const std::string objkind) {
     689            0 :       legendstr << "legend" << letter << " [label=<<font color=\"" << vertex_styles.at(eo.kind).color << "\"><b><i>"
     690            0 :                 << vertex_styles.at(eo.kind).color << ": " << objkind
     691            0 :                 << "</i></b></font>>, shape=plaintext, color=" << vertex_styles.at(eo.kind).color
     692            0 :                 << ", fontcolor=" << vertex_styles.at(eo.kind).color << "];";
     693            0 :     };
     694              : 
     695              :     // Note that the seemingly arbitrary single characters added
     696              :     // after "legend" aren't just to uniquely identify each entry in
     697              :     // the legend, it's also so that when the entries are sorted
     698              :     // alphabetically they'll appear in the correct order
     699              : 
     700            0 :     switch (eo.kind) {
     701            0 :       case ObjectKind::kSession:
     702            0 :         add_vertex_info();
     703            0 :         add_legend_entry('A', "session");
     704            0 :         break;
     705            0 :       case ObjectKind::kSegment:
     706            0 :         add_vertex_info();
     707            0 :         add_legend_entry('B', "segment");
     708            0 :         break;
     709            0 :       case ObjectKind::kApplication:
     710            0 :         add_vertex_info();
     711            0 :         add_legend_entry('C', "application");
     712            0 :         break;
     713            0 :       case ObjectKind::kModule:
     714            0 :         add_vertex_info();
     715            0 :         add_legend_entry('D', "DAQModule");
     716            0 :         if (!internal_legend_added) {
     717            0 :           legend_entries.insert(legend_entries.end(),
     718              :                                 internal_legend_entries.begin(),
     719              :                                 internal_legend_entries.end());
     720            0 :           internal_legend_added = true;
     721              :         }
     722              :         break;
     723            0 :       case ObjectKind::kIncomingExternal:
     724            0 :         legendstr
     725            0 :           << "legendE [label=<<font color=\"black\">O:<b><i> External Data Source</i></b></font>>, shape=plaintext];";
     726              :         break;
     727            0 :       case ObjectKind::kOutgoingExternal:
     728            0 :         legendstr
     729            0 :           << "legendF [label=<<font color=\"black\">X:<b><i> External Data Sink</i></b></font>>, shape=plaintext];";
     730              :         break;
     731            0 :       default:
     732            0 :         assert(false);
     733              :     }
     734              : 
     735            0 :     if (std::ranges::find(legend_entries, legendstr.str()) == legend_entries.end()) {
     736            0 :       legend_entries.emplace_back(legendstr.str());
     737              :     }
     738            0 :   }
     739              : 
     740            0 :   std::ranges::sort(legend_entries);
     741              : 
     742              :   // We have the line-by-line entries containing the labels in our
     743              :   // legend and their colors, but more DOT code is needed in order
     744              :   // to show the labels in order. E.g., if we have:
     745              : 
     746              :   // legendA [label=<<font color="blue">Blue: Segment</font>>, shape=box, color=blue, fontcolor=blue];
     747              :   // legendB [label=<<font color="red">Red: Application</font>>, shape=box, color=red, fontcolor=red];
     748              :   //
     749              :   // Then we'll also need
     750              :   //
     751              :   // legendA -> legendB [style=invis];
     752              : 
     753            0 :   auto legend_tokens = legend_entries | std::views::transform([](const std::string& line) {
     754            0 :                          return line.substr(0, line.find(' ')); // i.e., grab the first word on the line
     755            0 :                        });
     756              : 
     757            0 :   auto it = legend_tokens.begin();
     758            0 :   for (auto next_it = std::next(it); next_it != legend_tokens.end(); ++it, ++next_it) {
     759            0 :     std::stringstream astr{};
     760            0 :     astr << "        " << *it << " -> " << *next_it << " [style=invis];";
     761            0 :     legend_ordering_code.push_back(astr.str());
     762            0 :   }
     763              : 
     764            0 :   constexpr int chars_to_last_brace = 2;
     765            0 :   auto last_brace_iter = dotfile_slurped.end() - chars_to_last_brace;
     766            0 :   assert(*last_brace_iter == '}');
     767            0 :   size_t last_brace_loc = last_brace_iter - dotfile_slurped.begin();
     768              : 
     769            0 :   std::string legend_code{};
     770            0 :   legend_code += "\n\n\n";
     771              : 
     772            0 :   for (const auto& l : legend_entries) {
     773            0 :     legend_code += l + "\n";
     774              :   }
     775              : 
     776            0 :   legend_code += "\n\n\n";
     777              : 
     778            0 :   for (const auto& l : legend_ordering_code) {
     779            0 :     legend_code += l + "\n";
     780              :   }
     781              : 
     782            0 :   dotfile_slurped.insert(last_brace_loc, legend_code);
     783              : 
     784              :   // Take advantage of the fact that the edges describing ownership
     785              :   // rather than data flow (e.g., hsi-segment owning hsi-01, the
     786              :   // FakeHSIApplication) have null labels in order to turn them into
     787              :   // arrow-free dotted lines
     788              : 
     789            0 :   const std::string unlabeled_edge = "label=\"\"";
     790            0 :   const std::string edge_modifier = ", style=\"dotted\", arrowhead=\"none\"";
     791              : 
     792            0 :   size_t pos = 0;
     793            0 :   while ((pos = dotfile_slurped.find(unlabeled_edge, pos)) != std::string::npos) {
     794            0 :     dotfile_slurped.replace(pos, unlabeled_edge.length(), unlabeled_edge + edge_modifier);
     795            0 :     pos += (unlabeled_edge + edge_modifier).length();
     796              :   }
     797              : 
     798              :   // Replace the connection types with color information
     799            0 :   std::vector<std::pair<std::string, std::string>> connection_colors = { { "@NetworkConnection@kSendRecv\"", "\", color=black" },
     800              :                                                                          { "@NetworkConnection@kPubSub\"", "\", color=blue" },
     801              :                                                                          { "@QueueWithSourceId\"", "\", color=orange" },
     802              :                                                                          { "@Queue\"", "\", color=red" },
     803              :                                                                          { "@DataMoveCallbackConf\"",
     804            0 :                                                                            "\", color=green" } };
     805            0 :   for (auto& color_pair : connection_colors) {
     806            0 :     auto conn_type = color_pair.first;
     807            0 :     auto color_info = color_pair.second;
     808              :     pos = 0;
     809            0 :     while ((pos = dotfile_slurped.find(conn_type, pos)) != std::string::npos) {
     810            0 :       dotfile_slurped.replace(pos, conn_type.length(), color_info);
     811            0 :       pos += color_info.length();
     812              :     }
     813            0 :   }
     814              : 
     815              :   // And now with all the edits made to the contents of the DOT code, write it to file
     816              : 
     817            0 :   std::ofstream outputfile;
     818            0 :   outputfile.open(outputfilename);
     819              : 
     820            0 :   if (outputfile.is_open()) {
     821            0 :     outputfile << dotfile_slurped.c_str();
     822              :   } else {
     823            0 :     std::stringstream errmsg;
     824            0 :     errmsg << "Unable to open requested file \"" << outputfilename << "\" for writing";
     825            0 :     throw daqconf::GeneralGraphToolError(ERS_HERE, errmsg.str());
     826            0 :   }
     827            0 : }
     828              : 
     829              : constexpr GraphBuilder::ObjectKind
     830            0 : get_object_kind(const std::string& class_name)
     831              : {
     832              : 
     833            0 :   using ObjectKind = GraphBuilder::ObjectKind;
     834              : 
     835            0 :   ObjectKind kind = ObjectKind::kSession;
     836              : 
     837            0 :   if (class_name.find("Session") != std::string::npos) {
     838              :     kind = ObjectKind::kSession;
     839            0 :   } else if (class_name.find("Segment") != std::string::npos) {
     840              :     kind = ObjectKind::kSegment;
     841            0 :   } else if (class_name.find("Application") != std::string::npos) {
     842              :     kind = ObjectKind::kApplication;
     843            0 :   } else if (class_name.find("Module") != std::string::npos) {
     844              :     kind = ObjectKind::kModule;
     845              :   } else {
     846            0 :     throw daqconf::GeneralGraphToolError(
     847            0 :       ERS_HERE, "Unsupported class type \"" + std::string(class_name) + "\"passed to get_object_kind");
     848              :   }
     849              : 
     850            0 :   return kind;
     851              : }
     852              : 
     853              : } // namespace appmodel
        

Generated by: LCOV version 2.0-1