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. --- CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) (limited to 'CMakeLists.txt') diff --git a/CMakeLists.txt b/CMakeLists.txt index 285abca..4469db2 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/Cardinality src/core/model/Document src/core/model/Domain src/core/model/Typesystem @@ -184,6 +185,8 @@ 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 # test/core/script/ObjectTest -- 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 'CMakeLists.txt') 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 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 'CMakeLists.txt') 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