From 23e593eb69f6d0beb197f33ae31b479c60d9316f Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Fri, 19 Dec 2014 00:29:55 +0100 Subject: added convenience function for document construction and tested them. --- src/core/model/Document.cpp | 104 ++++++++++++++++++++++++++++++++++++++- src/core/model/Document.hpp | 100 +++++++++++++++++++++++++++++++++++-- test/core/model/DocumentTest.cpp | 12 +++-- test/core/model/TestDocument.hpp | 67 +++++++++++++++++++++++++ 4 files changed, 275 insertions(+), 8 deletions(-) create mode 100644 test/core/model/TestDocument.hpp diff --git a/src/core/model/Document.cpp b/src/core/model/Document.cpp index 31b22e3..709981b 100644 --- a/src/core/model/Document.cpp +++ b/src/core/model/Document.cpp @@ -58,7 +58,7 @@ int DocumentEntity::getFieldDescriptorIndex(const std::string &fieldName) } void DocumentEntity::getField(ManagedVector &res, - const std::string &fieldName) + const std::string &fieldName) { int f = getFieldDescriptorIndex(fieldName); if (f < 0) { @@ -85,6 +85,108 @@ ManagedVector &DocumentEntity::getField( "The given FieldDescriptor is not specified in the Descriptor of this " "node."); } + +static Rooted resolveDescriptor( + std::vector> domains, const std::string &className) +{ + // iterate over all domains. + for (auto &d : domains) { + // use the actual resolve method. + std::vector> resolved = d->resolve(className); + // if we don't find anything, continue. + if (resolved.size() == 0) { + continue; + } + // Otherwise take the first valid result. + for (auto &r : resolved) { + Managed *m = &(*r); + StructuredClass *c = dynamic_cast(m); + if (c != nullptr) { + return Rooted(c); + } + } + } + return {nullptr}; +} + +Rooted StructuredEntity::buildRootEntity( + Handle document, std::vector> domains, + const std::string &className, Variant attributes, std::string name) +{ + // If the parent is not set, we can not build the entity. + if (document == nullptr) { + return {nullptr}; + } + // If we can not find the correct descriptor, we can not build the entity + // either. + Rooted descriptor = resolveDescriptor(domains, className); + if (descriptor == nullptr) { + return {nullptr}; + } + // Then construct the StructuredEntity itself. + Rooted root{ + new StructuredEntity(document->getManager(), document, descriptor, + attributes, std::move(name))}; + // append it to the document. + document->setRoot(root); + // and return it. + return root; +} + +Rooted StructuredEntity::buildEntity( + Handle parent, std::vector> domains, + const std::string &className, const std::string &fieldName, + Variant attributes, std::string name) +{ + // If the parent is not set, we can not build the entity. + if (parent == nullptr) { + return {nullptr}; + } + // If we can not find the correct descriptor, we can not build the entity + // either. + Rooted descriptor = resolveDescriptor(domains, className); + if (descriptor == nullptr) { + return {nullptr}; + } + // Then construct the StructuredEntity itself. + Rooted entity{new StructuredEntity( + parent->getManager(), parent, descriptor, attributes, std::move(name))}; + // if the field does not exist, return null handle as well. + if (!parent->hasField(fieldName)) { + return {nullptr}; + } + // append the new entity to the right field. + ManagedVector field{parent}; + parent->getField(field, fieldName); + field.push_back(entity); + + // and return it. + return entity; +} + +Rooted DocumentPrimitive::buildEntity( + Handle parent, Variant content, + const std::string &fieldName) +{ + // If the parent is not set, we can not build the entity. + if (parent == nullptr) { + return {nullptr}; + } + // Then construct the StructuredEntity itself. + Rooted entity{ + new DocumentPrimitive(parent->getManager(), parent, content)}; + // if the field does not exist, return null handle as well. + if (!parent->hasField(fieldName)) { + return {nullptr}; + } + // append the new entity to the right field. + ManagedVector field{parent}; + parent->getField(field, fieldName); + field.push_back(entity); + + // and return it. + return entity; +} } } diff --git a/src/core/model/Document.hpp b/src/core/model/Document.hpp index a31e52f..15a4599 100644 --- a/src/core/model/Document.hpp +++ b/src/core/model/Document.hpp @@ -80,6 +80,7 @@ namespace model { class StructuredEntity; class AnnotationEntity; +class Document; /** * A DocumentEntity is the common superclass for StructuredEntities and @@ -110,8 +111,11 @@ public: { // TODO: Validation at construction time? // insert empty vectors for each field. - for (size_t f = 0; f < descriptor->getFieldDescriptors().size(); f++) { - fields.push_back(ManagedVector(this)); + if (!descriptor.isNull()) { + for (size_t f = 0; f < descriptor->getFieldDescriptors().size(); + f++) { + fields.push_back(ManagedVector(this)); + } } } @@ -202,6 +206,57 @@ public: } ManagedVector &getAnnotations() { return annotations; } + + /** + * This builds the root StructuredEntity for the given document. It + * automatically appends the newly build entity to the given document. + * + * @param document is the document this entity shall be build for. The + * resulting entity will automatically be appended to that + * document. Also the manager of that document will be + * used to register the new node. + * @param domains are the domains that are used to find the + * StructuredClass for the new node. The domains will be + * searched in the given order. + * @param className is the name of the StructuredClass. + * @param attributes are the attributes of the new node in terms of a Struct + * variant (empty per default). + * @param name is the name of this StructuredEntity (empty per + * default). + * @return the newly created StructuredEntity or a nullptr if some + * input handle was empty or the given domains did not + * contain a StructuredClass with the given name. + */ + static Rooted buildRootEntity( + Handle document, std::vector> domains, + const std::string &className, Variant attributes = Variant(), + std::string name = ""); + + /** + * This builds a StructuredEntity as child of the given DocumentEntity. It + * automatically appends the newly build entity to its parent. + * + * @param parent is the parent DocumentEntity. The newly constructed + * StructuredEntity will automatically be appended to it. + * @param domains are the domains that are used to find the + * StructuredClass for the new node. The domains will be + * searched in the given order. + * @param className is the name of the StructuredClass. + * @param fieldName is the name of the field where the newly constructed + * StructuredEntity shall be appended. + * @param attributes are the attributes of the new node in terms of a Struct + * variant (empty per default). + * @param name is the name of this StructuredEntity (empty per + * default). + * + * @return the newly created StructuredEntity or a nullptr if some + * input handle was empty or the given domains did not + * contain a StructuredClass with the given name. + */ + static Rooted buildEntity( + Handle parent, std::vector> domains, + const std::string &className, const std::string &fieldName = "", + Variant attributes = Variant(), std::string name = ""); }; /** @@ -211,7 +266,7 @@ public: */ class DocumentPrimitive : public StructuredEntity { public: - DocumentPrimitive(Manager &mgr, Handle parent, + DocumentPrimitive(Manager &mgr, Handle parent, Variant content) : StructuredEntity(mgr, parent, nullptr, std::move(content)) { @@ -220,6 +275,25 @@ public: Variant getContent() const { return getAttributes(); } // TODO: Override such methods like "getField" to disable them? + + /** + * This builds a DocumentPrimitive as child of the given DocumentEntity. It + * automatically appends the newly build entity to its parent. + * + * @param parent is the parent DocumentEntity. The newly constructed + * DocumentPrimitive will automatically be appended to it. + * @param content is the primitive content of the new node in terms of a + * Struct variant. + * @param fieldName is the name of the field where the newly constructed + * StructuredEntity shall be appended. + * + * @return the newly created StructuredEntity or a nullptr if some + * input handle was empty or the given domains did not + * contain a StructuredClass with the given name. + */ + static Rooted buildEntity( + Handle parent, + Variant content, const std::string &fieldName = ""); }; /** @@ -284,6 +358,26 @@ public: Rooted getEnd() { return end; } }; + +/** + * A Document is mainly a wrapper for the Root structure node of the Document + * Graph. + */ +class Document : public Node { +private: + Owned root; + +public: + Document(Manager &mgr, std::string name) + // TODO: Can a document have a parent? + : Node(mgr, std::move(name), nullptr) + { + } + + void setRoot(Handle root) { root = acquire(root); }; + + Rooted getRoot() const { return root; } +}; } } diff --git a/test/core/model/DocumentTest.cpp b/test/core/model/DocumentTest.cpp index dd883a4..26553dd 100644 --- a/test/core/model/DocumentTest.cpp +++ b/test/core/model/DocumentTest.cpp @@ -19,8 +19,10 @@ #include #include +#include -#include "ModelTestUtils.hpp" +#include "TestDocument.hpp" +#include "TestDomain.hpp" namespace ousia { namespace model { @@ -32,9 +34,11 @@ TEST(Document, testDocumentConstruction) Manager mgr{1}; // Get the domain. Rooted domain = constructBookDomain(mgr); - - // TODO: IMPLEMENT - ASSERT_TRUE(true); + // Construct the document. + Rooted doc = constructBookDocument(mgr, domain); + + // If that works we are happy already. + ASSERT_FALSE(doc.isNull()); } } } diff --git a/test/core/model/TestDocument.hpp b/test/core/model/TestDocument.hpp new file mode 100644 index 0000000..c99bdd3 --- /dev/null +++ b/test/core/model/TestDocument.hpp @@ -0,0 +1,67 @@ +/* + 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 { + +/** + * This constructs a fairly simple test document for the "book" domain. The + * structure of the document can be seen in the Code below. + */ +static Rooted constructBookDocument(Manager &mgr, + Rooted bookDomain) +{ + // Start with the (empty) document. + Rooted doc{new Document(mgr, "myDoc.oxd")}; + + // Add the root. + Rooted root = + StructuredEntity::buildRootEntity(doc, {bookDomain}, "book"); + if (root.isNull()) { + return {nullptr}; + } + + // Add a paragraph. + Rooted foreword = + StructuredEntity::buildEntity(root, {bookDomain}, "paragraph"); + if (foreword.isNull()) { + return {nullptr}; + } + // Add its text. + Variant text{std::map{ + {"content", Variant("Some introductory text")}}}; + Rooted text_primitive = + DocumentPrimitive::buildEntity(foreword, text, "text"); + // Add a section. + Rooted section = + StructuredEntity::buildEntity(root, {bookDomain}, "section"); + + return doc; +} +} +} + +#endif /* _TEST_DOCUMENT_HPP_ */ + -- cgit v1.2.3