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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
|
/*
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/>.
*/
/**
* @file Document.hpp
*
* This header contains the class hierarchy of actual document classes. A graph
* of connected instances of these nodes is a "Document". How the different
* DocumentEntity instances may be connected within the graph is subject to the
* specification in the respective Domain(s) (see also the Domain.hpp).
*
* A Document, from top to bottom, consists of "Document" instance,
* which "owns" the structural root node of the in-document graph. This might
* for example be a "book" node of the "book" domain. That root node in turn has
* structure nodes as children, which in turn may have children. This
* constitutes a Structure Tree. Additionally annotations may be attached to
* Structure Nodes, effectively resulting in a Document Graph instead of a
* Document Tree (other references may introduce cycles as well).
*
* Consider this XML representation of a document using the "book" domain:
*
* \code{.xml}
* <doc>
* <head>
* <import rel="domain" src="book_domain.oxm"/>
* <import rel="domain" src="emphasized_domain.oxm"/>
* <alias tag="paragraph" aka="p"/>
* </head>
* <book>
* This might be some introductory text or a dedication. Ideally, of
* course, such elements would be semantically specified as such in
* additional domains (or in this one).
* <chapter name="myFirstChapter">
* Here we might have an introduction to the chapter, including some
* overview of the chapters structure.
* <section name="myFirstSection">
* Here we might find the actual section content.
* </section>
* <section name="mySndSection">
* Here we might find the actual section <em>content</em>.
*
*
* And there might even be another paragraph.
* </section>
* </chapter>
* </book>
* </doc>
* \endcode
*
* As can be seen the StructureEntities inherently follow a tree structure that
* is restricted by the implicit context free grammar of the "book" Domain
* definition (e.g. it is not allowed to have a "book" node inside a "section";
* refer to te Domain.hpp for more information).
*
* Another interesting fact is the special place of AnnotationEntities: They are
* Defined by start and end Anchors in the text. Note that this allows for
* overlapping annotations and provides a more intuitive (and semantically
* sound) handling of such span-like concepts. So the
*
* \code{.xml}
* <em>content</em>
* \endcode
*
* is implicitly expanded to:
*
* \code{.xml}
* <a id="1"/>content<a id="2"/>
* <emphasized start="1" end="2"/>
* \endcode
*
* Note that the place of an AnnotationEntity within the XML above is not
* strictly defined. It might as well be placed as a child of the "book" node.
* In general it is recommended to use the lowest possible place in the
* StructureTree to include the AnnotationEntity for better readability.
*
* Also note that text content like
*
* Here we might find the actual section content.
*
* is implicitly expanded using transparency to:
*
* \code{.xml}
* <paragraph>
* <text>
* Here we might find the actual section content.
* </text>
* </paragraph>
* \endcode
*
* @author Benjamin Paaßen (bpaassen@techfak.uni-bielefeld.de)
*/
#ifndef _OUSIA_MODEL_DOCUMENT_HPP_
#define _OUSIA_MODEL_DOCUMENT_HPP_
#include <set>
#include <core/managed/ManagedContainer.hpp>
#include <core/common/Variant.hpp>
#include "Node.hpp"
#include "Domain.hpp"
#include "RootNode.hpp"
#include "Typesystem.hpp"
namespace ousia {
// Forward declarations
class Rtti;
class Document;
class StructureNode;
class StructuredEntity;
class DocumentPrimitive;
class AnnotationEntity;
class Anchor;
/**
* A DocumentEntity is the common superclass for StructuredEntities and
* AnnotationEntities. Similarly to DescriptorEntity in the Domain.hpp it
* defines that each node in the Document graph may have attributes (in form
* of a struct Variant), and fields.
* The fields here are a vector of vectors. The first vector implements all
* fields while the inner vector contains all children in this field.
* We provide, however, convenience functions for better access via the field
* name.
*
*/
class DocumentEntity {
private:
/*
* this is a rather dirty method that should not be used in other cases:
* We store a handle to the Node instance that inherits from
* DocumentEntity. This Handle is not registered and would lead to Segfaults
* if we could not garantuee that it lives exactly as long as this
* DocumentEntity because the handle is for the subclass instance.
*/
Handle<Node> subInst;
Owned<Descriptor> descriptor;
Variant attributes;
std::vector<NodeVector<StructureNode>> fields;
void invalidateSubInstance();
protected:
bool doValidate(Logger &logger) const;
public:
/**
* The constructor for a DocumentEntity. Node that this does not inherit
* from Node. Therefore we need to have a handle to the subclass Node
* instance to create NodeVectors and Owned references.
*
* @param subInst is a handle to the subclass instance
* (e.g. StructuredEntity), such that the fields vectors
* and the descriptor reference can be obtained.
* @param descriptor is the Descriptor for this DocumentEntity, which will
* transformed to an Owned reference of the given owner.
* @param attributes is a Map Variant adhering to the attribute StructType
* in the given descriptor.
*/
DocumentEntity(Handle<Node> subInst, Handle<Descriptor> descriptor,
Variant attributes);
/**
* Returns the Descriptor for this DocumentEntity.
*
* @return the Descriptor for this DocumentEntity.
*/
Rooted<Descriptor> getDescriptor() const { return descriptor; }
/**
* Sets the Descriptor for this DocumentEntity.
*
* @param d is the new Descriptor for this DocumentEntity.
*/
void setDescriptor(Handle<Descriptor> d);
/**
* Returns a Map Variant adhering to the attribute StructType in the given
* descriptor.
*
* @return a Map Variant adhering to the attribute StructType in the given
* descriptor.
*/
Variant getAttributes() const { return attributes; }
/**
* Sets the attributes for this DocumentEntity. Attributes are set as a Map
* variant.
*
* @param a is a Map variant containing the attributes for this
* DocumentEntity.
*/
void setAttributes(const Variant &a);
/**
* This returns the vector of entities containing all members of the field
* with the given name.
*
* If the name is unknown an exception is thrown.
*
* @param fieldName is the name of a field as specified in the
* FieldDescriptor in the Domain description.
* @return a NodeVector of all StructuredEntities in that field.
*/
const NodeVector<StructureNode> &getField(
const std::string &fieldName = DEFAULT_FIELD_NAME) const;
/**
* This returns the vector of entities containing all members of the field
* with the given FieldDescriptor.
*
* If the FieldDescriptor does not belong to the Descriptor of this node
* an exception is thrown.
*
* @param fieldDescriptor is a FieldDescriptor defined in the Descriptor for
* this DocumentEntity.
* @return a NodeVector of all StructuredEntities in that
* field.
*/
const NodeVector<StructureNode> &getField(
Handle<FieldDescriptor> fieldDescriptor) const;
/**
* This returns the vector of entities containing all members of the field
* with the given index.
*
* If the index is out of bounds an exception is thrown.
*
* @param idx is the index of a field as specified in the
* FieldDescriptor in the Domain description.
* @return a NodeVector of all StructuredEntities in that field.
*/
const NodeVector<StructureNode> &getField(const size_t &idx) const;
/**
* This adds a StructureNode to the field with the given index.
*
* This method also changes the parent of the newly added StructureNode if
* it is not set to this DocumentEntity already and removes it from the
* old parent.
*
* @param s is the StructureNode that shall be added.
* @param fieldIdx is the index of a field as specified in the
* FieldDescriptor in the Domain description.
*/
void addStructureNode(Handle<StructureNode> s, const size_t &fieldIdx);
/**
* This adds a StructureNode to the field with the given name.
*
* If the name is unknown an exception is thrown.
*
* This method also changes the parent of the newly added StructureNode if
* it is not set to this DocumentEntity already and removes it from the
* old parent.
*
* @param s is the StructureNode that shall be added.
* @param fieldName is the name of a field as specified in the
* FieldDescriptor in the Domain description.
*/
void addStructureNode(Handle<StructureNode> s,
const std::string &fieldName = DEFAULT_FIELD_NAME);
/**
* This adds multiple StructureNodes to the field with the given name.
*
* If the name is unknown an exception is thrown.
*
* This method also changes the parent of each newly added StructureNode if
* it is not set to this DocumentEntity already and removes it from the
* old parent.
*
* @param ss are the StructureNodes that shall be added.
* @param fieldName is the name of a field as specified in the
* FieldDescriptor in the Domain description.
*/
void addStructureNodes(const std::vector<Handle<StructureNode>> &ss,
const std::string &fieldName = DEFAULT_FIELD_NAME);
/**
* This removes a StructureNode from the field with the given index.
*
* This method also changes the parent of the removed StructureNode to null.
*
* @param s is the StructureNode that shall be removed.
* @param fieldIdx is the index of a field as specified in the
* FieldDescriptor in the Domain description.
* @return true if this StructureNode was a child here and false if
* if was not found.
*/
bool removeStructureNodeFromField(Handle<StructureNode> s,
const int &fieldIdx);
/**
* This removes a StructureNode from the field with the given name.
*
* If the name is unknown an exception is thrown.
*
* This method also changes the parent of the removed StructureNode to null.
*
* @param s is the StructureNode that shall be removed.
* @param fieldName is the name of a field as specified in the
* FieldDescriptor in the Domain description.
* @return true if this StructureNode was a child here and false if
* if was not found.
*/
bool removeStructureNodeFromField(
Handle<StructureNode> s,
const std::string &fieldName = DEFAULT_FIELD_NAME);
/**
* This adds a StructureNode to the field with the given FieldDescriptor.
*
* If the FieldDescriptor does not belong to the Descriptor of this node
* an exception is thrown.
*
* This method also changes the parent of the newly added StructureNode if
* it is not set to this DocumentEntity already and removes it from the
* old parent.
*
* @param s is the StructureNode that shall be added.
* @param fieldDescriptor is a FieldDescriptor defined in the Descriptor for
* this DocumentEntity.
*/
void addStructureNode(Handle<StructureNode> s,
Handle<FieldDescriptor> fieldDescriptor);
/**
* This adds multiple StructureNodes to the field with the given
* FieldDescriptor.
*
* If the FieldDescriptor does not belong to the Descriptor of this node
* an exception is thrown.
*
* This method also changes the parent of each newly added StructureNode if
* it is not set to this DocumentEntity already and removes it from the
* old parent.
*
* @param ss are the StructureNodes that shall be added.
* @param fieldDescriptor is a FieldDescriptor defined in the Descriptor for
* this DocumentEntity.
*/
void addStructureNodes(const std::vector<Handle<StructureNode>> &ss,
Handle<FieldDescriptor> fieldDescriptor);
/**
* This removes a StructureNode from the field with the given
* FieldDescriptor.
*
* This method also changes the parent of the removed StructureNode to null.
*
* @param s is the StructureNode that shall be removed.
* @param fieldDescriptor is a FieldDescriptor defined in the Descriptor for
* this DocumentEntity.
* @return true if this StructureNode was a child here and false if
* if was not found.
*/
bool removeStructureNodeFromField(Handle<StructureNode> s,
Handle<FieldDescriptor> fieldDescriptor);
/**
* This removes a StructureNode from this DocumentEntity. It iterates
* through all fields to find it.
*
* This method also changes the parent of the removed StructureNode to null.
*
* @param s is the StructureNode that shall be removed.
* @return true if this StructureNode was a child here and false if if was
* not found.
*/
bool removeStructureNode(Handle<StructureNode> s);
/**
* This creates a new StructuredEntity as child of this DocumentEntity.
*
* @param descriptor is the StructuredClass of this StructuredEntity.
* @param attributes is a Map Variant containing attribute fillings for this
* StructuredEntity. It is empty per default.
* @param fieldName is the name of the field, where the newly created
* StructuredEntity shall be added to this DocumentEntity.
* @param name is some name for this StructuredEntity that may be used
* for later reference. It is empty per default.
*
* @return the newly created StructuredEntity.
*/
Rooted<StructuredEntity> createChildStructuredEntity(
Handle<StructuredClass> descriptor,
Variant attributes = Variant::mapType{},
const std::string &fieldName = DEFAULT_FIELD_NAME,
std::string name = "");
/**
* This creates a new StructuredEntity as child of this DocumentEntity.
*
* @param descriptor is the StructuredClass of this StructuredEntity.
* @param attributes is a Map Variant containing attribute fillings for this
* StructuredEntity. It is empty per default.
* @param fieldIdx is the index of the field, where the newly created
* StructuredEntity shall be added to this DocumentEntity.
* @param name is some name for this StructuredEntity that may be used
* for later reference. It is empty per default.
*
* @return the newly created StructuredEntity.
*/
Rooted<StructuredEntity> createChildStructuredEntity(
Handle<StructuredClass> descriptor, const size_t &fieldIdx,
Variant attributes = Variant::mapType{}, std::string name = "");
/*
* Creates a new DocumentPrimitive as child of this DocumentEntity.
*
* @param content is a Variant containing the content of this
* DocumentPrimitive. The Type of this Variant is
* specified at the parents Descriptor for the given
* fieldName.
* @param fieldName is the name of the field, where the newly created
* StructuredEntity shall be added to this DocumentEntity.
*
* @return the newly created DocumentPrimitive.
*/
Rooted<DocumentPrimitive> createChildDocumentPrimitive(
Variant content, const std::string &fieldName = DEFAULT_FIELD_NAME);
/*
* Creates a new DocumentPrimitive as child of this DocumentEntity.
*
* @param fieldIdx is the index of the field, where the newly created
* StructuredEntity shall be added to this DocumentEntity.
* @param content is a Variant containing the content of this
* DocumentPrimitive. The Type of this Variant is
* specified at the parents Descriptor for the given
* fieldName.
*
* @return the newly created DocumentPrimitive.
*/
Rooted<DocumentPrimitive> createChildDocumentPrimitive(
Variant content, const size_t &fieldIdx);
/**
* Creates a new Anchor as child of this DocumentEntity.
*
* @param fieldName is the name of the field, where the newly created
* Anchor shall be added to this DocumentEntity.
*
* @return the newly created Anchor.
*/
Rooted<Anchor> createChildAnchor(
const std::string &fieldName = DEFAULT_FIELD_NAME);
/**
* Creates a new Anchor as child of this DocumentEntity.
*
* @param fieldIdx is the index of the field, where the newly created
* Anchor shall be added to this DocumentEntity.
*
* @return the newly created Anchor.
*/
Rooted<Anchor> createChildAnchor(const size_t &fieldIdx);
};
/**
* A StructureNode is a Node of the StructureTree of the document. This is a
* common superclass for StructuredEntity, Anchor and DocumentPrimitive.
*/
class StructureNode : public Node {
friend DocumentEntity;
protected:
bool doValidate(Logger &logger) const override;
public:
/**
* Constructor for a StructureNode in the StructureTree.
*/
StructureNode(Manager &mgr, std::string name, Handle<Node> parent,
const std::string &fieldName);
/**
* Constructor for a StructureNode in the StructureTree.
*/
StructureNode(Manager &mgr, std::string name, Handle<Node> parent,
const size_t &fieldIdx);
/**
* Constructor for an empty StructureNode.
*/
StructureNode(Manager &mgr, std::string name = "",
Handle<Node> parent = nullptr)
: Node(mgr, std::move(name), parent)
{
}
};
/**
* A StructuredEntity is an instance of a StructuredClass. For more
* information please refer to the header documentation above.
*/
class StructuredEntity : public StructureNode, public DocumentEntity {
friend Document;
private:
bool transparent = false;
protected:
bool doValidate(Logger &logger) const override;
public:
/**
* Constructor for a StructuredEntity in the Structure Tree.
*
* @param mgr is the Manager instance.
* @param parent is the parent DocumentEntity of this StructuredEntity
* in the DocumentTree. Note that this StructuredEntity
* will automatically register itself as child of this
* parent.
* @param descriptor is the StructuredClass of this StructuredEntity.
* @param attributes is a Map Variant containing attribute fillings for this
* StructuredEntity. It is empty per default.
* @param fieldName is the name of the field in the parent DocumentEntity
* where this StructuredEntity shall be added.
* @param name is some name for this StructuredEntity that may be used
* for later reference. It is empty per default.
*/
StructuredEntity(Manager &mgr, Handle<Node> parent,
Handle<StructuredClass> descriptor,
Variant attributes = Variant::mapType{},
const std::string &fieldName = DEFAULT_FIELD_NAME,
std::string name = "")
: StructureNode(mgr, std::move(name), parent, fieldName),
DocumentEntity(this, descriptor, std::move(attributes))
{
}
/**
* Constructor for a StructuredEntity in the Structure Tree.
*
* @param mgr is the Manager instance.
* @param parent is the parent DocumentEntity of this StructuredEntity
* in the DocumentTree. Note that this StructuredEntity
* will automatically register itself as child of this
* parent.
* @param descriptor is the StructuredClass of this StructuredEntity.
* @param fieldIdx is the index of the field in the parent DocumentEntity
* where this StructuredEntity shall be added.
* @param attributes is a Map Variant containing attribute fillings for this
* StructuredEntity. It is empty per default.
* @param name is some name for this StructuredEntity that may be used
* for later reference. It is empty per default.
*/
StructuredEntity(Manager &mgr, Handle<Node> parent,
Handle<StructuredClass> descriptor, const size_t &fieldIdx,
Variant attributes = Variant::mapType{},
std::string name = "")
: StructureNode(mgr, std::move(name), parent, fieldIdx),
DocumentEntity(this, descriptor, std::move(attributes))
{
}
/**
* Constructor for a StructuredEntity at the document root.
*
* @param mgr is the Manager instance.
* @param parent is the parent Document of this StructuredEntity. Note
* that this StructuredEntity will automatically register
* itself as child of this Document.
* @param descriptor is the StructuredClass of this StructuredEntity.
* @param attributes is a Map Variant containing attribute fillings for this
* StructuredEntity. It is empty per default.
* @param name is some name for this StructuredEntity that may be used
* for later reference. It is empty per default.
*/
StructuredEntity(Manager &mgr, Handle<Document> doc,
Handle<StructuredClass> descriptor,
Variant attributes = Variant::mapType{},
std::string name = "");
/**
* Constructor for an empty StructuredEntity that is not yet connected.
*
* @param mgr is the Manager instance.
* @param parent is the parent Document of this StructuredEntity. Note
* that this StructuredEntity will automatically register
* itself as child of this Document.
* @param descriptor is the StructuredClass of this StructuredEntity.
* @param attributes is a Map Variant containing attribute fillings for this
* StructuredEntity. It is empty per default.
* @param name is some name for this StructuredEntity that may be used
* for later reference. It is empty per default.
*/
StructuredEntity(Manager &mgr, Handle<Node> parent = nullptr,
Handle<StructuredClass> descriptor = nullptr,
Variant attributes = Variant::mapType{},
std::string name = "");
/**
* Returns true if and only if this element was created using transparency/
* if and only if this is an implicit element.
*
* @return true if and only if this element was created using transparency.
*/
bool isTransparent() const { return transparent; }
/**
* @param trans true if and only if this element was created using
* transparency/if and only if this is an implicit element.
*/
void setTransparent(bool trans) { transparent = trans; }
};
/**
* This is a wrapper for primitive types (Variants) inside the document graph.
* The most straightforward example for this is the actual document text, e.g.
* inside a paragraph. In that case this would represent a mere string.
*/
class DocumentPrimitive : public StructureNode {
private:
Variant content;
public:
/**
* Constructor for a DocumentPrimitive.
*
* @param mgr is the Manager instance.
* @param parent is the parent DocumentEntity of this DocumentPrimitive
* in the DocumentTree. Note that this DocumentPrimitive
* will automatically register itself as child of this
* parent.
* @param content is a Variant containing the content of this
* DocumentPrimitive. The Type of this Variant is
* specified at the parents Descriptor for the given
* fieldName.
* @param fieldName is the name of the field in the parent DocumentEntity
* where this DocumentPrimitive shall be added.
*/
DocumentPrimitive(Manager &mgr, Handle<Node> parent, Variant content,
const std::string &fieldName = DEFAULT_FIELD_NAME)
: StructureNode(mgr, "", parent, fieldName), content(content)
{
}
/**
* Constructor for a DocumentPrimitive.
*
* @param mgr is the Manager instance.
* @param parent is the parent DocumentEntity of this DocumentPrimitive
* in the DocumentTree. Note that this DocumentPrimitive
* will automatically register itself as child of this
* parent.
* @param content is a Variant containing the content of this
* DocumentPrimitive. The Type of this Variant is
* specified at the parents Descriptor for the given
* fieldName.
* @param fieldIdx is the index of the field in the parent DocumentEntity
* where this DocumentPrimitive shall be added.
*/
DocumentPrimitive(Manager &mgr, Handle<Node> parent, Variant content,
const size_t &fieldIdx)
: StructureNode(mgr, "", parent, fieldIdx), content(content)
{
}
/**
* Returns the content of this DocumentPrimitive.
*
* @return the content of this DocumentPrimitive.
*/
Variant getContent() const { return content; }
/**
* Sets the content of this DocumentPrimitive to the given Variant.
*
* @param c is the new content of this DocumentPrimitive.
*/
void setContent(const Variant &c)
{
invalidate();
content = c;
}
};
/**
* An Anchor is an elementary StructureNode without any children that
* marks a point in the text content of the document that can later be
* referenced by an AnnotationEntity as it start and end point.
* Please refer to the AnnotationEntity documentation for more information.
*/
class Anchor : public StructureNode {
private:
Owned<AnnotationEntity> annotation;
protected:
bool doValidate(Logger &logger) const override;
public:
/**
* Constructor for Anchor.
*
* @param mgr is the Manager instance.
* @param parent is the parent of this Anchor in the Structure Tree (!),
* not the AnnotationEntity that references this Anchor.
* Note that this Anchor will automatically register itself
* as child of the given parent.
* @param fieldName is the name of the field in the parent DocumentEntity
* where this Anchor shall be added.
*/
Anchor(Manager &mgr, Handle<Node> parent,
const std::string &fieldName = DEFAULT_FIELD_NAME)
: StructureNode(mgr, "", parent, fieldName)
{
}
/**
* Constructor for Anchor.
*
* @param mgr is the Manager instance.
* @param parent is the parent of this Anchor in the Structure Tree (!),
* not the AnnotationEntity that references this Anchor.
* Note that this Anchor will automatically register itself
* as child of the given parent.
* @param fieldIdx is the index of the field in the parent DocumentEntity
* where this Anchor shall be added.
*/
Anchor(Manager &mgr, Handle<Node> parent, const size_t &fieldIdx)
: StructureNode(mgr, "", parent, fieldIdx)
{
}
/**
* Returns the AnnotationEntity this Anchor belongs to.
*
* @return the AnnotationEntity this Anchor belongs to.
*/
Rooted<AnnotationEntity> getAnnotation() const { return annotation; }
/**
* Sets the AnnotationEntity this Anchor belongs to. If this Anchor belonged
* to an AnnotationEntity before already, this reference is removed. This
* also sets the start/end reference of the new AnnotationEntity this Anchor
* shall belong to.
*
* @param anno the new AnnotationEntity this Anchor shall belong to.
* @param start true if this Anchor should be added as start anchor, false
* if it should be added as end Anchor.
*/
void setAnnotation(Handle<AnnotationEntity> anno, bool start);
/**
* Returns true if and only if this Anchor is the start Anchor of the
* AnnotationEntity it belongs to. Note that this will return false also if
* no AnnotationEntity is set yet. So isStart() == false and isEnd() ==
* false is possible and occurs if and only if getAnnotation() == nullptr.
*
* @return true if and only if this Anchor is the start Anchor of the
* AnnotationEntity it belongs to.
*/
bool isStart() const;
/**
* Returns true if and only if this Anchor is the end Anchor of the
* AnnotationEntity it belongs to. Note that this will return false also if
* no AnnotationEntity is set yet. So isStart() == false and isEnd() ==
* false is possible and occurs if and only if getAnnotation() == nullptr.
*
* @return true if and only if this Anchor is the end Anchor of the
* AnnotationEntity it belongs to.
*/
bool isEnd() const;
};
/**
* An AnnotationEntity is a span-like instance that is not bound by the elements
* of the Structure Tree. An annotation may very well overlap and cross the
* limits of StructureEntities. A typical example for AnnotationEntities are
* the markups "emphasized" and "strong". In HTML like markup languages these
* concepts are handeled as structure elements, like this:
*
* \code{.xml}
* <em>emphasized</em> <em><strong>and</strong></em> <strong>strong</strong>
* \endcode
*
* which is neither intuitive nor semantically sound. Therefore we take the
* approach of anchoring the Annotation entities in the text like this:
*
* \code{.xml}
* <Anchor id=1/>emphasized <Anchor id=2/>and<Anchor id=3/> strong<Anchor id=4/>
* <AnnotationEntity class="emphasized" start=1 end=3/>
* <AnnotationEntity class="strong" start=2 end=4/>
* \endcode
*
* Which signifies that indeed the text "emphasized and" is emphasized, not
* the two text exerpts "emphasized" and "and" separately.
*
*/
class AnnotationEntity : public Node, public DocumentEntity {
friend DocumentEntity;
friend Document;
private:
Owned<Anchor> start;
Owned<Anchor> end;
protected:
bool doValidate(Logger &logger) const override;
public:
/**
* The constructor for an AnnotationEntity.
*
* @param mgr is the Manager instance.
* @param parent is the Document this AnnotationEntity is part of. The
* constructor will automatically register this
* AnnotationEntity at that document.
* @param descriptor is the AnnotationClass of this AnnotationEntity.
* @param start is the start Anchor of this AnnotationEntity. It has to
* be part of the Document given as parent.
* @param end is the end Anchor of this Annotationentity. It has to
* be part of the Document given as parent.
* @param attributes is a Map Variant containing attribute fillings for this
* AnnotationEntity. It is empty per default.
* @param name is some name for this AnnotationEntity that might be
* used for references later on. It is empty per default.
*/
AnnotationEntity(Manager &mgr, Handle<Document> parent = nullptr,
Handle<AnnotationClass> descriptor = nullptr,
Handle<Anchor> start = nullptr,
Handle<Anchor> end = nullptr,
Variant attributes = Variant::mapType{},
std::string name = "");
/**
* Returns the start Anchor of this AnnotationEntity.
*
* @return the start Anchor of this AnnotationEntity.
*/
Rooted<Anchor> getStart() const { return start; }
/**
* Returns the end Anchor of this AnnotationEntity.
*
* @return the end Anchor of this AnnotationEntity.
*/
Rooted<Anchor> getEnd() const { return end; }
/**
* Sets the start Anchor of this AnnotationEntity.
*
* @param s is the new start Anchor for this AnnotationEntity.
*/
void setStart(Handle<Anchor> s);
/**
* Sets the end Anchor of this AnnotationEntity.
*
* @param e is the new end Anchor for this AnnotationEntity.
*/
void setEnd(Handle<Anchor> e);
};
/**
* A Document is mainly a wrapper for the Root structure node of the Document
* Graph. It also references the domains that have been used within this
* document and the AnnotationEntities that span over Anchors in this Document.
*/
class Document : public RootNode {
private:
// TODO: Might there be several roots? E.g. metadata?
Owned<StructuredEntity> root;
NodeVector<AnnotationEntity> annotations;
NodeVector<Domain> domains;
NodeVector<Typesystem> typesystems;
protected:
void doResolve(ResolutionState &state) override;
bool doValidate(Logger &logger) const override;
void doReference(Handle<Node> node) override;
RttiSet doGetReferenceTypes() const override;
public:
/**
* This sets up an empty document.
*
* @param mgr is the Manager instance.
* @param name is a name for this Document.
*/
Document(Manager &mgr, std::string name)
: RootNode(mgr, std::move(name), nullptr),
annotations(this),
domains(this),
typesystems(this)
{
}
/**
* Sets the root StructuredEntity of this Document. This also sets the
* parent of the given StructuredEntity if it is not set to this Document
* already.
*/
void setRoot(Handle<StructuredEntity> root);
/**
* Returns the root StructuredEntity of this Document.
*
* @return the root StructuredEntity of this Document.
*/
Rooted<StructuredEntity> getRoot() const { return root; }
/**
* This creates a new StructuredEntity and adds it as root to this Document.
*
* @param descriptor is the StructuredClass of this StructuredEntity.
* @param attributes is a Map Variant containing attribute fillings for this
* StructuredEntity. It is empty per default.
* @param name is some name for this StructuredEntity that may be used
* for later reference. It is empty per default.
*
* @return the newly constructed StructuredEntity.
*/
Rooted<StructuredEntity> createRootStructuredEntity(
Handle<StructuredClass> descriptor,
Variant attributes = Variant::mapType{}, std::string name = "");
/**
* Returns a const reference to the NodeVector of AnnotationEntities that
* span over Anchors in this Documents structure.
*
* @return a const reference to the NodeVector of AnnotationEntities that
* span over Anchors in this Documents structure.
*/
const NodeVector<AnnotationEntity> &getAnnotations() const
{
return annotations;
}
/**
* Adds an AnnotationEntity to this Document. This also sets the parent
* of the given AnnotationEntity if it is not set to this Document already
* and removes it from the old Document.
*
* @param a is some AnnotationEntity
*/
void addAnnotation(Handle<AnnotationEntity> a);
/**
* Adds multiple AnnotationEntities to this Document. This also sets the
* parent of each given AnnotationEntity if it is not set to this Document
* already and removes it from the old Document.
*
* @param as is a vector of AnnotationEntities.
*/
void addAnnotations(const std::vector<Handle<AnnotationEntity>> &as);
/**
* Removes an AnnotationEntity from this Document. This also sets the parent
* of the given AnnotationEntity to null.
*
* @param a is some AnnotationEntity.
* @return true if the given AnnotationEntity was removed and false if this
* Document did not have the given AnnotationEntity as child.
*/
bool removeAnnotation(Handle<AnnotationEntity> a);
/**
* Creates a new AnnotationEntity as child of this Document.
*
* @param descriptor is the AnnotationClass of this AnnotationEntity.
* @param start is the start Anchor of this AnnotationEntity. It has to
* be part of this Document.
* @param end is the end Anchor of this Annotationentity. It has to
* be part of this Document.
* @param attributes is a Map Variant containing attribute fillings for this
* AnnotationEntity. It is empty per default.
* @param name is some name for this AnnotationEntity that might be
* used for references later on. It is empty per default.
*
* @return the newly constructed AnnotationEntity.
*/
Rooted<AnnotationEntity> createChildAnnotation(
Handle<AnnotationClass> descriptor, Handle<Anchor> start,
Handle<Anchor> end, Variant attributes = Variant::mapType{},
std::string name = "");
/**
* Returns a const reference to the NodeVector of Domains that are used
* within this Document.
*
* @return a const reference to the NodeVector of Domains that are used
* within this Document.
*/
const NodeVector<Domain> &getDomains() const { return domains; }
/**
* Adds a Domain reference to this Document.
*/
void referenceDomain(Handle<Domain> d)
{
invalidate();
domains.push_back(d);
}
/**
* Adds multiple Domain references to this Document.
*/
void referenceDomains(const std::vector<Handle<Domain>> &d)
{
invalidate();
domains.insert(domains.end(), d.begin(), d.end());
}
/**
* Adds a Typesystem reference to this Document.
*/
void referenceTypesystem(Handle<Typesystem> d)
{
invalidate();
typesystems.push_back(d);
}
/**
* Adds multiple Typesystem references to this Document.
*/
void referenceTypesystems(const std::vector<Handle<Typesystem>> &d)
{
invalidate();
typesystems.insert(typesystems.end(), d.begin(), d.end());
}
/**
* Returns true if and only if the given StructureNode is part of this
* document, meaning that there is a path of parent references in the
* Structure Tree leading from the given StructureNode to this Document.
*
* @param s is some StructureNode.
* @return true if and only if the given StructureNode is part of this
* document.
*/
bool hasChild(Handle<StructureNode> s) const;
};
namespace RttiTypes {
extern const Rtti Document;
extern const Rtti DocumentEntity;
extern const Rtti AnnotationEntity;
extern const Rtti StructureNode;
extern const Rtti StructuredEntity;
extern const Rtti DocumentPrimitive;
extern const Rtti Anchor;
}
}
#endif /* _OUSIA_MODEL_DOCUMENT_HPP_ */
|