From 02b85481d345fcb38337e59678b9c98e8c51456f Mon Sep 17 00:00:00 2001 From: Caglar Demir Date: Wed, 10 Apr 2024 12:26:40 +0200 Subject: [PATCH] Refactoring completed --- README.md | 6 +--- owlapy/class_expression/__init__.py | 35 +++++++++++++++++-- .../restriction.py} | 17 ++++----- owlapy/model/__init__.py | 2 +- owlapy/model/providers.py | 2 +- owlapy/owl2sparql/converter.py | 2 +- owlapy/parser.py | 2 +- owlapy/render.py | 2 +- owlapy/util.py | 2 +- tests/test_class_expression_semantics.py | 2 +- tests/test_examples.py | 30 ++++++++++++++++ tests/test_owlapy_cnf_dnf.py | 2 +- tests/test_owlapy_nnf.py | 2 +- tests/test_owlapy_parser.py | 2 +- tests/test_owlapy_render.py | 2 +- 15 files changed, 84 insertions(+), 26 deletions(-) rename owlapy/{owl_restriction.py => class_expression/restriction.py} (98%) create mode 100644 tests/test_examples.py diff --git a/README.md b/README.md index 2975c3a..d35e99b 100644 --- a/README.md +++ b/README.md @@ -22,14 +22,10 @@ ones and finally render and print the last of them in description logics syntax. ```python from owlapy.iri import IRI -from owlapy.owl_class_expression import OWLClass, OWLObjectIntersectionOf +from owlapy.class_expression import OWLClass, OWLObjectIntersectionOf, OWLObjectSomeValuesFrom from owlapy.owl_property import OWLObjectProperty -from owlapy.owl_restriction import OWLObjectSomeValuesFrom - from owlapy.owl2sparql.converter import owl_expression_to_sparql from owlapy.render import owl_expression_to_dl - - # Create the male class male = OWLClass("http://example.com/society#male") diff --git a/owlapy/class_expression/__init__.py b/owlapy/class_expression/__init__.py index 163c8cd..d679715 100644 --- a/owlapy/class_expression/__init__.py +++ b/owlapy/class_expression/__init__.py @@ -1,7 +1,38 @@ -from .class_expression import OWLClassExpression, OWLAnonymousClassExpression, OWLBooleanClassExpression, OWLObjectComplementOf +"""https://www.w3.org/TR/owl2-syntax/#Class_Expressions +ClassExpression := + owl_class.py: + Class + nary_boolean_expression.py: + ObjectIntersectionOf, ObjectUnionOf + class_expression.py: ObjectComplementOf + + restriction.py: + ObjectOneOf, ObjectSomeValuesFrom, ObjectAllValuesFrom, ObjectHasValue,ObjectHasSelf, + ObjectMinCardinality, ObjectMaxCardinality, ObjectExactCardinality, DataSomeValuesFrom, DataAllValuesFrom, + DataHasValue, DataMinCardinality, DataMaxCardinality, DataExactCardinality +""" +from .class_expression import OWLClassExpression, OWLAnonymousClassExpression, OWLBooleanClassExpression, \ + OWLObjectComplementOf from .owl_class import OWLClass from .nary_boolean_expression import OWLNaryBooleanClassExpression, OWLObjectUnionOf, OWLObjectIntersectionOf - +from .restriction import (OWLRestriction, OWLQuantifiedRestriction, OWLQuantifiedObjectRestriction, + OWLObjectRestriction, + OWLHasValueRestriction, OWLDataRestriction, OWLCardinalityRestriction, + OWLObjectCardinalityRestriction, OWLObjectHasSelf, + OWLDataOneOf, OWLQuantifiedDataRestriction, OWLDataCardinalityRestriction, + OWLObjectSomeValuesFrom, OWLObjectAllValuesFrom, OWLObjectHasValue, + OWLDatatypeRestriction, OWLFacet, OWLFacetRestriction, + OWLObjectMinCardinality, + OWLObjectMaxCardinality, + OWLObjectExactCardinality, + OWLDataSomeValuesFrom, + OWLDataAllValuesFrom, + OWLDataHasValue, + OWLDataMinCardinality, + OWLDataMaxCardinality, + OWLDataExactCardinality, + OWLObjectOneOf + ) from typing import Final from ..vocab import OWLRDFVocabulary diff --git a/owlapy/owl_restriction.py b/owlapy/class_expression/restriction.py similarity index 98% rename from owlapy/owl_restriction.py rename to owlapy/class_expression/restriction.py index dbb380a..24f2c8d 100644 --- a/owlapy/owl_restriction.py +++ b/owlapy/class_expression/restriction.py @@ -1,13 +1,14 @@ from abc import ABCMeta, abstractmethod -from .meta_classes import HasFiller, HasCardinality, HasOperands +from ..meta_classes import HasFiller, HasCardinality, HasOperands from typing import TypeVar, Generic, Final, Sequence, Union, Iterable -from .class_expression import OWLAnonymousClassExpression, OWLClassExpression, OWLObjectIntersectionOf -from .owl_property import OWLPropertyExpression, OWLObjectPropertyExpression, OWLDataPropertyExpression -from .data_ranges import OWLPropertyRange, OWLDataRange -from .owl_literal import OWLLiteral -from .owl_individual import OWLIndividual -from .types import OWLDatatype -from .owlobject import OWLObject +from .nary_boolean_expression import OWLObjectIntersectionOf +from .class_expression import OWLAnonymousClassExpression, OWLClassExpression +from ..owl_property import OWLPropertyExpression, OWLObjectPropertyExpression, OWLDataPropertyExpression +from ..data_ranges import OWLPropertyRange, OWLDataRange +from ..owl_literal import OWLLiteral +from ..owl_individual import OWLIndividual +from ..types import OWLDatatype +from ..owlobject import OWLObject from owlapy.vocab import OWLRDFVocabulary, XSDVocabulary, OWLFacet from datetime import datetime, date from pandas import Timedelta diff --git a/owlapy/model/__init__.py b/owlapy/model/__init__.py index 61b7619..ac96a88 100644 --- a/owlapy/model/__init__.py +++ b/owlapy/model/__init__.py @@ -17,7 +17,7 @@ from owlapy.owl_property import OWLObjectPropertyExpression, OWLProperty, OWLPropertyExpression, \ OWLDataPropertyExpression, OWLDataProperty, OWLObjectProperty -from owlapy.owl_restriction import (OWLRestriction, OWLObjectAllValuesFrom, OWLObjectSomeValuesFrom, +from owlapy.class_expression import (OWLRestriction, OWLObjectAllValuesFrom, OWLObjectSomeValuesFrom, OWLQuantifiedRestriction, OWLQuantifiedObjectRestriction, OWLObjectRestriction, OWLHasValueRestriction, OWLDataRestriction, OWLCardinalityRestriction, OWLObjectMinCardinality, OWLObjectCardinalityRestriction, diff --git a/owlapy/model/providers.py b/owlapy/model/providers.py index df457e5..3904c46 100644 --- a/owlapy/model/providers.py +++ b/owlapy/model/providers.py @@ -2,7 +2,7 @@ from typing import Union from datetime import datetime, date from owlapy.owl_literal import OWLLiteral -from owlapy.owl_restriction import OWLDatatypeRestriction, OWLFacet, OWLFacetRestriction +from owlapy.class_expression import OWLDatatypeRestriction, OWLFacet, OWLFacetRestriction from pandas import Timedelta Restriction_Literals = Union[OWLLiteral, int, float, Timedelta, datetime, date] diff --git a/owlapy/owl2sparql/converter.py b/owlapy/owl2sparql/converter.py index a82b596..0bb758d 100644 --- a/owlapy/owl2sparql/converter.py +++ b/owlapy/owl2sparql/converter.py @@ -12,7 +12,7 @@ OWLNamedIndividual, OWLObjectCardinalityRestriction, OWLObjectMinCardinality, OWLObjectExactCardinality, \ OWLObjectMaxCardinality, OWLDataCardinalityRestriction, OWLDataProperty, OWLObjectHasSelf, \ OWLDataSomeValuesFrom, OWLDataAllValuesFrom, OWLDataHasValue, OWLDatatype, TopOWLDatatype, OWLDataOneOf, OWLObjectIntersectionOf -from owlapy.owl_restriction import OWLObjectHasValue, OWLObjectOneOf, OWLDatatypeRestriction +from owlapy.class_expression import OWLObjectHasValue, OWLObjectOneOf, OWLDatatypeRestriction from owlapy.owl_literal import OWLLiteral from owlapy.vocab import OWLFacet, OWLRDFVocabulary diff --git a/owlapy/parser.py b/owlapy/parser.py index 8978fc5..d9d5c55 100644 --- a/owlapy/parser.py +++ b/owlapy/parser.py @@ -20,7 +20,7 @@ OWLDataCardinalityRestriction, OWLObjectAllValuesFrom, OWLDataAllValuesFrom, BooleanOWLDatatype from owlapy.data_ranges import OWLDataIntersectionOf, OWLDataUnionOf, OWLDataComplementOf -from owlapy.owl_restriction import OWLObjectHasValue, OWLDatatypeRestriction, OWLFacetRestriction, OWLObjectOneOf +from owlapy.class_expression import OWLObjectHasValue, OWLDatatypeRestriction, OWLFacetRestriction, OWLObjectOneOf MANCHESTER_GRAMMAR = Grammar(r""" diff --git a/owlapy/render.py b/owlapy/render.py index ea46322..6a754f0 100644 --- a/owlapy/render.py +++ b/owlapy/render.py @@ -21,7 +21,7 @@ from owlapy.vocab import OWLFacet from .data_ranges import OWLNaryDataRange, OWLDataComplementOf, OWLDataUnionOf, OWLDataIntersectionOf -from .owl_restriction import OWLObjectHasValue, OWLFacetRestriction, OWLDatatypeRestriction, OWLObjectOneOf +from .class_expression import OWLObjectHasValue, OWLFacetRestriction, OWLDatatypeRestriction, OWLObjectOneOf _DL_SYNTAX = types.SimpleNamespace( SUBCLASS="⊑", diff --git a/owlapy/util.py b/owlapy/util.py index 8b0b25e..7d1c77b 100644 --- a/owlapy/util.py +++ b/owlapy/util.py @@ -12,7 +12,7 @@ OWLDataCardinalityRestriction, OWLNaryBooleanClassExpression, OWLObjectUnionOf, \ OWLDataRange, OWLObject from .data_ranges import OWLDataComplementOf, OWLDataUnionOf, OWLDataIntersectionOf, OWLNaryDataRange -from .owl_restriction import OWLObjectHasValue, OWLDatatypeRestriction, OWLFacetRestriction, OWLObjectOneOf +from .class_expression import OWLObjectHasValue, OWLDatatypeRestriction, OWLFacetRestriction, OWLObjectOneOf _HasIRI = TypeVar('_HasIRI', bound=HasIRI) #: _HasIndex = TypeVar('_HasIndex', bound=HasIndex) #: diff --git a/tests/test_class_expression_semantics.py b/tests/test_class_expression_semantics.py index 270d236..ad2dbb4 100644 --- a/tests/test_class_expression_semantics.py +++ b/tests/test_class_expression_semantics.py @@ -3,7 +3,7 @@ from owlapy.class_expression import OWLClass, OWLObjectComplementOf, OWLObjectUnionOf from owlapy.class_expression import OWLBooleanClassExpression, OWLObjectIntersectionOf, OWLClassExpression from owlapy.owl_property import OWLObjectProperty -from owlapy.owl_restriction import OWLObjectSomeValuesFrom, OWLObjectAllValuesFrom +from owlapy.class_expression import OWLObjectSomeValuesFrom, OWLObjectAllValuesFrom from owlapy.owl2sparql.converter import owl_expression_to_sparql from owlapy.render import owl_expression_to_dl diff --git a/tests/test_examples.py b/tests/test_examples.py new file mode 100644 index 0000000..cb3d60d --- /dev/null +++ b/tests/test_examples.py @@ -0,0 +1,30 @@ + +from owlapy.iri import IRI +from owlapy.class_expression import OWLClass, OWLObjectIntersectionOf +from owlapy.owl_property import OWLObjectProperty +from owlapy.class_expression import OWLObjectSomeValuesFrom +from owlapy.owl2sparql.converter import owl_expression_to_sparql +from owlapy.render import owl_expression_to_dl + +class TestRunningExamples: + def test_readme(self): + # Create the male class + male = OWLClass("http://example.com/society#male") + + # Create an object property using the iri as a string for 'hasChild' property. + hasChild = OWLObjectProperty("http://example.com/society#hasChild") + + # Create an existential restrictions + males_with_children = OWLObjectSomeValuesFrom(hasChild, male) + + # Let's make it more complex by intersecting with another class + teacher = OWLClass("http://example.com/society#teacher") + male_teachers_with_children = OWLObjectIntersectionOf([males_with_children, teacher]) + + assert owl_expression_to_dl(male_teachers_with_children)=="(∃ hasChild.male) ⊓ teacher" + assert owl_expression_to_sparql("?x", male_teachers_with_children)=="""SELECT + DISTINCT ?x WHERE { +?x ?s_1 . +?s_1 a . +?x a . + }""" \ No newline at end of file diff --git a/tests/test_owlapy_cnf_dnf.py b/tests/test_owlapy_cnf_dnf.py index dadc339..f8432df 100644 --- a/tests/test_owlapy_cnf_dnf.py +++ b/tests/test_owlapy_cnf_dnf.py @@ -5,7 +5,7 @@ OWLObjectIntersectionOf, OWLObjectMinCardinality from owlapy.model.providers import OWLDatatypeMinExclusiveRestriction from owlapy.util import TopLevelCNF, TopLevelDNF -from owlapy.owl_restriction import OWLObjectOneOf +from owlapy.class_expression import OWLObjectOneOf class TopLevelNFTest(unittest.TestCase): diff --git a/tests/test_owlapy_nnf.py b/tests/test_owlapy_nnf.py index 3e3d0a6..60d34b0 100644 --- a/tests/test_owlapy_nnf.py +++ b/tests/test_owlapy_nnf.py @@ -33,7 +33,7 @@ from owlapy.util import NNF from owlapy.data_ranges import OWLDataComplementOf, OWLDataIntersectionOf, OWLDataUnionOf -from owlapy.owl_restriction import OWLObjectHasValue, OWLObjectOneOf +from owlapy.class_expression import OWLObjectHasValue, OWLObjectOneOf def iri(suffix): NS = "http://example.org/" diff --git a/tests/test_owlapy_parser.py b/tests/test_owlapy_parser.py index a16e0cc..370b32e 100644 --- a/tests/test_owlapy_parser.py +++ b/tests/test_owlapy_parser.py @@ -13,7 +13,7 @@ from owlapy.data_ranges import OWLDataIntersectionOf, OWLDataComplementOf, OWLDataUnionOf from owlapy.model.providers import OWLDatatypeMinExclusiveRestriction,\ OWLDatatypeMinMaxExclusiveRestriction, OWLDatatypeMaxExclusiveRestriction -from owlapy.owl_restriction import OWLDataSomeValuesFrom, OWLDatatypeRestriction, OWLFacetRestriction, OWLObjectSomeValuesFrom, OWLObjectMinCardinality, OWLObjectHasValue,OWLObjectOneOf +from owlapy.class_expression import OWLDataSomeValuesFrom, OWLDatatypeRestriction, OWLFacetRestriction, OWLObjectSomeValuesFrom, OWLObjectMinCardinality, OWLObjectHasValue,OWLObjectOneOf from owlapy.parser import DLSyntaxParser, ManchesterOWLSyntaxParser from owlapy.vocab import OWLFacet diff --git a/tests/test_owlapy_render.py b/tests/test_owlapy_render.py index 6dc5f0c..b0a67fd 100644 --- a/tests/test_owlapy_render.py +++ b/tests/test_owlapy_render.py @@ -9,7 +9,7 @@ from owlapy.data_ranges import OWLDataComplementOf, OWLDataIntersectionOf, OWLDataUnionOf from owlapy.model.providers import OWLDatatypeMinMaxInclusiveRestriction from owlapy.render import DLSyntaxObjectRenderer, ManchesterOWLSyntaxOWLObjectRenderer -from owlapy.owl_restriction import OWLObjectHasValue, OWLObjectOneOf +from owlapy.class_expression import OWLObjectHasValue, OWLObjectOneOf class Owlapy_DLRenderer_Test(unittest.TestCase): def test_ce_render(self):