1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
|
/*
Ousía
Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <set>
#include <core/common/RttiBuilder.hpp>
#include <core/common/Exceptions.hpp>
#include "Domain.hpp"
namespace ousia {
/* Class FieldDescriptor */
FieldDescriptor::FieldDescriptor(Manager &mgr, Handle<Descriptor> parent,
Handle<Type> primitiveType, std::string name,
bool optional)
: Node(mgr, std::move(name), parent),
children(this),
fieldType(FieldType::PRIMITIVE),
primitiveType(acquire(primitiveType)),
optional(optional)
{
if (parent != nullptr) {
parent->addFieldDescriptor(this);
}
}
FieldDescriptor::FieldDescriptor(Manager &mgr, Handle<Descriptor> parent,
FieldType fieldType, std::string name,
bool optional)
: Node(mgr, std::move(name), parent),
children(this),
fieldType(fieldType),
optional(optional)
{
if (parent != nullptr) {
parent->addFieldDescriptor(this);
}
}
bool FieldDescriptor::doValidate(Logger &logger) const
{
bool valid = true;
// check parent type
if (getParent() == nullptr) {
logger.error("This field has no parent!", *this);
valid = false;
} else if (!getParent()->isa(RttiTypes::Descriptor)) {
logger.error("The parent of this field is not a descriptor!", *this);
valid = false;
}
// check name
if (getName() != DEFAULT_FIELD_NAME) {
valid = valid & validateName(logger);
}
// check consistency of FieldType with the rest of the FieldDescriptor.
if (fieldType == FieldType::PRIMITIVE) {
if (children.size() > 0) {
logger.error(
"This field is supposed to be primitive but has "
"registered child classes!",
*this);
valid = false;
}
if (primitiveType == nullptr) {
logger.error(
"This field is supposed to be primitive but has "
"no primitive type!",
*this);
valid = false;
}
} else {
if (primitiveType != nullptr) {
logger.error(
"This field is supposed to be non-primitive but has "
"a primitive type!",
*this);
valid = false;
}
}
/*
* we are not allowed to call the validation functions of each child because
* this might lead to cycles. What we should do, however, is to check if
* there are no duplicates.
*/
std::set<std::string> names;
for (Handle<StructuredClass> c : children) {
if (!names.insert(c->getName()).second) {
logger.error(std::string("Field \"") + getName() +
"\" had multiple children with the name \"" +
c->getName() + "\"",
*this);
valid = false;
}
}
return valid;
}
bool FieldDescriptor::removeChild(Handle<StructuredClass> c)
{
auto it = children.find(c);
if (it != children.end()) {
invalidate();
children.erase(it);
return true;
}
return false;
}
/* Class Descriptor */
void Descriptor::doResolve(ResolutionState &state)
{
const NodeVector<Attribute> &attributes =
attributesDescriptor->getAttributes();
continueResolveComposita(attributes, attributes.getIndex(), state);
continueResolveComposita(fieldDescriptors, fieldDescriptors.getIndex(),
state);
}
bool Descriptor::doValidate(Logger &logger) const
{
bool valid = true;
// check parent type
if (getParent() == nullptr) {
logger.error("This Descriptor has no parent!", *this);
valid = false;
} else if (!getParent()->isa(RttiTypes::Domain)) {
logger.error("The parent of this Descriptor is not a Domain!", *this);
valid = false;
}
// check name
if (getName().empty()) {
logger.error("The name of this Descriptor is empty!", *this);
valid = false;
} else {
valid = valid & validateName(logger);
}
// check attributes and the FieldDescriptors
return valid & attributesDescriptor->validate(logger) &
continueValidationCheckDuplicates(fieldDescriptors, logger);
}
std::vector<Rooted<Node>> Descriptor::pathTo(
Handle<StructuredClass> target) const
{
std::vector<Rooted<Node>> path;
continuePath(target, path);
return path;
}
bool Descriptor::continuePath(Handle<StructuredClass> target,
std::vector<Rooted<Node>> ¤tPath) const
{
// check if we are at the target already
if (this == target) {
return true;
}
// a variable to determine if we already found a solution
bool found = false;
// the currently optimal path.
std::vector<Rooted<Node>> optimum;
// use recursive depth-first search from the top to reach the given child
// get the list of effective FieldDescriptors.
NodeVector<FieldDescriptor> fields;
if (isa(RttiTypes::StructuredClass)) {
const StructuredClass *tis = static_cast<const StructuredClass *>(this);
fields = tis->getEffectiveFieldDescriptors();
} else {
fields = getFieldDescriptors();
}
for (auto &fd : fields) {
for (auto &c : fd->getChildren()) {
// check if a child is the target node.
if (c == target) {
// if we have made the connection, stop the search.
currentPath.push_back(fd);
return true;
}
// look for transparent intermediate nodes.
if (c->isTransparent()) {
// copy the path.
std::vector<Rooted<Node>> cPath = currentPath;
cPath.push_back(fd);
cPath.push_back(c);
// recursion.
if (c->continuePath(target, cPath) &&
(!found || optimum.size() > cPath.size())) {
// look if this path is better than the current optimum.
optimum = std::move(cPath);
found = true;
}
}
}
}
if (isa(RttiTypes::StructuredClass)) {
const StructuredClass *tis = static_cast<const StructuredClass *>(this);
// if this is a StructuredClass we also can call the subclasses.
for (auto &c : tis->getSubclasses()) {
// copy the path.
std::vector<Rooted<Node>> cPath = currentPath;
if (c->continuePath(target, cPath) &&
(!found || optimum.size() > cPath.size())) {
// look if this path is better than the current optimum.
optimum = std::move(cPath);
found = true;
}
}
}
// put the optimum in the given path reference.
currentPath = std::move(optimum);
// return if we found something.
return found;
}
void Descriptor::addFieldDescriptor(Handle<FieldDescriptor> fd)
{
// only add it if we need to.
if (fieldDescriptors.find(fd) == fieldDescriptors.end()) {
invalidate();
fieldDescriptors.push_back(fd);
}
if (fd->getParent() == nullptr) {
fd->setParent(this);
}
}
void Descriptor::moveFieldDescriptor(Handle<FieldDescriptor> fd)
{
// only add it if we need to.
if (fieldDescriptors.find(fd) == fieldDescriptors.end()) {
invalidate();
fieldDescriptors.push_back(fd);
}
Handle<Managed> par = fd->getParent();
if (par != this) {
if (par != nullptr) {
// remove the FieldDescriptor from the old parent.
par.cast<Descriptor>()->removeFieldDescriptor(fd);
}
fd->setParent(this);
}
}
void Descriptor::copyFieldDescriptor(Handle<FieldDescriptor> fd)
{
if (fd->getFieldType() == FieldDescriptor::FieldType::PRIMITIVE) {
/*
* To call the "new" operation is enough here, because the
* constructor will add the newly constructed FieldDescriptor to this
* Descriptor automatically.
*/
new FieldDescriptor(getManager(), this, fd->getPrimitiveType(),
fd->getName(), fd->isOptional());
} else {
/*
* In case of non-primitive FieldDescriptors we also want to copy the
* child references.
*/
Rooted<FieldDescriptor> copy = {
new FieldDescriptor(getManager(), this, fd->getFieldType(),
fd->getName(), fd->isOptional())};
for (auto &c : fd->getChildren()) {
copy->addChild(c);
}
}
}
bool Descriptor::removeFieldDescriptor(Handle<FieldDescriptor> fd)
{
auto it = fieldDescriptors.find(fd);
if (it != fieldDescriptors.end()) {
invalidate();
fieldDescriptors.erase(it);
fd->setParent(nullptr);
return true;
}
return false;
}
Rooted<FieldDescriptor> Descriptor::createPrimitiveFieldDescriptor(
Handle<Type> primitiveType, std::string name, bool optional)
{
return Rooted<FieldDescriptor>{new FieldDescriptor(
getManager(), this, primitiveType, std::move(name), optional)};
}
Rooted<FieldDescriptor> Descriptor::createFieldDescriptor(
FieldDescriptor::FieldType fieldType, std::string name, bool optional)
{
return Rooted<FieldDescriptor>{new FieldDescriptor(
getManager(), this, fieldType, std::move(name), optional)};
}
/* Class StructuredClass */
StructuredClass::StructuredClass(Manager &mgr, std::string name,
Handle<Domain> domain, Variant cardinality,
Handle<StructuredClass> superclass,
bool transparent, bool root)
: Descriptor(mgr, std::move(name), domain),
cardinality(cardinality),
superclass(acquire(superclass)),
subclasses(this),
transparent(transparent),
root(root)
{
ExceptionLogger logger;
if (superclass != nullptr) {
superclass->addSubclass(this, logger);
}
if (domain != nullptr) {
domain->addStructuredClass(this);
}
}
bool StructuredClass::doValidate(Logger &logger) const
{
bool valid = true;
// check if all registered subclasses have this StructuredClass as parent.
for (Handle<StructuredClass> sub : subclasses) {
if (sub->getSuperclass() != this) {
logger.error(std::string("Struct \"") + sub->getName() +
"\" is registered as subclass of \"" + getName() +
"\" but does not have it as superclass!",
*this);
valid = false;
}
}
// check the cardinality.
if (!cardinality.isCardinality()) {
logger.error(cardinality.toString() + " is not a cardinality!", *this);
valid = false;
}
// check the validity of this superclass.
if (superclass != nullptr) {
valid = valid & superclass->validate(logger);
}
// check the validity as a Descriptor.
/*
* Note that we do not check the validity of all subclasses. This is because
* it will lead to cycles as the subclasses would call validate on their
* superclass, which is this one.
*/
return valid & Descriptor::doValidate(logger);
}
void StructuredClass::setSuperclass(Handle<StructuredClass> sup, Logger &logger)
{
if (superclass == sup) {
return;
}
// remove this subclass from the old superclass.
if (superclass != nullptr) {
superclass->removeSubclass(this, logger);
}
// set the new superclass
superclass = acquire(sup);
invalidate();
// add this class as new subclass of the new superclass.
if (sup != nullptr) {
sup->addSubclass(this, logger);
// set the attribute descriptor supertype
getAttributesDescriptor()->setParentStructure(
sup->getAttributesDescriptor(), logger);
} else {
getAttributesDescriptor()->setParentStructure(nullptr, logger);
}
}
bool StructuredClass::isSubclassOf(Handle<StructuredClass> c) const
{
if (c == nullptr || superclass == nullptr) {
return false;
}
if (c == superclass) {
return true;
}
return superclass->isSubclassOf(c);
}
void StructuredClass::addSubclass(Handle<StructuredClass> sc, Logger &logger)
{
if (sc == nullptr) {
return;
}
// check if we already have that class.
if (subclasses.find(sc) == subclasses.end()) {
invalidate();
subclasses.push_back(sc);
}
sc->setSuperclass(this, logger);
}
void StructuredClass::removeSubclass(Handle<StructuredClass> sc, Logger &logger)
{
// if we don't have this subclass we can return directly.
if (sc == nullptr) {
return;
}
auto it = subclasses.find(sc);
if (it == subclasses.end()) {
return;
}
// otherwise we have to erase it.
invalidate();
subclasses.erase(it);
sc->setSuperclass(nullptr, logger);
}
const void StructuredClass::gatherFieldDescriptors(
NodeVector<FieldDescriptor> ¤t,
std::set<std::string> &overriddenFields) const
{
// append all FieldDescriptors that are not overridden.
for (auto &f : Descriptor::getFieldDescriptors()) {
if (overriddenFields.insert(f->getName()).second) {
current.push_back(f);
}
}
if (superclass != nullptr) {
superclass->gatherFieldDescriptors(current, overriddenFields);
}
}
NodeVector<FieldDescriptor> StructuredClass::getEffectiveFieldDescriptors()
const
{
// in this case we return a NodeVector of Rooted entries without owner.
NodeVector<FieldDescriptor> vec;
std::set<std::string> overriddenFields;
gatherFieldDescriptors(vec, overriddenFields);
return std::move(vec);
}
/* Class AnnotationClass */
AnnotationClass::AnnotationClass(Manager &mgr, std::string name,
Handle<Domain> domain)
: Descriptor(mgr, std::move(name), domain)
{
if (domain != nullptr) {
domain->addAnnotationClass(this);
}
}
/* Class Domain */
void Domain::doResolve(ResolutionState &state)
{
if (!continueResolveComposita(structuredClasses,
structuredClasses.getIndex(), state) |
continueResolveComposita(annotationClasses,
annotationClasses.getIndex(), state)) {
continueResolveReferences(typesystems, state);
}
}
bool Domain::doValidate(Logger &logger) const
{
// check validity of name, of StructuredClasses, of AnnotationClasses and
// TypeSystems.
return validateName(logger) &
continueValidationCheckDuplicates(structuredClasses, logger) &
continueValidationCheckDuplicates(annotationClasses, logger) &
continueValidationCheckDuplicates(typesystems, logger);
}
void Domain::doReference(Handle<Node> node)
{
if (node->isa(RttiTypes::Domain)) {
referenceTypesystem(node.cast<Typesystem>());
}
}
RttiSet Domain::doGetReferenceTypes() const
{
return RttiSet{&RttiTypes::Domain};
}
void Domain::addStructuredClass(Handle<StructuredClass> s)
{
// only add it if we need to.
if (structuredClasses.find(s) == structuredClasses.end()) {
invalidate();
structuredClasses.push_back(s);
}
Handle<Managed> par = s->getParent();
if (par != this) {
if (par != nullptr) {
// remove the StructuredClass from the old parent.
par.cast<Domain>()->removeStructuredClass(s);
}
s->setParent(this);
}
}
bool Domain::removeStructuredClass(Handle<StructuredClass> s)
{
auto it = structuredClasses.find(s);
if (it != structuredClasses.end()) {
invalidate();
structuredClasses.erase(it);
s->setParent(nullptr);
return true;
}
return false;
}
Rooted<StructuredClass> Domain::createStructuredClass(
std::string name, Variant cardinality, Handle<StructuredClass> superclass,
bool transparent, bool root)
{
return Rooted<StructuredClass>{new StructuredClass(
getManager(), std::move(name), this, cardinality, superclass,
std::move(transparent), std::move(root))};
}
void Domain::addAnnotationClass(Handle<AnnotationClass> a)
{
// only add it if we need to.
if (annotationClasses.find(a) == annotationClasses.end()) {
invalidate();
annotationClasses.push_back(a);
}
Handle<Managed> par = a->getParent();
if (par != this) {
if (par != nullptr) {
// remove the StructuredClass from the old parent.
par.cast<Domain>()->removeAnnotationClass(a);
}
a->setParent(this);
}
}
bool Domain::removeAnnotationClass(Handle<AnnotationClass> a)
{
auto it = annotationClasses.find(a);
if (it != annotationClasses.end()) {
invalidate();
annotationClasses.erase(it);
a->setParent(nullptr);
return true;
}
return false;
}
Rooted<AnnotationClass> Domain::createAnnotationClass(std::string name)
{
return Rooted<AnnotationClass>{
new AnnotationClass(getManager(), std::move(name), this)};
}
/* Type registrations */
namespace RttiTypes {
const Rtti FieldDescriptor =
RttiBuilder<ousia::FieldDescriptor>("FieldDescriptor").parent(&Node);
const Rtti Descriptor =
RttiBuilder<ousia::Descriptor>("Descriptor").parent(&Node);
const Rtti StructuredClass =
RttiBuilder<ousia::StructuredClass>("StructuredClass")
.parent(&Descriptor)
.composedOf(&FieldDescriptor);
const Rtti AnnotationClass =
RttiBuilder<ousia::AnnotationClass>("AnnotationClass").parent(&Descriptor);
const Rtti Domain = RttiBuilder<ousia::Domain>("Domain")
.parent(&RootNode)
.composedOf({&StructuredClass, &AnnotationClass});
}
}
|