summaryrefslogtreecommitdiff
path: root/src/core/model/Document.cpp
blob: 219e512b2460164b3fef5e5df93c6c93f4e5553e (plain)
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
    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 "Document.hpp"

#include <map>
#include <set>

#include <core/common/Exceptions.hpp>
#include <core/common/RttiBuilder.hpp>

namespace ousia {

/* Class DocumentEntity */

void DocumentEntity::invalidateSubInstance()
{
	if (subInst->isa(&RttiTypes::StructuredEntity)) {
		subInst.cast<StructuredEntity>()->invalidate();
	} else {
		subInst.cast<AnnotationEntity>()->invalidate();
	}
}

DocumentEntity::DocumentEntity(Handle<Node> subInst,
                               Handle<Descriptor> descriptor,
                               Variant attributes)
    : subInst(subInst),
      // initialize descriptor as nullptr first and then set it right
      attributes(std::move(attributes))
{
	// insert empty vectors for each field.
	if (descriptor != nullptr) {
		setDescriptor(descriptor);
	}
}

void DocumentEntity::setDescriptor(Handle<Descriptor> d)
{
	// check if we have to do anything.
	if (descriptor == d) {
		return;
	}
	invalidateSubInstance();
	descriptor = subInst->acquire(d);
	// clear the fields vector.
	fields.clear();
	// fill it again.
	for (size_t f = 0; f < descriptor->getFieldDescriptors().size(); f++) {
		fields.push_back(NodeVector<StructureNode>(subInst));
	}
}

bool DocumentEntity::doValidate(Logger &logger) const
{
	// if we have no descriptor, this is invalid.
	if (descriptor == nullptr) {
		logger.error("This entity has no descriptor!", *subInst);
		// in this case we have to stop the validation process, because without
		// a constructor we can not check anything else.
		return false;
	}
	// if we have an invalid descriptor we can not proceed either.
	if (!descriptor->validate(logger)) {
		return false;
	}
	// check the attribute primitive content.
	bool valid;
	if (descriptor->getAttributesDescriptor() == nullptr) {
		valid = getAttributes() == nullptr;
	} else {
		valid = descriptor->getAttributesDescriptor()->isValid(getAttributes(),
		                                                       logger);
	}
	/*
	 * generate the set of effective fields. This is trivial for
	 * AnnotationEntities, but in the case of StructuredEntities we have to
	 * gather all fields of superclasses as well, that have not been
	 * overridden in the subclasses.
	 */
	NodeVector<FieldDescriptor> fieldDescs = descriptor->getFieldDescriptors();
	// iterate over every field
	for (unsigned int f = 0; f < fields.size(); f++) {
		// we have a special check for primitive fields.
		if (fieldDescs[f]->isPrimitive()) {
			switch (fields[f].size()) {
				case 0:
					if (!fieldDescs[f]->isOptional()) {
						logger.error(std::string("Primitive Field \"") +
						                 fieldDescs[f]->getName() +
						                 "\" had no content!",
						             *subInst);
						valid = false;
					}
					continue;
				case 1:
					break;
				default:
					logger.error(std::string("Primitive Field \"") +
					                 fieldDescs[f]->getName() +
					                 "\" had more than one child!",
					             *subInst);
					valid = false;
					continue;
			}
			// if we are here we know that exactly one child exists.
			if (!fields[f][0]->isa(&RttiTypes::DocumentPrimitive)) {
				logger.error(std::string("Primitive Field \"") +
				                 fieldDescs[f]->getName() +
				                 "\" has non primitive content!",
				             *subInst);
				valid = false;
			} else {
				Handle<DocumentPrimitive> primitive =
				    fields[f][0].cast<DocumentPrimitive>();
				valid = valid &
				        fieldDescs[f]->getPrimitiveType()->isValid(
				            primitive->getContent(), logger);
			}
			continue;
		}

		std::unordered_set<StructuredClass *> childClasses;
		{
			NodeVector<StructuredClass> tmp =
			    fieldDescs[f]->getChildrenWithSubclasses();
			for (auto s : tmp) {
				childClasses.insert(s.get());
			}
		}

		// we can do a faster check if this field is empty.
		if (fields[f].size() == 0) {
			// if this field is optional, an empty field is valid anyways.
			if (fieldDescs[f]->isOptional()) {
				continue;
			}
			/*
			 * if it is not optional we have to check if zero is a valid
			 * cardinality.
			 */
			for (auto childClass : childClasses) {
				const size_t min =
				    childClass->getCardinality().asCardinality().min();
				if (min > 0) {
					logger.error(
					    std::string("Field \"") + fieldDescs[f]->getName() +
					        "\" was empty but needs at least " +
					        std::to_string(min) + " elements of class \"" +
					        childClass->getName() +
					        "\" according to the definition of \"" +
					        descriptor->getName() + "\"",
					    *subInst);
					valid = false;
				}
			}
			continue;
		}

		// store the actual numbers of children for each child class in a map
		std::unordered_map<StructuredClass *, unsigned int> nums;

		// iterate over every actual child of this field
		for (auto child : fields[f]) {
			// check if the parent reference is correct.
			if (child->getParent() != subInst) {
				logger.error(std::string("A child of field \"") +
				                 fieldDescs[f]->getNameOrDefaultName() +
				                 "\" has the wrong parent reference!",
				             *child);
				valid = false;
			}
			if (child->isa(&RttiTypes::Anchor)) {
				// Anchors are uninteresting and can be ignored.
				continue;
			}
			if (child->isa(&RttiTypes::DocumentPrimitive)) {
				logger.error(std::string("Non-primitive Field \"") +
				                 fieldDescs[f]->getName() +
				                 "\" had primitive content!",
				             *child);
				valid = false;
				continue;
			}
			// otherwise this is a StructuredEntity
			Handle<StructuredEntity> c = child.cast<StructuredEntity>();
			StructuredClass *classPtr =
			    c->getDescriptor().cast<StructuredClass>().get();

			// check if its class is allowed.
			if (childClasses.find(classPtr) == childClasses.end()) {
				logger.error(
				    std::string("An instance of \"") +
				        c->getDescriptor()->getName() +
				        "\" is not allowed as child of an instance of \"" +
				        descriptor->getName() + "\" in field \"" +
				        fieldDescs[f]->getNameOrDefaultName() + "\"",
				    *child);
				valid = false;
				continue;
			}
			// note the number of occurences for this class and all
			// superclasses, because a subclass instance should count for
			// superclasses as well.
			while (classPtr != nullptr &&
			       childClasses.find(classPtr) != childClasses.end()) {
				const auto &n = nums.find(classPtr);
				if (n != nums.end()) {
					n->second++;
				} else {
					nums.emplace(classPtr, 1);
				}
				classPtr = classPtr->getSuperclass().get();
			}
		}

		// now check if the cardinalities are right.
		for (auto childClass : childClasses) {
			const auto &n = nums.find(childClass);
			unsigned int num = 0;
			if (n != nums.end()) {
				num = n->second;
			}
			if (!childClass->getCardinality().asCardinality().contains(num)) {
				logger.error(std::string("Field \"") +
				                 fieldDescs[f]->getName() + "\" had " +
				                 std::to_string(num) + " elements of class \"" +
				                 childClass->getName() +
				                 "\", which is invalid according to the "
				                 "definition of \"" +
				                 descriptor->getName() + "\"",
				             *subInst);
				valid = false;
				continue;
			}
		}
	}

	// go into recursion.
	for (auto f : fields) {
		for (auto n : f) {
			valid = valid & n->validate(logger);
		}
	}
	return valid;
}

void DocumentEntity::setAttributes(const Variant &a)
{
	invalidateSubInstance();
	attributes = a;
}

static int enforceGetFieldDescriptorIndex(Handle<Descriptor> desc,
                                          const std::string &fieldName)
{
	ssize_t idx = desc->getFieldDescriptorIndex(fieldName);
	if (idx == -1) {
		throw OusiaException(std::string("Descriptor \"") + desc->getName() +
		                     "\" has no field with the name \"" + fieldName +
		                     "\"");
	}
	return idx;
}

static int enforceGetFieldDescriptorIndex(
    Handle<Descriptor> desc, Handle<FieldDescriptor> fieldDescriptor)
{
	ssize_t idx = desc->getFieldDescriptorIndex(fieldDescriptor);
	if (idx == -1) {
		throw OusiaException(std::string("Descriptor \"") + desc->getName() +
		                     "\" does not reference the given field \"" +
		                     fieldDescriptor->getNameOrDefaultName() + "\"");
	}
	return idx;
}

const NodeVector<StructureNode> &DocumentEntity::getField(
    const std::string &fieldName) const
{
	return fields[enforceGetFieldDescriptorIndex(descriptor, fieldName)];
}

const NodeVector<StructureNode> &DocumentEntity::getField(
    Handle<FieldDescriptor> fieldDescriptor) const
{
	return fields[enforceGetFieldDescriptorIndex(descriptor, fieldDescriptor)];
}

const NodeVector<StructureNode> &DocumentEntity::getField(size_t idx) const
{
	if (idx >= fields.size()) {
		throw OusiaException(std::string("Descriptor \"") +
		                     descriptor->getName() +
		                     "\" does not have enough fields for index \"" +
		                     std::to_string(idx) + "\".");
	}
	return fields[idx];
}

void DocumentEntity::addStructureNode(Handle<StructureNode> s, size_t i)
{
	// only add the new node if we don't have it already.
	auto it = fields[i].find(s);
	if (it == fields[i].end()) {
		invalidateSubInstance();
		fields[i].push_back(s);
	}
	Handle<Managed> par = s->getParent();
	if (par != subInst) {
		// if a previous parent existed, remove the StructureNode from it
		if (par != nullptr) {
			if (par->isa(&RttiTypes::StructuredEntity)) {
				par.cast<StructuredEntity>()->removeStructureNode(s);
			} else if(par->isa(&RttiTypes::AnnotationEntity)){
				par.cast<AnnotationEntity>()->removeStructureNode(s);
			} else if(par->isa(&RttiTypes::Document)){
				par.cast<Document>()->setRoot(nullptr);
			}
		}
		s->setParent(subInst);
	}
}

void DocumentEntity::addStructureNode(Handle<StructureNode> s,
                                      Handle<FieldDescriptor> fieldDescriptor)
{
	addStructureNode(
	    s, enforceGetFieldDescriptorIndex(descriptor, fieldDescriptor));
}

void DocumentEntity::addStructureNodes(
    const std::vector<Handle<StructureNode>> &ss,
    Handle<FieldDescriptor> fieldDescriptor)
{
	const int i = enforceGetFieldDescriptorIndex(descriptor, fieldDescriptor);
	for (Handle<StructureNode> s : ss) {
		addStructureNode(s, i);
	}
}

void DocumentEntity::addStructureNode(Handle<StructureNode> s,
                                      const std::string &fieldName)
{
	addStructureNode(s, enforceGetFieldDescriptorIndex(descriptor, fieldName));
}

void DocumentEntity::addStructureNodes(
    const std::vector<Handle<StructureNode>> &ss, const std::string &fieldName)
{
	const int idx = enforceGetFieldDescriptorIndex(descriptor, fieldName);
	for (Handle<StructureNode> s : ss) {
		addStructureNode(s, idx);
	}
}

bool DocumentEntity::removeStructureNodeFromField(Handle<StructureNode> s,
                                                  size_t i)
{
	auto it = fields[i].find(s);
	if (it != fields[i].end()) {
		invalidateSubInstance();
		fields[i].erase(it);
		s->setParent(nullptr);
		return true;
	}
	return false;
}

bool DocumentEntity::removeStructureNodeFromField(Handle<StructureNode> s,
                                                  const std::string &fieldName)
{
	return removeStructureNodeFromField(
	    s, enforceGetFieldDescriptorIndex(descriptor, fieldName));
}

bool DocumentEntity::removeStructureNodeFromField(
    Handle<StructureNode> s, Handle<FieldDescriptor> fieldDescriptor)
{
	return removeStructureNodeFromField(
	    s, enforceGetFieldDescriptorIndex(descriptor, fieldDescriptor));
}

bool DocumentEntity::removeStructureNode(Handle<StructureNode> s)
{
	for (auto field : fields) {
		auto it = field.find(s);
		if (it != field.end()) {
			invalidateSubInstance();
			field.erase(it);
			s->setParent(nullptr);
			return true;
		}
	}

	return false;
}

Rooted<StructuredEntity> DocumentEntity::createChildStructuredEntity(
    Handle<StructuredClass> descriptor, Variant attributes,
    const std::string &fieldName, std::string name)
{
	return Rooted<StructuredEntity>{new StructuredEntity(
	    subInst->getManager(), subInst, descriptor, std::move(attributes),
	    fieldName, std::move(name))};
}

Rooted<StructuredEntity> DocumentEntity::createChildStructuredEntity(
    Handle<StructuredClass> descriptor, size_t fieldIdx, Variant attributes,
    std::string name)
{
	return Rooted<StructuredEntity>{
	    new StructuredEntity(subInst->getManager(), subInst, descriptor,
	                         fieldIdx, std::move(attributes), std::move(name))};
}

Rooted<DocumentPrimitive> DocumentEntity::createChildDocumentPrimitive(
    Variant content, const std::string &fieldName)
{
	return Rooted<DocumentPrimitive>{new DocumentPrimitive(
	    subInst->getManager(), subInst, std::move(content), fieldName)};
}

Rooted<DocumentPrimitive> DocumentEntity::createChildDocumentPrimitive(
    Variant content, size_t fieldIdx)
{
	return Rooted<DocumentPrimitive>{new DocumentPrimitive(
	    subInst->getManager(), subInst, std::move(content), fieldIdx)};
}

Rooted<Anchor> DocumentEntity::createChildAnchor(const std::string &fieldName)
{
	return Rooted<Anchor>{
	    new Anchor(subInst->getManager(), subInst, fieldName)};
}
Rooted<Anchor> DocumentEntity::createChildAnchor(size_t fieldIdx)
{
	return Rooted<Anchor>{new Anchor(subInst->getManager(), subInst, fieldIdx)};
}

static bool matchStartAnchor(Handle<AnnotationClass> desc,
                             const std::string &name, Handle<Anchor> a)
{
	return (a->getAnnotation() != nullptr) &&
	       (a->getAnnotation()->getEnd() == nullptr) &&
	       (desc == nullptr || a->getAnnotation()->getDescriptor() == desc) &&
	       (name.empty() || a->getAnnotation()->getName() == name);
}

template <typename Iterator>
Rooted<Anchor> DocumentEntity::searchStartAnchorInField(
    Handle<AnnotationClass> desc, const std::string &name, Iterator begin,
    Iterator end, std::unordered_set<const DocumentEntity *> &visited)
{
	for (Iterator it = begin; it != end; it++) {
		Handle<StructureNode> strct = *it;
		if (strct->isa(&RttiTypes::Anchor)) {
			// check if this Anchor is the right one.
			Handle<Anchor> a = strct.cast<Anchor>();
			if (matchStartAnchor(desc, name, a)) {
				return a;
			}
			continue;
		} else if (strct->isa(&RttiTypes::StructuredEntity)) {
			// search downwards.
			Rooted<Anchor> a =
			    strct.cast<StructuredEntity>()->searchStartAnchorDownwards(
			        desc, name, visited);
			if (a != nullptr) {
				return a;
			}
		}
	}
	return nullptr;
}

Rooted<Anchor> DocumentEntity::searchStartAnchorDownwards(
    Handle<AnnotationClass> desc, const std::string &name,
    std::unordered_set<const DocumentEntity *> &visited)
{
	if (!visited.insert(this).second) {
		return nullptr;
	}
	if (fields.empty()) {
		return nullptr;
	}
	// get the default field.
	NodeVector<StructureNode> children = fields[fields.size() - 1];
	// search it from back to front.
	return searchStartAnchorInField(desc, name, children.rbegin(),
	                                children.rend(), visited);
}

Rooted<Anchor> DocumentEntity::searchStartAnchorUpwards(
    Handle<AnnotationClass> desc, const std::string &name,
    const DocumentEntity *child,
    std::unordered_set<const DocumentEntity *> &visited)
{
	if (!visited.insert(this).second) {
		return nullptr;
	}
	if (fields.empty()) {
		return nullptr;
	}
	// get the default field.
	NodeVector<StructureNode> children = fields[fields.size() - 1];
	// search for the child from back to front.
	auto it = children.rbegin();
	while (static_cast<void *>(it->get()) != child->subInst.get() &&
	       it != children.rend()) {
		it++;
	}
	// increment the reverse iterator once more to prevent downwards search
	// to the child.
	if (it != children.rend()) {
		it++;
		return searchStartAnchorInField(desc, name, it, children.rend(),
		                                visited);
	}
	throw OusiaException("Internal error: Child node not found in parent!");
}

Rooted<Anchor> DocumentEntity::searchStartAnchor(size_t fieldIdx,
                                                 Handle<AnnotationClass> desc,
                                                 const std::string &name)
{
	std::unordered_set<const DocumentEntity *> visited;
	visited.insert(this);
	// get the correct field.
	NodeVector<StructureNode> children = fields[fieldIdx];
	// search it from back to front.
	Rooted<Anchor> a = searchStartAnchorInField(desc, name, children.rbegin(),
	                                            children.rend(), visited);
	// if we found the Anchor, return it.
	if (a != nullptr) {
		return a;
	}

	// If this is either an AnnotationEntity or a SUBTREE field we can not
	// search upwards.
	if (subInst->isa(&RttiTypes::AnnotationEntity) ||
	    fieldIdx + 1 < fields.size()) {
		return nullptr;
	}
	// if the children here did not contain the right start Anchor go upwards.
	if (subInst->getParent()->isa(&RttiTypes::StructuredEntity)) {
		return subInst->getParent()
		    .cast<StructuredEntity>()
		    ->searchStartAnchorUpwards(desc, name, this, visited);
	}
	if (subInst->getParent()->isa(&RttiTypes::AnnotationEntity)) {
		subInst->getParent().cast<AnnotationEntity>()->searchStartAnchorUpwards(
		    desc, name, this, visited);
	}
	return nullptr;
}

/* Class StructureNode */

bool StructureNode::doValidate(Logger &logger) const
{
	bool valid = true;
	// check name
	if (!getName().empty()) {
		valid = validateName(logger);
	}
	// check the parent.
	if (getParent() == nullptr) {
		logger.error("The parent is not set!", *this);
		valid = false;
	}
	if (!getParent()->isa(&RttiTypes::StructuredEntity) &&
	    !getParent()->isa(&RttiTypes::AnnotationEntity) &&
	    !getParent()->isa(&RttiTypes::Document)) {
		logger.error("The parent does not have a valid type!", *this);
		valid = false;
	}
	return valid;
}

StructureNode::StructureNode(Manager &mgr, std::string name,
                             Handle<Node> parent, const std::string &fieldName)
    : Node(mgr, std::move(name), parent)
{
	if (parent->isa(&RttiTypes::StructuredEntity)) {
		parent.cast<StructuredEntity>()->addStructureNode(this, fieldName);
	} else if (parent->isa(&RttiTypes::AnnotationEntity)) {
		parent.cast<AnnotationEntity>()->addStructureNode(this, fieldName);
	} else {
		throw OusiaException("The proposed parent was no DocumentEntity!");
	}
}

StructureNode::StructureNode(Manager &mgr, std::string name,
                             Handle<Node> parent, size_t fieldIdx)
    : Node(mgr, std::move(name), parent)
{
	if (parent->isa(&RttiTypes::StructuredEntity)) {
		parent.cast<StructuredEntity>()->addStructureNode(this, fieldIdx);
	} else if (parent->isa(&RttiTypes::AnnotationEntity)) {
		parent.cast<AnnotationEntity>()->addStructureNode(this, fieldIdx);
	} else {
		throw OusiaException("The proposed parent was no DocumentEntity!");
	}
}

/* Class StructuredEntity */

StructuredEntity::StructuredEntity(Manager &mgr, Handle<Document> doc,
                                   Handle<StructuredClass> descriptor,
                                   Variant attributes, std::string name)
    : StructureNode(mgr, std::move(name), doc),
      DocumentEntity(this, descriptor, std::move(attributes))
{
	doc->setRoot(this);
}

StructuredEntity::StructuredEntity(Manager &mgr, Handle<Node> parent,
                                   Handle<StructuredClass> descriptor,
                                   Variant attributes, std::string name)
    : StructureNode(mgr, std::move(name), parent),
      DocumentEntity(this, descriptor, std::move(attributes))
{
}

bool StructuredEntity::doValidate(Logger &logger) const
{
	bool valid = true;
	// check the parent.
	if (getDescriptor() == nullptr) {
		logger.error("The descriptor is not set!", *this);
		valid = false;
	} else if (!getDescriptor()->isa(&RttiTypes::StructuredClass)) {
		logger.error("The descriptor is not a structure descriptor!", *this);
		valid = false;
	} else if (transparent &&
	           !getDescriptor().cast<StructuredClass>()->isTransparent()) {
		logger.error(
		    "The entity is marked as transparent but the descriptor "
		    "does not allow transparency!",
		    *this);
		valid = false;
	}

	// check the validity as a StructureNode and as a DocumentEntity.
	return valid & StructureNode::doValidate(logger) &
	       DocumentEntity::doValidate(logger);
}

/* Class Anchor */

bool Anchor::doValidate(Logger &logger) const
{
	bool valid = true;
	// check name
	if (!getName().empty()) {
		logger.error(
		    "This anchor has a name! Anchors should only be referred to by "
		    "reference, not by name!",
		    *this);
		valid = false;
	}
	if (annotation == nullptr) {
		// this is valid but should throw a warning.
		logger.warning("This anchor is disconnected.", *this);
	}
	return valid & StructureNode::doValidate(logger);
}

void Anchor::setAnnotation(Handle<AnnotationEntity> anno, bool start)
{
	if (annotation == anno) {
		return;
	}
	invalidate();
	// unset the old reference.
	if (annotation != nullptr) {
		if (isStart()) {
			annotation->setStart(nullptr);
		} else {
			annotation->setEnd(nullptr);
		}
	}
	annotation = acquire(anno);
	// set the new reference.
	if (anno != nullptr) {
		if (start) {
			anno->setStart(this);
		} else {
			anno->setEnd(this);
		}
	}
}

bool Anchor::isStart() const
{
	if (annotation == nullptr) {
		return false;
	}
	return annotation->getStart() == this;
}

bool Anchor::isEnd() const
{
	if (annotation == nullptr) {
		return false;
	}
	return annotation->getEnd() == this;
}

/* Class AnnotationEntity */

AnnotationEntity::AnnotationEntity(Manager &mgr, Handle<Document> parent,
                                   Handle<AnnotationClass> descriptor,
                                   Handle<Anchor> start, Handle<Anchor> end,
                                   Variant attributes, std::string name)
    : Node(mgr, std::move(name), parent),
      DocumentEntity(this, descriptor, attributes)
{
	if (parent != nullptr) {
		parent->addAnnotation(this);
	}
	setStart(start);
	setEnd(end);
}

bool AnnotationEntity::doValidate(Logger &logger) const
{
	bool valid = true;
	// check name
	if (!getName().empty()) {
		valid = valid & validateName(logger);
	}
	// check if this AnnotationEntity is correctly registered at its document.
	if (getParent() == nullptr) {
		logger.error("The parent is not set!", *this);
		valid = false;
	} else if (!getParent()->isa(&RttiTypes::Document)) {
		logger.error("The parent is not a document!", *this);
		valid = false;
	} else {
		Handle<Document> doc = getParent().cast<Document>();
		bool found = false;
		for (auto &a : doc->getAnnotations()) {
			if (a == this) {
				found = true;
				break;
			}
		}
		if (!found) {
			logger.error("This annotation was not registered at the document.",
			             *this);
			valid = false;
		}
		// check if the Anchors are part of the right document.
		if (start == nullptr) {
			logger.error("This annotation has no start Anchor!", *this);
			valid = false;
		} else if (!doc->hasChild(start)) {
			logger.error(
			    "This annotations start anchor was not part of the same "
			    "document!",
			    *this);
			valid = false;
		}
		if (end == nullptr) {
			logger.error("This annotation has no end Anchor!", *this);
			valid = false;
		} else if (!doc->hasChild(end)) {
			logger.error(
			    "This annotations end anchor was not part of the same "
			    "document!",
			    *this);
			valid = false;
		}
	}
	// check if the Anchors reference this AnnotationEntity correctly.
	if (start != nullptr) {
		if (start->getAnnotation() != this) {
			logger.error(
			    "This annotations start anchor does not have the correct "
			    "annotation as parent!",
			    *this);
			valid = false;
		}
	}
	if (end != nullptr) {
		if (end->getAnnotation() != this) {
			logger.error(
			    "This annotations end anchor does not have the correct "
			    "annotation as parent!",
			    *this);
			valid = false;
		}
	}

	// check the validity as a DocumentEntity.
	return valid & DocumentEntity::doValidate(logger);
}

void AnnotationEntity::setStart(Handle<Anchor> s)
{
	if (start == s) {
		return;
	}
	invalidate();
	start = acquire(s);
	if (s != nullptr) {
		s->setAnnotation(this, true);
	}
}

void AnnotationEntity::setEnd(Handle<Anchor> e)
{
	if (end == e) {
		return;
	}
	invalidate();
	end = acquire(e);
	if (e != nullptr) {
		e->setAnnotation(this, false);
	}
}

/* Class Document */

void Document::doResolve(ResolutionState &state)
{
	continueResolveComposita(annotations, annotations.getIndex(), state);
	if (root != nullptr) {
		continueResolveCompositum(root, state);
	}
	continueResolveReferences(ontologies, state);
	continueResolveReferences(typesystems, state);
}

bool Document::doValidate(Logger &logger) const
{
	// An empty document is always invalid. TODO: Is this a smart choice?
	bool valid = true;
	if (root == nullptr) {
		logger.error("This document is empty (it has no root)!", *this);
		valid = false;
	} else {
		// check if the root is allowed to be a root.
		if (!root->getDescriptor()
		         .cast<StructuredClass>()
		         ->hasRootPermission()) {
			logger.error(std::string("A node of type \"") +
			                 root->getDescriptor()->getName() +
			                 "\" is not allowed to be the Document root!",
			             *root);
			valid = false;
		}
		// check if it has this document as parent.
		if (root->getParent() != this) {
			logger.error(
			    "The document root does not have the document as parent!",
			    *root);
			valid = false;
		}
		// then call validate on the root
		valid = valid & root->validate(logger);
	}
	// call validate on the AnnotationEntities
	return valid & continueValidation(annotations, logger);
}

void Document::doReference(Handle<Node> node)
{
	if (node->isa(&RttiTypes::Ontology)) {
		referenceOntology(node.cast<Ontology>());
	}
	if (node->isa(&RttiTypes::Typesystem)) {
		referenceTypesystem(node.cast<Typesystem>());
	}
}

RttiSet Document::doGetReferenceTypes() const
{
	return RttiSet{&RttiTypes::Ontology, &RttiTypes::Typesystem};
}

Rooted<StructuredEntity> Document::createRootStructuredEntity(
    Handle<StructuredClass> descriptor, Variant attributes, std::string name)
{
	return Rooted<StructuredEntity>{
	    new StructuredEntity(getManager(), Handle<Document>{this}, descriptor,
	                         attributes, std::move(name))};
}

void Document::addAnnotation(Handle<AnnotationEntity> a)
{
	// only add it if we need to.
	if (annotations.find(a) == annotations.end()) {
		invalidate();
		annotations.push_back(a);
	}
	Handle<Managed> par = a->getParent();
	if (par != this) {
		if (par != nullptr) {
			// remove the StructuredClass from the old parent.
			par.cast<Document>()->removeAnnotation(a);
		}
		a->setParent(this);
	}
}

void Document::addAnnotations(const std::vector<Handle<AnnotationEntity>> &as)
{
	for (Handle<AnnotationEntity> a : as) {
		addAnnotation(a);
	}
}

bool Document::removeAnnotation(Handle<AnnotationEntity> a)
{
	auto it = annotations.find(a);
	if (it != annotations.end()) {
		invalidate();
		annotations.erase(it);
		a->setParent(nullptr);
		return true;
	}
	return false;
}

Rooted<AnnotationEntity> Document::createChildAnnotation(
    Handle<AnnotationClass> descriptor, Handle<Anchor> start,
    Handle<Anchor> end, Variant attributes, std::string name)
{
	return Rooted<AnnotationEntity>{
	    new AnnotationEntity(getManager(), this, descriptor, start, end,
	                         attributes, std::move(name))};
}

bool Document::hasChild(Handle<StructureNode> s) const
{
	Rooted<Managed> parent = s->getParent();
	if (parent->isa(&RttiTypes::StructureNode)) {
		return hasChild(parent.cast<StructureNode>());
	} else if (parent->isa(&RttiTypes::AnnotationEntity)) {
		Handle<AnnotationEntity> a = parent.cast<AnnotationEntity>();
		return this == a->getParent();
	} else if (parent->isa(&RttiTypes::Document)) {
		return this == parent;
	}
	return false;
}

void Document::setRoot(Handle<StructuredEntity> rt)
{
	if (rt == nullptr || root == rt) {
		return;
	}
	if (root != nullptr) {
		root->setParent(nullptr);
	}
	root = acquire(rt);
	if (rt->getParent() != this) {
		rt->setParent(this);
	}
	invalidate();
}

/* Type registrations */
namespace RttiTypes {
const Rtti Document = RttiBuilder<ousia::Document>("Document")
                          .parent(&RootNode)
                          .composedOf({&AnnotationEntity, &StructuredEntity});
const Rtti StructureNode =
    RttiBuilder<ousia::StructureNode>("StructureNode").parent(&Node);
const Rtti StructuredEntity =
    RttiBuilder<ousia::StructuredEntity>("StructuredEntity")
        .parent(&StructureNode)
        .composedOf({&StructuredEntity, &DocumentPrimitive, &Anchor});
const Rtti DocumentPrimitive = RttiBuilder<ousia::DocumentPrimitive>(
                                   "DocumentPrimitive").parent(&StructureNode);
const Rtti Anchor = RttiBuilder<ousia::Anchor>("Anchor").parent(&StructureNode);
const Rtti AnnotationEntity =
    RttiBuilder<ousia::AnnotationEntity>("AnnotationEntity")
        .parent(&Node)
        .composedOf({&StructuredEntity, &DocumentPrimitive, &Anchor});
}
}