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
|
/*
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/Rtti.hpp>
namespace ousia {
namespace model {
/* Class DocumentEntity */
int DocumentEntity::getFieldDescriptorIndex(const std::string &fieldName,
bool enforce) const
{
const NodeVector<FieldDescriptor> &fds = descriptor->getFieldDescriptors();
unsigned int f = 0;
// look if we have an empty name.
if (fieldName == "") {
// in that case we look for a default field.
// First: Do we only have one field?
if (fds.size() == 1) {
// if so we return that one.
return f;
}
// Second: Do we have a TREE field?
for (auto &fd : fds) {
if (fd->getFieldType() == FieldDescriptor::FieldType::TREE) {
return f;
}
f++;
}
} else {
// otherwise we return the FieldDescriptor with the correct name (if
// such a descriptor exists).
for (auto &fd : fds) {
if (fd->getName() == fieldName) {
return f;
}
f++;
}
}
if (enforce) {
throw OusiaException(descriptor->getName() +
" has no field with name " + fieldName);
} else {
return -1;
}
}
int DocumentEntity::getFieldDescriptorIndex(
Handle<FieldDescriptor> fieldDescriptor, bool enforce) const
{
if (fieldDescriptor.isNull()) {
throw OusiaException("The given FieldDescriptor handle is null!");
}
const NodeVector<FieldDescriptor> &fds = descriptor->getFieldDescriptors();
int f = 0;
for (auto &fd : fds) {
if (fd->getName() == fieldDescriptor->getName() &&
fd->getFieldType() == fieldDescriptor->getFieldType()) {
return f;
}
f++;
}
if (enforce) {
throw OusiaException(descriptor->getName() +
" has no field with name " +
fieldDescriptor->getName());
} else {
return -1;
}
}
bool DocumentEntity::validate(Logger &logger) const
{
// TODO: check the validated form of Attributes
// iterate over every field
for (unsigned int f = 0; f < fields.size(); f++) {
// 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 (descriptor->getFieldDescriptors()[f]->optional) {
continue;
}
/*
* if it is not optional we have to chack if zero is a valid
* cardinality.
*/
for (auto &ac :
descriptor->getFieldDescriptors()[f]->getChildren()) {
const size_t min = ac->getCardinality().min();
if (min > 0) {
logger.error(
std::string("Field ") +
descriptor->getFieldDescriptors()[f]->getName() +
" was empty but needs at least " + std::to_string(min) +
" elements of class " + ac->getName() +
" according to the definition of " +
descriptor->getName());
return false;
}
}
continue;
}
// create a set of allowed classes identified by their unique id.
std::set<ManagedUid> accs;
for (auto &ac : descriptor->getFieldDescriptors()[f]->getChildren()) {
accs.insert(ac->getUid());
}
// store the actual numbers of children for each child class in a map
std::map<ManagedUid, unsigned int> nums;
// iterate over every actual child of this DocumentEntity
for (auto &rc : fields[f]) {
if (!rc->isa(RttiTypes::StructuredEntity)) {
continue;
}
Handle<StructuredEntity> c = rc.cast<StructuredEntity>();
ManagedUid id = c->getDescriptor()->getUid();
// check if its class is allowed.
bool allowed = accs.find(id) != accs.end();
/*
* if it is not allowed directly, we have to check if the class is a
* child of a permitted class.
*/
if (!allowed) {
for (auto &ac :
descriptor->getFieldDescriptors()[f]->getChildren()) {
if (c->getDescriptor()
.cast<StructuredClass>()
->isSubclassOf(ac)) {
allowed = true;
id = ac->getUid();
}
}
}
if (!allowed) {
logger.error(std::string("An instance of ") +
c->getDescriptor()->getName() +
" is not allowed as child of an instance of " +
descriptor->getName() + " in field " +
descriptor->getFieldDescriptors()[f]->getName());
return false;
}
// note the number of occurences.
const auto &n = nums.find(id);
if (n != nums.end()) {
n->second++;
} else {
nums.emplace(id, 1);
}
}
// now check if the cardinalities are right.
for (auto &ac : descriptor->getFieldDescriptors()[f]->getChildren()) {
const auto &n = nums.find(ac->getUid());
unsigned int num = 0;
if (n != nums.end()) {
num = n->second;
}
if (!ac->getCardinality().contains(num)) {
logger.error(
std::string("Field ") +
descriptor->getFieldDescriptors()[f]->getName() + " had " +
std::to_string(num) + " elements of class " +
ac->getName() +
", which is invalid according to the definition of " +
descriptor->getName());
return false;
}
}
}
return true;
}
/* Class StructureNode */
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!");
}
}
/* 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);
}
/* 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),
start(acquire(start)),
end(acquire(end))
{
parent->annotations.push_back(this);
}
/* Class Document */
void Document::continueResolve(ResolutionState &state)
{
continueResolveComposita(annotations, annotations.getIndex(), state);
if (root != nullptr) {
continueResolveCompositum(root, state);
}
continueResolveReferences(domains, state);
}
}
/* Type registrations */
namespace RttiTypes {
const Rtti<model::Document> Document =
RttiBuilder("Document").parent(&Node).composedOf(
{&AnnotationEntity, &StructuredEntity});
const Rtti<model::StructureNode> StructureNode =
RttiBuilder("StructureNode").parent(&Node);
const Rtti<model::StructuredEntity> StructuredEntity =
RttiBuilder("StructuredEntity").parent(&StructureNode).composedOf(
{&StructuredEntity, &DocumentPrimitive, &Anchor});
const Rtti<model::DocumentPrimitive> DocumentPrimitive =
RttiBuilder("DocumentPrimitive").parent(&StructureNode);
const Rtti<model::Anchor> Anchor = RttiBuilder("Anchor").parent(&StructureNode);
const Rtti<model::AnnotationEntity> AnnotationEntity =
RttiBuilder("AnnotationEntity").parent(&Node).composedOf(
{&StructuredEntity, &DocumentPrimitive, &Anchor});
}
}
|