From 6dd18a83a1f2c89c5bca435090c80d72cb8716e3 Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Wed, 17 Dec 2014 11:44:36 +0100 Subject: First draft of Cardinality. There are still semantic improvements to be made, though. --- test/core/model/CardinalityTest.cpp | 112 ++++++++++++++++++++++++++++++++++++ test/core/model/DocumentTest.cpp | 32 +++++++++++ 2 files changed, 144 insertions(+) create mode 100644 test/core/model/CardinalityTest.cpp create mode 100644 test/core/model/DocumentTest.cpp (limited to 'test') diff --git a/test/core/model/CardinalityTest.cpp b/test/core/model/CardinalityTest.cpp new file mode 100644 index 0000000..34fa272 --- /dev/null +++ b/test/core/model/CardinalityTest.cpp @@ -0,0 +1,112 @@ +/* + 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 . +*/ + +#include + +#include + +namespace ousia { +namespace model { +TEST(Cardinality, testCardinalities) +{ + // Start with the elementary Cardinalities. + { + SingleCardinality c{1}; + for (size_t s = 0; s < 100; s++) { + if (s != 1) { + ASSERT_FALSE(c.permits(s)); + } else { + ASSERT_TRUE(c.permits(s)); + } + } + } + + { + OpenRangeCardinality c{4}; + for (size_t s = 0; s < 100; s++) { + if (s < 4) { + ASSERT_FALSE(c.permits(s)); + } else { + ASSERT_TRUE(c.permits(s)); + } + } + } + + { + RangeCardinality c{1, 10}; + for (size_t s = 0; s < 100; s++) { + if (s < 1 || s > 10) { + ASSERT_FALSE(c.permits(s)); + } else { + ASSERT_TRUE(c.permits(s)); + } + } + } + + // Then construct more complex ones as unions. + + { + UnionCardinality c = + unite(SingleCardinality(1), + unite(RangeCardinality(4, 6), OpenRangeCardinality(16))); + for (size_t s = 0; s < 100; s++) { + if (s < 1 || (s > 1 && s < 4) || (s > 6 && s < 16)) { + ASSERT_FALSE(c.permits(s)); + } else { + ASSERT_TRUE(c.permits(s)); + } + } + } +} + +TEST(Cardinality, testEquals) +{ + { + SingleCardinality a{1}; + SingleCardinality b{2}; + OpenRangeCardinality c{1}; + + ASSERT_EQ(a, a); + ASSERT_EQ(SingleCardinality(1), a); + ASSERT_EQ(b, b); + ASSERT_EQ(c, c); + + ASSERT_FALSE(a == b); + ASSERT_FALSE(b == c); + ASSERT_FALSE(a == c); + } + + { + RangeCardinality a{1, 1}; + RangeCardinality b{1, 2}; + RangeCardinality c{2, 2}; + + ASSERT_EQ(a, a); + ASSERT_EQ(RangeCardinality(1, 1), a); + ASSERT_EQ(b, b); + ASSERT_EQ(c, c); + + ASSERT_FALSE(a == b); + // TODO: Here the semantics break down. It should be equal, in fact. + ASSERT_FALSE(a == SingleCardinality(1)); + ASSERT_FALSE(b == c); + ASSERT_FALSE(a == c); + } +} +} +} diff --git a/test/core/model/DocumentTest.cpp b/test/core/model/DocumentTest.cpp new file mode 100644 index 0000000..36e7c02 --- /dev/null +++ b/test/core/model/DocumentTest.cpp @@ -0,0 +1,32 @@ +/* + 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 . +*/ + +#include + +#include + +namespace ousia { +namespace model { +TEST(Document, testDocumentConstruction) +{ + // Start by constructing the domain. + //TODO: IMPLEMENT + ASSERT_TRUE(true); +} +} +} -- cgit v1.2.3 From 3e124a41f14fa3a76febba09f4b58b3f5361efdb Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Wed, 17 Dec 2014 14:52:58 +0100 Subject: noticed that Andreas did already implement Cardinality in form of the RangeSet. Removed my own attempts at programming the Cardinality and used his code. --- CMakeLists.txt | 2 - src/core/model/Cardinality.hpp | 181 ------------------------------------ src/core/model/Domain.hpp | 6 +- test/core/model/CardinalityTest.cpp | 112 ---------------------- 4 files changed, 4 insertions(+), 297 deletions(-) delete mode 100644 src/core/model/Cardinality.hpp delete mode 100644 test/core/model/CardinalityTest.cpp (limited to 'test') diff --git a/CMakeLists.txt b/CMakeLists.txt index 4469db2..cddf159 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -114,7 +114,6 @@ ADD_LIBRARY(ousia_core src/core/managed/Managed src/core/managed/ManagedType src/core/managed/Manager - src/core/model/Cardinality src/core/model/Document src/core/model/Domain src/core/model/Typesystem @@ -185,7 +184,6 @@ IF(TEST) test/core/managed/ManagedContainerTest test/core/managed/ManagedTest test/core/managed/ManagerTest - test/core/model/CardinalityTest test/core/model/DocumentTest test/core/parser/ParserStackTest # test/core/script/FunctionTest diff --git a/src/core/model/Cardinality.hpp b/src/core/model/Cardinality.hpp deleted file mode 100644 index aad5891..0000000 --- a/src/core/model/Cardinality.hpp +++ /dev/null @@ -1,181 +0,0 @@ -/* - 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 . -*/ - -/** - * @file Cardinality.hpp - * - * A Cardinality in this term here is some arbitrary subset of natural numbers - * (including zero), that specifies the permits size of some other set. - * - * We define Cardinalities in a constructive process, meaning constructive - * operators on elementary sets (either single numbers or ranges of numbers). - * - * Examples for such constructions are: - * - * {1} - * {1,...,4} - * {1,...,4} union {9,...,12} union {16} - * {0,...,infinity} - * - * Note that the only construction operator needed is union (or +). - * - * @author Benjamin Paaßen (bpaassen@techfak.uni-bielefeld.de) - */ - -#ifndef _OUSIA_MODEL_CARDINALITY_HPP_ -#define _OUSIA_MODEL_CARDINALITY_HPP_ - -namespace ousia { -namespace model { - -/** - * This class is an abstract interface for Cardinality implementations, meaning - * either a Union of two other Cardinalities or elementary Cardinalities. - */ -class Cardinality { -public: - /** - * Returns true if and only if the given size is permits according to this - * Cardinality. - * - * @param is some natural number (size). - * @return true if and only if that size is permits. - */ - virtual bool permits(const size_t &size) const = 0; - - virtual bool operator==(const Cardinality &rhs) const = 0; -}; - -/** - * A UnionCardinality is in fact just the binary or applied to the - * permits-criteria of two other cardinalities. - */ -class UnionCardinality : public Cardinality { -private: - const Cardinality &left; - const Cardinality &right; - -public: - UnionCardinality(const Cardinality &left, const Cardinality &right) - : left(left), right(right) - { - } - - bool permits(const size_t &size) const override - { - return left.permits(size) || right.permits(size); - } - - bool operator==(const Cardinality &obj) const override - { - const UnionCardinality *o = - dynamic_cast(&obj); - if (o == NULL) - return false; - return left == o->left && right == o->right; - } -}; - -/** - * The unite function is basically just a wrapper for constructing a - * UnionCardinality. - */ -inline UnionCardinality unite(const Cardinality &lhs, const Cardinality &rhs) -{ - return std::move(UnionCardinality(lhs, rhs)); -} - -/** - * A SingleCardinality permits exactly one number. - */ -class SingleCardinality : public Cardinality { -private: - size_t num; - -public: - SingleCardinality(size_t num) : num(std::move(num)) {} - - bool permits(const size_t &size) const override { return size == num; } - - bool operator==(const Cardinality &obj) const override - { - const SingleCardinality *o = - dynamic_cast(&obj); - if (o == NULL) - return false; - return num == o->num; - } -}; - -/** - * A RangeCardinality permits all numbers between the two bounds (lo and hi), - * inclusively. - */ -class RangeCardinality : public Cardinality { -private: - size_t lo; - size_t hi; - -public: - RangeCardinality(size_t lo, size_t hi) - : lo(std::move(lo)), hi(std::move(hi)) - { - } - - bool permits(const size_t &size) const override - { - return size >= lo && size <= hi; - } - - bool operator==(const Cardinality &obj) const override - { - const RangeCardinality *o = - dynamic_cast(&obj); - if (o == NULL) - return false; - return lo == o->lo && hi == o->hi; - } -}; - -/** - * An OpenRangeCardinality permits all numbers higher or equal than the lower - * bound. - */ -class OpenRangeCardinality : public Cardinality { -private: - size_t lo; - -public: - OpenRangeCardinality(size_t lo) : lo(std::move(lo)) {} - - bool permits(const size_t &size) const override { return size >= lo; } - - bool operator==(const Cardinality &obj) const override - { - const OpenRangeCardinality *o = - dynamic_cast(&obj); - if (o == NULL) - return false; - return lo == o->lo; - } -}; -} -} - -#endif /* _OUSIA_MODEL_CARDINALITY_HPP_ */ - diff --git a/src/core/model/Domain.hpp b/src/core/model/Domain.hpp index 65b9b1d..9ae8871 100644 --- a/src/core/model/Domain.hpp +++ b/src/core/model/Domain.hpp @@ -84,8 +84,8 @@ #include #include +#include -#include "Cardinality.hpp" #include "Typesystem.hpp" namespace ousia { @@ -265,6 +265,8 @@ public: } }; +typedef RangeSet Cardinality; + /** * A StructuredClass specifies nodes in the StructureTree of a document that * implements this domain. For more information on the StructureTree please @@ -342,7 +344,7 @@ public: */ class StructuredClass : public Descriptor { private: - const Cardinality& cardinality; + const Cardinality cardinality; Owned isa; ManagedVector parents; diff --git a/test/core/model/CardinalityTest.cpp b/test/core/model/CardinalityTest.cpp deleted file mode 100644 index 34fa272..0000000 --- a/test/core/model/CardinalityTest.cpp +++ /dev/null @@ -1,112 +0,0 @@ -/* - 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 . -*/ - -#include - -#include - -namespace ousia { -namespace model { -TEST(Cardinality, testCardinalities) -{ - // Start with the elementary Cardinalities. - { - SingleCardinality c{1}; - for (size_t s = 0; s < 100; s++) { - if (s != 1) { - ASSERT_FALSE(c.permits(s)); - } else { - ASSERT_TRUE(c.permits(s)); - } - } - } - - { - OpenRangeCardinality c{4}; - for (size_t s = 0; s < 100; s++) { - if (s < 4) { - ASSERT_FALSE(c.permits(s)); - } else { - ASSERT_TRUE(c.permits(s)); - } - } - } - - { - RangeCardinality c{1, 10}; - for (size_t s = 0; s < 100; s++) { - if (s < 1 || s > 10) { - ASSERT_FALSE(c.permits(s)); - } else { - ASSERT_TRUE(c.permits(s)); - } - } - } - - // Then construct more complex ones as unions. - - { - UnionCardinality c = - unite(SingleCardinality(1), - unite(RangeCardinality(4, 6), OpenRangeCardinality(16))); - for (size_t s = 0; s < 100; s++) { - if (s < 1 || (s > 1 && s < 4) || (s > 6 && s < 16)) { - ASSERT_FALSE(c.permits(s)); - } else { - ASSERT_TRUE(c.permits(s)); - } - } - } -} - -TEST(Cardinality, testEquals) -{ - { - SingleCardinality a{1}; - SingleCardinality b{2}; - OpenRangeCardinality c{1}; - - ASSERT_EQ(a, a); - ASSERT_EQ(SingleCardinality(1), a); - ASSERT_EQ(b, b); - ASSERT_EQ(c, c); - - ASSERT_FALSE(a == b); - ASSERT_FALSE(b == c); - ASSERT_FALSE(a == c); - } - - { - RangeCardinality a{1, 1}; - RangeCardinality b{1, 2}; - RangeCardinality c{2, 2}; - - ASSERT_EQ(a, a); - ASSERT_EQ(RangeCardinality(1, 1), a); - ASSERT_EQ(b, b); - ASSERT_EQ(c, c); - - ASSERT_FALSE(a == b); - // TODO: Here the semantics break down. It should be equal, in fact. - ASSERT_FALSE(a == SingleCardinality(1)); - ASSERT_FALSE(b == c); - ASSERT_FALSE(a == c); - } -} -} -} -- cgit v1.2.3 From 93c065174e1aa306ee724dd523ef6b2254c1d388 Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Wed, 17 Dec 2014 17:11:51 +0100 Subject: Some (slight) changes to existing structures and first attempts on a test to construct a test comain and a test document out of thin air. I am, indeed, an alchemist. --- src/core/managed/ManagedContainer.hpp | 11 ++++ src/core/model/Document.hpp | 8 ++- src/core/model/Domain.hpp | 41 +++++++++----- src/core/model/Typesystem.hpp | 2 + test/core/managed/ManagedContainerTest.cpp | 41 ++++++++++---- test/core/model/DocumentTest.cpp | 12 +++- test/core/model/ModelTestUtils.hpp | 90 ++++++++++++++++++++++++++++++ 7 files changed, 176 insertions(+), 29 deletions(-) create mode 100644 test/core/model/ModelTestUtils.hpp (limited to 'test') diff --git a/src/core/managed/ManagedContainer.hpp b/src/core/managed/ManagedContainer.hpp index 19bff3f..6bf1915 100644 --- a/src/core/managed/ManagedContainer.hpp +++ b/src/core/managed/ManagedContainer.hpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include "Manager.hpp" @@ -496,6 +497,16 @@ public: } Base::c.pop_back(); } + + Rooted operator[](int i) const { + for (const_iterator it = Base::cbegin(); it != Base::cend(); it++) { + if (i == 0) { + return Rooted(*it); + } + i--; + } + throw std::out_of_range(std::to_string(i) + " is out of range!"); + } }; /** diff --git a/src/core/model/Document.hpp b/src/core/model/Document.hpp index 3114480..a31e52f 100644 --- a/src/core/model/Document.hpp +++ b/src/core/model/Document.hpp @@ -26,9 +26,11 @@ * * A Document, from top to bottom, consists of "Document" instance, * which "owns" the structural root node of the in-document graph. This might - * for example be a "book" node, if the respective document implements the - * "book" domain. That root node in turn has structure nodes as children as well - * as annotations that refer to the content of that structure node. + * for example be a "book" node of the "book" domain. That root node in turn has + * structure nodes as children, which in turn may have children. This + * constitutes a Structure Tree. Additionally annotations may be attached to + * Structure Nodes, effectively resulting in a Document Graph instead of a + * Document Tree (other references may introduce cycles as well). * * Consider this simplified XML representation of a document (TODO: Use * non-simplified XML as soon as possible): diff --git a/src/core/model/Domain.hpp b/src/core/model/Domain.hpp index 9ae8871..e348136 100644 --- a/src/core/model/Domain.hpp +++ b/src/core/model/Domain.hpp @@ -91,8 +91,9 @@ namespace ousia { namespace model { -class StructuredClass; class Descriptor; +class StructuredClass; +class Domain; /** * As mentioned in the description above a FieldDescriptor specifies the @@ -147,17 +148,18 @@ public: * set to "PRIMITIVE". * * @param mgr is the global Manager instance. - * @param name is the name of this field. * @param parent is a handle of the Descriptor node that has this * FieldDescriptor. * @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. */ - FieldDescriptor(Manager &mgr, std::string name, Handle parent, - Handle primitiveType, bool optional) + FieldDescriptor(Manager &mgr, Handle parent, + Handle primitiveType, std::string name = "", + bool optional = false) : Node(mgr, std::move(name), parent), children(this), fieldType(FieldType::PRIMITIVE), @@ -171,18 +173,19 @@ public: * children here. * * @param mgr is the global Manager instance. - * @param name is the name of this field. * @param parent is a handle of the Descriptor node that has this * FieldDescriptor. * @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. */ - FieldDescriptor(Manager &mgr, std::string name, Handle parent, - FieldType fieldType, bool optional) + FieldDescriptor(Manager &mgr, Handle parent, + FieldType fieldType = FieldType::TREE, + std::string name = "", bool optional = false) : Node(mgr, std::move(name), parent), children(this), fieldType(fieldType), @@ -239,10 +242,10 @@ private: ManagedVector fieldDescriptors; public: - Descriptor(Manager &mgr, std::string name, Handle parent, + Descriptor(Manager &mgr, std::string name, Handle domain, // TODO: What would be a wise default value for attributes? Handle attributesDescriptor) - : Node(mgr, std::move(name), parent), + : Node(mgr, std::move(name), domain), attributesDescriptor(acquire(attributesDescriptor)), fieldDescriptors(this) { @@ -351,12 +354,13 @@ private: public: const bool transparent; - StructuredClass(Manager &mgr, std::string name, Handle parent, - Handle attributesDescriptor, + StructuredClass(Manager &mgr, std::string name, Handle domain, const Cardinality &cardinality, + Handle attributesDescriptor = {nullptr}, // TODO: What would be a wise default value for isa? - Handle isa, bool transparent) - : Descriptor(mgr, std::move(name), parent, attributesDescriptor), + Handle isa = {nullptr}, + bool transparent = false) + : Descriptor(mgr, std::move(name), domain, attributesDescriptor), cardinality(cardinality), isa(acquire(isa)), parents(this), @@ -393,13 +397,15 @@ class Domain : public Node { private: ManagedVector rootStructures; ManagedVector annotationClasses; + ManagedVector typesystems; public: Domain(Manager &mgr, std::string name) // TODO: Can a domain have a parent? : Node(mgr, std::move(name), nullptr), rootStructures(this), - annotationClasses(this) + annotationClasses(this), + typesystems(this) { } @@ -423,6 +429,13 @@ public: { return annotationClasses; } + + ManagedVector &getTypesystems() { return typesystems; } + + const ManagedVector &getTypesystems() const + { + return typesystems; + } }; } } diff --git a/src/core/model/Typesystem.hpp b/src/core/model/Typesystem.hpp index 347adb8..90154ce 100644 --- a/src/core/model/Typesystem.hpp +++ b/src/core/model/Typesystem.hpp @@ -372,6 +372,8 @@ public: * TODO: DOC */ void addType(Handle type) { types.push_back(type); } + + const NodeVector &getTypes() const { return types; } }; } } diff --git a/test/core/managed/ManagedContainerTest.cpp b/test/core/managed/ManagedContainerTest.cpp index c34541a..7ff819f 100644 --- a/test/core/managed/ManagedContainerTest.cpp +++ b/test/core/managed/ManagedContainerTest.cpp @@ -38,7 +38,7 @@ TEST(ManagedVector, managedVector) { Rooted root{new Managed{mgr}}; - std::vector elems; + std::vector elems; for (int i = 0; i < nElem; i++) { elems.push_back(new TestManaged{mgr, a[i]}); } @@ -49,7 +49,8 @@ TEST(ManagedVector, managedVector) ManagedVector v(root, elems.begin(), elems.end()); - // Remove the last element from the list. It should be garbage collected. + // Remove the last element from the list. It should be garbage + // collected. v.pop_back(); ASSERT_FALSE(a[nElem - 1]); @@ -85,7 +86,6 @@ TEST(ManagedVector, managedVector) } } - TEST(ManagedVector, moveAssignment) { constexpr int nElem = 16; @@ -220,16 +220,39 @@ TEST(ManagedVector, moveWithNewOwner) } } -class TestManagedWithContainer : public Managed { +TEST(ManagedVector, accessOperator) +{ + Manager mgr{1}; + Rooted root{new Managed{mgr}}; + ManagedVector instance{root}; + Rooted elem{new Managed{mgr}}; + instance.push_back(elem); + + ASSERT_EQ(elem, instance[0]); + + // Test out of bounds. + bool caught = false; + try { + instance[1]; + } + catch (std::out_of_range ex) { + caught = true; + } + ASSERT_TRUE(caught); + + instance.push_back(elem); + ASSERT_EQ(elem, instance[1]); +} +class TestManagedWithContainer : public Managed { public: ManagedVector elems; - TestManagedWithContainer(Manager &mgr) : Managed(mgr), elems(this) {}; - + TestManagedWithContainer(Manager &mgr) : Managed(mgr), elems(this){}; }; -TEST(ManagedVector, embedded) { +TEST(ManagedVector, embedded) +{ // Note: This test depends on the correct deletion order -- otherwise // valgrind shows an error bool a; @@ -248,7 +271,6 @@ TEST(ManagedVector, embedded) { ASSERT_FALSE(a); } - TEST(ManagedMap, managedMap) { // TODO: This test is highly incomplete @@ -260,7 +282,7 @@ TEST(ManagedMap, managedMap) { Rooted root{new Managed{mgr}}; - std::map elems; + std::map elems; for (int i = 0; i < nElem; i++) { elems.insert(std::make_pair(i, new TestManaged{mgr, a[i]})); } @@ -298,6 +320,5 @@ TEST(ManagedMap, managedMap) ASSERT_FALSE(v); } } - } diff --git a/test/core/model/DocumentTest.cpp b/test/core/model/DocumentTest.cpp index 36e7c02..dd883a4 100644 --- a/test/core/model/DocumentTest.cpp +++ b/test/core/model/DocumentTest.cpp @@ -20,12 +20,20 @@ #include +#include "ModelTestUtils.hpp" + namespace ousia { namespace model { + + TEST(Document, testDocumentConstruction) { - // Start by constructing the domain. - //TODO: IMPLEMENT + // Construct Manager + Manager mgr{1}; + // Get the domain. + Rooted domain = constructBookDomain(mgr); + + // TODO: IMPLEMENT ASSERT_TRUE(true); } } diff --git a/test/core/model/ModelTestUtils.hpp b/test/core/model/ModelTestUtils.hpp new file mode 100644 index 0000000..665e351 --- /dev/null +++ b/test/core/model/ModelTestUtils.hpp @@ -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 . +*/ + +#ifndef _MODEL_TEST_UTILS_HPP_ +#define _MODEL_TEST_UTILS_HPP_ + +#include +#include + +namespace ousia { +namespace model { + +/** + * This constructs a somewhat trivial system of standard types. + * + * Currently contained: string + */ +static Rooted constructTypeSystem(Manager &mgr) +{ + Rooted sys{new Typesystem(mgr, "std")}; + Rooted string{new StringType(mgr, sys)}; + sys->addType(string); + + return sys; +} + +/** + * This constructs the "book" domain for test purposes. The structure of the + * domain is fairly and can be seen from the construction itself. + */ +static Rooted constructBookDomain(Manager &mgr) +{ + // Start with the Domain itself. + Rooted domain{new Domain(mgr, "book")}; + // The standard type system. + domain->getTypesystems().push_back(constructTypeSystem(mgr)); + // Set up the cardinalities we'll need. + Cardinality single; + single.merge({1}); + Cardinality any; + any.merge(Range::typeRangeFrom(0)); + + // Set up the "book" node. + Rooted book{ + new StructuredClass(mgr, "book", domain, single)}; + domain->getRootStructures().push_back(book); + // The structure field of it. + Rooted book_field{new FieldDescriptor(mgr, book)}; + book->getFieldDescriptors().push_back(book_field); + + // From there on the "section". + Rooted section{ + new StructuredClass(mgr, "section", domain, any)}; + book_field->getChildren().push_back(section); + // And the field of it. + Rooted section_field{new FieldDescriptor(mgr, section)}; + section->getFieldDescriptors().push_back(section_field); + + // We also add the "paragraph", which is transparent. + Rooted paragraph{new StructuredClass( + mgr, "paragraph", domain, any, {nullptr}, {nullptr}, true)}; + section_field->getChildren().push_back(paragraph); + book_field->getChildren().push_back(paragraph); + // ... and has a primitive field. + Rooted text{new FieldDescriptor( + mgr, paragraph, domain->getTypesystems()[0]->getTypes()[0], "text", + false)}; + + return domain; +} +} +} + +#endif /* _TEST_MANAGED_H_ */ + -- cgit v1.2.3 From 94a60d364203f633370e1b0a77ec5b89428032e3 Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Thu, 18 Dec 2014 14:49:50 +0100 Subject: Hopefully implemented a working version of the Domain resolve mechanism. --- CMakeLists.txt | 1 + src/core/Node.hpp | 5 ++++ src/core/model/Domain.cpp | 54 ++++++++++++++++++++++++++++++++++ src/core/model/Domain.hpp | 26 +++++++++++++++- test/core/model/DomainTest.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 test/core/model/DomainTest.cpp (limited to 'test') diff --git a/CMakeLists.txt b/CMakeLists.txt index cddf159..077ac47 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -184,6 +184,7 @@ IF(TEST) test/core/managed/ManagedContainerTest test/core/managed/ManagedTest test/core/managed/ManagerTest + test/core/model/DomainTest test/core/model/DocumentTest test/core/parser/ParserStackTest # test/core/script/FunctionTest diff --git a/src/core/Node.hpp b/src/core/Node.hpp index 4bc95be..516da03 100644 --- a/src/core/Node.hpp +++ b/src/core/Node.hpp @@ -350,6 +350,11 @@ public: * Returns the name of the node. */ std::string getName() const { return name; } + + /** + * Returns a reference to the name of the node. + */ + const std::string& getNameRef() const { return name; } /** * Specifies whether the node has a name, e.g. whether the current name is diff --git a/src/core/model/Domain.cpp b/src/core/model/Domain.cpp index bafd205..8eee86a 100644 --- a/src/core/model/Domain.cpp +++ b/src/core/model/Domain.cpp @@ -21,6 +21,60 @@ namespace ousia { namespace model { +void FieldDescriptor::doResolve(std::vector> &res, + const std::vector &path, + Filter filter, void *filterData, unsigned idx, + VisitorSet &visited) +{ + // We call resolve for the children, but give them the field name as + // alias. + for (auto &c : children) { + c->resolve(res, path, filter, filterData, idx, visited, &getNameRef()); + } +} + +// TODO: better alias? +static std::string DESCRIPTOR_ATTRIBUTES_ALIAS {"attributes"}; + +void Descriptor::doResolve(std::vector> &res, + const std::vector &path, Filter filter, + void *filterData, unsigned idx, VisitorSet &visited) +{ + // TODO: This could be a problem, because the name of the field might be + // needed in the path. + for (auto &fd : fieldDescriptors) { + fd->resolve(res, path, filter, filterData, idx, visited, nullptr); + } + // TODO: This throws a SEGFAULT for some reason. +// attributesDescriptor->resolve(res, path, filter, filterData, idx, visited, +// &DESCRIPTOR_ATTRIBUTES_ALIAS); +} + +void StructuredClass::doResolve(std::vector> &res, + const std::vector &path, + Filter filter, void *filterData, unsigned idx, + VisitorSet &visited) +{ + Descriptor::doResolve(res, path, filter, filterData, idx, visited); + if(!isa.isNull()){ + isa->doResolve(res, path, filter, filterData, idx, visited); + } +} + +void Domain::doResolve(std::vector> &res, + const std::vector &path, Filter filter, + void *filterData, unsigned idx, VisitorSet &visited) +{ + for (auto &s : rootStructures) { + s->resolve(res, path, filter, filterData, idx, visited, nullptr); + } + for (auto &a : annotationClasses) { + a->resolve(res, path, filter, filterData, idx, visited, nullptr); + } + for (auto &t : typesystems) { + t->resolve(res, path, filter, filterData, idx, visited, nullptr); + } +} } } diff --git a/src/core/model/Domain.hpp b/src/core/model/Domain.hpp index e348136..112f2fa 100644 --- a/src/core/model/Domain.hpp +++ b/src/core/model/Domain.hpp @@ -139,6 +139,12 @@ private: FieldType fieldType; Owned primitiveType; +protected: + void doResolve(std::vector> &res, + const std::vector &path, Filter filter, + void *filterData, unsigned idx, + VisitorSet &visited) override; + public: const bool optional; @@ -220,7 +226,7 @@ public: * the attribute specification of a descriptor is done by referencing an * appropriate StructType that contains all permitted keys and value types. * - * TODO: What aout optional attributes? + * TODO: What about optional attributes? * * In XML terms the difference between primitive fields and attributes can be * explained as the difference between node attributes and node children. @@ -241,6 +247,12 @@ private: Owned attributesDescriptor; ManagedVector fieldDescriptors; +protected: + void doResolve(std::vector> &res, + const std::vector &path, Filter filter, + void *filterData, unsigned idx, + VisitorSet &visited) override; + public: Descriptor(Manager &mgr, std::string name, Handle domain, // TODO: What would be a wise default value for attributes? @@ -351,6 +363,12 @@ private: Owned isa; ManagedVector parents; +protected: + void doResolve(std::vector> &res, + const std::vector &path, Filter filter, + void *filterData, unsigned idx, + VisitorSet &visited) override; + public: const bool transparent; @@ -399,6 +417,12 @@ private: ManagedVector annotationClasses; ManagedVector typesystems; +protected: + void doResolve(std::vector> &res, + const std::vector &path, Filter filter, + void *filterData, unsigned idx, + VisitorSet &visited) override; + public: Domain(Manager &mgr, std::string name) // TODO: Can a domain have a parent? diff --git a/test/core/model/DomainTest.cpp b/test/core/model/DomainTest.cpp new file mode 100644 index 0000000..b1538cf --- /dev/null +++ b/test/core/model/DomainTest.cpp @@ -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 . +*/ + +#include + +#include + +#include + +#include "ModelTestUtils.hpp" + +namespace ousia { +namespace model { + +void assert_path(std::vector> &result, size_t idx, + const std::type_info& expected_type, + std::vector expected_path) +{ + ASSERT_TRUE(result.size() > idx); + // check class/type + ASSERT_EQ(expected_type, typeid(*result[idx])); + // transform to node + Managed *m = &(*result[idx]); + Node *n = dynamic_cast(m); + ASSERT_TRUE(n); + // extract actual path + std::vector actual_path = n->path(); + // check path + ASSERT_EQ(expected_path, actual_path); +} + +TEST(Document, testDomainResolving) +{ + // Construct Manager + Manager mgr{1}; + // Get the domain. + Rooted domain = constructBookDomain(mgr); + + /* + * Start with the "book" search keyword. This should resolve to the domain + * itself (because it is called "book"), as well as the structure "book" + * node. + */ + std::vector> res = + domain->resolve(std::vector{"book"}); + // First we expect the book domain. + assert_path(res, 0, typeid(Domain), {"book"}); + // Then the book structure. + assert_path(res, 1, typeid(StructuredClass), {"book", "book"}); +} +} +} -- cgit v1.2.3 From 776fbb600da5fb5268e1b803e74fe7f8b0ea2746 Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Thu, 18 Dec 2014 21:44:17 +0100 Subject: Finished the Domain test for now. Resolving mechanism works as expected (or at least well enough). --- test/core/model/DomainTest.cpp | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/core/model/DomainTest.cpp b/test/core/model/DomainTest.cpp index b1538cf..57ec91c 100644 --- a/test/core/model/DomainTest.cpp +++ b/test/core/model/DomainTest.cpp @@ -28,7 +28,7 @@ namespace ousia { namespace model { void assert_path(std::vector> &result, size_t idx, - const std::type_info& expected_type, + const std::type_info &expected_type, std::vector expected_path) { ASSERT_TRUE(result.size() > idx); @@ -62,6 +62,39 @@ TEST(Document, testDomainResolving) assert_path(res, 0, typeid(Domain), {"book"}); // Then the book structure. assert_path(res, 1, typeid(StructuredClass), {"book", "book"}); + ASSERT_EQ(2, res.size()); + + /* + * If we explicitly ask for the "book, book" path, then only the + * StructuredClass should be returned. + */ + res = domain->resolve(std::vector{"book", "book"}); + assert_path(res, 0, typeid(StructuredClass), {"book", "book"}); + ASSERT_EQ(1, res.size()); + + /* + * If we ask for "section" the result should be unique as well. + */ + res = domain->resolve(std::vector{"section"}); + // TODO: Is that the path result we want? + assert_path(res, 0, typeid(StructuredClass), {"book", "section"}); + ASSERT_EQ(1, res.size()); + + /* + * If we ask for the path "book", "book", "" we reference the + * FieldDescriptor of the StructuredClass "book". + */ + res = domain->resolve(std::vector{"book", "book", ""}); + assert_path(res, 0, typeid(FieldDescriptor), {"book", "book", ""}); + ASSERT_EQ(1, res.size()); + + /* + * If we ask for "paragraph" it is references two times in the Domain graph, + * but should be returned only once. + */ + res = domain->resolve("paragraph"); + assert_path(res, 0, typeid(StructuredClass), {"book", "paragraph"}); + ASSERT_EQ(1, res.size()); } } } -- cgit v1.2.3 From 55d363bea503c0607c80604280c26c235a5d7ee1 Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Fri, 19 Dec 2014 00:29:02 +0100 Subject: refactored DomainTest somewhat to prevent an unused function warning. --- test/core/model/DomainTest.cpp | 2 +- test/core/model/ModelTestUtils.hpp | 90 ------------------------------------ test/core/model/TestDomain.hpp | 94 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 91 deletions(-) delete mode 100644 test/core/model/ModelTestUtils.hpp create mode 100644 test/core/model/TestDomain.hpp (limited to 'test') diff --git a/test/core/model/DomainTest.cpp b/test/core/model/DomainTest.cpp index 57ec91c..f6dff3c 100644 --- a/test/core/model/DomainTest.cpp +++ b/test/core/model/DomainTest.cpp @@ -22,7 +22,7 @@ #include -#include "ModelTestUtils.hpp" +#include "TestDomain.hpp" namespace ousia { namespace model { diff --git a/test/core/model/ModelTestUtils.hpp b/test/core/model/ModelTestUtils.hpp deleted file mode 100644 index 665e351..0000000 --- a/test/core/model/ModelTestUtils.hpp +++ /dev/null @@ -1,90 +0,0 @@ -/* - 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_UTILS_HPP_ -#define _MODEL_TEST_UTILS_HPP_ - -#include -#include - -namespace ousia { -namespace model { - -/** - * This constructs a somewhat trivial system of standard types. - * - * Currently contained: string - */ -static Rooted constructTypeSystem(Manager &mgr) -{ - Rooted sys{new Typesystem(mgr, "std")}; - Rooted string{new StringType(mgr, sys)}; - sys->addType(string); - - return sys; -} - -/** - * This constructs the "book" domain for test purposes. The structure of the - * domain is fairly and can be seen from the construction itself. - */ -static Rooted constructBookDomain(Manager &mgr) -{ - // Start with the Domain itself. - Rooted domain{new Domain(mgr, "book")}; - // The standard type system. - domain->getTypesystems().push_back(constructTypeSystem(mgr)); - // Set up the cardinalities we'll need. - Cardinality single; - single.merge({1}); - Cardinality any; - any.merge(Range::typeRangeFrom(0)); - - // Set up the "book" node. - Rooted book{ - new StructuredClass(mgr, "book", domain, single)}; - domain->getRootStructures().push_back(book); - // The structure field of it. - Rooted book_field{new FieldDescriptor(mgr, book)}; - book->getFieldDescriptors().push_back(book_field); - - // From there on the "section". - Rooted section{ - new StructuredClass(mgr, "section", domain, any)}; - book_field->getChildren().push_back(section); - // And the field of it. - Rooted section_field{new FieldDescriptor(mgr, section)}; - section->getFieldDescriptors().push_back(section_field); - - // We also add the "paragraph", which is transparent. - Rooted paragraph{new StructuredClass( - mgr, "paragraph", domain, any, {nullptr}, {nullptr}, true)}; - section_field->getChildren().push_back(paragraph); - book_field->getChildren().push_back(paragraph); - // ... and has a primitive field. - Rooted text{new FieldDescriptor( - mgr, paragraph, domain->getTypesystems()[0]->getTypes()[0], "text", - false)}; - - return domain; -} -} -} - -#endif /* _TEST_MANAGED_H_ */ - diff --git a/test/core/model/TestDomain.hpp b/test/core/model/TestDomain.hpp new file mode 100644 index 0000000..41fcdef --- /dev/null +++ b/test/core/model/TestDomain.hpp @@ -0,0 +1,94 @@ +/* + 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_DOMAIN_HPP_ +#define _MODEL_TEST_DOMAIN_HPP_ + +#include +#include + +namespace ousia { +namespace model { + +/** + * This constructs a somewhat trivial system of standard types. + * + * Currently contained: string, text (struct wrapper for string) + */ +static Rooted constructTypeSystem(Manager &mgr) +{ + Rooted sys{new Typesystem(mgr, "std")}; + Rooted string{new StringType(mgr, sys)}; + sys->addType(string); + Rooted string_struct{new StructType( + mgr, "text", sys, {{"content", "", false, sys->acquire(string)}})}; + sys->addType(string_struct); + + return sys; +} + +/** + * This constructs the "book" domain for test purposes. The structure of the + * domain is fairly simple and can be seen from the construction itself. + */ +static Rooted constructBookDomain(Manager &mgr) +{ + // Start with the Domain itself. + Rooted domain{new Domain(mgr, "book")}; + // The standard type system. + domain->getTypesystems().push_back(constructTypeSystem(mgr)); + // Set up the cardinalities we'll need. + Cardinality single; + single.merge({1}); + Cardinality any; + any.merge(Range::typeRangeFrom(0)); + + // Set up the "book" node. + Rooted book{ + new StructuredClass(mgr, "book", domain, single)}; + domain->getRootStructures().push_back(book); + // The structure field of it. + Rooted book_field{new FieldDescriptor(mgr, book)}; + book->getFieldDescriptors().push_back(book_field); + + // From there on the "section". + Rooted section{ + new StructuredClass(mgr, "section", domain, any)}; + book_field->getChildren().push_back(section); + // And the field of it. + Rooted section_field{new FieldDescriptor(mgr, section)}; + section->getFieldDescriptors().push_back(section_field); + + // We also add the "paragraph", which is transparent. + Rooted paragraph{new StructuredClass( + mgr, "paragraph", domain, any, {nullptr}, {nullptr}, true)}; + section_field->getChildren().push_back(paragraph); + book_field->getChildren().push_back(paragraph); + // ... and has a primitive field. + Rooted paragraph_field{new FieldDescriptor( + mgr, paragraph, domain->getTypesystems()[0]->getTypes()[1], "text", + false)}; + paragraph->getFieldDescriptors().push_back(paragraph_field); + + return domain; +} +} +} + +#endif /* _TEST_DOMAIN_HPP_ */ + -- cgit v1.2.3 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 (limited to 'test') 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 From 3654efa59547d23b3c27715483999570daedc0b3 Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Fri, 19 Dec 2014 00:36:45 +0100 Subject: completed the work on a primitive Test Document. --- test/core/model/TestDocument.hpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/core/model/TestDocument.hpp b/test/core/model/TestDocument.hpp index c99bdd3..a1a3434 100644 --- a/test/core/model/TestDocument.hpp +++ b/test/core/model/TestDocument.hpp @@ -52,11 +52,28 @@ static Rooted constructBookDocument(Manager &mgr, // Add its text. Variant text{std::map{ {"content", Variant("Some introductory text")}}}; - Rooted text_primitive = + Rooted foreword_text = DocumentPrimitive::buildEntity(foreword, text, "text"); + if (foreword_text.isNull()) { + return {nullptr}; + } // Add a section. Rooted section = StructuredEntity::buildEntity(root, {bookDomain}, "section"); + // Add a paragraph for it. + Rooted main = + StructuredEntity::buildEntity(section, {bookDomain}, "paragraph"); + if (main.isNull()) { + return {nullptr}; + } + // Add its text. + text = Variant{std::map{ + {"content", Variant("Some introductory text")}}}; + Rooted main_text = + DocumentPrimitive::buildEntity(foreword, text, "text"); + if (main_text.isNull()) { + return {nullptr}; + } return doc; } -- cgit v1.2.3