/* 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_ADVANCED_HPP_ #define _MODEL_TEST_ADVANCED_HPP_ #include #include #include #include "TestDocumentBuilder.hpp" namespace ousia { static Rooted resolveDescriptor(Handle domain, const std::string &className) { // use the actual resolve method. std::vector resolved = domain->resolve(RttiTypes::StructuredClass, className); // 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}, true)}; // as field want to copy the field of paragraph. Rooted p = resolveDescriptor(bookDomain, "paragraph"); heading->copyFieldDescriptor(p->getFieldDescriptors()[0]); // 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->addChild(heading); } 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")}; // get book.paragraph Rooted p = resolveDescriptor(bookDomain, "paragraph"); // set up item StructuredClass; Rooted item{new StructuredClass( mgr, "item", domain, Cardinality::any(), {nullptr}, false)}; // as field we want to copy the field of paragraph. item->copyFieldDescriptor(p->getFieldDescriptors()[0]); // set up list StructuredClasses. std::vector listTypes{"ol", "ul"}; for (auto &listType : listTypes) { Rooted list{new StructuredClass( mgr, listType, domain, Cardinality::any(), p, false)}; Rooted list_field{new FieldDescriptor(mgr, list)}; list_field->addChild(item); } 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)}; Rooted strong{new AnnotationClass(mgr, "strong", domain)}; return domain; } static bool addText(Logger &logger, Handle doc, Handle parent, const std::string &content) { // Add the text. Rooted text = buildStructuredEntity(doc, logger, parent, {"text"}); if (text.isNull()) { return false; } // And the primitive content Variant content_var{content.c_str()}; Rooted primitive{new DocumentPrimitive( parent->getManager(), text, content_var, "content")}; return true; } static bool addHeading(Logger &logger, Handle doc, Handle parent, const std::string &text) { // Add the heading. Rooted heading = buildStructuredEntity( doc, logger, parent, {"heading"}, "heading", {}, ""); if (heading.isNull()) { return false; } // Add its text. if (!addText(logger, doc, heading, text)) { return false; } return true; } static int annoIdx = 1; // Only works for non-overlapping annotations! static bool addAnnotation(Logger &logger, Handle doc, Handle parent, const std::string &text, const std::string &annoClass) { Manager& mgr = parent->getManager(); Rooted start{new Anchor(mgr, std::to_string(annoIdx++), parent)}; if (!addText(logger, doc, parent, text)) { return false; } Rooted end{new Anchor(mgr, std::to_string(annoIdx++), parent)}; Rooted anno = buildAnnotationEntity(doc, logger, {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. */ static Rooted constructAdvancedDocument(Manager &mgr, Logger &logger, Handle bookDom, Handle headingDom, Handle listDom, Handle emphasisDom) { // Start with the (empty) document. Rooted doc{new Document(mgr, "kant_was_ist_aufklaerung.oxd")}; doc->referenceDomains({bookDom, headingDom, listDom, emphasisDom}); // Add the root. Rooted book = buildRootStructuredEntity(doc, logger, {"book"}); if (book.isNull()) { return {nullptr}; } // Add the heading. { Rooted heading = buildStructuredEntity( doc, logger, book, {"heading"}, "heading", {}, ""); if (heading.isNull()) { return {nullptr}; } if (!addText(logger, doc, heading, "Beantwortung der Frage: ")) { return {nullptr}; } if (!addAnnotation(logger, doc, heading, "Was ist Aufklärung?", "emphasized")) { return {nullptr}; } } // Add the main section. Rooted sec = buildStructuredEntity(doc, logger, book, {"section"}); if (sec.isNull()) { return {nullptr}; } // Add the heading. if (!addHeading(logger, doc, sec, "Was ist Aufklärung?")) { return {nullptr}; } // Add paragraph with main text. { Rooted p = buildStructuredEntity(doc, logger, sec, {"paragraph"}); if (p.isNull()) { return {nullptr}; } // Add its text. { if (!addAnnotation(logger, doc, p, "Aufklärung ist der Ausgang des Menschen aus " "seiner selbstverschuldeten Unmündigkeit!", "strong")) { return {nullptr}; } if (!addAnnotation(logger, doc, p, "Unmündigkeit", "emphasized")) { return {nullptr}; } if (!addText(logger, doc, p, "ist das Unvermögen, sich seines Verstandes ohne " "Leitung eines anderen zu bedienen. ")) { return {nullptr}; } if (!addAnnotation(logger, doc, p, "Selbstverschuldet", "emphasized")) { return {nullptr}; } if (!addText(logger, doc, p, " 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(logger, doc, p, "Sapere aude! Habe Mut, dich deines eigenen " "Verstandes zu bedienen!", "emphasized")) { return {nullptr}; } if (!addText(logger, doc, p, " ist also der Wahlspruch der Aufklärung.")) { return {nullptr}; } } } // Add the "Lesarten" section Rooted lesarten = buildStructuredEntity(doc, logger, book, {"section"}); if (lesarten.isNull()) { return {nullptr}; } // Add the heading. if (!addHeading(logger, doc, lesarten, "Lesarten")) { return {nullptr}; } // Add list with citations { Rooted ul = buildStructuredEntity(doc, logger, lesarten, {"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) { Rooted item = buildStructuredEntity(doc, logger, ul, {"item"}); if (item.isNull()) { return {nullptr}; } if (!addText(logger, doc, item, cit)) { return {nullptr}; } } } return doc; } } #endif /* _TEST_DOCUMENT_HPP_ */