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
|
/*
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 <gtest/gtest.h>
#include <iostream>
#include <core/common/Rtti.hpp>
#include <core/frontend/TerminalLogger.hpp>
#include <core/model/Document.hpp>
#include <core/model/Ontology.hpp>
#include "TestDocument.hpp"
#include "TestOntology.hpp"
namespace ousia {
TEST(DocumentEntity, searchStartAnchor)
{
// create a trivial ontology.
TerminalLogger logger{std::cerr, true};
Manager mgr{1};
Rooted<SystemTypesystem> sys{new SystemTypesystem(mgr)};
Rooted<Ontology> ontology{new Ontology(mgr, sys, "trivial")};
// we only have one StructuredClass that may have itself as a child.
Rooted<StructuredClass> A = ontology->createStructuredClass(
"A", Cardinality::any(), nullptr, false, true);
Rooted<FieldDescriptor> A_field = A->createFieldDescriptor(logger).first;
A_field->addChild(A);
// create two AnnotationClasses.
Rooted<AnnotationClass> Alpha = ontology->createAnnotationClass("Alpha");
Rooted<AnnotationClass> Beta = ontology->createAnnotationClass("Beta");
// validate this ontology.
ASSERT_TRUE(ontology->validate(logger));
// create a trivial document.
Rooted<Document> doc{new Document(mgr, "myDoc")};
Rooted<StructuredEntity> root = doc->createRootStructuredEntity(A);
// add an Anchor.
Rooted<Anchor> a = root->createChildAnchor();
// create an AnnotationEntity with the Anchor as start.
doc->createChildAnnotation(Alpha, a, nullptr, Variant::mapType{}, "myAnno");
// We should be able to find the Anchor now if we look for it.
ASSERT_EQ(a, root->searchStartAnchor(0));
ASSERT_EQ(a, root->searchStartAnchor(0, Alpha));
ASSERT_EQ(a, root->searchStartAnchor(0, nullptr, "myAnno"));
ASSERT_EQ(a, root->searchStartAnchor(0, Alpha, "myAnno"));
// but we should not find it if we look for an Anchor of a different
// AnnotationClass.
ASSERT_EQ(nullptr, root->searchStartAnchor(0, Beta));
// now add a child to the root node and place the Anchor there.
Rooted<StructuredEntity> child = root->createChildStructuredEntity(A);
Rooted<Anchor> b = root->createChildAnchor();
doc->createChildAnnotation(Alpha, b, nullptr, Variant::mapType{}, "myAnno");
// now b should be returned because its closer.
ASSERT_EQ(b, root->searchStartAnchor(0));
ASSERT_EQ(b, root->searchStartAnchor(0, Alpha));
ASSERT_EQ(b, root->searchStartAnchor(0, nullptr, "myAnno"));
ASSERT_EQ(b, root->searchStartAnchor(0, Alpha, "myAnno"));
}
TEST(DocumentEntity, searchStartAnchorCycles)
{
// create a trivial ontology.
Logger logger;
Manager mgr{1};
Rooted<SystemTypesystem> sys{new SystemTypesystem(mgr)};
Rooted<Ontology> ontology{new Ontology(mgr, sys, "trivial")};
// we only have one StructuredClass that may have itself as a child.
Rooted<StructuredClass> A = ontology->createStructuredClass(
"A", Cardinality::any(), nullptr, false, true);
Rooted<FieldDescriptor> A_field = A->createFieldDescriptor(logger).first;
A_field->addChild(A);
// create an AnnotationClass.
Rooted<AnnotationClass> Alpha = ontology->createAnnotationClass("Alpha");
// validate this ontology.
ASSERT_TRUE(ontology->validate(logger));
// create a trivial but cyclic document.
Rooted<Document> doc{new Document(mgr, "myDoc")};
Rooted<StructuredEntity> root = doc->createRootStructuredEntity(A);
// add an Anchor.
Rooted<Anchor> a = root->createChildAnchor();
// create an AnnotationEntity with the Anchor as start.
doc->createChildAnnotation(Alpha, a, nullptr, Variant::mapType{}, "myAnno");
// add the cyclic reference.
root->addStructureNode(root, 0);
// We should be able to find the Anchor now if we look for it. There should
// be no loops.
ASSERT_EQ(a, root->searchStartAnchor(0));
ASSERT_EQ(a, root->searchStartAnchor(0, Alpha));
ASSERT_EQ(a, root->searchStartAnchor(0, nullptr, "myAnno"));
ASSERT_EQ(a, root->searchStartAnchor(0, Alpha, "myAnno"));
}
TEST(DocumentEntity, searchStartAnchorUpwards)
{
// create a trivial ontology.
TerminalLogger logger{std::cerr, true};
Manager mgr{1};
Rooted<SystemTypesystem> sys{new SystemTypesystem(mgr)};
Rooted<Ontology> ontology{new Ontology(mgr, sys, "trivial")};
// we only have one StructuredClass that may have itself as a child in the
// default field or a subtree field.
Rooted<StructuredClass> A = ontology->createStructuredClass(
"A", Cardinality::any(), nullptr, false, true);
Rooted<FieldDescriptor> A_field = A->createFieldDescriptor(logger).first;
Rooted<FieldDescriptor> A_sub_field =
A->createFieldDescriptor(logger, FieldDescriptor::FieldType::SUBTREE,
"sub").first;
A_field->addChild(A);
A_sub_field->addChild(A);
// create two AnnotationClasses.
Rooted<AnnotationClass> Alpha = ontology->createAnnotationClass("Alpha");
Rooted<AnnotationClass> Beta = ontology->createAnnotationClass("Beta");
// add a tree field to the annotation class.
Rooted<FieldDescriptor> Alpha_field =
Alpha->createFieldDescriptor(logger).first;
Alpha_field->addChild(A);
// validate this ontology.
ASSERT_TRUE(ontology->validate(logger));
// create a document with a root node, and two children, one in the
// default and one in the subtree field.
Rooted<Document> doc{new Document(mgr, "myDoc")};
Rooted<StructuredEntity> root = doc->createRootStructuredEntity(A);
// add an Anchor.
Rooted<Anchor> a = root->createChildAnchor();
// create an AnnotationEntity with the Anchor as start.
Rooted<AnnotationEntity> anno = doc->createChildAnnotation(
Alpha, a, nullptr, Variant::mapType{}, "myAnno");
// add a child.
Rooted<StructuredEntity> child = root->createChildStructuredEntity(A);
// We should be able to find the Anchor from the child node now. if we look
// for it.
ASSERT_EQ(a, child->searchStartAnchor(1));
ASSERT_EQ(a, child->searchStartAnchor(1, Alpha));
ASSERT_EQ(a, child->searchStartAnchor(1, nullptr, "myAnno"));
ASSERT_EQ(a, child->searchStartAnchor(1, Alpha, "myAnno"));
// we should not be able to find it from the subtree field, however.
ASSERT_EQ(nullptr, child->searchStartAnchor(0));
// and also we should not be able to find it from the annotation itself.
ASSERT_EQ(nullptr, anno->searchStartAnchor(0));
// but we can find a new anchor inside the annotation.
Rooted<Anchor> b = anno->createChildAnchor();
doc->createChildAnnotation(Beta, b, nullptr, Variant::mapType{}, "myAnno");
ASSERT_EQ(b, anno->searchStartAnchor(0));
ASSERT_EQ(b, anno->searchStartAnchor(0, Beta));
ASSERT_EQ(b, anno->searchStartAnchor(0, nullptr, "myAnno"));
ASSERT_EQ(b, anno->searchStartAnchor(0, Beta, "myAnno"));
}
TEST(Document, construct)
{
// Construct Manager
TerminalLogger logger{std::cerr, true};
Manager mgr{1};
Rooted<SystemTypesystem> sys{new SystemTypesystem(mgr)};
// Get the ontology.
Rooted<Ontology> ontology = constructBookOntology(mgr, sys, logger);
// Construct the document.
Rooted<Document> doc = constructBookDocument(mgr, logger, ontology);
// Check the document content.
ASSERT_FALSE(doc.isNull());
// get root node.
Rooted<StructuredEntity> root = doc->getRoot();
ASSERT_FALSE(root.isNull());
ASSERT_EQ("book", root->getDescriptor()->getName());
ASSERT_TRUE(root->getDescriptor()->hasField());
ASSERT_EQ(2U, root->getField().size());
// get foreword (paragraph)
{
Rooted<StructuredEntity> foreword =
root->getField()[0].cast<StructuredEntity>();
ASSERT_FALSE(foreword.isNull());
ASSERT_EQ("paragraph", foreword->getDescriptor()->getName());
// it should contain one text node
ASSERT_TRUE(foreword->getDescriptor()->hasField());
ASSERT_EQ(1U, foreword->getField().size());
// which in turn should have a primitive content field containing the
// right text.
{
Rooted<StructuredEntity> text =
foreword->getField()[0].cast<StructuredEntity>();
ASSERT_FALSE(text.isNull());
ASSERT_EQ("text", text->getDescriptor()->getName());
ASSERT_TRUE(text->getDescriptor()->hasField());
ASSERT_EQ(1U, text->getField().size());
ASSERT_TRUE(text->getField()[0]->isa(typeOf<DocumentPrimitive>()));
Variant content =
text->getField()[0].cast<DocumentPrimitive>()->getContent();
ASSERT_EQ("Some introductory text", content.asString());
}
}
// get section
{
Rooted<StructuredEntity> section =
root->getField()[1].cast<StructuredEntity>();
ASSERT_FALSE(section.isNull());
ASSERT_EQ("section", section->getDescriptor()->getName());
// it should contain one paragraph
ASSERT_TRUE(section->getDescriptor()->hasField());
ASSERT_EQ(1U, section->getField().size());
{
Rooted<StructuredEntity> par =
section->getField()[0].cast<StructuredEntity>();
ASSERT_FALSE(par.isNull());
ASSERT_EQ("paragraph", par->getDescriptor()->getName());
// it should contain one text node
ASSERT_TRUE(par->getDescriptor()->hasField());
ASSERT_EQ(1U, par->getField().size());
// which in turn should have a primitive content field containing
// the right text.
{
Rooted<StructuredEntity> text =
par->getField()[0].cast<StructuredEntity>();
ASSERT_FALSE(text.isNull());
ASSERT_EQ("text", text->getDescriptor()->getName());
ASSERT_TRUE(text->getDescriptor()->hasField());
ASSERT_EQ(1U, text->getField().size());
ASSERT_TRUE(
text->getField()[0]->isa(typeOf<DocumentPrimitive>()));
Variant content =
text->getField()[0].cast<DocumentPrimitive>()->getContent();
ASSERT_EQ("Some actual text", content.asString());
}
}
}
}
TEST(Document, validate)
{
// Let's start with a trivial ontology and a trivial document.
TerminalLogger logger{std::cerr, true};
Manager mgr{1};
Rooted<SystemTypesystem> sys{new SystemTypesystem(mgr)};
Rooted<Ontology> ontology{new Ontology(mgr, sys, "trivial")};
Cardinality single;
single.merge({1});
// Set up the "root" StructuredClass.
Rooted<StructuredClass> rootClass{new StructuredClass(
mgr, "root", ontology, single, {nullptr}, false, true)};
// set up a document for it.
{
// first an invalid one, which is empty.
Rooted<Document> doc{new Document(mgr, "myDoc.oxd")};
doc->referenceOntology(ontology);
ASSERT_EQ(ValidationState::UNKNOWN, doc->getValidationState());
ASSERT_FALSE(doc->validate(logger));
// then add a root, which should make it valid.
Rooted<StructuredEntity> root =
buildRootStructuredEntity(doc, logger, {"root"});
ASSERT_EQ(ValidationState::UNKNOWN, doc->getValidationState());
ASSERT_TRUE(doc->validate(logger));
}
{
// A root with an invalid name, however, should make it invalid
Rooted<Document> doc{new Document(mgr, "myDoc.oxd")};
doc->referenceOntology(ontology);
Rooted<StructuredEntity> root = buildRootStructuredEntity(
doc, logger, {"root"}, {}, "my invalid root");
ASSERT_EQ(ValidationState::UNKNOWN, doc->getValidationState());
ASSERT_FALSE(doc->validate(logger));
}
// now let's extend the rootClass with a default field.
Rooted<FieldDescriptor> rootField =
rootClass->createFieldDescriptor(logger).first;
// and add a child class for it.
Rooted<StructuredClass> childClass{
new StructuredClass(mgr, "child", ontology, single)};
rootField->addChild(childClass);
{
/*
* now check again: Because the child has the cardinality {1} our
* document should be invalid again.
*/
Rooted<Document> doc{new Document(mgr, "myDoc.oxd")};
doc->referenceOntology(ontology);
Rooted<StructuredEntity> root =
buildRootStructuredEntity(doc, logger, {"root"});
ASSERT_EQ(ValidationState::UNKNOWN, doc->getValidationState());
ASSERT_FALSE(doc->validate(logger));
// but it should get valid if we add a proper child.
buildStructuredEntity(doc, logger, root, {"child"});
ASSERT_EQ(ValidationState::UNKNOWN, doc->getValidationState());
ASSERT_TRUE(doc->validate(logger));
// and it should get invalid again if we add one more child.
buildStructuredEntity(doc, logger, root, {"child"});
ASSERT_EQ(ValidationState::UNKNOWN, doc->getValidationState());
ASSERT_FALSE(doc->validate(logger));
}
/*
* Add a further extension to the ontology: We add a subclass to child.
*/
Rooted<StructuredClass> childSubClass{
new StructuredClass(mgr, "childSub", ontology, single, childClass)};
{
/*
* A document with one instance of the Child subclass should be valid.
*/
Rooted<Document> doc{new Document(mgr, "myDoc.oxd")};
doc->referenceOntology(ontology);
Rooted<StructuredEntity> root =
buildRootStructuredEntity(doc, logger, {"root"});
buildStructuredEntity(doc, logger, root, {"childSub"});
ASSERT_EQ(ValidationState::UNKNOWN, doc->getValidationState());
ASSERT_TRUE(doc->validate(logger));
}
/*
* Make it even more complicated: child gets a field for further child
* instances now.
*/
Rooted<FieldDescriptor> childField =
childClass->createFieldDescriptor(logger).first;
childField->addChild(childClass);
{
/*
* Now a document with one instance of the Child subclass should be
* invalid, because it has no children of itself.
*/
Rooted<Document> doc{new Document(mgr, "myDoc.oxd")};
doc->referenceOntology(ontology);
Rooted<StructuredEntity> root =
buildRootStructuredEntity(doc, logger, {"root"});
buildStructuredEntity(doc, logger, root, {"childSub"});
ASSERT_EQ(ValidationState::UNKNOWN, doc->getValidationState());
ASSERT_FALSE(doc->validate(logger));
}
/*
* Override the default field in childSubClass with an optional field.
*/
Rooted<FieldDescriptor> childSubField =
childSubClass->createFieldDescriptor(logger,
FieldDescriptor::FieldType::TREE,
"dummy", true).first;
// add a child pro forma to make it valid.
childSubField->addChild(childSubClass);
{
/*
* Now a document with one instance of the Child subclass should be
* valid, because of the override.
*/
Rooted<Document> doc{new Document(mgr, "myDoc.oxd")};
doc->referenceOntology(ontology);
Rooted<StructuredEntity> root =
buildRootStructuredEntity(doc, logger, {"root"});
buildStructuredEntity(doc, logger, root, {"childSub"});
ASSERT_EQ(ValidationState::UNKNOWN, doc->getValidationState());
ASSERT_TRUE(doc->validate(logger));
}
// add a primitive field to the subclass with integer content.
Rooted<FieldDescriptor> primitive_field =
childSubClass->createPrimitiveFieldDescriptor(
sys->getIntType(), logger,
FieldDescriptor::FieldType::SUBTREE, "int",
false).first;
{
/*
* Now a document with one instance of the Child subclass should be
* invalid again, because we are missing the primitive content.
*/
Rooted<Document> doc{new Document(mgr, "myDoc.oxd")};
doc->referenceOntology(ontology);
Rooted<StructuredEntity> root =
buildRootStructuredEntity(doc, logger, {"root"});
Rooted<StructuredEntity> child =
buildStructuredEntity(doc, logger, root, {"childSub"});
ASSERT_EQ(ValidationState::UNKNOWN, doc->getValidationState());
ASSERT_FALSE(doc->validate(logger));
// if we add a DocumentPrimitive with the wrong content it should not
// work either.
Rooted<DocumentPrimitive> primitive{
new DocumentPrimitive(mgr, child, {"ololol"}, "int")};
ASSERT_FALSE(doc->validate(logger));
// but if we set the content right, it should work.
primitive->setContent({2});
ASSERT_EQ(ValidationState::UNKNOWN, doc->getValidationState());
ASSERT_TRUE(doc->validate(logger));
}
// Now add an Annotation class to the ontology.
Rooted<AnnotationClass> annoClass{
new AnnotationClass(mgr, "anno", ontology)};
{
/*
* Create a valid document in itself.
*/
Rooted<Document> doc{new Document(mgr, "myDoc.oxd")};
doc->referenceOntology(ontology);
Rooted<StructuredEntity> root =
buildRootStructuredEntity(doc, logger, {"root"});
Rooted<Anchor> start{new Anchor(mgr, root)};
Rooted<StructuredEntity> child =
buildStructuredEntity(doc, logger, root, {"childSub"});
Rooted<DocumentPrimitive> primitive{
new DocumentPrimitive(mgr, child, {2}, "int")};
Rooted<Anchor> end{new Anchor(mgr, root)};
ASSERT_EQ(ValidationState::UNKNOWN, doc->getValidationState());
ASSERT_TRUE(doc->validate(logger));
// then add an AnnotationEntity without Anchors.
Rooted<AnnotationEntity> anno =
buildAnnotationEntity(doc, logger, {"anno"}, nullptr, nullptr);
ASSERT_EQ(ValidationState::UNKNOWN, doc->getValidationState());
ASSERT_FALSE(doc->validate(logger));
// but it should be valid again if we set the start end and Anchor.
anno->setStart(start);
anno->setEnd(end);
ASSERT_EQ(ValidationState::UNKNOWN, doc->getValidationState());
ASSERT_TRUE(doc->validate(logger));
// add an attribute to the root, which should make it invalid.
root->setAttributes(Variant::mapType{{"bla", 2}});
ASSERT_EQ(ValidationState::UNKNOWN, doc->getValidationState());
ASSERT_FALSE(doc->validate(logger));
// if we reset it to an empty map it should be valid again
root->setAttributes(Variant::mapType{});
ASSERT_EQ(ValidationState::UNKNOWN, doc->getValidationState());
ASSERT_TRUE(doc->validate(logger));
// let's set an attribute descriptor.
childSubClass->getAttributesDescriptor()->addAttribute(
new Attribute{mgr, "myAttr", sys->getStringType(), "default"},
logger);
// the right map content should be valid now.
child->setAttributes(Variant::mapType{{"myAttr", "bla"}});
ASSERT_EQ(ValidationState::UNKNOWN, doc->getValidationState());
ASSERT_TRUE(doc->validate(logger));
// but an empty map as well
child->setAttributes(std::map<std::string, Variant>());
ASSERT_EQ(ValidationState::UNKNOWN, doc->getValidationState());
ASSERT_TRUE(doc->validate(logger));
}
}
}
|