Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class expression script will become a python module #23

Merged
merged 5 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions owlapy/class_expression/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from .class_expression import OWLClassExpression, OWLAnonymousClassExpression, OWLBooleanClassExpression, OWLObjectComplementOf
from .owl_class import OWLClass
from .nary_boolean_expression import OWLNaryBooleanClassExpression, OWLObjectUnionOf, OWLObjectIntersectionOf

from typing import Final
from ..vocab import OWLRDFVocabulary

OWLThing: Final = OWLClass(OWLRDFVocabulary.OWL_THING.get_iri()) #: : :The OWL Class corresponding to owl:Thing
OWLNothing: Final = OWLClass(OWLRDFVocabulary.OWL_NOTHING.get_iri()) #: : :The OWL Class corresponding to owl:Nothing
107 changes: 107 additions & 0 deletions owlapy/class_expression/class_expression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
from abc import abstractmethod, ABCMeta
from ..data_ranges import OWLPropertyRange, OWLDataRange
from ..meta_classes import HasOperands

from typing import Final, Iterable
class OWLClassExpression(OWLPropertyRange):
"""An OWL 2 Class Expression (https://www.w3.org/TR/owl2-syntax/#Class_Expressions) """
__slots__ = ()

@abstractmethod
def is_owl_thing(self) -> bool:
"""Determines if this expression is the built in class owl:Thing. This method does not determine if the class
is equivalent to owl:Thing.

Returns:
True if this expression is owl:Thing.
"""
pass

@abstractmethod
def is_owl_nothing(self) -> bool:
"""Determines if this expression is the built in class owl:Nothing. This method does not determine if the class
is equivalent to owl:Nothing.
"""
pass

@abstractmethod
def get_object_complement_of(self) -> 'OWLObjectComplementOf':
"""Gets the object complement of this class expression.

Returns:
A class expression that is the complement of this class expression.
"""
pass

@abstractmethod
def get_nnf(self) -> 'OWLClassExpression':
"""Gets the negation normal form of the complement of this expression.

Returns:
A expression that represents the NNF of the complement of this expression.
"""
pass


class OWLAnonymousClassExpression(OWLClassExpression, metaclass=ABCMeta):
"""A Class Expression which is not a named Class."""

def is_owl_nothing(self) -> bool:
# documented in parent
return False

def is_owl_thing(self) -> bool:
# documented in parent
return False

def get_object_complement_of(self) -> 'OWLObjectComplementOf':
# documented in parent
return OWLObjectComplementOf(self)

def get_nnf(self) -> 'OWLClassExpression':
# documented in parent
from owlapy.util import NNF
return NNF().get_class_nnf(self)


class OWLBooleanClassExpression(OWLAnonymousClassExpression, metaclass=ABCMeta):
"""Represent an anonymous boolean class expression."""
__slots__ = ()
pass


class OWLObjectComplementOf(OWLBooleanClassExpression, HasOperands[OWLClassExpression]):
"""Represents an ObjectComplementOf class expression in the OWL 2 Specification."""
__slots__ = '_operand'
type_index: Final = 3003

_operand: OWLClassExpression

def __init__(self, op: OWLClassExpression):
"""
Args:
op: Class expression to complement.
"""
self._operand = op

def get_operand(self) -> OWLClassExpression:
"""
Returns:
The wrapped expression.
"""
return self._operand

def operands(self) -> Iterable[OWLClassExpression]:
# documented in parent
yield self._operand

def __repr__(self):
return f"OWLObjectComplementOf({repr(self._operand)})"

def __eq__(self, other):
if type(other) is type(self):
return self._operand == other._operand
return NotImplemented

def __hash__(self):
return hash(self._operand)
48 changes: 48 additions & 0 deletions owlapy/class_expression/nary_boolean_expression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from .class_expression import OWLClassExpression, OWLBooleanClassExpression
from ..meta_classes import HasOperands
from typing import Final, Sequence, Iterable
class OWLNaryBooleanClassExpression(OWLBooleanClassExpression, HasOperands[OWLClassExpression]):
"""OWLNaryBooleanClassExpression."""
__slots__ = ()

_operands: Sequence[OWLClassExpression]

def __init__(self, operands: Iterable[OWLClassExpression]):
"""
Args:
operands: Class expressions.
"""
self._operands = tuple(operands)

def operands(self) -> Iterable[OWLClassExpression]:
# documented in parent
yield from self._operands

def __repr__(self):
return f'{type(self).__name__}({repr(self._operands)})'

def __eq__(self, other):
if type(other) == type(self):
return self._operands == other._operands
return NotImplemented

def __hash__(self):
return hash(self._operands)




