From a7019614896fdd3e29b9a28f6a8cfd2c1b365983 Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Sun, 1 Mar 2015 20:47:25 +0100 Subject: Renamed domain to ontology. --- src/core/parser/ParserScope.cpp | 2 +- src/core/parser/stack/DocumentHandler.cpp | 2 +- src/core/parser/stack/DocumentHandler.hpp | 2 +- src/core/parser/stack/DomainHandler.cpp | 417 ------------------------- src/core/parser/stack/DomainHandler.hpp | 257 --------------- src/core/parser/stack/GenericParserStates.cpp | 26 +- src/core/parser/stack/ImportIncludeHandler.cpp | 6 +- src/core/parser/stack/OntologyHandler.cpp | 417 +++++++++++++++++++++++++ src/core/parser/stack/OntologyHandler.hpp | 257 +++++++++++++++ src/core/parser/stack/TypesystemHandler.cpp | 16 +- 10 files changed, 701 insertions(+), 701 deletions(-) delete mode 100644 src/core/parser/stack/DomainHandler.cpp delete mode 100644 src/core/parser/stack/DomainHandler.hpp create mode 100644 src/core/parser/stack/OntologyHandler.cpp create mode 100644 src/core/parser/stack/OntologyHandler.hpp (limited to 'src/core/parser') diff --git a/src/core/parser/ParserScope.cpp b/src/core/parser/ParserScope.cpp index dabb03c..c46dc51 100644 --- a/src/core/parser/ParserScope.cpp +++ b/src/core/parser/ParserScope.cpp @@ -19,7 +19,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/core/parser/stack/DocumentHandler.cpp b/src/core/parser/stack/DocumentHandler.cpp index bb04bd3..2144c34 100644 --- a/src/core/parser/stack/DocumentHandler.cpp +++ b/src/core/parser/stack/DocumentHandler.cpp @@ -22,7 +22,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/core/parser/stack/DocumentHandler.hpp b/src/core/parser/stack/DocumentHandler.hpp index 862081c..44feb2b 100644 --- a/src/core/parser/stack/DocumentHandler.hpp +++ b/src/core/parser/stack/DocumentHandler.hpp @@ -46,7 +46,7 @@ namespace parser_stack { /** * The DocumentHandler class parses the "document" tag that is used to introduce * a new document. Note that this tag is not mandatory in osml files -- if the - * first command is not a typesystem, domain or any other declarative command, + * first command is not a typesystem, ontology or any other declarative command, * the DocumentHandler will be implicitly called. */ class DocumentHandler : public StaticHandler { diff --git a/src/core/parser/stack/DomainHandler.cpp b/src/core/parser/stack/DomainHandler.cpp deleted file mode 100644 index e86f893..0000000 --- a/src/core/parser/stack/DomainHandler.cpp +++ /dev/null @@ -1,417 +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 -#include -#include -#include -#include - -#include "DocumentHandler.hpp" -#include "DomainHandler.hpp" -#include "State.hpp" -#include "TypesystemHandler.hpp" - -namespace ousia { -namespace parser_stack { - -/* DomainHandler */ - -bool DomainHandler::start(Variant::mapType &args) -{ - // Create the Domain node - Rooted domain = - context().getProject()->createDomain(args["name"].asString()); - domain->setLocation(location()); - - // If the domain is defined inside a document, add the reference to the - // document - Rooted document = scope().select(); - if (document != nullptr) { - document->reference(domain); - } - - // Push the typesystem onto the scope, set the POST_HEAD flag to true - scope().push(domain); - scope().setFlag(ParserFlag::POST_HEAD, false); - return true; -} - -void DomainHandler::end() { scope().pop(logger()); } - -/* DomainStructHandler */ - -bool DomainStructHandler::start(Variant::mapType &args) -{ - scope().setFlag(ParserFlag::POST_HEAD, true); - - Rooted domain = scope().selectOrThrow(); - - Rooted structuredClass = domain->createStructuredClass( - args["name"].asString(), args["cardinality"].asCardinality(), nullptr, - args["transparent"].asBool(), args["isRoot"].asBool()); - structuredClass->setLocation(location()); - - const std::string &isa = args["isa"].asString(); - if (!isa.empty()) { - scope().resolve( - isa, structuredClass, logger(), - [](Handle superclass, Handle structuredClass, - Logger &logger) { - if (superclass != nullptr) { - structuredClass.cast()->setSuperclass( - superclass.cast(), logger); - } - }); - } - - scope().push(structuredClass); - return true; -} - -void DomainStructHandler::end() { scope().pop(logger()); } - -/* DomainAnnotationHandler */ -bool DomainAnnotationHandler::start(Variant::mapType &args) -{ - scope().setFlag(ParserFlag::POST_HEAD, true); - - Rooted domain = scope().selectOrThrow(); - - Rooted annotationClass = - domain->createAnnotationClass(args["name"].asString()); - annotationClass->setLocation(location()); - - scope().push(annotationClass); - return true; -} - -void DomainAnnotationHandler::end() { scope().pop(logger()); } - -/* DomainAttributesHandler */ - -bool DomainAttributesHandler::start(Variant::mapType &args) -{ - // Fetch the current typesystem and create the struct node - Rooted parent = scope().selectOrThrow(); - - Rooted attrDesc = parent->getAttributesDescriptor(); - attrDesc->setLocation(location()); - - scope().push(attrDesc); - return true; -} - -void DomainAttributesHandler::end() { scope().pop(logger()); } - -/* DomainFieldHandler */ - -bool DomainFieldHandler::start(Variant::mapType &args) -{ - FieldDescriptor::FieldType type; - if (args["isSubtree"].asBool()) { - type = FieldDescriptor::FieldType::SUBTREE; - } else { - type = FieldDescriptor::FieldType::TREE; - } - - Rooted parent = scope().selectOrThrow(); - - auto res = parent->createFieldDescriptor( - logger(), type, args["name"].asString(), args["optional"].asBool()); - res.first->setLocation(location()); - if (res.second) { - logger().warning( - std::string("Field \"") + res.first->getName() + - "\" was declared after main field. The order of fields " - "was changed to make the main field the last field.", - *res.first); - } - - scope().push(res.first); - return true; -} - -void DomainFieldHandler::end() { scope().pop(logger()); } - -/* DomainFieldRefHandler */ - -bool DomainFieldRefHandler::start(Variant::mapType &args) -{ - Rooted parent = scope().selectOrThrow(); - - const std::string &name = args["ref"].asString(); - - auto loc = location(); - - scope().resolveFieldDescriptor(name, parent, logger(), - [loc](Handle field, - Handle parent, Logger &logger) { - if (field != nullptr) { - if (parent.cast()->addFieldDescriptor( - field.cast(), logger)) { - logger.warning( - std::string("Field \"") + field->getName() + - "\" was referenced after main field was declared. The " - "order of fields was changed to make the main field " - "the last field.", - loc); - } - } - }); - return true; -} - -void DomainFieldRefHandler::end() {} - -/* DomainPrimitiveHandler */ - -bool DomainPrimitiveHandler::start(Variant::mapType &args) -{ - Rooted parent = scope().selectOrThrow(); - - FieldDescriptor::FieldType fieldType; - if (args["isSubtree"].asBool()) { - fieldType = FieldDescriptor::FieldType::SUBTREE; - } else { - fieldType = FieldDescriptor::FieldType::TREE; - } - - auto res = parent->createPrimitiveFieldDescriptor( - new UnknownType(manager()), logger(), fieldType, - args["name"].asString(), args["optional"].asBool()); - res.first->setLocation(location()); - if (res.second) { - logger().warning( - std::string("Field \"") + res.first->getName() + - "\" was declared after main field. The order of fields " - "was changed to make the main field the last field.", - *res.first); - } - - const std::string &type = args["type"].asString(); - scope().resolveType(type, res.first, logger(), - [](Handle type, Handle field, - Logger &logger) { - if (type != nullptr) { - field.cast()->setPrimitiveType(type.cast()); - } - }); - - scope().push(res.first); - return true; -} - -void DomainPrimitiveHandler::end() { scope().pop(logger()); } - -/* DomainChildHandler */ - -bool DomainChildHandler::start(Variant::mapType &args) -{ - Rooted field = scope().selectOrThrow(); - - const std::string &ref = args["ref"].asString(); - scope().resolve( - ref, field, logger(), - [](Handle child, Handle field, Logger &logger) { - if (child != nullptr) { - field.cast()->addChild( - child.cast()); - } - }); - return true; -} - -/* DomainParentHandler */ - -bool DomainParentHandler::start(Variant::mapType &args) -{ - Rooted strct = scope().selectOrThrow(); - - Rooted parent{ - new DomainParent(strct->getManager(), args["ref"].asString(), strct)}; - parent->setLocation(location()); - scope().push(parent); - return true; -} - -void DomainParentHandler::end() { scope().pop(logger()); } - -/* DomainParentFieldHandler */ - -bool DomainParentFieldHandler::start(Variant::mapType &args) -{ - Rooted parentNameNode = scope().selectOrThrow(); - FieldDescriptor::FieldType type; - if (args["isSubtree"].asBool()) { - type = FieldDescriptor::FieldType::SUBTREE; - } else { - type = FieldDescriptor::FieldType::TREE; - } - - const std::string &name = args["name"].asString(); - const bool optional = args["optional"].asBool(); - Rooted strct = - parentNameNode->getParent().cast(); - - // resolve the parent, create the declared field and add the declared - // StructuredClass as child to it. - scope().resolve( - parentNameNode->getName(), strct, logger(), - [type, name, optional](Handle parent, Handle strct, - Logger &logger) { - if (parent != nullptr) { - Rooted field = - (parent.cast()->createFieldDescriptor( - logger, type, name, optional)).first; - field->addChild(strct.cast()); - } - }); - return true; -} - -/* DomainParentFieldRefHandler */ - -bool DomainParentFieldRefHandler::start(Variant::mapType &args) -{ - Rooted parentNameNode = scope().selectOrThrow(); - - const std::string &name = args["ref"].asString(); - Rooted strct = - parentNameNode->getParent().cast(); - auto loc = location(); - - // resolve the parent, get the referenced field and add the declared - // StructuredClass as child to it. - scope().resolve( - parentNameNode->getName(), strct, logger(), - [name, loc](Handle parent, Handle strct, Logger &logger) { - if (parent != nullptr) { - Rooted field = - parent.cast()->getFieldDescriptor(name); - if (field == nullptr) { - logger.error( - std::string("Could not find referenced field ") + name, - loc); - return; - } - field->addChild(strct.cast()); - } - }); - return true; -} - -namespace States { -const State Domain = StateBuilder() - .parents({&None, &Document}) - .createdNodeType(&RttiTypes::Domain) - .elementHandler(DomainHandler::create) - .arguments({Argument::String("name")}); - -const State DomainStruct = - StateBuilder() - .parent(&Domain) - .createdNodeType(&RttiTypes::StructuredClass) - .elementHandler(DomainStructHandler::create) - .arguments({Argument::String("name"), - Argument::Cardinality("cardinality", Cardinality::any()), - Argument::Bool("isRoot", false), - Argument::Bool("transparent", false), - Argument::String("isa", "")}); - -const State DomainAnnotation = - StateBuilder() - .parent(&Domain) - .createdNodeType(&RttiTypes::AnnotationClass) - .elementHandler(DomainAnnotationHandler::create) - .arguments({Argument::String("name")}); - -const State DomainAttributes = - StateBuilder() - .parents({&DomainStruct, &DomainAnnotation}) - .createdNodeType(&RttiTypes::StructType) - .elementHandler(DomainAttributesHandler::create) - .arguments({}); - -const State DomainAttribute = - StateBuilder() - .parent(&DomainAttributes) - .elementHandler(TypesystemStructFieldHandler::create) - .arguments({Argument::String("name"), Argument::String("type"), - Argument::Any("default", Variant::fromObject(nullptr))}); - -const State DomainField = StateBuilder() - .parents({&DomainStruct, &DomainAnnotation}) - .createdNodeType(&RttiTypes::FieldDescriptor) - .elementHandler(DomainFieldHandler::create) - .arguments({Argument::String("name", ""), - Argument::Bool("isSubtree", false), - Argument::Bool("optional", false)}); - -const State DomainFieldRef = - StateBuilder() - .parents({&DomainStruct, &DomainAnnotation}) - .createdNodeType(&RttiTypes::FieldDescriptor) - .elementHandler(DomainFieldRefHandler::create) - .arguments({Argument::String("ref", DEFAULT_FIELD_NAME)}); - -const State DomainStructPrimitive = - StateBuilder() - .parents({&DomainStruct, &DomainAnnotation}) - .createdNodeType(&RttiTypes::FieldDescriptor) - .elementHandler(DomainPrimitiveHandler::create) - .arguments( - {Argument::String("name", ""), Argument::Bool("isSubtree", false), - Argument::Bool("optional", false), Argument::String("type")}); - -const State DomainStructChild = StateBuilder() - .parent(&DomainField) - .elementHandler(DomainChildHandler::create) - .arguments({Argument::String("ref")}); - -const State DomainStructParent = - StateBuilder() - .parent(&DomainStruct) - .createdNodeType(&RttiTypes::DomainParent) - .elementHandler(DomainParentHandler::create) - .arguments({Argument::String("ref")}); - -const State DomainStructParentField = - StateBuilder() - .parent(&DomainStructParent) - .createdNodeType(&RttiTypes::FieldDescriptor) - .elementHandler(DomainParentFieldHandler::create) - .arguments({Argument::String("name", ""), - Argument::Bool("isSubtree", false), - Argument::Bool("optional", false)}); - -const State DomainStructParentFieldRef = - StateBuilder() - .parent(&DomainStructParent) - .createdNodeType(&RttiTypes::FieldDescriptor) - .elementHandler(DomainParentFieldRefHandler::create) - .arguments({Argument::String("ref", DEFAULT_FIELD_NAME)}); -} -} - -namespace RttiTypes { -const Rtti DomainParent = RttiBuilder( - "DomainParent").parent(&Node); -} -} diff --git a/src/core/parser/stack/DomainHandler.hpp b/src/core/parser/stack/DomainHandler.hpp deleted file mode 100644 index 76172d6..0000000 --- a/src/core/parser/stack/DomainHandler.hpp +++ /dev/null @@ -1,257 +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 DomainHandler.hpp - * - * Contains the Handler classes used for parsing Domain descriptors. This - * includes the "domain" tag and all describing tags below the "domain" tag. - * - * @author Benjamin Paaßen (bpaassen@techfak.uni-bielefeld.de) - */ - -#ifndef _OUSIA_DOMAIN_HANDLER_HPP_ -#define _OUSIA_DOMAIN_HANDLER_HPP_ - -#include -#include - -#include "Handler.hpp" - -namespace ousia { - -// Forward declarations -class Rtti; - -namespace parser_stack { - -// TODO: Documentation - -class DomainHandler : public StaticHandler { -public: - using StaticHandler::StaticHandler; - - bool start(Variant::mapType &args) override; - void end() override; - - static Handler *create(const HandlerData &handlerData) - { - return new DomainHandler{handlerData}; - } -}; - -class DomainStructHandler : public StaticHandler { -public: - using StaticHandler::StaticHandler; - - bool start(Variant::mapType &args) override; - void end() override; - - static Handler *create(const HandlerData &handlerData) - { - return new DomainStructHandler{handlerData}; - } -}; - -class DomainAnnotationHandler : public StaticHandler { -public: - using StaticHandler::StaticHandler; - - bool start(Variant::mapType &args) override; - void end() override; - - static Handler *create(const HandlerData &handlerData) - { - return new DomainAnnotationHandler{handlerData}; - } -}; - -class DomainAttributesHandler : public StaticHandler { -public: - using StaticHandler::StaticHandler; - - bool start(Variant::mapType &args) override; - void end() override; - - static Handler *create(const HandlerData &handlerData) - { - return new DomainAttributesHandler{handlerData}; - } -}; - -class DomainFieldHandler : public StaticHandler { -public: - using StaticHandler::StaticHandler; - - bool start(Variant::mapType &args) override; - void end() override; - - static Handler *create(const HandlerData &handlerData) - { - return new DomainFieldHandler{handlerData}; - } -}; - -class DomainFieldRefHandler : public StaticHandler { -public: - using StaticHandler::StaticHandler; - - bool start(Variant::mapType &args) override; - void end() override; - - static Handler *create(const HandlerData &handlerData) - { - return new DomainFieldRefHandler{handlerData}; - } -}; - -class DomainPrimitiveHandler : public StaticHandler { -public: - using StaticHandler::StaticHandler; - - bool start(Variant::mapType &args) override; - void end() override; - - static Handler *create(const HandlerData &handlerData) - { - return new DomainPrimitiveHandler{handlerData}; - } -}; - -class DomainChildHandler : public StaticHandler { -public: - using StaticHandler::StaticHandler; - - bool start(Variant::mapType &args) override; - - static Handler *create(const HandlerData &handlerData) - { - return new DomainChildHandler{handlerData}; - } -}; - -class DomainParent : public Node { -public: - using Node::Node; -}; - -class DomainParentHandler : public StaticHandler { -public: - using StaticHandler::StaticHandler; - - bool start(Variant::mapType &args) override; - void end() override; - - static Handler *create(const HandlerData &handlerData) - { - return new DomainParentHandler{handlerData}; - } -}; - -class DomainParentFieldHandler : public StaticHandler { -public: - using StaticHandler::StaticHandler; - - bool start(Variant::mapType &args) override; - - static Handler *create(const HandlerData &handlerData) - { - return new DomainParentFieldHandler{handlerData}; - } -}; - -class DomainParentFieldRefHandler : public StaticHandler { -public: - using StaticHandler::StaticHandler; - - bool start(Variant::mapType &args) override; - - static Handler *create(const HandlerData &handlerData) - { - return new DomainParentFieldRefHandler{handlerData}; - } -}; - -namespace States { -/** - * State representing a "domain" struct. - */ -extern const State Domain; - -/** - * State representing a "struct" tag within a domain description. - */ -extern const State DomainStruct; - -/** - * State representing an "annotation" tag within a domain description. - */ -extern const State DomainAnnotation; - -/** - * State representing an "attributes" tag within a structure or annotation. - */ -extern const State DomainAttributes; - -/** - * State representing an "attribute" tag within the "attributes". - */ -extern const State DomainAttribute; - -/** - * State representing a "field" tag within a structure or annotation. - */ -extern const State DomainField; - -/** - * State representing a "fieldref" tag within a structure or annotation. - */ -extern const State DomainFieldRef; - -/** - * State representing a "primitive" tag within a structure or annotation. - */ -extern const State DomainStructPrimitive; - -/** - * State representing a "child" tag within a structure or annotation. - */ -extern const State DomainStructChild; - -/** - * State representing a "parent" tag within a structure or annotation. - */ -extern const State DomainStructParent; - -/** - * State representing a "field" tag within a "parent" tag. - */ -extern const State DomainStructParentField; - -/** - * State representing a "fieldRef" tag within a "parent" tag. - */ -extern const State DomainStructParentFieldRef; -} -} - -namespace RttiTypes { -extern const Rtti DomainParent; -} -} -#endif diff --git a/src/core/parser/stack/GenericParserStates.cpp b/src/core/parser/stack/GenericParserStates.cpp index 69a6e0e..7287524 100644 --- a/src/core/parser/stack/GenericParserStates.cpp +++ b/src/core/parser/stack/GenericParserStates.cpp @@ -17,7 +17,7 @@ */ #include "DocumentHandler.hpp" -#include "DomainHandler.hpp" +#include "OntologyHandler.hpp" #include "GenericParserStates.hpp" #include "ImportIncludeHandler.hpp" #include "TypesystemHandler.hpp" @@ -28,18 +28,18 @@ namespace parser_stack { const std::multimap GenericParserStates{ {"document", &States::Document}, {"*", &States::DocumentChild}, - {"domain", &States::Domain}, - {"struct", &States::DomainStruct}, - {"annotation", &States::DomainAnnotation}, - {"attributes", &States::DomainAttributes}, - {"attribute", &States::DomainAttribute}, - {"field", &States::DomainField}, - {"fieldRef", &States::DomainFieldRef}, - {"primitive", &States::DomainStructPrimitive}, - {"childRef", &States::DomainStructChild}, - {"parentRef", &States::DomainStructParent}, - {"field", &States::DomainStructParentField}, - {"fieldRef", &States::DomainStructParentFieldRef}, + {"ontology", &States::Ontology}, + {"struct", &States::OntologyStruct}, + {"annotation", &States::OntologyAnnotation}, + {"attributes", &States::OntologyAttributes}, + {"attribute", &States::OntologyAttribute}, + {"field", &States::OntologyField}, + {"fieldRef", &States::OntologyFieldRef}, + {"primitive", &States::OntologyStructPrimitive}, + {"childRef", &States::OntologyStructChild}, + {"parentRef", &States::OntologyStructParent}, + {"field", &States::OntologyStructParentField}, + {"fieldRef", &States::OntologyStructParentFieldRef}, {"typesystem", &States::Typesystem}, {"enum", &States::TypesystemEnum}, {"entry", &States::TypesystemEnumEntry}, diff --git a/src/core/parser/stack/ImportIncludeHandler.cpp b/src/core/parser/stack/ImportIncludeHandler.cpp index d1ea97d..a6cbaea 100644 --- a/src/core/parser/stack/ImportIncludeHandler.cpp +++ b/src/core/parser/stack/ImportIncludeHandler.cpp @@ -20,7 +20,7 @@ #include #include -#include "DomainHandler.hpp" +#include "OntologyHandler.hpp" #include "DocumentHandler.hpp" #include "ImportIncludeHandler.hpp" #include "State.hpp" @@ -38,7 +38,7 @@ void ImportHandler::doHandle(const Variant &fieldData, Variant::mapType &args) Rooted leaf = scope().getLeaf(); if (leaf == nullptr || !leaf->isa(&RttiTypes::RootNode)) { logger().error( - "Import not supported here, must be inside a document, domain " + "Import not supported here, must be inside a document, ontology " "or typesystem command.", location()); return; @@ -66,7 +66,7 @@ void IncludeHandler::doHandle(const Variant &fieldData, Variant::mapType &args) namespace States { const State Import = StateBuilder() - .parents({&Document, &Typesystem, &Domain}) + .parents({&Document, &Typesystem, &Ontology}) .elementHandler(ImportHandler::create) .arguments({Argument::String("rel", ""), Argument::String("type", ""), Argument::String("src", "")}); diff --git a/src/core/parser/stack/OntologyHandler.cpp b/src/core/parser/stack/OntologyHandler.cpp new file mode 100644 index 0000000..8c0e4d9 --- /dev/null +++ b/src/core/parser/stack/OntologyHandler.cpp @@ -0,0 +1,417 @@ +/* + 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 +#include +#include + +#include "DocumentHandler.hpp" +#include "OntologyHandler.hpp" +#include "State.hpp" +#include "TypesystemHandler.hpp" + +namespace ousia { +namespace parser_stack { + +/* OntologyHandler */ + +bool OntologyHandler::start(Variant::mapType &args) +{ + // Create the Ontology node + Rooted ontology = + context().getProject()->createOntology(args["name"].asString()); + ontology->setLocation(location()); + + // If the ontology is defined inside a document, add the reference to the + // document + Rooted document = scope().select(); + if (document != nullptr) { + document->reference(ontology); + } + + // Push the typesystem onto the scope, set the POST_HEAD flag to true + scope().push(ontology); + scope().setFlag(ParserFlag::POST_HEAD, false); + return true; +} + +void OntologyHandler::end() { scope().pop(logger()); } + +/* OntologyStructHandler */ + +bool OntologyStructHandler::start(Variant::mapType &args) +{ + scope().setFlag(ParserFlag::POST_HEAD, true); + + Rooted ontology = scope().selectOrThrow(); + + Rooted structuredClass = ontology->createStructuredClass( + args["name"].asString(), args["cardinality"].asCardinality(), nullptr, + args["transparent"].asBool(), args["isRoot"].asBool()); + structuredClass->setLocation(location()); + + const std::string &isa = args["isa"].asString(); + if (!isa.empty()) { + scope().resolve( + isa, structuredClass, logger(), + [](Handle superclass, Handle structuredClass, + Logger &logger) { + if (superclass != nullptr) { + structuredClass.cast()->setSuperclass( + superclass.cast(), logger); + } + }); + } + + scope().push(structuredClass); + return true; +} + +void OntologyStructHandler::end() { scope().pop(logger()); } + +/* OntologyAnnotationHandler */ +bool OntologyAnnotationHandler::start(Variant::mapType &args) +{ + scope().setFlag(ParserFlag::POST_HEAD, true); + + Rooted ontology = scope().selectOrThrow(); + + Rooted annotationClass = + ontology->createAnnotationClass(args["name"].asString()); + annotationClass->setLocation(location()); + + scope().push(annotationClass); + return true; +} + +void OntologyAnnotationHandler::end() { scope().pop(logger()); } + +/* OntologyAttributesHandler */ + +bool OntologyAttributesHandler::start(Variant::mapType &args) +{ + // Fetch the current typesystem and create the struct node + Rooted parent = scope().selectOrThrow(); + + Rooted attrDesc = parent->getAttributesDescriptor(); + attrDesc->setLocation(location()); + + scope().push(attrDesc); + return true; +} + +void OntologyAttributesHandler::end() { scope().pop(logger()); } + +/* OntologyFieldHandler */ + +bool OntologyFieldHandler::start(Variant::mapType &args) +{ + FieldDescriptor::FieldType type; + if (args["isSubtree"].asBool()) { + type = FieldDescriptor::FieldType::SUBTREE; + } else { + type = FieldDescriptor::FieldType::TREE; + } + + Rooted parent = scope().selectOrThrow(); + + auto res = parent->createFieldDescriptor( + logger(), type, args["name"].asString(), args["optional"].asBool()); + res.first->setLocation(location()); + if (res.second) { + logger().warning( + std::string("Field \"") + res.first->getName() + + "\" was declared after main field. The order of fields " + "was changed to make the main field the last field.", + *res.first); + } + + scope().push(res.first); + return true; +} + +void OntologyFieldHandler::end() { scope().pop(logger()); } + +/* OntologyFieldRefHandler */ + +bool OntologyFieldRefHandler::start(Variant::mapType &args) +{ + Rooted parent = scope().selectOrThrow(); + + const std::string &name = args["ref"].asString(); + + auto loc = location(); + + scope().resolveFieldDescriptor(name, parent, logger(), + [loc](Handle field, + Handle parent, Logger &logger) { + if (field != nullptr) { + if (parent.cast()->addFieldDescriptor( + field.cast(), logger)) { + logger.warning( + std::string("Field \"") + field->getName() + + "\" was referenced after main field was declared. The " + "order of fields was changed to make the main field " + "the last field.", + loc); + } + } + }); + return true; +} + +void OntologyFieldRefHandler::end() {} + +/* OntologyPrimitiveHandler */ + +bool OntologyPrimitiveHandler::start(Variant::mapType &args) +{ + Rooted parent = scope().selectOrThrow(); + + FieldDescriptor::FieldType fieldType; + if (args["isSubtree"].asBool()) { + fieldType = FieldDescriptor::FieldType::SUBTREE; + } else { + fieldType = FieldDescriptor::FieldType::TREE; + } + + auto res = parent->createPrimitiveFieldDescriptor( + new UnknownType(manager()), logger(), fieldType, + args["name"].asString(), args["optional"].asBool()); + res.first->setLocation(location()); + if (res.second) { + logger().warning( + std::string("Field \"") + res.first->getName() + + "\" was declared after main field. The order of fields " + "was changed to make the main field the last field.", + *res.first); + } + + const std::string &type = args["type"].asString(); + scope().resolveType(type, res.first, logger(), + [](Handle type, Handle field, + Logger &logger) { + if (type != nullptr) { + field.cast()->setPrimitiveType(type.cast()); + } + }); + + scope().push(res.first); + return true; +} + +void OntologyPrimitiveHandler::end() { scope().pop(logger()); } + +/* OntologyChildHandler */ + +bool OntologyChildHandler::start(Variant::mapType &args) +{ + Rooted field = scope().selectOrThrow(); + + const std::string &ref = args["ref"].asString(); + scope().resolve( + ref, field, logger(), + [](Handle child, Handle field, Logger &logger) { + if (child != nullptr) { + field.cast()->addChild( + child.cast()); + } + }); + return true; +} + +/* OntologyParentHandler */ + +bool OntologyParentHandler::start(Variant::mapType &args) +{ + Rooted strct = scope().selectOrThrow(); + + Rooted parent{ + new OntologyParent(strct->getManager(), args["ref"].asString(), strct)}; + parent->setLocation(location()); + scope().push(parent); + return true; +} + +void OntologyParentHandler::end() { scope().pop(logger()); } + +/* OntologyParentFieldHandler */ + +bool OntologyParentFieldHandler::start(Variant::mapType &args) +{ + Rooted parentNameNode = scope().selectOrThrow(); + FieldDescriptor::FieldType type; + if (args["isSubtree"].asBool()) { + type = FieldDescriptor::FieldType::SUBTREE; + } else { + type = FieldDescriptor::FieldType::TREE; + } + + const std::string &name = args["name"].asString(); + const bool optional = args["optional"].asBool(); + Rooted strct = + parentNameNode->getParent().cast(); + + // resolve the parent, create the declared field and add the declared + // StructuredClass as child to it. + scope().resolve( + parentNameNode->getName(), strct, logger(), + [type, name, optional](Handle parent, Handle strct, + Logger &logger) { + if (parent != nullptr) { + Rooted field = + (parent.cast()->createFieldDescriptor( + logger, type, name, optional)).first; + field->addChild(strct.cast()); + } + }); + return true; +} + +/* OntologyParentFieldRefHandler */ + +bool OntologyParentFieldRefHandler::start(Variant::mapType &args) +{ + Rooted parentNameNode = scope().selectOrThrow(); + + const std::string &name = args["ref"].asString(); + Rooted strct = + parentNameNode->getParent().cast(); + auto loc = location(); + + // resolve the parent, get the referenced field and add the declared + // StructuredClass as child to it. + scope().resolve( + parentNameNode->getName(), strct, logger(), + [name, loc](Handle parent, Handle strct, Logger &logger) { + if (parent != nullptr) { + Rooted field = + parent.cast()->getFieldDescriptor(name); + if (field == nullptr) { + logger.error( + std::string("Could not find referenced field ") + name, + loc); + return; + } + field->addChild(strct.cast()); + } + }); + return true; +} + +namespace States { +const State Ontology = StateBuilder() + .parents({&None, &Document}) + .createdNodeType(&RttiTypes::Ontology) + .elementHandler(OntologyHandler::create) + .arguments({Argument::String("name")}); + +const State OntologyStruct = + StateBuilder() + .parent(&Ontology) + .createdNodeType(&RttiTypes::StructuredClass) + .elementHandler(OntologyStructHandler::create) + .arguments({Argument::String("name"), + Argument::Cardinality("cardinality", Cardinality::any()), + Argument::Bool("isRoot", false), + Argument::Bool("transparent", false), + Argument::String("isa", "")}); + +const State OntologyAnnotation = + StateBuilder() + .parent(&Ontology) + .createdNodeType(&RttiTypes::AnnotationClass) + .elementHandler(OntologyAnnotationHandler::create) + .arguments({Argument::String("name")}); + +const State OntologyAttributes = + StateBuilder() + .parents({&OntologyStruct, &OntologyAnnotation}) + .createdNodeType(&RttiTypes::StructType) + .elementHandler(OntologyAttributesHandler::create) + .arguments({}); + +const State OntologyAttribute = + StateBuilder() + .parent(&OntologyAttributes) + .elementHandler(TypesystemStructFieldHandler::create) + .arguments({Argument::String("name"), Argument::String("type"), + Argument::Any("default", Variant::fromObject(nullptr))}); + +const State OntologyField = StateBuilder() + .parents({&OntologyStruct, &OntologyAnnotation}) + .createdNodeType(&RttiTypes::FieldDescriptor) + .elementHandler(OntologyFieldHandler::create) + .arguments({Argument::String("name", ""), + Argument::Bool("isSubtree", false), + Argument::Bool("optional", false)}); + +const State OntologyFieldRef = + StateBuilder() + .parents({&OntologyStruct, &OntologyAnnotation}) + .createdNodeType(&RttiTypes::FieldDescriptor) + .elementHandler(OntologyFieldRefHandler::create) + .arguments({Argument::String("ref", DEFAULT_FIELD_NAME)}); + +const State OntologyStructPrimitive = + StateBuilder() + .parents({&OntologyStruct, &OntologyAnnotation}) + .createdNodeType(&RttiTypes::FieldDescriptor) + .elementHandler(OntologyPrimitiveHandler::create) + .arguments( + {Argument::String("name", ""), Argument::Bool("isSubtree", false), + Argument::Bool("optional", false), Argument::String("type")}); + +const State OntologyStructChild = StateBuilder() + .parent(&OntologyField) + .elementHandler(OntologyChildHandler::create) + .arguments({Argument::String("ref")}); + +const State OntologyStructParent = + StateBuilder() + .parent(&OntologyStruct) + .createdNodeType(&RttiTypes::OntologyParent) + .elementHandler(OntologyParentHandler::create) + .arguments({Argument::String("ref")}); + +const State OntologyStructParentField = + StateBuilder() + .parent(&OntologyStructParent) + .createdNodeType(&RttiTypes::FieldDescriptor) + .elementHandler(OntologyParentFieldHandler::create) + .arguments({Argument::String("name", ""), + Argument::Bool("isSubtree", false), + Argument::Bool("optional", false)}); + +const State OntologyStructParentFieldRef = + StateBuilder() + .parent(&OntologyStructParent) + .createdNodeType(&RttiTypes::FieldDescriptor) + .elementHandler(OntologyParentFieldRefHandler::create) + .arguments({Argument::String("ref", DEFAULT_FIELD_NAME)}); +} +} + +namespace RttiTypes { +const Rtti OntologyParent = RttiBuilder( + "OntologyParent").parent(&Node); +} +} diff --git a/src/core/parser/stack/OntologyHandler.hpp b/src/core/parser/stack/OntologyHandler.hpp new file mode 100644 index 0000000..caeacc7 --- /dev/null +++ b/src/core/parser/stack/OntologyHandler.hpp @@ -0,0 +1,257 @@ +/* + 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 OntologyHandler.hpp + * + * Contains the Handler classes used for parsing Ontology descriptors. This + * includes the "ontology" tag and all describing tags below the "ontology" tag. + * + * @author Benjamin Paaßen (bpaassen@techfak.uni-bielefeld.de) + */ + +#ifndef _OUSIA_DOMAIN_HANDLER_HPP_ +#define _OUSIA_DOMAIN_HANDLER_HPP_ + +#include +#include + +#include "Handler.hpp" + +namespace ousia { + +// Forward declarations +class Rtti; + +namespace parser_stack { + +// TODO: Documentation + +class OntologyHandler : public StaticHandler { +public: + using StaticHandler::StaticHandler; + + bool start(Variant::mapType &args) override; + void end() override; + + static Handler *create(const HandlerData &handlerData) + { + return new OntologyHandler{handlerData}; + } +}; + +class OntologyStructHandler : public StaticHandler { +public: + using StaticHandler::StaticHandler; + + bool start(Variant::mapType &args) override; + void end() override; + + static Handler *create(const HandlerData &handlerData) + { + return new OntologyStructHandler{handlerData}; + } +}; + +class OntologyAnnotationHandler : public StaticHandler { +public: + using StaticHandler::StaticHandler; + + bool start(Variant::mapType &args) override; + void end() override; + + static Handler *create(const HandlerData &handlerData) + { + return new OntologyAnnotationHandler{handlerData}; + } +}; + +class OntologyAttributesHandler : public StaticHandler { +public: + using StaticHandler::StaticHandler; + + bool start(Variant::mapType &args) override; + void end() override; + + static Handler *create(const HandlerData &handlerData) + { + return new OntologyAttributesHandler{handlerData}; + } +}; + +class OntologyFieldHandler : public StaticHandler { +public: + using StaticHandler::StaticHandler; + + bool start(Variant::mapType &args) override; + void end() override; + + static Handler *create(const HandlerData &handlerData) + { + return new OntologyFieldHandler{handlerData}; + } +}; + +class OntologyFieldRefHandler : public StaticHandler { +public: + using StaticHandler::StaticHandler; + + bool start(Variant::mapType &args) override; + void end() override; + + static Handler *create(const HandlerData &handlerData) + { + return new OntologyFieldRefHandler{handlerData}; + } +}; + +class OntologyPrimitiveHandler : public StaticHandler { +public: + using StaticHandler::StaticHandler; + + bool start(Variant::mapType &args) override; + void end() override; + + static Handler *create(const HandlerData &handlerData) + { + return new OntologyPrimitiveHandler{handlerData}; + } +}; + +class OntologyChildHandler : public StaticHandler { +public: + using StaticHandler::StaticHandler; + + bool start(Variant::mapType &args) override; + + static Handler *create(const HandlerData &handlerData) + { + return new OntologyChildHandler{handlerData}; + } +}; + +class OntologyParent : public Node { +public: + using Node::Node; +}; + +class OntologyParentHandler : public StaticHandler { +public: + using StaticHandler::StaticHandler; + + bool start(Variant::mapType &args) override; + void end() override; + + static Handler *create(const HandlerData &handlerData) + { + return new OntologyParentHandler{handlerData}; + } +}; + +class OntologyParentFieldHandler : public StaticHandler { +public: + using StaticHandler::StaticHandler; + + bool start(Variant::mapType &args) override; + + static Handler *create(const HandlerData &handlerData) + { + return new OntologyParentFieldHandler{handlerData}; + } +}; + +class OntologyParentFieldRefHandler : public StaticHandler { +public: + using StaticHandler::StaticHandler; + + bool start(Variant::mapType &args) override; + + static Handler *create(const HandlerData &handlerData) + { + return new OntologyParentFieldRefHandler{handlerData}; + } +}; + +namespace States { +/** + * State representing a "ontology" struct. + */ +extern const State Ontology; + +/** + * State representing a "struct" tag within a ontology description. + */ +extern const State OntologyStruct; + +/** + * State representing an "annotation" tag within a ontology description. + */ +extern const State OntologyAnnotation; + +/** + * State representing an "attributes" tag within a structure or annotation. + */ +extern const State OntologyAttributes; + +/** + * State representing an "attribute" tag within the "attributes". + */ +extern const State OntologyAttribute; + +/** + * State representing a "field" tag within a structure or annotation. + */ +extern const State OntologyField; + +/** + * State representing a "fieldref" tag within a structure or annotation. + */ +extern const State OntologyFieldRef; + +/** + * State representing a "primitive" tag within a structure or annotation. + */ +extern const State OntologyStructPrimitive; + +/** + * State representing a "child" tag within a structure or annotation. + */ +extern const State OntologyStructChild; + +/** + * State representing a "parent" tag within a structure or annotation. + */ +extern const State OntologyStructParent; + +/** + * State representing a "field" tag within a "parent" tag. + */ +extern const State OntologyStructParentField; + +/** + * State representing a "fieldRef" tag within a "parent" tag. + */ +extern const State OntologyStructParentFieldRef; +} +} + +namespace RttiTypes { +extern const Rtti OntologyParent; +} +} +#endif diff --git a/src/core/parser/stack/TypesystemHandler.cpp b/src/core/parser/stack/TypesystemHandler.cpp index de8ee49..b62f684 100644 --- a/src/core/parser/stack/TypesystemHandler.cpp +++ b/src/core/parser/stack/TypesystemHandler.cpp @@ -17,13 +17,13 @@ */ #include -#include +#include #include #include #include #include "DocumentHandler.hpp" -#include "DomainHandler.hpp" +#include "OntologyHandler.hpp" #include "State.hpp" #include "TypesystemHandler.hpp" @@ -39,12 +39,12 @@ bool TypesystemHandler::start(Variant::mapType &args) context().getProject()->createTypesystem(args["name"].asString()); typesystem->setLocation(location()); - // If the typesystem is defined inside a domain, add a reference to the - // typesystem to the domain -- do the same with a document, if no domain + // If the typesystem is defined inside a ontology, add a reference to the + // typesystem to the ontology -- do the same with a document, if no ontology // is found - Rooted domain = scope().select(); - if (domain != nullptr) { - domain->reference(typesystem); + Rooted ontology = scope().select(); + if (ontology != nullptr) { + ontology->reference(typesystem); } else { Rooted document = scope().select(); if (document != nullptr) { @@ -190,7 +190,7 @@ bool TypesystemConstantHandler::start(Variant::mapType &args) namespace States { const State Typesystem = StateBuilder() - .parents({&None, &Domain, &Document}) + .parents({&None, &Ontology, &Document}) .createdNodeType(&RttiTypes::Typesystem) .elementHandler(TypesystemHandler::create) .arguments({Argument::String("name", "")}); -- cgit v1.2.3