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