summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Paassen <bpaassen@techfak.uni-bielefeld.de>2015-01-08 17:20:56 +0100
committerBenjamin Paassen <bpaassen@techfak.uni-bielefeld.de>2015-01-08 17:20:56 +0100
commit7269e0e232c7971248ffa47aa2ae44786f3d303a (patch)
treeeabdb8a545efd86a3aab3f63deb806e33f08d7a0
parent33b92b72ed160f22dc627e841d5f84de4ebc0c6c (diff)
slight changes to Domain and Document. Started to add a more advanced test document creation function as well as the respective domain creation functions. The DemoOutputTest for it looks good so far.
-rw-r--r--src/core/model/Document.cpp6
-rw-r--r--src/core/model/Domain.hpp12
-rw-r--r--test/core/model/TestAdvanced.hpp129
-rw-r--r--test/core/model/TestDomain.hpp12
-rw-r--r--test/plugins/html/DemoOutputTest.cpp12
5 files changed, 158 insertions, 13 deletions
diff --git a/src/core/model/Document.cpp b/src/core/model/Document.cpp
index b700ba4..073f728 100644
--- a/src/core/model/Document.cpp
+++ b/src/core/model/Document.cpp
@@ -100,10 +100,8 @@ static Rooted<StructuredClass> resolveDescriptor(
}
// Otherwise take the first valid result.
for (auto &r : resolved) {
- Managed *m = &(*r);
- StructuredClass *c = dynamic_cast<StructuredClass *>(m);
- if (c != nullptr) {
- return Rooted<StructuredClass>(c);
+ if(r->isa(typeOf<StructuredClass>())){
+ return r.cast<StructuredClass>();
}
}
}
diff --git a/src/core/model/Domain.hpp b/src/core/model/Domain.hpp
index 6995d14..96e13c7 100644
--- a/src/core/model/Domain.hpp
+++ b/src/core/model/Domain.hpp
@@ -261,6 +261,11 @@ protected:
VisitorSet &visited) override;
public:
+ /**
+ * Note, that this flag will always be set to "false" for non-primitive
+ * FieldDescriptors, because in that case the cardinalities regulate
+ * whether children have to be inserted or not.
+ */
const bool optional;
// TODO: What about the name of default fields?
@@ -300,18 +305,15 @@ public:
* TREE for the main or default structure or SUBTREE
* for supporting structures.
* @param name is the name of this field.
- * @param optional should be set to 'false' is this field needs to be
- * filled in order for an instance of the parent
- * Descriptor to be valid.
*/
FieldDescriptor(Manager &mgr, Handle<Descriptor> parent,
FieldType fieldType = FieldType::TREE,
- std::string name = "", bool optional = false)
+ std::string name = "")
: Node(mgr, std::move(name), parent),
children(this),
fieldType(fieldType),
// TODO: What would be a wise initialization of the primitiveType?
- optional(optional)
+ optional(false)
{
}
diff --git a/test/core/model/TestAdvanced.hpp b/test/core/model/TestAdvanced.hpp
new file mode 100644
index 0000000..95c5402
--- /dev/null
+++ b/test/core/model/TestAdvanced.hpp
@@ -0,0 +1,129 @@
+/*
+ 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/>.
+*/
+
+#ifndef _MODEL_TEST_DOCUMENT_HPP_
+#define _MODEL_TEST_DOCUMENT_HPP_
+
+#include <core/model/Document.hpp>
+#include <core/model/Domain.hpp>
+#include <core/model/Typesystem.hpp>
+
+namespace ousia {
+namespace model {
+
+
+static Rooted<StructuredClass> resolveDescriptor(Handle<Domain> domain,
+ const std::string &className)
+{
+ // use the actual resolve method.
+ std::vector<Rooted<Managed>> resolved = domain->resolve(className);
+ // take the first valid result.
+ for (auto &r : resolved) {
+ if (r->isa(typeOf<StructuredClass>())) {
+ return r.cast<StructuredClass>();
+ }
+ }
+ // if no valid result exists, return nullptr.
+ return {nullptr};
+}
+
+/**
+ * This constructs the "heading" domain given the book domain.
+ */
+static Rooted<Domain> constructHeadingDomain(Manager &mgr,
+ Handle<SystemTypesystem> sys,
+ Handle<Domain> bookDomain,
+ Logger &logger)
+{
+ // set up domain node.
+ Rooted<Domain> 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<StructuredClass> heading{new StructuredClass(
+ mgr, "heading", {nullptr}, card, {nullptr}, {nullptr}, true)};
+ // as field we actually want to refer to the field of paragraph.
+ Rooted<StructuredClass> 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<std::string> secclasses{"book", "section", "subsection",
+ "paragraph"};
+ for (auto &s : secclasses) {
+ Rooted<StructuredClass> desc = resolveDescriptor(bookDomain, s);
+ Rooted<FieldDescriptor> 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 a more advanced book document using not only the book
+ * domain but also headings, emphasis and lists.
+ * TODO: insert emphasis and lists.
+ */
+static Rooted<Document> constructAdvancedDocument(Manager &mgr,
+ Rooted<Domain> bookDom,
+ Rooted<Domain> headingDom)
+{
+ std::vector<Handle<Domain>> doms{bookDom, headingDom};
+
+ // Start with the (empty) document.
+ Rooted<Document> doc{new Document(mgr, "kant_was_ist_aufklaerung.oxd")};
+
+ // Add the root.
+ Rooted<StructuredEntity> book =
+ StructuredEntity::buildRootEntity(doc, doms, "book");
+ if (book.isNull()) {
+ return {nullptr};
+ }
+ {
+ // Add the heading.
+ Rooted<StructuredEntity> heading = StructuredEntity::buildEntity(
+ book, doms, "heading", "heading", {}, "");
+ if (heading.isNull()) {
+ return {nullptr};
+ }
+ {
+ // Add its text.
+ Rooted<StructuredEntity> text =
+ StructuredEntity::buildEntity(heading, doms, "text");
+ if (text.isNull()) {
+ return {nullptr};
+ }
+ // And its primitive content
+ // TODO: use em here.
+ Variant content {"Beantwortung der Frage: <em>Was ist Aufklärung?</em>"};
+ Rooted<DocumentPrimitive> main_primitive =
+ DocumentPrimitive::buildEntity(text, content, "content");
+ if (main_primitive.isNull()) {
+ return {nullptr};
+ }
+ }}
+
+ return doc;
+}
+}
+}
+
+#endif /* _TEST_DOCUMENT_HPP_ */
+
diff --git a/test/core/model/TestDomain.hpp b/test/core/model/TestDomain.hpp
index 54e79ee..aa3096d 100644
--- a/test/core/model/TestDomain.hpp
+++ b/test/core/model/TestDomain.hpp
@@ -69,6 +69,18 @@ static Rooted<Domain> constructBookDomain(Manager &mgr,
new FieldDescriptor(mgr, paragraph)};
paragraph->getFieldDescriptors().push_back(paragraph_field);
+ // We append "subsection" to section.
+ Rooted<StructuredClass> subsection{
+ new StructuredClass(mgr, "subsection", domain, any)};
+ section_field->getChildren().push_back(subsection);
+ domain->getStructureClasses().push_back(subsection);
+ // And the field of it.
+ Rooted<FieldDescriptor> subsection_field{
+ new FieldDescriptor(mgr, subsection)};
+ subsection->getFieldDescriptors().push_back(subsection_field);
+ // and we add the paragraph to subsections fields
+ subsection_field->getChildren().push_back(paragraph);
+
// Finally we add the "text" node, which is transparent as well.
Rooted<StructuredClass> text{new StructuredClass(
mgr, "text", domain, any, {nullptr}, {nullptr}, true)};
diff --git a/test/plugins/html/DemoOutputTest.cpp b/test/plugins/html/DemoOutputTest.cpp
index 6123c79..7857314 100644
--- a/test/plugins/html/DemoOutputTest.cpp
+++ b/test/plugins/html/DemoOutputTest.cpp
@@ -25,6 +25,7 @@
#include <core/model/Document.hpp>
#include <core/model/Domain.hpp>
+#include <core/model/TestAdvanced.hpp>
#include <core/model/TestDocument.hpp>
#include <core/model/TestDomain.hpp>
@@ -37,17 +38,20 @@ TEST(DemoHTMLTransformer, writeHTML)
Logger logger;
Manager mgr{1};
Rooted<model::SystemTypesystem> sys{new model::SystemTypesystem(mgr)};
- // Get the domain.
- Rooted<model::Domain> domain = constructBookDomain(mgr, sys, logger);
+ // Get the domains.
+ Rooted<model::Domain> bookDom =
+ model::constructBookDomain(mgr, sys, logger);
+ Rooted<model::Domain> headingDom =
+ model::constructHeadingDomain(mgr, sys, bookDom, logger);
// Construct the document.
- Rooted<model::Document> doc = model::constructBookDocument(mgr, domain);
+ Rooted<model::Document> doc = model::constructAdvancedDocument(mgr, bookDom, headingDom);
#ifdef MANAGER_GRAPHVIZ_EXPORT
// dump the manager state
mgr.exportGraphviz("bookDocument.dot");
#endif
- // print it
+ // TODO: change this. Don't use printouts
DemoHTMLTransformer transformer;
transformer.writeHTML(doc, std::cout);
}