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

Generated by: LCOV version 2.0-1