From 57cbdaa164d1b7ff4987b86fe3cc2c8ba14b0b23 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Fri, 9 Jan 2015 13:33:44 +0100 Subject: Improvements to Document classes in accordance with Benjamin --- src/core/model/Document.cpp | 60 +++++++++++++++++++++++++++++++++------------ src/core/model/Document.hpp | 16 ++++++++++-- 2 files changed, 59 insertions(+), 17 deletions(-) diff --git a/src/core/model/Document.cpp b/src/core/model/Document.cpp index 945fb3e..5386525 100644 --- a/src/core/model/Document.cpp +++ b/src/core/model/Document.cpp @@ -24,6 +24,8 @@ namespace ousia { namespace model { +/* Class DocumentEntity */ + int DocumentEntity::getFieldDescriptorIndex(const std::string &fieldName) { const NodeVector &fds = descriptor->getFieldDescriptors(); @@ -87,20 +89,23 @@ NodeVector &DocumentEntity::getField( "node."); } -static Rooted resolveDescriptor( - std::vector> domains, const std::string &className) +/* Class StructuredEntity */ + +static Rooted resolveDescriptor(std::vector> domains, + const std::string &className, + const RttiBase &type) { // iterate over all domains. for (auto &d : domains) { // use the actual resolve method. - std::vector resolved = d->resolve(className, typeOf()); + std::vector resolved = d->resolve(className, type); // if we don't find anything, continue. if (resolved.size() == 0) { continue; } // Otherwise take the first valid result. for (auto &r : resolved) { - return r.node.cast(); + return r.node.cast(); } } return {nullptr}; @@ -116,14 +121,15 @@ Rooted StructuredEntity::buildRootEntity( } // If we can not find the correct descriptor, we can not build the entity // either. - Rooted descriptor = resolveDescriptor(domains, className); + Rooted descriptor = + resolveDescriptor(domains, className, RttiTypes::StructuredClass); if (descriptor == nullptr) { return {nullptr}; } // Then construct the StructuredEntity itself. - Rooted root{ - new StructuredEntity(document->getManager(), document, descriptor, - attributes, std::move(name))}; + Rooted root{new StructuredEntity( + document->getManager(), document, descriptor.cast(), + attributes, std::move(name))}; // append it to the document. document->setRoot(root); // and return it. @@ -141,13 +147,15 @@ Rooted StructuredEntity::buildEntity( } // If we can not find the correct descriptor, we can not build the entity // either. - Rooted descriptor = resolveDescriptor(domains, className); + Rooted descriptor = + resolveDescriptor(domains, className, RttiTypes::StructuredClass); if (descriptor == nullptr) { return {nullptr}; } // Then construct the StructuredEntity itself. Rooted entity{new StructuredEntity( - parent->getManager(), parent, descriptor, attributes, std::move(name))}; + parent->getManager(), parent, descriptor.cast(), + attributes, std::move(name))}; // if the field does not exist, return null handle as well. if (!parent->hasField(fieldName)) { return {nullptr}; @@ -163,6 +171,8 @@ Rooted StructuredEntity::buildEntity( return entity; } +/* Class DocumentPrimitive */ + Rooted DocumentPrimitive::buildEntity( Handle parent, Variant content, const std::string &fieldName) @@ -188,6 +198,8 @@ Rooted DocumentPrimitive::buildEntity( return entity; } +/* Class AnnotationEntity */ + Rooted AnnotationEntity::buildAnchor( Handle parent, std::string id, const std::string &fieldName) { @@ -219,23 +231,41 @@ Rooted AnnotationEntity::buildEntity( } // If we can not find the correct descriptor, we can not build the entity // either. - Rooted descriptor = resolveDescriptor(domains, className); + Rooted descriptor = + resolveDescriptor(domains, className, RttiTypes::AnnotationClass); if (descriptor == nullptr) { return {nullptr}; } // Then construct the AnnotationEntity itself - Rooted anno{ - new AnnotationEntity(parent->getManager(), parent, descriptor, - attributes, start, end, name)}; + Rooted anno{new AnnotationEntity( + parent->getManager(), parent, descriptor.cast(), + attributes, start, end, name)}; // append the new entity to the document parent->getAnnotations().push_back(anno); // and return it. return anno; } -/* Type registrations */ +/* Class Document */ + +void Document::continueResolve(ResolutionState &state) +{ + // Try to resolve annotations and other document nodes first + bool hasResult = + continueResolveComposita(annotations, annotations.getIndex(), state); + if (root != nullptr) { + hasResult = hasResult | continueResolveCompositum(root, state); + } + + // If no direct child has been found, continue resolving the referenced + // domains + if (!hasResult) { + continueResolveReferences(domains, state); + } +} } +/* Type registrations */ namespace RttiTypes { const Rtti DocumentEntity = RttiBuilder("DocumentEntity").parent(&Node); diff --git a/src/core/model/Document.hpp b/src/core/model/Document.hpp index 993df9e..466185b 100644 --- a/src/core/model/Document.hpp +++ b/src/core/model/Document.hpp @@ -353,7 +353,7 @@ private: public: AnnotationEntity(Manager &mgr, Handle parent, - Handle descriptor, Variant attributes, + Handle descriptor, Variant attributes, Handle start, Handle end, std::string name = "") : DocumentEntity(mgr, parent, descriptor, attributes, std::move(name)), @@ -415,6 +415,9 @@ private: // TODO: Might there be several roots? E.g. metadata? Owned root; NodeVector annotations; + NodeVector domains; + + void continueResolve(ResolutionState &state) override; public: Document(Manager &mgr, std::string name) @@ -428,7 +431,16 @@ public: Rooted getRoot() const { return root; } - NodeVector getAnnotations() { return annotations; } + NodeVector &getAnnotations() { return annotations; } + + const NodeVector &getDomains() const { return domains; } + + void addDomain(Handle d) { domains.push_back(d); } + + void addDomains(const std::vector> d) + { + domains.insert(domains.end(), d.begin(), d.end()); + } }; } -- cgit v1.2.3