class OWLObjectUnionOf(OWLNaryBooleanClassExpression):
"""Represents an ObjectUnionOf class expression in the OWL 2 Specification."""
__slots__ = '_operands'
type_index: Final = 3002

_operands: Sequence[OWLClassExpression]


class OWLObjectIntersectionOf(OWLNaryBooleanClassExpression):
"""Represents an OWLObjectIntersectionOf class expression in the OWL 2 Specification."""
__slots__ = '_operands'
type_index: Final = 3001

_operands: Sequence[OWLClassExpression]
57 changes: 57 additions & 0 deletions owlapy/class_expression/owl_class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from .class_expression import OWLClassExpression, OWLObjectComplementOf
from ..owlobject import OWLObject, OWLEntity
from typing import Final, Union
from ..iri import IRI


class OWLClass(OWLClassExpression, OWLEntity):
"""An OWL 2 named Class"""
__slots__ = '_iri', '_is_nothing', '_is_thing'
type_index: Final = 1001

_iri: 'IRI'
_is_nothing: bool
_is_thing: bool

def __init__(self, iri: Union[IRI, str]):
"""Gets an instance of OWLClass that has the specified IRI.

Args:
iri:
"""
if isinstance(iri, IRI):
self._iri = iri
else:
self._iri = IRI.create(iri)

self._is_nothing = self._iri.is_nothing()
self._is_thing = self._iri.is_thing()

def get_iri(self) -> 'IRI':
# documented in parent
return self._iri

def is_owl_thing(self) -> bool:
# documented in parent
return self._is_thing

def is_owl_nothing(self) -> bool:
# documented in parent
return self._is_nothing

def get_object_complement_of(self) -> OWLObjectComplementOf:
# documented in parent
return OWLObjectComplementOf(self)

def get_nnf(self) -> 'OWLClass':
# documented in parent
return self

@property
def str(self):
return self.get_iri().as_str()

@property
def reminder(self) -> str:
"""The reminder of the IRI """
return self.get_iri().get_remainder()
95 changes: 95 additions & 0 deletions owlapy/data_ranges/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"""https://www.w3.org/TR/owl2-syntax/#Data_Ranges

DataRange := Datatype | DataIntersectionOf | DataUnionOf | DataComplementOf | DataOneOf | DatatypeRestriction
"""
from abc import abstractmethod, ABCMeta
from ..owlobject import OWLObject, OWLEntity
from ..meta_classes import HasOperands
from typing import Final, Iterable, Sequence
# from ..owl_literal import OWLLiteral
from typing import Final, Sequence, Union, Iterable
from ..iri import IRI

from abc import ABCMeta

class OWLPropertyRange(OWLObject, metaclass=ABCMeta):
"""OWL Objects that can be the ranges of properties."""


class OWLDataRange(OWLPropertyRange, metaclass=ABCMeta):
"""Represents a DataRange in the OWL 2 Specification."""


class OWLDataComplementOf(OWLDataRange):
"""Represents DataComplementOf in the OWL 2 Specification."""
type_index: Final = 4002

_data_range: OWLDataRange

def __init__(self, data_range: OWLDataRange):
"""
Args:
data_range: Data range to complement.
"""
self._data_range = data_range

def get_data_range(self) -> OWLDataRange:
"""
Returns:
The wrapped data range.
"""
return self._data_range

def __repr__(self):
return f"OWLDataComplementOf({repr(self._data_range)})"

def __eq__(self, other):
if type(other) is type(self):
return self._data_range == other._data_range
return NotImplemented

def __hash__(self):
return hash(self._data_range)

class OWLNaryDataRange(OWLDataRange, HasOperands[OWLDataRange]):
"""OWLNaryDataRange."""
__slots__ = ()

_operands: Sequence[OWLDataRange]

def __init__(self, operands: Iterable[OWLDataRange]):
"""
Args:
operands: Data ranges.
"""
self._operands = tuple(operands)

def operands(self) -> Iterable[OWLDataRange]:
# documented in parent
yield from self._operands

def __repr__(self):
return f'{type(self).__name__}({repr(self._operands)})'

def __eq__(self, other):
if type(other) == type(self):
return self._operands == other._operands
return NotImplemented

def __hash__(self):
return hash(self._operands)

class OWLDataUnionOf(OWLNaryDataRange):
"""Represents a DataUnionOf data range in the OWL 2 Specification."""
__slots__ = '_operands'
type_index: Final = 4005

_operands: Sequence[OWLDataRange]

class OWLDataIntersectionOf(OWLNaryDataRange):
"""Represents DataIntersectionOf in the OWL 2 Specification."""
__slots__ = '_operands'
type_index: Final = 4004

