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/xml.hpp"
10#include "oks/class.hpp"
11#include "oks/kernel.hpp"
12#include "oks/cstring.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
30
32OksRelationship::str2card(const char * s) noexcept
33{
34 return (
35 oks::cmp_str3(s, "one") ? One :
36 oks::cmp_str4(s, "zero") ? Zero :
37 Many
38 );
39}
40
41const char *
43{
44 return (
45 cc == Zero ? "zero" :
46 cc == One ? "one" :
47 "many"
48 );
49}
50
51OksRelationship::OksRelationship(const std::string& nm, OksClass * p) :
52 p_name (nm),
53 p_low_cc (Zero),
55 p_composite (false),
56 p_exclusive (false),
57 p_dependent (false),
58 p_class (p),
59 p_class_type (nullptr),
60 p_ordered (false)
61{
62 oks::validate_not_empty(p_name, "relationship name");
63}
64
65OksRelationship::OksRelationship(const std::string& nm, const std::string& rc,
67 bool cm, bool excl, bool dp, const std::string& desc, OksClass * p) :
68 p_name (nm),
69 p_rclass (rc),
70 p_low_cc (l_cc),
71 p_high_cc (h_cc),
72 p_composite (cm),
73 p_exclusive (excl),
74 p_dependent (dp),
75 p_description (desc),
76 p_class (p),
77 p_class_type (nullptr),
78 p_ordered (false)
79{
80 oks::validate_not_empty(p_name, "relationship name");
81 oks::validate_not_empty(p_rclass, "relationship class");
82}
83
84bool
86{
87 return (
88 ( this == &r ) ||
89 (
90 ( p_name == r.p_name ) &&
91 ( p_rclass == r.p_rclass ) &&
92 ( p_low_cc == r.p_low_cc ) &&
93 ( p_high_cc == r.p_high_cc ) &&
94 ( p_composite == r.p_composite ) &&
95 ( p_exclusive == r.p_exclusive ) &&
96 ( p_dependent == r.p_dependent ) &&
97 ( p_description == r.p_description ) &&
98 ( p_ordered == r.p_ordered )
99 )
100 );
101}
102
103std::ostream&
104operator<<(std::ostream& s, const OksRelationship& r)
105{
106 s << "Relationship name: \"" << r.p_name << "\"\n"
107 " class type: \"" << r.p_rclass << "\"\n"
108 " low cardinality constraint is " << OksRelationship::card2str(r.p_low_cc) << "\n"
109 " high cardinality constraint is " << OksRelationship::card2str(r.p_high_cc) << "\n"
110 " is" << (r.p_composite == true ? "" : " not") << " composite reference\n"
111 " is" << (r.p_exclusive == true ? "" : " not") << " exclusive reference\n"
112 " is " << (r.p_dependent == true ? "dependent" : "shared") << " reference\n"
113 " has description: \"" << r.p_description << "\"\n"
114 " is " << (r.p_ordered == true ? "ordered" : "unordered") << std::endl;
115
116 return s;
117}
118
119
120void
122{
123 s.put(" ");
124
125 s.put_start_tag(relationship_xml_tag, sizeof(relationship_xml_tag) - 1);
126
127 s.put_attribute(name_xml_attr, sizeof(name_xml_attr) - 1, p_name.c_str());
128
129 if (!p_description.empty())
130 s.put_attribute(description_xml_attr, sizeof(description_xml_attr) - 1, p_description.c_str());
131
132 s.put_attribute(class_type_xml_attr, sizeof(class_type_xml_attr) - 1, p_rclass.c_str());
133 s.put_attribute(low_cc_xml_attr, sizeof(low_cc_xml_attr) - 1, card2str(p_low_cc));
134 s.put_attribute(high_cc_xml_attr, sizeof(high_cc_xml_attr) - 1, card2str(p_high_cc));
138
139 if (p_ordered)
140 s.put_attribute(ordered_xml_attr, sizeof(ordered_xml_attr) - 1, oks::xml::bool2str(p_ordered));
141
142 s.put_end_tag();
143}
144
145
147 p_low_cc (Zero),
148 p_high_cc (Many),
149 p_composite (false),
150 p_exclusive (false),
151 p_dependent (false),
152 p_class (parent),
153 p_class_type (nullptr),
154 p_ordered (false)
155{
156 try {
157 while(true) {
158 OksXmlAttribute attr(s);
159
160 // check for close of tag
161
162 if(oks::cmp_str1(attr.name(), "/")) { break; }
163
164
165 // check for known oks-relationship' attributes
166
167 else if(oks::cmp_str4(attr.name(), name_xml_attr)) p_name.assign(attr.value(), attr.value_len());
168 else if(oks::cmp_str11(attr.name(), description_xml_attr)) p_description.assign(attr.value(), attr.value_len());
169 else if(oks::cmp_str10(attr.name(), class_type_xml_attr)) p_rclass.assign(attr.value(), attr.value_len());
170 else if(oks::cmp_str6(attr.name(), low_cc_xml_attr)) p_low_cc = str2card(attr.value());
171 else if(oks::cmp_str7(attr.name(), high_cc_xml_attr)) p_high_cc = str2card(attr.value());
176 else if(!strcmp(attr.name(), "multi-value-implementation")) {
177 s.error_msg("OksRelationship::OksRelationship(OksXmlInputStream&)")
178 << "Obsolete oks-relationship\'s attribute \'" << attr.name() << "\'\n";
179 }
180 else {
181 s.throw_unexpected_attribute(attr.name());
182 }
183 }
184 }
185 catch(oks::exception & e) {
186 throw oks::FailedRead("xml attribute", e);
187 }
188 catch (std::exception & e) {
189 throw oks::FailedRead("xml attribute", e.what());
190 }
191
192
193 // check validity of read values
194
195 try {
196 oks::validate_not_empty(p_name, "relationship name");
197 oks::validate_not_empty(p_rclass, "relationship class");
198 }
199 catch(std::exception& ex) {
200 throw oks::FailedRead("oks relationship", oks::BadFileData(ex.what(), s.get_line_no(), s.get_line_pos()));
201 }
202}
203
204
205void
206OksRelationship::set_name(const std::string& new_name)
207{
208 // ignore when name is the same
209
210 if(p_name == new_name) return;
211
212
213 // additional checks are required,
214 // if the relationship already belongs to some class
215
216 if(p_class) {
217
218 // check maximum allowed length for relationship name
219
220 try {
221 oks::validate_not_empty(new_name, "name");
222 }
223 catch(std::exception& ex) {
224 throw oks::SetOperationFailed("OksRelationship::set_name", ex.what());
225 }
226
227
228 // having a direct relationship with the same name is an error
229
230 if(p_class->find_direct_relationship(new_name) != 0) {
231 std::ostringstream text;
232 text << "Class \"" << p_class->get_name() << "\" already has direct relationship \"" << new_name << '\"';
233 throw oks::SetOperationFailed("OksRelationship::set_name", text.str());
234 }
235
236
237 // check possibility to lock the file
238
239 p_class->lock_file("OksRelationship::set_name");
240
241
242 // probably a non-direct relationship already exists
243
244 OksRelationship * r = p_class->find_relationship(new_name);
245
246
247 // change the name
248
249 p_name = new_name;
250
251
252 // registrate the change
253
254 p_class->registrate_class_change(OksClass::ChangeRelationshipsList, (const void *)r);
255 }
256 else {
257 p_name = new_name;
258 }
259}
260
261void
262OksRelationship::set_type(const std::string& cn)
263{
264 if(p_rclass == cn) return;
265
266 if(!p_class || !p_class->p_kernel || (p_class_type = p_class->p_kernel->find_class(cn)) != 0) {
267 if(p_class) p_class->lock_file("OksRelationship::set_type");
268
269 p_rclass = cn;
270
271 if(p_class) {
272 p_class->registrate_class_change(OksClass::ChangeRelationshipClassType, (const void *)this);
273 p_class->registrate_relationship_change(this);
274 }
275 }
276 else {
277 std::ostringstream text;
278 text << "cannot find class \"" << cn << '\"';
279 throw oks::SetOperationFailed("OksRelationship::set_type", text.str());
280 }
281}
282
283void
284OksRelationship::set_description(const std::string& desc)
285{
286 if(p_description != desc) {
287 if(p_class) p_class->lock_file("OksRelationship::set_description");
288
289 p_description = desc;
290
291 if(p_class) p_class->registrate_class_change(OksClass::ChangeRelationshipDescription, (const void *)this);
292 }
293}
294
295void
297{
298 if(p_low_cc != l_cc) {
299 if(p_class) p_class->lock_file("OksRelationship::set_low_cardinality_constraint");
300
302
303 p_low_cc = l_cc;
304
305 if(p_class) {
306 p_class->registrate_class_change(OksClass::ChangeRelationshipLowCC, (const void *)this);
307
308 if(p_low_cc == Many || old_cc == Many) p_class->registrate_relationship_change(this);
309 }
310 }
311}
312
313void
315{
316 if(p_high_cc != h_cc) {
317 if(p_class) p_class->lock_file("OksRelationship::set_high_cardinality_constraint");
318
320
321 p_high_cc = h_cc;
322
323 if(p_class) {
324 p_class->registrate_class_change(OksClass::ChangeRelationshipHighCC, (const void *)this);
325
326 if(p_high_cc == Many || old_cc == Many) p_class->registrate_relationship_change(this);
327 }
328 }
329}
330
331void
333{
334 if(p_composite != cm) {
335 if(p_class) p_class->lock_file("OksRelationship::set_is_composite");
336
337 p_composite = cm;
338
339 if(p_class) p_class->registrate_class_change(OksClass::ChangeRelationshipComposite, (const void *)this);
340 }
341}
342
343void
345{
346 if(p_exclusive != ex) {
347 if(p_class) p_class->lock_file("OksRelationship::set_is_exclusive");
348
349 p_exclusive = ex;
350
351 if(p_class) p_class->registrate_class_change(OksClass::ChangeRelationshipExclusive, (const void *)this);
352 }
353}
354
355void
357{
358 if(p_dependent != dp) {
359 if(p_class) p_class->lock_file("OksRelationship::set_is_dependent");
360
361 p_dependent = dp;
362
363 if(p_class) p_class->registrate_class_change(OksClass::ChangeRelationshipDependent, (const void *)this);
364 }
365}
366
367} // namespace oks
368} // namespace dunedaq
void set_type(const std::string &type)
Set relationship type.
static const char class_type_xml_attr[]
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:28
bool str2bool(const char *s) noexcept
Definition xml.hpp:31
bool cmp_str1(const char *s1, const char s2[2])
Definition cstring.hpp:14
bool cmp_str4(const char *s1, const char s2[5])
Definition cstring.hpp:34
bool cmp_str6(const char *s1, const char s2[7])
Definition cstring.hpp:50
bool cmp_str10(const char *s1, const char s2[11])
Definition cstring.hpp:78
void validate_not_empty(const std::string &value, const char *name)
std::ostream & operator<<(std::ostream &s, const oks::exception &ex)
bool cmp_str11(const char *s1, const char s2[12])
Definition cstring.hpp:82
bool cmp_str3(const char *s1, const char s2[4])
Definition cstring.hpp:26
bool cmp_str12(const char *s1, const char s2[13])
Definition cstring.hpp:86
bool cmp_str7(const char *s1, const char s2[8])
Definition cstring.hpp:58
Including Qt Headers.
Definition module.cpp:16
Definition ral.hpp:35
size_t value_len() const
Definition xml.hpp:309