diff options
author | Benjamin Paassen <bpaassen@techfak.uni-bielefeld.de> | 2014-12-16 21:04:40 +0100 |
---|---|---|
committer | Benjamin Paassen <bpaassen@techfak.uni-bielefeld.de> | 2014-12-16 21:04:40 +0100 |
commit | 257c76122ad7db19382c66985d89815df676e45a (patch) | |
tree | 9a9d81ef613b1f5ecdc79fadebaf89960c46c21b | |
parent | b08c271ca4401ac8e99d49885da600122e5aa398 (diff) |
Document.hpp does compile now. Further Tests are needed.
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/core/model/Document.cpp | 90 | ||||
-rw-r--r-- | src/core/model/Document.hpp | 67 | ||||
-rw-r--r-- | src/core/model/Domain.hpp | 20 |
4 files changed, 135 insertions, 43 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 4303058..285abca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -114,6 +114,7 @@ ADD_LIBRARY(ousia_core src/core/managed/Managed src/core/managed/ManagedType src/core/managed/Manager + src/core/model/Document src/core/model/Domain src/core/model/Typesystem src/core/parser/Parser diff --git a/src/core/model/Document.cpp b/src/core/model/Document.cpp new file mode 100644 index 0000000..31b22e3 --- /dev/null +++ b/src/core/model/Document.cpp @@ -0,0 +1,90 @@ +/* + 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 "Document.hpp" + +#include <core/common/Exceptions.hpp> + +namespace ousia { +namespace model { + +int DocumentEntity::getFieldDescriptorIndex(const std::string &fieldName) +{ + const ManagedVector<FieldDescriptor> &fds = + descriptor->getFieldDescriptors(); + unsigned int f = 0; + + // look if we have an empty name. + if (fieldName == "") { + // in that case we look for a default field. + // First: Do we only have one field? + if (fds.size() == 1) { + // if so we return that one. + return f; + } + // Second: Do we have a TREE field? + for (auto &fd : fds) { + if (fd->getFieldType() == FieldDescriptor::FieldType::TREE) { + return f; + } + f++; + } + } else { + // otherwise we return the FieldDescriptor with the correct name (if + // such a descriptor exists). + for (auto &fd : fds) { + if (fd->getName() == fieldName) { + return f; + } + f++; + } + } + return -1; +} + +void DocumentEntity::getField(ManagedVector<StructuredEntity> &res, + const std::string &fieldName) +{ + int f = getFieldDescriptorIndex(fieldName); + if (f < 0) { + ManagedVector<StructuredEntity> empty{this}; + res = ManagedVector<StructuredEntity>(this); + } + res = fields[f]; +} + +ManagedVector<StructuredEntity> &DocumentEntity::getField( + Rooted<FieldDescriptor> fieldDescriptor) +{ + const ManagedVector<FieldDescriptor> &fds = + descriptor->getFieldDescriptors(); + int f = 0; + for (auto &fd : fds) { + if (fd->getName() == fieldDescriptor->getName() && + fd->getFieldType() == fieldDescriptor->getFieldType()) { + return fields[f]; + } + f++; + } + throw OusiaException( + "The given FieldDescriptor is not specified in the Descriptor of this " + "node."); +} +} +} + diff --git a/src/core/model/Document.hpp b/src/core/model/Document.hpp index ede07d6..3114480 100644 --- a/src/core/model/Document.hpp +++ b/src/core/model/Document.hpp @@ -96,27 +96,26 @@ private: const Variant attributes; std::vector<ManagedVector<StructuredEntity>> fields; - Rooted<FieldDescriptor> getFieldDescriptor(const std::string &fieldName); + int getFieldDescriptorIndex(const std::string &fieldName); public: - DocumentEntity(Manager &mgr, std::string name = "", Handle<Node> parent, - Handle<Descriptor> descriptor, Variant attributes) + DocumentEntity(Manager &mgr, Handle<Node> parent, + Handle<Descriptor> descriptor, Variant attributes, + std::string name = "") : Node(mgr, std::move(name), parent), descriptor(acquire(descriptor)), attributes(std::move(attributes)) { // TODO: Validation at construction time? // insert empty vectors for each field. - for (int f = 0; f < descriptor->getFieldDescriptors.size(); f++) { - fields.push_back(ManagedVector(this)); + for (size_t f = 0; f < descriptor->getFieldDescriptors().size(); f++) { + fields.push_back(ManagedVector<StructuredEntity>(this)); } } - Rooted<Descriptor> getDescriptor const() { return descriptor; } + Rooted<Descriptor> getDescriptor() const { return descriptor; } - const Variant &getAttributes() const { return attributes; } - - Variant getAttributes const { return attributes; } + Variant getAttributes() const { return attributes; } /** * This allows a direct manipulation of the internal data structure of a @@ -138,7 +137,7 @@ public: */ bool hasField(const std::string &fieldName = "") { - return getFieldDescriptor(fieldName) != nullptr; + return getFieldDescriptorIndex(fieldName) != -1; } /** @@ -153,25 +152,19 @@ public: * FieldDescriptor matches the given name an empty ManagedVector is * returned. This is also the case, however, if there are no members for an * existing field. Therefore it is recommended to additionally check the - * output of "hasField" or use the overloaded version of this method with + * output of "hasField" or use the version of this method with * a FieldDescriptor as input. * * @param fieldName is the name of the field as specified in the * FieldDescriptor in the Domain description. - * @return a ManagedVector of all StructuredEntities in that field. If the - * field is unknown or if no members exist in that field yet, the - * ManagedVector will be empty. Note that the ManagedVector is - * returned as a reference, so it is possible to manipulate this - * DocumentEntities content using this function. + * @param res is a ManagedVector reference where the result will be + * stored. After using this method the reference will + * either refer to all StructuredEntities in that field. If + * the field is unknown or if no members exist in that + * field yet, the ManagedVector will be empty. */ - ManagedVector<StructuredEntity> &getField(const std::string &fieldName = "") - { - Rooted<FieldDescriptor> fd = getFieldDescriptor(fieldName); - if (fd == nullptr) { - return ManagedVector<StructuredEntity>(this); - } - return getField(fd); - } + void getField(ManagedVector<StructuredEntity> &res, + const std::string &fieldName = ""); /** * This returns the vector of entities containing all members of the field @@ -197,9 +190,11 @@ private: ManagedVector<AnnotationEntity> annotations; public: - StructuredEntity(Manager &mgr, std::string name = "", Handle<Node> parent, - Handle<StructuredClass> descriptor, Variant attributes) - : DocumentEntity(mgr, std::move(name), parent, descriptor, attributes), + StructuredEntity(Manager &mgr, Handle<Node> parent, + Handle<StructuredClass> descriptor, Variant attributes, + std::string name = "") + : DocumentEntity(mgr, parent, descriptor, std::move(attributes), + std::move(name)), annotations(this) { } @@ -216,15 +211,14 @@ class DocumentPrimitive : public StructuredEntity { public: DocumentPrimitive(Manager &mgr, Handle<StructuredEntity> parent, Variant content) - : StructuredEntity(mgr, "", parent, nullptr, content) + : StructuredEntity(mgr, parent, nullptr, std::move(content)) { } Variant getContent() const { return getAttributes(); } - const Variant& getContent() const { return getAttributes(); } // TODO: Override such methods like "getField" to disable them? -} +}; /** * An AnnotationEntity is a span-like instance that is not bound by the elements @@ -262,9 +256,9 @@ public: * @param parent is the parent of this Anchor in the Structure Tree (!), * not the AnnotationEntity that references this Anchor. */ - Anchor(Manager &mgr, std::string name = "", - Handle<StructuredEntity> parent) - : StructuredEntity(mgr, name, parent, nullptr, Variant()) + Anchor(Manager &mgr, Handle<StructuredEntity> parent, + std::string name = "") + : StructuredEntity(mgr, parent, nullptr, Variant(), std::move(name)) { } }; @@ -274,10 +268,11 @@ private: Owned<Anchor> end; public: - AnnotationEntity(Manager &mgr, std::string name = "", Handle<Node> parent, + AnnotationEntity(Manager &mgr, Handle<Node> parent, Handle<StructuredClass> descriptor, Variant attributes, - Handle<Anchor> start, Handle<Anchor> end) - : DocumentEntity(mgr, std::move(name), parent, descriptor, attributes), + Handle<Anchor> start, Handle<Anchor> end, + std::string name = "") + : DocumentEntity(mgr, parent, descriptor, attributes, std::move(name)), start(acquire(start)), end(acquire(end)) { diff --git a/src/core/model/Domain.hpp b/src/core/model/Domain.hpp index 535330c..50c0bb1 100644 --- a/src/core/model/Domain.hpp +++ b/src/core/model/Domain.hpp @@ -250,7 +250,12 @@ public: } // TODO: Is returning a ManagedVector alright? - ManagedVector<FieldDescriptor> getFieldDescriptors() const + ManagedVector<FieldDescriptor> &getFieldDescriptors() + { + return fieldDescriptors; + } + + const ManagedVector<FieldDescriptor> &getFieldDescriptors() const { return fieldDescriptors; } @@ -350,8 +355,7 @@ public: const Cardinality &cardinality, // TODO: What would be a wise default value for isa? Handle<StructuredClass> isa, - ManagedVector<FieldDescriptor> parents, - bool transparent) + ManagedVector<FieldDescriptor> parents, bool transparent) : Descriptor(mgr, std::move(name), parent, attributesDescriptor, fieldDescriptors), cardinality(cardinality), @@ -363,10 +367,12 @@ public: const Cardinality &getCardinality() const { return cardinality; } - Rooted<StructuredClass> getIsA() const {return isa;} + Rooted<StructuredClass> getIsA() const { return isa; } // TODO: Is returning a ManagedVector alright? - ManagedVector<FieldDescriptor> getParents() { return parents; } + ManagedVector<FieldDescriptor>& getParents() { return parents; } + + const ManagedVector<FieldDescriptor> &getParents() const { return parents; } }; /** @@ -391,8 +397,8 @@ private: public: Domain(Manager &mgr, std::string name, - ManagedVector<StructuredClass> rootStructures, - ManagedVector<AnnotationClass> annotationClasses) + ManagedVector<StructuredClass> rootStructures, + ManagedVector<AnnotationClass> annotationClasses) // TODO: Can a domain have a parent? : Node(mgr, std::move(name), nullptr), rootStructures(rootStructures), |