/*
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 .
*/
#ifndef _MODEL_TEST_DOCUMENT_HPP_
#define _MODEL_TEST_DOCUMENT_HPP_
#include
#include
#include
namespace ousia {
namespace model {
static Rooted resolveDescriptor(Handle domain,
const std::string &className)
{
// use the actual resolve method.
std::vector resolved = domain->resolve(className, typeOf());
// take the first valid result.
for (auto &r : resolved) {
return r.node.cast();
}
// if no valid result exists, return nullptr.
return {nullptr};
}
/**
* This constructs the "heading" domain given the book domain.
*/
static Rooted constructHeadingDomain(Manager &mgr,
Handle sys,
Handle bookDomain,
Logger &logger)
{
// set up domain node.
Rooted domain{new Domain(mgr, sys, "headings")};
// set up cardinality (every section may have at most one heading).
Cardinality card;
card.merge({0, 1});
// set up heading StructuredClass.
Rooted heading{new StructuredClass(
mgr, "heading", domain, card, {nullptr}, {nullptr}, true)};
// as field we actually want to refer to the field of paragraph.
Rooted p = resolveDescriptor(bookDomain, "paragraph");
heading->getFieldDescriptors().push_back(p->getFieldDescriptors()[0]);
// add the class to the domain.
domain->getStructureClasses().push_back(heading);
// create a new field for headings in each section type.
std::vector secclasses{"book", "section", "subsection",
"paragraph"};
for (auto &s : secclasses) {
Rooted desc = resolveDescriptor(bookDomain, s);
Rooted heading_field{new FieldDescriptor(
mgr, desc, FieldDescriptor::FieldType::SUBTREE, "heading")};
heading_field->getChildren().push_back(heading);
desc->getFieldDescriptors().push_back(heading_field);
}
return domain;
}
/**
* This constructs the "list" domain given the book domain.
*/
static Rooted constructListDomain(Manager &mgr,
Handle sys,
Handle bookDomain,
Logger &logger)
{
// set up domain node.
Rooted domain{new Domain(mgr, sys, "list")};
// set up cardinality
Cardinality any;
any.merge(Range::typeRangeFrom(0));
// get book.paragraph
Rooted p = resolveDescriptor(bookDomain, "paragraph");
// set up item StructuredClass;
Rooted item{new StructuredClass(
mgr, "item", domain, any, {nullptr}, {nullptr}, false)};
domain->getStructureClasses().push_back(item);
// as field we actually want to refer to the field of paragraph.
item->getFieldDescriptors().push_back(p->getFieldDescriptors()[0]);
// set up list StructuredClasses.
std::vector listTypes{"ol", "ul"};
for (auto &listType : listTypes) {
Rooted list{new StructuredClass(
mgr, listType, domain, any, {nullptr}, p, false)};
Rooted list_field{new FieldDescriptor(mgr, list)};
list_field->getChildren().push_back(item);
list->getFieldDescriptors().push_back(list_field);
domain->getStructureClasses().push_back(list);
}
return domain;
}
/**
* This constructs the "emphasis" domain.
*/
static Rooted constructEmphasisDomain(Manager &mgr,
Handle sys,
Logger &logger)
{
// set up domain node.
Rooted domain{new Domain(mgr, sys, "emphasis")};
// create AnnotationClasses
Rooted em{
new AnnotationClass(mgr, "emphasized", domain, {nullptr})};
domain->getAnnotationClasses().push_back(em);
Rooted strong{
new AnnotationClass(mgr, "strong", domain, {nullptr})};
domain->getAnnotationClasses().push_back(strong);
return domain;
}
static bool addText(Handle parent,
std::vector> &doms,
const std::string &content)
{
// Add its text.
Rooted text =
StructuredEntity::buildEntity(parent, doms, "text");
if (text.isNull()) {
return false;
}
// And its primitive content
Variant content_var{content.c_str()};
Rooted primitive =
DocumentPrimitive::buildEntity(text, content_var, "content");
if (primitive.isNull()) {
return false;
}
return true;
}
static bool addHeading(Handle parent,
std::vector> &doms,
const std::string &text)
{
// Add the heading.
Rooted heading = StructuredEntity::buildEntity(
parent, doms, "heading", "heading", {}, "");
if (heading.isNull()) {
return false;
}
// Add its text.
if (!addText(heading, doms, text)) {
return false;
}
return true;
}
static int annoIdx = 1;
// Only works for non-overlapping annotations!
static bool addAnnotation(Handle doc, Handle parent,
std::vector> &doms,
const std::string &text, const std::string &annoClass)
{
Rooted start =
AnnotationEntity::buildAnchor(parent, std::to_string(annoIdx++));
if (start.isNull()) {
return false;
}
if (!addText(parent, doms, text)) {
return false;
}
Rooted end =
AnnotationEntity::buildAnchor(parent, std::to_string(annoIdx++));
if (end.isNull()) {
return false;
}
Rooted anno =
AnnotationEntity::buildEntity(doc, doms, annoClass, start, end);
if (anno.isNull()) {
return false;
}
return true;
}
/**
* This constructs a more advanced book document using not only the book
* domain but also headings, emphasis and lists.
* TODO: insert emphasis and lists.
*/
static Rooted constructAdvancedDocument(Manager &mgr,
Rooted bookDom,
Rooted headingDom,
Rooted listDom)
{
std::vector> doms{bookDom, headingDom, listDom};
// Start with the (empty) document.
Rooted doc{new Document(mgr, "kant_was_ist_aufklaerung.oxd")};
// Add the root.
Rooted book =
StructuredEntity::buildRootEntity(doc, doms, "book");
if (book.isNull()) {
return {nullptr};
}
// Add the heading.
{
Rooted heading = StructuredEntity::buildEntity(
book, doms, "heading", "heading", {}, "");
if (heading.isNull()) {
return {nullptr};
}
if (!addText(heading, doms, "Beantwortung der Frage: ")) {
return {nullptr};
}
if (!addAnnotation(doc, heading, doms, "Was ist Aufklärung?",
"emphasized")) {
return {nullptr};
}
}
// Add the main section.
Rooted sec =
StructuredEntity::buildEntity(book, doms, "section");
if (sec.isNull()) {
return {nullptr};
}
// Add the heading.
if (!addHeading(sec, doms, "Was ist Aufklärung?")) {
return {nullptr};
}
// Add paragraph with main text.
{
Rooted p =
StructuredEntity::buildEntity(sec, doms, "paragraph");
if (p.isNull()) {
return {nullptr};
}
// Add its text.
{
if (!addAnnotation(doc, p, doms,
"Aufklärung ist der Ausgang des Menschen aus "
"seiner selbstverschuldeten Unmündigkeit",
"strong")) {
return {nullptr};
}
if (!addAnnotation(doc, p, doms, "Unmündigkeit",
"emphasized")) {
return {nullptr};
}
if (!addText(p, doms,
"ist das Unvermögen, sich seines Verstandes ohne "
"Leitung eines anderen zu bedienen. ")) {
return {nullptr};
}
if (!addAnnotation(doc, p, doms, "Selbstverschuldet",
"emphasized")) {
return {nullptr};
}
if (!addText(p, doms,
" ist diese Unmündigkeit, wenn die Ursache derselben "
"nicht am Mangel des Verstandes, sondern der "
"Entschließung und des Mutes liegt, sich seiner ohne "
"Leitung eines andern zu bedienen.")) {
return {nullptr};
}
if (!addAnnotation(doc, p, doms,
"Sapere aude! Habe Mut, dich deines eigenen "
"Verstandes zu bedienen!",
"emphasized")) {
return {nullptr};
}
if (!addText(p, doms,
" ist also der Wahlspruch der Aufklärung.")) {
return {nullptr};
}
}
}
// Add the "Lesarten" section
Rooted lesarten =
StructuredEntity::buildEntity(book, doms, "section");
if (lesarten.isNull()) {
return {nullptr};
}
// Add the heading.
if (!addHeading(lesarten, doms, "Lesarten")) {
return {nullptr};
}
// Add list with citations
{
// TODO: We need to restrict this to the list domain. Otherwise
// this leads to resolve errors for some reason.
Rooted ul =
StructuredEntity::buildEntity(lesarten, {listDom}, "ul");
if (ul.isNull()) {
return {nullptr};
}
std::vector citations{
"Berlinische Monatsschrift. Dezember-Heft 1784. S. 481–494.",
"Kant. Kleine Schriften. Neuwied 1793. Haupt. 8o. S. 34–50.",
"I. Kant. Zerstreute Aufsätze. Frankfurt und Leipzig 1793. 8o. S. "
"25–37.",
"I. Kant. Sämmtliche kleine Schriften. 4 Bände. 1797–98. 8o. "
"Königsberg u. Leipzig (Voigt, Jena). Nachdruck. Bd. III, S. "
"159–172.",
" I. Kant's vermischte Schriften. 3 Bände. Halle 1799. "
"(Tieftrunk). Bd. II. S. 687–700.",
"Kant. Vorzügliche kleine Schriften und Aufsätze, hrsg. mit Noten "
"von F. Ch. Starke. 2 Bände. Leipzig 1833 und Quedlinburg 1838. "
"Bd. I, S. 75–84."};
for (auto &cit : citations) {
// TODO: This needs to be restricted as well.
Rooted item =
StructuredEntity::buildEntity(ul, {listDom}, "item");
if (item.isNull()) {
return {nullptr};
}
if (!addText(item, doms, cit)) {
return {nullptr};
}
}
}
return doc;
}
}
}
#endif /* _TEST_DOCUMENT_HPP_ */