_operands: Sequence[OWLDataRange]

8 changes: 8 additions & 0 deletions owlapy/entities/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# https://www.w3.org/TR/owl2-syntax/#Entities.2C_Literals.2C_and_Anonymous_Individuals
"""
Entities are the fundamental building blocks of OWL 2 ontologies, and they define the vocabulary — the named terms — of an ontology.
In logic, the set of entities is usually said to constitute the signature of an ontology.

Classes, datatypes, object properties, data properties, annotation properties, and named individuals
are entities, and they are all uniquely identified by an IR.
"""
22 changes: 12 additions & 10 deletions owlapy/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@
from owlapy.iri import IRI
from owlapy.has import HasIndex
from owlapy.meta_classes import HasIRI, HasOperands, HasFiller, HasCardinality
from owlapy.owl_class_expression import OWLNaryBooleanClassExpression, OWLClassExpression, OWLObjectComplementOf, \
OWLAnonymousClassExpression, OWLBooleanClassExpression, OWLPropertyRange, OWLDataRange, OWLClass, OWLObjectUnionOf, \
OWLObjectIntersectionOf, OWLThing, OWLNothing
from owlapy.class_expression import OWLClassExpression, OWLNaryBooleanClassExpression, OWLObjectIntersectionOf, \
OWLObjectUnionOf, OWLObjectComplementOf
from owlapy.class_expression import OWLThing, OWLNothing, OWLClass

from owlapy.data_ranges import OWLPropertyRange, OWLDataRange

from owlapy.owl_property import OWLObjectPropertyExpression, OWLProperty, OWLPropertyExpression, \
OWLDataPropertyExpression, OWLDataProperty, OWLObjectProperty
from owlapy.owl_restriction import (OWLRestriction, OWLObjectAllValuesFrom, OWLObjectSomeValuesFrom,
OWLQuantifiedRestriction, OWLQuantifiedObjectRestriction,
OWLObjectRestriction, OWLHasValueRestriction, OWLDataRestriction,
OWLCardinalityRestriction, OWLObjectMinCardinality, OWLObjectCardinalityRestriction,OWLDataAllValuesFrom,
OWLObjectHasSelf, OWLObjectMaxCardinality, OWLObjectExactCardinality,OWLDataExactCardinality,OWLDataMinCardinality,
OWLDataMaxCardinality,OWLDataSomeValuesFrom,OWLDataHasValue,OWLDataOneOf,OWLQuantifiedDataRestriction,OWLDataCardinalityRestriction)
OWLCardinalityRestriction, OWLObjectMinCardinality, OWLObjectCardinalityRestriction,
OWLDataAllValuesFrom,
OWLObjectHasSelf, OWLObjectMaxCardinality, OWLObjectExactCardinality,
OWLDataExactCardinality, OWLDataMinCardinality,
OWLDataMaxCardinality, OWLDataSomeValuesFrom, OWLDataHasValue, OWLDataOneOf,
OWLQuantifiedDataRestriction, OWLDataCardinalityRestriction)

from owlapy.owl_individual import OWLNamedIndividual, OWLIndividual
from owlapy.owl_axiom import (OWLEquivalentClassesAxiom, OWLClassAxiom,
Expand All @@ -28,7 +34,6 @@
from owlapy.types import OWLDatatype
from owlapy.owl_literal import OWLLiteral


MOVE(OWLObject, OWLAnnotationObject, OWLAnnotationSubject, OWLAnnotationValue, HasIRI, IRI)

_T = TypeVar('_T') #:
Expand All @@ -39,8 +44,6 @@
_M = TypeVar('_M', bound='OWLOntologyManager') #:




class OWLOntologyID:
"""An object that identifies an ontology. Since OWL 2, ontologies do not have to have an ontology IRI, or if they
have an ontology IRI then they can optionally also have a version IRI. Instances of this OWLOntologyID class bundle
Expand Down Expand Up @@ -104,7 +107,6 @@ def __eq__(self, other):
return NotImplemented



class OWLImportsDeclaration(HasIRI):
"""Represents an import statement in an ontology."""
__slots__ = '_iri'
Expand Down
2 changes: 1 addition & 1 deletion owlapy/owl_axiom.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .types import OWLDatatype, OWLDataRange
from .meta_classes import HasOperands
from .owl_property import OWLPropertyExpression, OWLProperty
from .owl_class_expression import OWLClassExpression, OWLClass
from .class_expression import OWLClassExpression, OWLClass
from .owl_individual import OWLIndividual
from .iri import IRI
from owlapy.owl_annotation import OWLAnnotationSubject, OWLAnnotationValue
Expand Down
Loading
Loading