DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
relationship.cpp
Go to the documentation of this file.
1// DUNE DAQ modification notice:
2// This file has been modified from the original ATLAS oks source for the DUNE DAQ project.
3// Fork baseline commit: oks-08-03-04 (2022-04-14).
4// Renamed since fork: no.
5
6#define _OksBuildDll_
7
9#include "oks/class.hpp"
10#include "oks/cstring.hpp"
11#include "oks/kernel.hpp"
12#include "oks/xml.hpp"
13
14#include <sstream>
15
16namespace dunedaq {
17namespace oks {
18
19const char OksRelationship::relationship_xml_tag[] = "relationship";
20const char OksRelationship::name_xml_attr[] = "name";
21const char OksRelationship::description_xml_attr[] = "description";
22const char OksRelationship::class_type_xml_attr[] = "class-type";
23const char OksRelationship::low_cc_xml_attr[] = "low-cc";
24const char OksRelationship::high_cc_xml_attr[] = "high-cc";
25const char OksRelationship::is_composite_xml_attr[] = "is-composite";
26const char OksRelationship::is_exclusive_xml_attr[] = "is-exclusive";
27const char OksRelationship::is_dependent_xml_attr[] = "is-dependent";
28const char OksRelationship::ordered_xml_attr[] = "ordered";
29
31OksRelationship::str2card(const char* s) noexcept
32{
33 return (oks::cmp_str3(s, "one") ? One : oks::cmp_str4(s, "zero") ? Zero : Many);
34}
35
36const char*
38{
39 return (cc == Zero ? "zero" : cc == One ? "one" : "many");
40}
41
43 : p_name(nm)
44 , p_low_cc(Zero)
46 , p_composite(false)
47 , p_exclusive(false)
48 , p_dependent(false)
49 , p_class(p)
50 , p_class_type(nullptr)
51 , p_ordered(false)
52{
53 oks::validate_not_empty(p_name, "relationship name");
54}
55
57 const std::string& rc,
60 bool cm,
61 bool excl,
62 bool dp,
63 const std::string& desc,
64 OksClass* p)
65 : p_name(nm)
66 , p_rclass(rc)
67 , p_low_cc(l_cc)
68 , p_high_cc(h_cc)
69 , p_composite(cm)
70 , p_exclusive(excl)
71 , p_dependent(dp)
72 , p_description(desc)
73 , p_class(p)
74 , p_class_type(nullptr)
75 , p_ordered(false)
76{
77 oks::validate_not_empty(p_name, "relationship name");
78 oks::validate_not_empty(p_rclass, "relationship class");
79}
80
81bool
83{
84 return ((this == &r) ||
85 ((p_name == r.p_name) && (p_rclass == r.p_rclass) && (p_low_cc == r.p_low_cc) && (p_high_cc == r.p_high_cc) &&
86 (p_composite == r.p_composite) && (p_exclusive == r.p_exclusive) && (p_dependent == r.p_dependent) &&
87 (p_description == r.p_description) && (p_ordered == r.p_ordered)));
88}
89
90std::ostream&
91operator<<(std::ostream& s, const OksRelationship& r)
92{
93 s << "Relationship name: \"" << r.p_name
94 << "\"\n"
95 " class type: \""
96 << r.p_rclass
97 << "\"\n"
98 " low cardinality constraint is "
99 << OksRelationship::card2str(r.p_low_cc)
100 << "\n"
101 " high cardinality constraint is "
102 << OksRelationship::card2str(r.p_high_cc)
103 << "\n"
104 " is"
105 << (r.p_composite == true ? "" : " not")
106 << " composite reference\n"
107 " is"
108 << (r.p_exclusive == true ? "" : " not")
109 << " exclusive reference\n"
110 " is "
111 << (r.p_dependent == true ? "dependent" : "shared")
112 << " reference\n"
113 " has description: \""
114 << r.p_description
115 << "\"\n"
116 " is "
117 << (r.p_ordered == true ? "ordered" : "unordered") << std::endl;
118
119 return s;
120}
121
122void
124{
125 s.put(" ");
126
127 s.put_start_tag(relationship_xml_tag, sizeof(relationship_xml_tag) - 1);
128
129 s.put_attribute(name_xml_attr, sizeof(name_xml_attr) - 1, p_name.c_str());
130
131 if (!p_description.empty())
132 s.put_attribute(description_xml_attr, sizeof(description_xml_attr) - 1, p_description.c_str());
133
134 s.put_attribute(class_type_xml_attr, sizeof(class_type_xml_attr) - 1, p_rclass.c_str());
135 s.put_attribute(low_cc_xml_attr, sizeof(low_cc_xml_attr) - 1, card2str(p_low_cc));
136 s.put_attribute(high_cc_xml_attr, sizeof(high_cc_xml_attr) - 1, card2str(p_high_cc));
140
141 if (p_ordered)
142 s.put_attribute(ordered_xml_attr, sizeof(ordered_xml_attr) - 1, oks::xml::bool2str(p_ordered));
143
144 s.put_end_tag();
145}
146
148 : p_low_cc(Zero)
149 , p_high_cc(Many)
150 , p_composite(false)
151 , p_exclusive(false)
152 , p_dependent(false)
153 , p_class(parent)
154 , p_class_type(nullptr)
155 , p_ordered(false)
156{
157 try {
158 while (true) {
159 OksXmlAttribute attr(s);
160
161 // check for close of tag
162
163 if (oks::cmp_str1(attr.name(), "/")) {
164 break;
165 }
166
167 // check for known oks-relationship' attributes
168
169 else if (oks::cmp_str4(attr.name(), name_xml_attr))
170 p_name.assign(attr.value(), attr.value_len());
171 else if (oks::cmp_str11(attr.name(), description_xml_attr))
172 p_description.assign(attr.value(), attr.value_len());
173 else if (oks::cmp_str10(attr.name(), class_type_xml_attr))
174 p_rclass.assign(attr.value(), attr.value_len());
175 else if (oks::cmp_str6(attr.name(), low_cc_xml_attr))
176 p_low_cc = str2card(attr.value());
177 else if (oks::cmp_str7(attr.name(), high_cc_xml_attr))
178 p_high_cc = str2card(attr.value());
179 else if (oks::cmp_str12(attr.name(), is_composite_xml_attr))
181 else if (oks::cmp_str12(attr.name(), is_exclusive_xml_attr))
183 else if (oks::cmp_str12(attr.name(), is_dependent_xml_attr))
185 else if (oks::cmp_str7(attr.name(), ordered_xml_attr))
187 else if (!strcmp(attr.name(), "multi-value-implementation")) {
188 s.error_msg("OksRelationship::OksRelationship(OksXmlInputStream&)")
189 << "Obsolete oks-relationship\'s attribute \'" << attr.name() << "\'\n";
190 } else {
191 s.throw_unexpected_attribute(attr.name());
192 }
193 }
194 } catch (oks::exception& e) {
195 throw oks::FailedRead("xml attribute", e);
196 } catch (std::exception& e) {
197 throw oks::FailedRead("xml attribute", e.what());
198 }
199
200 // check validity of read values
201
202 try {
203 oks::validate_not_empty(p_name, "relationship name");
204 oks::validate_not_empty(p_rclass, "relationship class");
205 } catch (std::exception& ex) {
206 throw oks::FailedRead("oks relationship", oks::BadFileData(ex.what(), s.get_line_no(), s.get_line_pos()));
207 }
208}
209
210void
211OksRelationship::set_name(const std::string& new_name)
212{
213 // ignore when name is the same
214
215 if (p_name == new_name)
216 return;
217
218 // additional checks are required,
219 // if the relationship already belongs to some class
220
221 if (p_class) {
222
223 // check maximum allowed length for relationship name
224
225 try {
226 oks::validate_not_empty(new_name, "name");
227 } catch (std::exception& ex) {
228 throw oks::SetOperationFailed("OksRelationship::set_name", ex.what());
229 }
230
231 // having a direct relationship with the same name is an error
232
233 if (p_class->find_direct_relationship(new_name) != 0) {
234 std::ostringstream text;
235 text << "Class \"" << p_class->get_name() << "\" already has direct relationship \"" << new_name << '\"';
236 throw oks::SetOperationFailed("OksRelationship::set_name", text.str());
237 }
238
239 // check possibility to lock the file
240
241 p_class->lock_file("OksRelationship::set_name");
242
243 // probably a non-direct relationship already exists
244
246
247 // change the name
248
249 p_name = new_name;
250
251 // registrate the change
252
254 } else {
255 p_name = new_name;
256 }
257}
258
259void
260OksRelationship::set_type(const std::string& cn)
261{
262 if (p_rclass == cn)
263 return;
264
265 if (!p_class || !p_class->p_kernel || (p_class_type = p_class->p_kernel->find_class(cn)) != 0) {
266 if (p_class)
267 p_class->lock_file("OksRelationship::set_type");
268
269 p_rclass = cn;
270
271 if (p_class) {
274 }
275 } else {
276 std::ostringstream text;
277 text << "cannot find class \"" << cn << '\"';
278 throw oks::SetOperationFailed("OksRelationship::set_type", text.str());
279 }
280}
281
282void
283OksRelationship::set_description(const std::string& desc)
284{
285 if (p_description != desc) {
286 if (p_class)
287 p_class->lock_file("OksRelationship::set_description");
288
289 p_description = desc;
290
291 if (p_class)
293 }
294}
295
296void
298{
299 if (p_low_cc != l_cc) {
300 if (p_class)
301 p_class->lock_file("OksRelationship::set_low_cardinality_constraint");
302
304
305 p_low_cc = l_cc;
306
307 if (p_class) {
309
310 if (p_low_cc == Many || old_cc == Many)
312 }
313 }
314}
315
316void
318{
319 if (p_high_cc != h_cc) {
320 if (p_class)
321 p_class->lock_file("OksRelationship::set_high_cardinality_constraint");
322
324
325 p_high_cc = h_cc;
326
327 if (p_class) {
329
330 if (p_high_cc == Many || old_cc == Many)
332 }
333 }
334}
335
336void
338{
339 if (p_composite != cm) {
340 if (p_class)
341 p_class->lock_file("OksRelationship::set_is_composite");
342
343 p_composite = cm;
344
345 if (p_class)
347 }
348}
349
350void
352{
353 if (p_exclusive != ex) {
354 if (p_class)
355 p_class->lock_file("OksRelationship::set_is_exclusive");
356
357 p_exclusive = ex;
358
359 if (p_class)
361 }
362}
363
364void
366{
367 if (p_dependent != dp) {
368 if (p_class)
369 p_class->lock_file("OksRelationship::set_is_dependent");
370
371 p_dependent = dp;
372
373 if (p_class)
375 }
376}
377
378} // namespace oks
379} // namespace dunedaq
OksKernel * p_kernel
Definition class.hpp:900
OksRelationship * find_direct_relationship(const std::string &name) const noexcept
Find direct relationship.
Definition class.cpp:1130
OksRelationship * find_relationship(const std::string &name) const noexcept
Find relationship (search in this and base classes).
Definition class.cpp:1142
void registrate_class_change(ChangeType, const void *, bool=true)
Definition class.cpp:2009
void registrate_relationship_change(OksRelationship *)
Definition class.cpp:1840
const std::string & get_name() const noexcept
Definition class.hpp:374
void lock_file(const char *)
Definition class.cpp:733
OksClass * find_class(const std::string &class_name) const
Find class by name (C++ string).
Definition kernel.hpp:1754
void set_type(const std::string &type)
Set relationship type.
static const char class_type_xml_attr[]
friend std::ostream & operator<<(std::ostream &, const OksRelationship &)
void set_is_exclusive(bool exclusive)
Set the composite relationship exclusive property.
static const char ordered_xml_attr[]
static const char low_cc_xml_attr[]
static const char relationship_xml_tag[]
private method to save in XML stream
CardinalityConstraint p_high_cc
static const char description_xml_attr[]
static const char is_composite_xml_attr[]
void set_is_dependent(bool dependent)
Set the composite relationship dependent property.
void set_is_composite(bool composite)
Set the composite relationship property.
void set_high_cardinality_constraint(CardinalityConstraint)
Set relationship high cardinality constraint.
OksRelationship(const std::string &name, OksClass *p=nullptr)
OKS relationship simple constructor.
static const char is_exclusive_xml_attr[]
void save(OksXmlOutputStream &) const
private constructor from XML stream
void set_low_cardinality_constraint(CardinalityConstraint cc)
Set relationship low cardinality constraint.
static const char name_xml_attr[]
static const char * card2str(CardinalityConstraint) noexcept
void set_description(const std::string &description)
Set relationship description.
static const char high_cc_xml_attr[]
bool operator==(const class OksRelationship &) const
CardinalityConstraint p_low_cc
void set_name(const std::string &)
Set relationship name.
static const char is_dependent_xml_attr[]
static CardinalityConstraint str2card(const char *) noexcept
virtual const char * what() const noexcept
const char * bool2str(bool b) noexcept
Definition xml.hpp:29
bool str2bool(const char *s) noexcept
Definition xml.hpp:36
bool cmp_str1(const char *s1, const char s2[2])
Definition cstring.hpp:15
bool cmp_str4(const char *s1, const char s2[5])
Definition cstring.hpp:45
bool cmp_str6(const char *s1, const char s2[7])
Definition cstring.hpp:69
bool cmp_str10(const char *s1, const char s2[11])
Definition cstring.hpp:111
void validate_not_empty(const std::string &value, const char *name)
bool cmp_str11(const char *s1, const char s2[12])
Definition cstring.hpp:117
bool cmp_str3(const char *s1, const char s2[4])
Definition cstring.hpp:33
bool cmp_str12(const char *s1, const char s2[13])
Definition cstring.hpp:123
bool cmp_str7(const char *s1, const char s2[8])
Definition cstring.hpp:81
The DUNE-DAQ namespace.
Definition ral.hpp:59
size_t value_len() const
Definition xml.hpp:288