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
|
/*
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, if the respective document implements the
* "book" domain. That root node in turn has structure nodes as children as well
* as annotations that refer to the content of that structure node.
*
* Consider this simplified XML representation of a document (TODO: Use
* non-simplified XML as soon as possible):
*
* <Document implements="book">
* <StructureEntity class="book">
* <StructureEntity class="section">
* <DocumentPrimitive>
* This is some text with some <Anchor id="1"/>emphasized and
* <Anchor id="2"/>strong<Anchor id="3"/> text.
* </DocumentPrimitive>
* <AnnotationEntity class="emphasized" start="1", end="3"/>
* <AnnotationEntity class="strong" start="2", end="3"/>
* </StructureEntity>
* </StructureEntity>
* </Document>
*
* 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.
* 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.
*
* @author Benjamin Paaßen (bpaassen@techfak.uni-bielefeld.de)
*/
#ifndef _OUSIA_MODEL_DOCUMENT_HPP_
#define _OUSIA_MODEL_DOCUMENT_HPP_
#include <core/managed/ManagedContainer.hpp>
#include <core/Node.hpp>
#include <core/common/Variant.hpp>
#include "Domain.hpp"
#include "Typesystem.hpp"
namespace ousia {
namespace model {
class StructuredEntity;
class AnnotationEntity;
/**
* 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 : public Node {
private:
Owned<Descriptor> descriptor;
const Variant attributes;
std::vector<ManagedVector<StructuredEntity>> fields;
Rooted<FieldDescriptor> getFieldDescriptor(const std::string &fieldName);
public:
DocumentEntity(Manager &mgr, std::string name = "", Handle<Node> parent,
Handle<Descriptor> descriptor, Variant attributes)
: Node(mgr, std::move(name), parent),
descriptor(acquire(descriptor)),
attributes(std::move(attributes))
{
// TODO: Validation at construction time?
// insert empty vectors for each field.
for (int f = 0; f < descriptor->getFieldDescriptors.size(); f++) {
fields.push_back(ManagedVector(this));
}
}
Rooted<Descriptor> getDescriptor const() { return descriptor; }
const Variant &getAttributes() const { return attributes; }
Variant getAttributes const { return attributes; }
/**
* This allows a direct manipulation of the internal data structure of a
* DocumentEntity and is not recommended. TODO: Delete this?
*/
std::vector<ManagedVector<StructuredEntity>> &getFields() { return fields; }
/**
* This returns true if there is a FieldDescriptor in the Descriptor for
* this DocumentEntity which has the given name. If an empty name is
* given it is assumed that the 'default' FieldDescriptor is referenced,
* where 'default' means either:
* 1.) The only TREE typed FieldDescriptor (if present) or
* 2.) the only FieldDescriptor (if only one is specified).
*
* @param fieldName is the name of a field as specified in the
* FieldDescriptor in the Domain description.
* @return true if this FieldDescriptor exists.
*/
bool hasField(const std::string &fieldName = "")
{
return getFieldDescriptor(fieldName) != nullptr;
}
/**
* This returns the vector of entities containing all members of the field
* for which the FieldDescriptor has the specified name. If an empty name is
* given it is assumed that the 'default' FieldDescriptor is referenced,
* where 'default' means either:
* 1.) The only TREE typed FieldDescriptor (if present) or
* 2.) the only FieldDescriptor (if only one is specified).
*
* Note that the output of this method might well be ambigous: If no
* FieldDescriptor matches the given name an empty ManagedVector is
* returned. This is also the case, however, if there are no members for an
* existing field. Therefore it is recommended to additionally check the
* output of "hasField" or use the overloaded version of this method with
* a FieldDescriptor as input.
*
* @param fieldName is the name of the field as specified in the
* FieldDescriptor in the Domain description.
* @return a ManagedVector of all StructuredEntities in that field. If the
* field is unknown or if no members exist in that field yet, the
* ManagedVector will be empty. Note that the ManagedVector is
* returned as a reference, so it is possible to manipulate this
* DocumentEntities content using this function.
*/
ManagedVector<StructuredEntity> &getField(const std::string &fieldName = "")
{
Rooted<FieldDescriptor> fd = getFieldDescriptor(fieldName);
if (fd == nullptr) {
return ManagedVector<StructuredEntity>(this);
}
return getField(fd);
}
/**
* 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 ManagedVector of all StructuredEntities in that field.
*/
ManagedVector<StructuredEntity> &getField(
Rooted<FieldDescriptor> fieldDescriptor);
};
/**
* A StructuredEntity is a node in the Structure Tree of a document. For more
* information please refer to the header documentation above.
*/
class StructuredEntity : public DocumentEntity {
private:
ManagedVector<AnnotationEntity> annotations;
public:
StructuredEntity(Manager &mgr, std::string name = "", Handle<Node> parent,
Handle<StructuredClass> descriptor, Variant attributes)
: DocumentEntity(mgr, std::move(name), parent, descriptor, attributes),
annotations(this)
{
}
ManagedVector<AnnotationEntity> &getAnnotations() { return annotations; }
};
/**
* 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 StructuredEntity {
public:
DocumentPrimitive(Manager &mgr, Handle<StructuredEntity> parent,
Variant content)
: StructuredEntity(mgr, "", parent, nullptr, content)
{
}
Variant getContent() const { return getAttributes(); }
const Variant& getContent() const { return getAttributes(); }
// TODO: Override such methods like "getField" to disable them?
}
/**
* 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:
*
* <em>emphasized</em> <em><strong>and</strong></em> <strong>strong</strong>
*
* which is neither intuitive nor semantically sound. Therefore we take the
* approach of anchoring the Annotation entities in the text like this:
*
* <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/>
*
* Which signifies that indeed the text "emphasized and" is emphasized, not
* the two text exerpts "emphasized" and "and" separately.
*
*/
class AnnotationEntity : public DocumentEntity {
public:
/**
* An Anchor is an elementary StructuredEntity 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 StructuredEntity {
public:
/**
* @param mgr is the Manager instance.
* @param name is the Anchor id.
* @param parent is the parent of this Anchor in the Structure Tree (!),
* not the AnnotationEntity that references this Anchor.
*/
Anchor(Manager &mgr, std::string name = "",
Handle<StructuredEntity> parent)
: StructuredEntity(mgr, name, parent, nullptr, Variant())
{
}
};
private:
Owned<Anchor> start;
Owned<Anchor> end;
public:
AnnotationEntity(Manager &mgr, std::string name = "", Handle<Node> parent,
Handle<StructuredClass> descriptor, Variant attributes,
Handle<Anchor> start, Handle<Anchor> end)
: DocumentEntity(mgr, std::move(name), parent, descriptor, attributes),
start(acquire(start)),
end(acquire(end))
{
}
Rooted<Anchor> getStart() { return start; }
Rooted<Anchor> getEnd() { return end; }
};
}
}
#endif /* _OUSIA_MODEL_DOCUMENT_HPP_ */
|