From 78bdb210ad81edd0ba41a6adfc4797b1ccc62228 Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Fri, 23 Jan 2015 00:40:51 +0100 Subject: Added 'createChild'-style methods to Document and Domain classes. --- src/core/model/Document.cpp | 46 ++++++++++++++++++++-- src/core/model/Document.hpp | 91 ++++++++++++++++++++++++++++++++++++++++++- src/core/model/Domain.cpp | 31 +++++++++++++++ src/core/model/Domain.hpp | 94 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 257 insertions(+), 5 deletions(-) (limited to 'src/core') diff --git a/src/core/model/Document.cpp b/src/core/model/Document.cpp index 2ae9107..61c384d 100644 --- a/src/core/model/Document.cpp +++ b/src/core/model/Document.cpp @@ -383,8 +383,8 @@ void DocumentEntity::addStructureNodes( } } -bool DocumentEntity::removeStructureNodeFromField( - Handle s, const std::string &fieldName) +bool DocumentEntity::removeStructureNodeFromField(Handle s, + const std::string &fieldName) { return removeStructureNodeFromField( s, getFieldDescriptorIndex(fieldName, true)); @@ -428,6 +428,29 @@ bool DocumentEntity::removeStructureNode(Handle s) return false; } +Rooted DocumentEntity::createChildStructuredEntity( + Handle descriptor, Variant attributes, + const std::string &fieldName, std::string name) +{ + return Rooted{new StructuredEntity( + subInst->getManager(), subInst, descriptor, std::move(attributes), + fieldName, std::move(name))}; +} + +Rooted DocumentEntity::createChildDocumentPrimitive( + Variant content, const std::string &fieldName) +{ + return Rooted{new DocumentPrimitive( + subInst->getManager(), subInst, std::move(content), fieldName)}; +} + +Rooted DocumentEntity::createChildAnchor(std::string name, + const std::string &fieldName) +{ + return Rooted{ + new Anchor(subInst->getManager(), std::move(name), subInst, fieldName)}; +} + /* Class StructureNode */ bool StructureNode::doValidate(Logger &logger) const @@ -611,6 +634,14 @@ bool Document::doValidate(Logger &logger) const return valid & continueValidation(annotations, logger); } +Rooted Document::createRootStructuredEntity( + Handle descriptor, Variant attributes, std::string name) +{ + return Rooted{ + new StructuredEntity(getManager(), Handle{this}, descriptor, + attributes, std::move(name))}; +} + void Document::addAnnotation(Handle a) { // only add it if we need to. @@ -635,8 +666,6 @@ void Document::addAnnotations(const std::vector> &as) } } - - bool Document::removeAnnotation(Handle a) { auto it = annotations.find(a); @@ -649,6 +678,15 @@ bool Document::removeAnnotation(Handle a) return false; } +Rooted Document::createChildAnnotation( + Handle descriptor, Handle start, + Handle end, Variant attributes, std::string name) +{ + return Rooted{ + new AnnotationEntity(getManager(), this, descriptor, start, end, + attributes, std::move(name))}; +} + bool Document::hasChild(Handle s) const { Rooted parent = s->getParent(); diff --git a/src/core/model/Document.hpp b/src/core/model/Document.hpp index 7357dd2..97bbb60 100644 --- a/src/core/model/Document.hpp +++ b/src/core/model/Document.hpp @@ -126,6 +126,9 @@ namespace model { class Document; class StructureNode; +class StructuredEntity; +class DocumentPrimitive; +class Anchor; /** * A DocumentEntity is the common superclass for StructuredEntities and @@ -309,7 +312,6 @@ public: */ void addStructureNodes(const std::vector> &ss, const std::string &fieldName = ""); - /** * This removes a StructureNode from the field with the given name. If an * empty name is given it is assumed that the 'default' FieldDescriptor is @@ -393,6 +395,49 @@ public: * not found. */ bool removeStructureNode(Handle s); + + /** + * This creates a new StructuredEntity as child of this DocumentEntity. + * + * @param descriptor is the StructuredClass of this StructuredEntity. + * @param attributes is a Map Variant containing attribute fillings for this + * StructuredEntity. It is empty per default. + * @param fieldName is the name of the field, where the newly created + * StructuredEntity shall be added to this DocumentEntity. + * @param name is some name for this StructuredEntity that may be used + * for later reference. It is empty per default. + * + * @return the newly created StructuredEntity. + */ + Rooted createChildStructuredEntity( + Handle descriptor, Variant attributes = {}, + const std::string &fieldName = "", std::string name = ""); + /* + * Creates a new DocumentPrimitive as child of this DocumentEntity. + * + * @param content is a Variant containing the content of this + * DocumentPrimitive. The Type of this Variant is + * specified at the parents Descriptor for the given + * fieldName. + * @param fieldName is the name of the field, where the newly created + * StructuredEntity shall be added to this DocumentEntity. + * + * @return the newly created DocumentPrimitive. + */ + Rooted createChildDocumentPrimitive( + Variant content = {}, const std::string &fieldName = ""); + + /** + * Creates a new Anchor as child of this DocumentEntity. + * + * @param name is the Anchor id. + * @param fieldName is the name of the field, where the newly created + * Anchor shall be added to this DocumentEntity. + * + * @return the newly created Anchor. + */ + Rooted createChildAnchor(std::string name, + const std::string &fieldName = ""); }; /** @@ -701,6 +746,17 @@ public: { } + /** + * This sets up an empty document. + * + * @param mgr is the Manager instance. + * @param name is a name for this Document. + */ + static Rooted createEmptyDocument(Manager &mgr, std::string name) + { + return Rooted{new Document(mgr, std::move(name))}; + } + /** * Sets the root StructuredEntity of this Document. This also sets the * parent of the given StructuredEntity if it is not set to this Document @@ -722,6 +778,21 @@ public: */ Rooted getRoot() const { return root; } + /** + * This creates a new StructuredEntity and adds it as root to this Document. + * + * @param descriptor is the StructuredClass of this StructuredEntity. + * @param attributes is a Map Variant containing attribute fillings for this + * StructuredEntity. It is empty per default. + * @param name is some name for this StructuredEntity that may be used + * for later reference. It is empty per default. + * + * @return the newly constructed StructuredEntity. + */ + Rooted createRootStructuredEntity( + Handle descriptor, Variant attributes = {}, + std::string name = ""); + /** * Returns a const reference to the NodeVector of AnnotationEntities that * span over Anchors in this Documents structure. @@ -761,6 +832,24 @@ public: * Document did not have the given AnnotationEntity as child. */ bool removeAnnotation(Handle a); + /** + * Creates a new AnnotationEntity as child of this Document. + * + * @param descriptor is the AnnotationClass of this AnnotationEntity. + * @param start is the start Anchor of this AnnotationEntity. It has to + * be part of this Document. + * @param end is the end Anchor of this Annotationentity. It has to + * be part of this Document. + * @param attributes is a Map Variant containing attribute fillings for this + * AnnotationEntity. It is empty per default. + * @param name is some name for this AnnotationEntity that might be + * used for references later on. It is empty per default. + * + * @return the newly constructed AnnotationEntity. + */ + Rooted createChildAnnotation( + Handle descriptor, Handle start, + Handle end, Variant attributes = {}, std::string name = ""); /** * Returns a const reference to the NodeVector of Domains that are used diff --git a/src/core/model/Domain.cpp b/src/core/model/Domain.cpp index b425174..c7afd22 100644 --- a/src/core/model/Domain.cpp +++ b/src/core/model/Domain.cpp @@ -289,6 +289,20 @@ bool Descriptor::removeFieldDescriptor(Handle fd) return false; } +Rooted Descriptor::createPrimitiveFieldDescriptor( + Handle primitiveType, std::string name, bool optional) +{ + return Rooted{new FieldDescriptor( + getManager(), this, primitiveType, std::move(name), optional)}; +} + +Rooted Descriptor::createFieldDescriptor( + FieldDescriptor::FieldType fieldType, std::string name, bool optional) +{ + return Rooted{new FieldDescriptor( + getManager(), this, fieldType, std::move(name), optional)}; +} + /* Class StructuredClass */ StructuredClass::StructuredClass(Manager &mgr, std::string name, @@ -481,6 +495,16 @@ bool Domain::removeStructuredClass(Handle s) return false; } +Rooted Domain::createStructuredClass( + std::string name, const Cardinality &cardinality, + Handle attributesDescriptor, Handle superclass, + bool transparent, bool root) +{ + return Rooted{new StructuredClass( + getManager(), std::move(name), this, cardinality, attributesDescriptor, + superclass, std::move(transparent), std::move(root))}; +} + void Domain::addAnnotationClass(Handle a) { // only add it if we need to. @@ -509,6 +533,13 @@ bool Domain::removeAnnotationClass(Handle a) } return false; } + +Rooted Domain::createAnnotationClass( + std::string name, Handle attributesDescriptor) +{ + return Rooted{new AnnotationClass( + getManager(), std::move(name), this, attributesDescriptor)}; +} } /* Type registrations */ diff --git a/src/core/model/Domain.hpp b/src/core/model/Domain.hpp index 12cb9b3..e40a9f3 100644 --- a/src/core/model/Domain.hpp +++ b/src/core/model/Domain.hpp @@ -548,6 +548,41 @@ public: */ bool removeFieldDescriptor(Handle fd); + /** + * This creates a new primitive FieldDescriptor and adds it to this + * Descriptor. + * + * @param primitiveType is a handle to some Type in some Typesystem of which + * one instance is allowed to fill this field. + * @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. + * + * @return the newly created FieldDescriptor. + */ + Rooted createPrimitiveFieldDescriptor( + Handle primitiveType, std::string name = "", + bool optional = false); + + /** + * This creates a new primitive FieldDescriptor and adds it to this + * Descriptor. + * + * @param fieldType is the FieldType of this FieldDescriptor, either + * 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. + * + * @return the newly created FieldDescriptor. + */ + Rooted createFieldDescriptor( + FieldDescriptor::FieldType fieldType = FieldDescriptor::FieldType::TREE, + std::string name = "", bool optional = false); + /** * This tries to construct the shortest possible path of this Descriptor * to the given child Descriptor. As an example consider the book domain @@ -910,6 +945,18 @@ public: includeTypesystem(sys); } + /** + * Creates a new Domain and returns it. + * + * @param mgr is the Manager instance. + * @param name is a name for this domain which will be used for later + * references to this Domain. + */ + static Rooted createEmptyDomain(Manager &mgr, std::string name) + { + return Rooted{new Domain(mgr, std::move(name))}; + } + /** * Returns a const reference to the NodeVector of StructuredClasses that are * part of this Domain. @@ -940,6 +987,40 @@ public: */ bool removeStructuredClass(Handle s); + /** + * This creates a new StructuredClass and appends it to this Domain. + * + * @param name is the name of the StructuredClass. + * @param cardinality specifies how often an element of this type + * may occur at a specific point in the + * StructureTree. For example: A document should + * have at least one author. This is set to * + * per default, meaning that any number of + * of instances is valid, including zero. + * @param attributesDescriptor is a StructType that specifies the attribute + * keys as well as value domains for this + * Descriptor. + * @param superclass references a parent StructuredClass. Please + * look for more information on inheritance in + * the class documentation above. The default is + * a null reference, meaning no super class. + * The constructor automatically registers this + * class as a subclass at the super class. + * @param transparent specifies whether this StructuredClass is + * transparent. For more information on + * transparency please refer to the class + * documentation above. The default is false. + * @param root specifies whether this StructuredClass is + * allowed to be at the root of a Document. + * + * @return the newly created StructuredClass. + */ + Rooted createStructuredClass( + std::string name, const Cardinality &cardinality = AnyCardinality, + Handle attributesDescriptor = nullptr, + Handle superclass = nullptr, bool transparent = false, + bool root = false); + /** * Returns a const reference to the NodeVector of AnnotationClasses that are * part of this Domain. @@ -970,6 +1051,19 @@ public: */ bool removeAnnotationClass(Handle a); + /** + * This creates a new AnnotationClass and appends it to this Domain. + * + * @param name is a name for this AnnotationClass that will + * be used for later references to this + * AnnotationClass. + * @param attributesDescriptor is a StructType that specifies the attribute + * keys as well as value domains for this + * Descriptor. + */ + Rooted createAnnotationClass( + std::string name, Handle attributesDescriptor = nullptr); + /** * Returns a const reference to the NodeVector of TypeSystems that are * references in this Domain. -- cgit v1.2.3