Line data Source code
1 : /**
2 : * @file dal_methods.cpp
3 : *
4 : * This is part of the DUNE DAQ Software Suite, copyright 2020.
5 : * Licensing/copyright details are in the COPYING file that you should have
6 : * received with this code.
7 : */
8 :
9 : #include "pybind11/operators.h"
10 : #include "pybind11/pybind11.h"
11 : #include "pybind11/stl.h"
12 :
13 : #include "confmodel/Application.hpp"
14 : #include "confmodel/DaqApplication.hpp"
15 : #include "confmodel/DetDataReceiver.hpp"
16 : #include "confmodel/DetDataSender.hpp"
17 : #include "confmodel/DetectorStream.hpp"
18 : #include "confmodel/DetectorToDaqConnection.hpp"
19 : #include "confmodel/HostComponent.hpp"
20 : #include "confmodel/RCApplication.hpp"
21 : #include "confmodel/Session.hpp"
22 :
23 : #include <sstream>
24 :
25 : namespace py = pybind11;
26 : using namespace dunedaq::conffwk;
27 :
28 : namespace dunedaq::confmodel::python {
29 :
30 : struct ObjectLocator
31 : {
32 0 : ObjectLocator(const std::string& id_arg, const std::string& class_name_arg)
33 0 : : id(id_arg)
34 0 : , class_name(class_name_arg)
35 : {
36 0 : }
37 : const std::string id;
38 : const std::string class_name;
39 : };
40 :
41 : std::vector<ObjectLocator>
42 0 : session_get_all_applications(Configuration& db, const std::string& session_name)
43 : {
44 0 : auto session = db.get<Session>(session_name);
45 0 : std::vector<ObjectLocator> apps;
46 0 : for (auto app : session->all_applications()) {
47 0 : apps.push_back({ app->UID(), app->class_name() });
48 0 : }
49 0 : return apps;
50 0 : }
51 :
52 : std::vector<ObjectLocator>
53 0 : session_get_included_applications(Configuration& db, const std::string& session_name)
54 : {
55 0 : auto session = db.get<Session>(session_name);
56 0 : std::vector<ObjectLocator> apps;
57 0 : for (auto app : session->included_applications()) {
58 0 : apps.push_back({ app->UID(), app->class_name() });
59 0 : }
60 0 : return apps;
61 0 : }
62 :
63 : void
64 0 : exclude_entity(Configuration& db, const std::string& session_id, const std::string& component_id)
65 : {
66 0 : auto session_ptr = const_cast<dunedaq::confmodel::Session*>(db.get<dunedaq::confmodel::Session>(session_id));
67 0 : auto component_ptr = db.get<dunedaq::confmodel::ExcludableEntity>(component_id);
68 0 : if (session_ptr == nullptr) {
69 0 : throw(std::runtime_error(std::format("Session {} not found", session_id)));
70 : }
71 0 : if (component_ptr == nullptr) {
72 0 : throw(std::runtime_error(std::format("Component {} not found", component_id)));
73 : }
74 0 : session_ptr->exclude(component_ptr);
75 0 : }
76 : void
77 0 : include_entity(Configuration& db, const std::string& session_id, const std::string& component_id)
78 : {
79 0 : auto session_ptr = const_cast<dunedaq::confmodel::Session*>(db.get<dunedaq::confmodel::Session>(session_id));
80 0 : auto component_ptr = db.get<dunedaq::confmodel::ExcludableEntity>(component_id);
81 0 : if (session_ptr == nullptr) {
82 0 : throw(std::runtime_error(std::format("Session {} not found", session_id)));
83 : }
84 0 : if (component_ptr == nullptr) {
85 0 : throw(std::runtime_error(std::format("Component {} not found", component_id)));
86 : }
87 0 : session_ptr->include(component_ptr);
88 0 : }
89 :
90 : bool
91 0 : entity_excluded(Configuration& db, const std::string& session_id, const std::string& component_id)
92 : {
93 0 : const dunedaq::confmodel::Session* session_ptr = db.get<dunedaq::confmodel::Session>(session_id);
94 0 : const dunedaq::confmodel::ExcludableEntity* component_ptr =
95 0 : db.get<dunedaq::confmodel::ExcludableEntity>(component_id);
96 0 : if (component_ptr == nullptr) {
97 : return false;
98 : }
99 0 : return component_ptr->is_excluded(*session_ptr);
100 : }
101 :
102 : std::vector<std::vector<ObjectLocator>>
103 0 : entity_get_parents(Configuration& db, const std::string& session_id, const std::string& component_id)
104 : {
105 0 : const dunedaq::confmodel::Session* session_ptr = db.get<dunedaq::confmodel::Session>(session_id);
106 0 : const dunedaq::confmodel::ExcludableEntity* component_ptr =
107 0 : db.get<dunedaq::confmodel::ExcludableEntity>(component_id);
108 :
109 0 : std::list<std::vector<const dunedaq::confmodel::ExcludableEntity*>> parents;
110 0 : std::vector<std::vector<ObjectLocator>> parent_ids;
111 :
112 0 : component_ptr->parents(*session_ptr, parents);
113 :
114 0 : for (const auto& parent : parents) {
115 0 : std::vector<ObjectLocator> parents_components;
116 0 : for (const auto& ancestor_component_ptr : parent) {
117 0 : parents_components.emplace_back(
118 0 : ObjectLocator(ancestor_component_ptr->UID(), ancestor_component_ptr->class_name()));
119 : }
120 0 : parent_ids.emplace_back(parents_components);
121 0 : }
122 0 : return parent_ids;
123 0 : }
124 :
125 : std::vector<std::string>
126 0 : daq_application_get_used_host_components(Configuration& db, const std::string& app_id)
127 : {
128 0 : auto app = db.get<dunedaq::confmodel::DaqApplication>(app_id);
129 0 : std::vector<std::string> resources;
130 0 : for (auto res : app->get_used_host_components()) {
131 0 : resources.push_back(res->UID());
132 0 : }
133 0 : return resources;
134 0 : }
135 :
136 : std::vector<std::string>
137 0 : daq_application_construct_commandline_parameters(Configuration& db,
138 : const std::string& session_id,
139 : const std::string& app_id)
140 : {
141 0 : const auto* app = db.get<dunedaq::confmodel::DaqApplication>(app_id);
142 0 : const auto* session = db.get<dunedaq::confmodel::Session>(session_id);
143 0 : return app->construct_commandline_parameters(db, session);
144 : }
145 :
146 : std::vector<std::string>
147 0 : rc_application_construct_commandline_parameters(Configuration& db,
148 : const std::string& session_id,
149 : const std::string& app_id)
150 : {
151 0 : const auto* app = db.get<dunedaq::confmodel::RCApplication>(app_id);
152 0 : const auto* session = db.get<dunedaq::confmodel::Session>(session_id);
153 0 : return app->construct_commandline_parameters(db, session);
154 : }
155 :
156 : std::string
157 0 : d2d_receiver(Configuration& db, const std::string& d2d_id)
158 : {
159 0 : const auto* d2d = db.get<dunedaq::confmodel::DetectorToDaqConnection>(d2d_id);
160 0 : if (d2d == nullptr) {
161 0 : return "";
162 : }
163 0 : return d2d->receiver()->UID();
164 : }
165 :
166 : std::vector<std::string>
167 0 : d2d_senders(Configuration& db, const std::string& d2d_id)
168 : {
169 0 : std::vector<std::string> senders;
170 0 : const auto* d2d = db.get<dunedaq::confmodel::DetectorToDaqConnection>(d2d_id);
171 0 : if (d2d != nullptr) {
172 0 : for (auto sender : d2d->senders()) {
173 0 : senders.push_back(sender->UID());
174 0 : }
175 : }
176 0 : return senders;
177 0 : }
178 :
179 : std::vector<std::string>
180 0 : d2d_streams(Configuration& db, const std::string& d2d_id)
181 : {
182 0 : std::vector<std::string> streams;
183 0 : const auto* d2d = db.get<dunedaq::confmodel::DetectorToDaqConnection>(d2d_id);
184 0 : if (d2d != nullptr) {
185 0 : for (auto stream : d2d->streams()) {
186 0 : streams.push_back(stream->UID());
187 0 : }
188 : }
189 0 : return streams;
190 0 : }
191 :
192 : std::vector<std::string>
193 0 : exclude_entity_set_contains(Configuration& db, const std::string& res_id)
194 : {
195 0 : std::vector<std::string> resources;
196 0 : auto res_set = db.get<dunedaq::confmodel::ExcludableEntitySet>(res_id);
197 0 : if (res_set != nullptr) {
198 0 : for (auto res : res_set->contained_excludable_entities()) {
199 0 : resources.push_back(res->UID());
200 0 : }
201 : }
202 0 : return resources;
203 0 : }
204 :
205 : void
206 0 : register_dal_methods(py::module& m)
207 : {
208 0 : py::class_<ObjectLocator>(m, "ObjectLocator")
209 0 : .def(py::init<const std::string&, const std::string&>())
210 0 : .def_readonly("id", &ObjectLocator::id)
211 0 : .def_readonly("class_name", &ObjectLocator::class_name);
212 :
213 0 : m.def("session_get_all_applications",
214 0 : &session_get_all_applications,
215 : "Get list of ALL applications (regardless of included/excluded state) in the requested session");
216 0 : m.def("session_get_included_applications",
217 0 : &session_get_included_applications,
218 : "Get list of included applications in the requested session");
219 :
220 0 : m.def("exclude_entity", &exclude_entity, "Exclude a ExcludableEntity-derived object (e.g. a Segment)");
221 0 : m.def("include_entity", &include_entity, "Include a ExcludableEntity-derived object (e.g. a Segment)");
222 :
223 0 : m.def("entity_excluded",
224 0 : &entity_excluded,
225 : "Determine if a ExcludableEntity-derived object (e.g. a Segment) has been excluded");
226 0 : m.def("entity_get_parents",
227 0 : &entity_get_parents,
228 : "Get the ExcludableEntity-derived class instances of the parent(s) of the ExcludableEntity-derived object in "
229 : "question");
230 0 : m.def("daqapp_get_used_host_components",
231 0 : &daq_application_get_used_host_components,
232 : "Get list of HostExcludableEntitys used by DAQApplication");
233 0 : m.def("daq_application_construct_commandline_parameters",
234 0 : &daq_application_construct_commandline_parameters,
235 : "Get a version of the command line agruments parsed");
236 0 : m.def("rc_application_construct_commandline_parameters",
237 0 : &rc_application_construct_commandline_parameters,
238 : "Get a version of the command line agruments parsed");
239 :
240 0 : m.def("d2d_receiver", &d2d_receiver, "Get receiver associated with DetectorToDaqConnection");
241 0 : m.def("d2d_senders", &d2d_senders, "Get senders associated with DetectorToDaqConnection");
242 0 : m.def("d2d_streams", &d2d_streams, "Get streams associated with DetectorToDaqConnection");
243 0 : m.def("exclude_entity_set_contains",
244 0 : &exclude_entity_set_contains,
245 : "Get contained ExcludableEntitys from ExcludableEntitySet");
246 0 : }
247 :
248 : } // namespace dunedaq::confmodel::python
|