From 503971ea9fc5a08bd8d33905e0a3bb033b1211e9 Mon Sep 17 00:00:00 2001 From: Pedro Paulo Favato Barcelos Date: Sat, 14 Oct 2023 16:37:06 +0200 Subject: [PATCH] code migrated and adapted from ontouml-py --- .gitignore | 2 +- ontouml_vocabulary_lib/__init__.py | 0 ontouml_vocabulary_lib/constants/__init__.py | 0 .../constants/constants_classes.py | 108 +++++ .../constants/constants_misc.py | 88 ++++ ontouml_vocabulary_lib/ontouml.py | 400 ++++++++++++++++++ ontouml_vocabulary_lib/ouexception.py | 23 + ontouml_vocabulary_lib/tests/__init__.py | 0 .../tests/test_constants/__init__.py | 0 .../test_constants/fixtures_test_constants.py | 94 ++++ .../tests/test_constants/test_constants.py | 99 +++++ .../tests/test_ontouml/__init__.py | 0 .../test_ontouml/fixtures_test_ontouml.py | 311 ++++++++++++++ .../test_ontouml/test_ontouml_attributes.py | 116 +++++ .../test_ontouml_get_namespace.py | 46 ++ .../test_ontouml/test_ontouml_get_term.py | 86 ++++ .../test_ontouml/test_ontouml_list_all.py | 83 ++++ 17 files changed, 1455 insertions(+), 1 deletion(-) create mode 100644 ontouml_vocabulary_lib/__init__.py create mode 100644 ontouml_vocabulary_lib/constants/__init__.py create mode 100644 ontouml_vocabulary_lib/constants/constants_classes.py create mode 100644 ontouml_vocabulary_lib/constants/constants_misc.py create mode 100644 ontouml_vocabulary_lib/ontouml.py create mode 100644 ontouml_vocabulary_lib/ouexception.py create mode 100644 ontouml_vocabulary_lib/tests/__init__.py create mode 100644 ontouml_vocabulary_lib/tests/test_constants/__init__.py create mode 100644 ontouml_vocabulary_lib/tests/test_constants/fixtures_test_constants.py create mode 100644 ontouml_vocabulary_lib/tests/test_constants/test_constants.py create mode 100644 ontouml_vocabulary_lib/tests/test_ontouml/__init__.py create mode 100644 ontouml_vocabulary_lib/tests/test_ontouml/fixtures_test_ontouml.py create mode 100644 ontouml_vocabulary_lib/tests/test_ontouml/test_ontouml_attributes.py create mode 100644 ontouml_vocabulary_lib/tests/test_ontouml/test_ontouml_get_namespace.py create mode 100644 ontouml_vocabulary_lib/tests/test_ontouml/test_ontouml_get_term.py create mode 100644 ontouml_vocabulary_lib/tests/test_ontouml/test_ontouml_list_all.py diff --git a/.gitignore b/.gitignore index 68bc17f..2dc53ca 100644 --- a/.gitignore +++ b/.gitignore @@ -157,4 +157,4 @@ cython_debug/ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ +.idea/ diff --git a/ontouml_vocabulary_lib/__init__.py b/ontouml_vocabulary_lib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ontouml_vocabulary_lib/constants/__init__.py b/ontouml_vocabulary_lib/constants/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ontouml_vocabulary_lib/constants/constants_classes.py b/ontouml_vocabulary_lib/constants/constants_classes.py new file mode 100644 index 0000000..857df3e --- /dev/null +++ b/ontouml_vocabulary_lib/constants/constants_classes.py @@ -0,0 +1,108 @@ +"""This module defines various tuples of OntoUML Class Stereotypes used in OntoUML modeling. + +Each tuple represents a specific grouping of stereotypes, providing a structured and +semantically relevant means to access and utilize the stereotypes in OntoUML modeling. + +All tuples are provided sorted in alphabetical order. +""" +from ..ontouml import OntoUML + +ONTOUML_BASE_SORTAL_CLASS_STEREOTYPES = ( + OntoUML.historicalRole, + OntoUML.phase, + OntoUML.role, + OntoUML.subkind, +) + + +ONTOUML_ULTIMATE_SORTAL_CLASS_STEREOTYPES = ( + OntoUML.collective, + OntoUML.kind, + OntoUML.mode, + OntoUML.quality, + OntoUML.quantity, + OntoUML.relator, + OntoUML.type, +) + + +ONTOUML_SORTAL_CLASS_STEREOTYPES = ( + OntoUML.collective, + OntoUML.historicalRole, + OntoUML.kind, + OntoUML.mode, + OntoUML.phase, + OntoUML.quality, + OntoUML.quantity, + OntoUML.relator, + OntoUML.role, + OntoUML.subkind, + OntoUML.type, +) + + +ONTOUML_NON_SORTAL_CLASS_STEREOTYPES = ( + OntoUML.category, + OntoUML.historicalRoleMixin, + OntoUML.mixin, + OntoUML.phaseMixin, + OntoUML.roleMixin, +) + + +ONTOUML_ABSTRACT_CLASS_STEREOTYPES = ( + OntoUML.abstract, + OntoUML.datatype, + OntoUML.enumeration, +) + + +ONTOUML_RIGID_CLASS_STEREOTYPES = ( + OntoUML.category, + OntoUML.collective, + OntoUML.kind, + OntoUML.mode, + OntoUML.quality, + OntoUML.quantity, + OntoUML.relator, + OntoUML.subkind, +) + + +ONTOUML_ANTI_RIGID_CLASS_STEREOTYPES = ( + OntoUML.historicalRole, + OntoUML.historicalRoleMixin, + OntoUML.phase, + OntoUML.phaseMixin, + OntoUML.role, + OntoUML.roleMixin, +) + + +# The comma ensures that Python will understand the following as a tuple +ONTOUML_SEMI_RIGID_CLASS_STEREOTYPES = (OntoUML.mixin,) + + +ONTOUML_CLASS_STEREOTYPES = ( + OntoUML.abstract, + OntoUML.category, + OntoUML.collective, + OntoUML.datatype, + OntoUML.enumeration, + OntoUML.event, + OntoUML.historicalRole, + OntoUML.historicalRoleMixin, + OntoUML.kind, + OntoUML.mixin, + OntoUML.mode, + OntoUML.phase, + OntoUML.phaseMixin, + OntoUML.quality, + OntoUML.quantity, + OntoUML.relator, + OntoUML.role, + OntoUML.roleMixin, + OntoUML.situation, + OntoUML.subkind, + OntoUML.type, +) diff --git a/ontouml_vocabulary_lib/constants/constants_misc.py b/ontouml_vocabulary_lib/constants/constants_misc.py new file mode 100644 index 0000000..a80f36a --- /dev/null +++ b/ontouml_vocabulary_lib/constants/constants_misc.py @@ -0,0 +1,88 @@ +"""This module defines various tuples of OntoUML Stereotypes related to relations, properties, +aggregation kinds, ontological natures, and general elements, which are utilized in OntoUML modeling. + +Each tuple groups related stereotypes, offering a structured and semantically relevant method +for utilizing OntoUML stereotypes in model construction and verification. + +All tuples are provided sorted in alphabetical order. +""" +from ..ontouml import OntoUML + +ONTOUML_RELATION_STEREOTYPES = ( + OntoUML.bringsAbout, + OntoUML.characterization, + OntoUML.comparative, + OntoUML.componentOf, + OntoUML.creation, + OntoUML.derivation, + OntoUML.externalDependence, + OntoUML.historicalDependence, + OntoUML.instantiation, + OntoUML.manifestation, + OntoUML.material, + OntoUML.mediation, + OntoUML.memberOf, + OntoUML.participation, + OntoUML.participational, + OntoUML.subCollectionOf, + OntoUML.subQuantityOf, + OntoUML.termination, + OntoUML.triggers, +) + + +ONTOUML_PROPERTY_STEREOTYPES = ( + OntoUML.begin, + OntoUML.end, +) + + +ONTOUML_AGGREGATION_KINDS = ( + OntoUML.composite, + OntoUML.none, + OntoUML.shared, +) + + +ONTOUML_ONTOLOGICAL_NATURES = ( + OntoUML.abstractNature, + OntoUML.collectiveNature, + OntoUML.eventNature, + OntoUML.extrinsicModeNature, + OntoUML.functionalComplexNature, + OntoUML.intrinsicModeNature, + OntoUML.qualityNature, + OntoUML.quantityNature, + OntoUML.relatorNature, + OntoUML.situationNature, + OntoUML.typeNature, +) + + +ONTOUML_ABSTRACT_ELEMENTS = ( + OntoUML.Cardinality, + OntoUML.Class, + OntoUML.Diagram, + OntoUML.Generalization, + OntoUML.GeneralizationSet, + OntoUML.Literal, + OntoUML.Note, + OntoUML.Package, + OntoUML.Project, + OntoUML.Property, + OntoUML.Relation, +) + + +ONTOUML_CONCRETE_ELEMENTS = ( + OntoUML.ClassView, + OntoUML.GeneralizationSetView, + OntoUML.GeneralizationView, + OntoUML.NoteView, + OntoUML.PackageView, + OntoUML.Path, + OntoUML.Point, + OntoUML.Rectangle, + OntoUML.RelationView, + OntoUML.Text, +) diff --git a/ontouml_vocabulary_lib/ontouml.py b/ontouml_vocabulary_lib/ontouml.py new file mode 100644 index 0000000..f957abb --- /dev/null +++ b/ontouml_vocabulary_lib/ontouml.py @@ -0,0 +1,400 @@ +"""Module for the ONTOUML namespace mapping to the OntoUML vocabulary. + +This module defines the ONTOUML class, which serves as a convenient way to access OntoUML terms and concepts in Python +code. It complies with the OntoUML vocabulary version 1.1.0 available at https://w3id.org/ontouml/vocabulary/v1.1.0. + +Usage: + - Import the ONTOUML class from this module to access OntoUML terms with their associated URIs. + - Use ONTOUML.term_name to access specific OntoUML terms. + - Use ONTOUML.all() to access all OntoUML terms defined in the OntoUML Vocabulary as a list of URIRefs. + +Example: + ``` + from ontouml_namespace import ONTOUML + + my_ontouml_class = OntoUML.Class + all_terms = OntoUML.get_list_all() + + # Results: + my_ontouml_class: rdflib.term.URIRef('https://w3id.org/ontouml#Class') + all_terms: [rdflib.term.URIRef('https://w3id.org/ontouml#AggregationKind'), + rdflib.term.URIRef('https://w3id.org/ontouml#Cardinality'), + rdflib.term.URIRef('https://w3id.org/ontouml#Class'), ...] + ``` + +For more information about the OntoUML vocabulary, +refer to the official documentation at: https://w3id.org/ontouml/vocabulary +""" +from rdflib import URIRef +from rdflib.namespace import DefinedNamespace, Namespace + +from .ouexception import OUUnavailableTerm + + +class OntoUML(DefinedNamespace): + """Class to provide terms from the OntoUML vocabulary in an easy way. + + This class utilizes RDFLib structure for defining and accessing OntoUML terms. It provides access to OntoUML + vocabulary terms and their associated URIs. + + It complies with the OntoUML vocabulary version 1.1.0 available at https://w3id.org/ontouml/vocabulary/v1.1.0. + + :cvar abstract: URI for OntoUML term 'abstract' + :cvar abstractNature: URI for OntoUML term 'abstractNature' + :cvar aggregationKind: URI for OntoUML term 'aggregationKind' + :cvar AggregationKind: URI for OntoUML term 'AggregationKind' + :cvar attribute: URI for OntoUML term 'attribute' + :cvar begin: URI for OntoUML term 'begin' + :cvar bringsAbout: URI for OntoUML term 'bringsAbout' + :cvar cardinality: URI for OntoUML term 'cardinality' + :cvar Cardinality: URI for OntoUML term 'Cardinality' + :cvar cardinalityValue: URI for OntoUML term 'cardinalityValue' + :cvar categorizer: URI for OntoUML term 'categorizer' + :cvar category: URI for OntoUML term 'category' + :cvar characterization: URI for OntoUML term 'characterization' + :cvar Class: URI for OntoUML term 'Class' + :cvar Classifier: URI for OntoUML term 'Classifier' + :cvar ClassStereotype: URI for OntoUML term 'ClassStereotype' + :cvar ClassView: URI for OntoUML term 'ClassView' + :cvar collective: URI for OntoUML term 'collective' + :cvar collectiveNature: URI for OntoUML term 'collectiveNature' + :cvar comparative: URI for OntoUML term 'comparative' + :cvar componentOf: URI for OntoUML term 'componentOf' + :cvar composite: URI for OntoUML term 'composite' + :cvar ConnectorView: URI for OntoUML term 'ConnectorView' + :cvar containsModelElement: URI for OntoUML term 'containsModelElement' + :cvar containsView: URI for OntoUML term 'containsView' + :cvar creation: URI for OntoUML term 'creation' + :cvar datatype: URI for OntoUML term 'datatype' + :cvar DecoratableElement: URI for OntoUML term 'DecoratableElement' + :cvar derivation: URI for OntoUML term 'derivation' + :cvar description: URI for OntoUML term 'description' + :cvar diagram: URI for OntoUML term 'diagram' + :cvar Diagram: URI for OntoUML term 'Diagram' + :cvar DiagramElement: URI for OntoUML term 'DiagramElement' + :cvar ElementView: URI for OntoUML term 'ElementView' + :cvar end: URI for OntoUML term 'end' + :cvar enumeration: URI for OntoUML term 'enumeration' + :cvar event: URI for OntoUML term 'event' + :cvar eventNature: URI for OntoUML term 'eventNature' + :cvar externalDependence: URI for OntoUML term 'externalDependence' + :cvar extrinsicModeNature: URI for OntoUML term 'extrinsicModeNature' + :cvar functionalComplexNature: URI for OntoUML term 'functionalComplexNature' + :cvar general: URI for OntoUML term 'general' + :cvar generalization: URI for OntoUML term 'generalization' + :cvar Generalization: URI for OntoUML term 'Generalization' + :cvar GeneralizationSet: URI for OntoUML term 'GeneralizationSet' + :cvar GeneralizationSetView: URI for OntoUML term 'GeneralizationSetView' + :cvar GeneralizationView: URI for OntoUML term 'GeneralizationView' + :cvar height: URI for OntoUML term 'height' + :cvar historicalDependence: URI for OntoUML term 'historicalDependence' + :cvar historicalRole: URI for OntoUML term 'historicalRole' + :cvar historicalRoleMixin: URI for OntoUML term 'historicalRoleMixin' + :cvar instantiation: URI for OntoUML term 'instantiation' + :cvar intrinsicModeNature: URI for OntoUML term 'intrinsicModeNature' + :cvar isAbstract: URI for OntoUML term 'isAbstract' + :cvar isComplete: URI for OntoUML term 'isComplete' + :cvar isDerived: URI for OntoUML term 'isDerived' + :cvar isDisjoint: URI for OntoUML term 'isDisjoint' + :cvar isExtensional: URI for OntoUML term 'isExtensional' + :cvar isOrdered: URI for OntoUML term 'isOrdered' + :cvar isPowertype: URI for OntoUML term 'isPowertype' + :cvar isReadOnly: URI for OntoUML term 'isReadOnly' + :cvar isViewOf: URI for OntoUML term 'isViewOf' + :cvar kind: URI for OntoUML term 'kind' + :cvar literal: URI for OntoUML term 'literal' + :cvar Literal: URI for OntoUML term 'Literal' + :cvar lowerBound: URI for OntoUML term 'lowerBound' + :cvar manifestation: URI for OntoUML term 'manifestation' + :cvar material: URI for OntoUML term 'material' + :cvar mediation: URI for OntoUML term 'mediation' + :cvar memberOf: URI for OntoUML term 'memberOf' + :cvar mixin: URI for OntoUML term 'mixin' + :cvar mode: URI for OntoUML term 'mode' + :cvar model: URI for OntoUML term 'model' + :cvar ModelElement: URI for OntoUML term 'ModelElement' + :cvar name: URI for OntoUML term 'name' + :cvar NodeView: URI for OntoUML term 'NodeView' + :cvar none: URI for OntoUML term 'none' + :cvar Note: URI for OntoUML term 'Note' + :cvar NoteView: URI for OntoUML term 'NoteView' + :cvar OntologicalNature: URI for OntoUML term 'OntologicalNature' + :cvar OntoumlElement: URI for OntoUML term 'OntoumlElement' + :cvar order: URI for OntoUML term 'order' + :cvar owner: URI for OntoUML term 'owner' + :cvar Package: URI for OntoUML term 'Package' + :cvar PackageView: URI for OntoUML term 'PackageView' + :cvar participation: URI for OntoUML term 'participation' + :cvar participational: URI for OntoUML term 'participational' + :cvar Path: URI for OntoUML term 'Path' + :cvar phase: URI for OntoUML term 'phase' + :cvar phaseMixin: URI for OntoUML term 'phaseMixin' + :cvar point: URI for OntoUML term 'point' + :cvar Point: URI for OntoUML term 'Point' + :cvar project: URI for OntoUML term 'project' + :cvar Project: URI for OntoUML term 'Project' + :cvar property: URI for OntoUML term 'property' + :cvar Property: URI for OntoUML term 'Property' + :cvar PropertyStereotype: URI for OntoUML term 'PropertyStereotype' + :cvar propertyType: URI for OntoUML term 'propertyType' + :cvar quality: URI for OntoUML term 'quality' + :cvar qualityNature: URI for OntoUML term 'qualityNature' + :cvar quantity: URI for OntoUML term 'quantity' + :cvar quantityNature: URI for OntoUML term 'quantityNature' + :cvar Rectangle: URI for OntoUML term 'Rectangle' + :cvar RectangularShape: URI for OntoUML term 'RectangularShape' + :cvar redefinesProperty: URI for OntoUML term 'redefinesProperty' + :cvar Relation: URI for OntoUML term 'Relation' + :cvar relationEnd: URI for OntoUML term 'relationEnd' + :cvar RelationStereotype: URI for OntoUML term 'RelationStereotype' + :cvar RelationView: URI for OntoUML term 'RelationView' + :cvar relator: URI for OntoUML term 'relator' + :cvar relatorNature: URI for OntoUML term 'relatorNature' + :cvar restrictedTo: URI for OntoUML term 'restrictedTo' + :cvar role: URI for OntoUML term 'role' + :cvar roleMixin: URI for OntoUML term 'roleMixin' + :cvar shape: URI for OntoUML term 'shape' + :cvar Shape: URI for OntoUML term 'Shape' + :cvar shared: URI for OntoUML term 'shared' + :cvar situation: URI for OntoUML term 'situation' + :cvar situationNature: URI for OntoUML term 'situationNature' + :cvar sourceEnd: URI for OntoUML term 'sourceEnd' + :cvar sourceView: URI for OntoUML term 'sourceView' + :cvar specific: URI for OntoUML term 'specific' + :cvar stereotype: URI for OntoUML term 'stereotype' + :cvar Stereotype: URI for OntoUML term 'Stereotype' + :cvar subCollectionOf: URI for OntoUML term 'subCollectionOf' + :cvar subkind: URI for OntoUML term 'subkind' + :cvar subQuantityOf: URI for OntoUML term 'subQuantityOf' + :cvar subsetsProperty: URI for OntoUML term 'subsetsProperty' + :cvar targetEnd: URI for OntoUML term 'targetEnd' + :cvar targetView: URI for OntoUML term 'targetView' + :cvar termination: URI for OntoUML term 'termination' + :cvar text: URI for OntoUML term 'text' + :cvar Text: URI for OntoUML term 'Text' + :cvar topLeftPosition: URI for OntoUML term 'topLeftPosition' + :cvar triggers: URI for OntoUML term 'triggers' + :cvar type: URI for OntoUML term 'type' + :cvar typeNature: URI for OntoUML term 'typeNature' + :cvar upperBound: URI for OntoUML term 'upperBound' + :cvar width: URI for OntoUML term 'width' + :cvar xCoordinate: URI for OntoUML term 'xCoordinate' + :cvar yCoordinate: URI for OntoUML term 'yCoordinate' + + :cvar _fail: Flag indicating whether failed lookups should raise an exception. + :cvar _NS: Namespace for the OntoUML vocabulary. + + """ + + abstract: URIRef # https://w3id.org/ontouml#abstract + abstractNature: URIRef # https://w3id.org/ontouml#abstractNature + aggregationKind: URIRef # https://w3id.org/ontouml#aggregationKind + AggregationKind: URIRef # https://w3id.org/ontouml#AggregationKind + attribute: URIRef # https://w3id.org/ontouml#attribute + begin: URIRef # https://w3id.org/ontouml#begin + bringsAbout: URIRef # https://w3id.org/ontouml#bringsAbout + cardinality: URIRef # https://w3id.org/ontouml#cardinality + Cardinality: URIRef # https://w3id.org/ontouml#Cardinality + cardinalityValue: URIRef # https://w3id.org/ontouml#cardinalityValue + categorizer: URIRef # https://w3id.org/ontouml#categorizer + category: URIRef # https://w3id.org/ontouml#category + characterization: URIRef # https://w3id.org/ontouml#characterization + Class: URIRef # https://w3id.org/ontouml#Class + Classifier: URIRef # https://w3id.org/ontouml#Classifier + ClassStereotype: URIRef # https://w3id.org/ontouml#ClassStereotype + ClassView: URIRef # https://w3id.org/ontouml#ClassView + collective: URIRef # https://w3id.org/ontouml#collective + collectiveNature: URIRef # https://w3id.org/ontouml#collectiveNature + comparative: URIRef # https://w3id.org/ontouml#comparative + componentOf: URIRef # https://w3id.org/ontouml#componentOf + composite: URIRef # https://w3id.org/ontouml#composite + ConnectorView: URIRef # https://w3id.org/ontouml#ConnectorView + containsModelElement: URIRef # https://w3id.org/ontouml#containsModelElement + containsView: URIRef # https://w3id.org/ontouml#containsView + creation: URIRef # https://w3id.org/ontouml#creation + datatype: URIRef # https://w3id.org/ontouml#datatype + DecoratableElement: URIRef # https://w3id.org/ontouml#DecoratableElement + derivation: URIRef # https://w3id.org/ontouml#derivation + description: URIRef # https://w3id.org/ontouml#description + diagram: URIRef # https://w3id.org/ontouml#diagram + Diagram: URIRef # https://w3id.org/ontouml#Diagram + DiagramElement: URIRef # https://w3id.org/ontouml#DiagramElement + ElementView: URIRef # https://w3id.org/ontouml#ElementView + end: URIRef # https://w3id.org/ontouml#end + enumeration: URIRef # https://w3id.org/ontouml#enumeration + event: URIRef # https://w3id.org/ontouml#event + eventNature: URIRef # https://w3id.org/ontouml#eventNature + externalDependence: URIRef # https://w3id.org/ontouml#externalDependence + extrinsicModeNature: URIRef # https://w3id.org/ontouml#extrinsicModeNature + functionalComplexNature: URIRef # https://w3id.org/ontouml#functionalComplexNature + general: URIRef # https://w3id.org/ontouml#general + generalization: URIRef # https://w3id.org/ontouml#generalization + Generalization: URIRef # https://w3id.org/ontouml#Generalization + GeneralizationSet: URIRef # https://w3id.org/ontouml#GeneralizationSet + GeneralizationSetView: URIRef # https://w3id.org/ontouml#GeneralizationSetView + GeneralizationView: URIRef # https://w3id.org/ontouml#GeneralizationView + height: URIRef # https://w3id.org/ontouml#height + historicalDependence: URIRef # https://w3id.org/ontouml#historicalDependence + historicalRole: URIRef # https://w3id.org/ontouml#historicalRole + historicalRoleMixin: URIRef # https://w3id.org/ontouml#historicalRoleMixin + instantiation: URIRef # https://w3id.org/ontouml#instantiation + intrinsicModeNature: URIRef # https://w3id.org/ontouml#intrinsicModeNature + isAbstract: URIRef # https://w3id.org/ontouml#isAbstract + isComplete: URIRef # https://w3id.org/ontouml#isComplete + isDerived: URIRef # https://w3id.org/ontouml#isDerived + isDisjoint: URIRef # https://w3id.org/ontouml#isDisjoint + isExtensional: URIRef # https://w3id.org/ontouml#isExtensional + isOrdered: URIRef # https://w3id.org/ontouml#isOrdered + isPowertype: URIRef # https://w3id.org/ontouml#isPowertype + isReadOnly: URIRef # https://w3id.org/ontouml#isReadOnly + isViewOf: URIRef # https://w3id.org/ontouml#isViewOf + kind: URIRef # https://w3id.org/ontouml#kind + literal: URIRef # https://w3id.org/ontouml#literal + Literal: URIRef # https://w3id.org/ontouml#Literal + lowerBound: URIRef # https://w3id.org/ontouml#lowerBound + manifestation: URIRef # https://w3id.org/ontouml#manifestation + material: URIRef # https://w3id.org/ontouml#material + mediation: URIRef # https://w3id.org/ontouml#mediation + memberOf: URIRef # https://w3id.org/ontouml#memberOf + mixin: URIRef # https://w3id.org/ontouml#mixin + mode: URIRef # https://w3id.org/ontouml#mode + model: URIRef # https://w3id.org/ontouml#model + ModelElement: URIRef # https://w3id.org/ontouml#ModelElement + name: URIRef # https://w3id.org/ontouml#name + NodeView: URIRef # https://w3id.org/ontouml#NodeView + none: URIRef # https://w3id.org/ontouml#none + Note: URIRef # https://w3id.org/ontouml#Note + NoteView: URIRef # https://w3id.org/ontouml#NoteView + OntologicalNature: URIRef # https://w3id.org/ontouml#OntologicalNature + OntoumlElement: URIRef # https://w3id.org/ontouml#OntoumlElement + order: URIRef # https://w3id.org/ontouml#order + owner: URIRef # https://w3id.org/ontouml#owner + Package: URIRef # https://w3id.org/ontouml#Package + PackageView: URIRef # https://w3id.org/ontouml#PackageView + participation: URIRef # https://w3id.org/ontouml#participation + participational: URIRef # https://w3id.org/ontouml#participational + Path: URIRef # https://w3id.org/ontouml#Path + phase: URIRef # https://w3id.org/ontouml#phase + phaseMixin: URIRef # https://w3id.org/ontouml#phaseMixin + point: URIRef # https://w3id.org/ontouml#point + Point: URIRef # https://w3id.org/ontouml#Point + project: URIRef # https://w3id.org/ontouml#project + Project: URIRef # https://w3id.org/ontouml#Project + property: URIRef # https://w3id.org/ontouml#property + Property: URIRef # https://w3id.org/ontouml#Property + PropertyStereotype: URIRef # https://w3id.org/ontouml#PropertyStereotype + propertyType: URIRef # https://w3id.org/ontouml#propertyType + quality: URIRef # https://w3id.org/ontouml#quality + qualityNature: URIRef # https://w3id.org/ontouml#qualityNature + quantity: URIRef # https://w3id.org/ontouml#quantity + quantityNature: URIRef # https://w3id.org/ontouml#quantityNature + Rectangle: URIRef # https://w3id.org/ontouml#Rectangle + RectangularShape: URIRef # https://w3id.org/ontouml#RectangularShape + redefinesProperty: URIRef # https://w3id.org/ontouml#redefinesProperty + Relation: URIRef # https://w3id.org/ontouml#Relation + relationEnd: URIRef # https://w3id.org/ontouml#relationEnd + RelationStereotype: URIRef # https://w3id.org/ontouml#RelationStereotype + RelationView: URIRef # https://w3id.org/ontouml#RelationView + relator: URIRef # https://w3id.org/ontouml#relator + relatorNature: URIRef # https://w3id.org/ontouml#relatorNature + restrictedTo: URIRef # https://w3id.org/ontouml#restrictedTo + role: URIRef # https://w3id.org/ontouml#role + roleMixin: URIRef # https://w3id.org/ontouml#roleMixin + shape: URIRef # https://w3id.org/ontouml#shape + Shape: URIRef # https://w3id.org/ontouml#Shape + shared: URIRef # https://w3id.org/ontouml#shared + situation: URIRef # https://w3id.org/ontouml#situation + situationNature: URIRef # https://w3id.org/ontouml#situationNature + sourceEnd: URIRef # https://w3id.org/ontouml#sourceEnd + sourceView: URIRef # https://w3id.org/ontouml#sourceView + specific: URIRef # https://w3id.org/ontouml#specific + stereotype: URIRef # https://w3id.org/ontouml#stereotype + Stereotype: URIRef # https://w3id.org/ontouml#Stereotype + subCollectionOf: URIRef # https://w3id.org/ontouml#subCollectionOf + subkind: URIRef # https://w3id.org/ontouml#subkind + subQuantityOf: URIRef # https://w3id.org/ontouml#subQuantityOf + subsetsProperty: URIRef # https://w3id.org/ontouml#subsetsProperty + targetEnd: URIRef # https://w3id.org/ontouml#targetEnd + targetView: URIRef # https://w3id.org/ontouml#targetView + termination: URIRef # https://w3id.org/ontouml#termination + text: URIRef # https://w3id.org/ontouml#text + Text: URIRef # https://w3id.org/ontouml#Text + topLeftPosition: URIRef # https://w3id.org/ontouml#topLeftPosition + triggers: URIRef # https://w3id.org/ontouml#triggers + type: URIRef # https://w3id.org/ontouml#type + typeNature: URIRef # https://w3id.org/ontouml#typeNature + upperBound: URIRef # https://w3id.org/ontouml#upperBound + width: URIRef # https://w3id.org/ontouml#width + xCoordinate: URIRef # https://w3id.org/ontouml#xCoordinate + yCoordinate: URIRef # https://w3id.org/ontouml#yCoordinate + + _fail = True + + _NS = Namespace("https://w3id.org/ontouml#") # has type RDFLib.Namespace. + + @classmethod + def get_list_all(cls) -> list[URIRef]: + """ + Retrieves a list of all public attributes of the OntoUML class, i.e., all terms contained in the OntoUML \ + Vocabulary. + + This method uses introspection to find all attributes and methods of the OntoUML + class that do not begin with an underscore (which by convention are considered private), + and returns a list containing the names of these attributes and methods. + + :return: A list containing the names of all public attributes and methods of the OntoUML class. + :rtype: list[URIRef] + """ + all_dict = {k: getattr(OntoUML, k) for k in dir(OntoUML) if not k.startswith("_")} + return list(all_dict.keys()) + + @classmethod + def get_namespace(cls) -> str: + """Retrieve the OntoUML namespace URI. + + This method returns the OntoUML namespace URI as a string. + The namespace URI is a prefix that can be used to construct full URIs for terms in the OntoUML vocabulary. + + Usage example: + + ``` + ns = OntoUML.get_namespace() + ``` + + :return: The OntoUML namespace URI as a string. + :rtype: str + """ + # The OntoUML namespace URI. This constant defines the URI for the OntoUML namespace used in RDF triples. + return str(cls._NS) + + @classmethod + def get_term(cls, str_term: str) -> URIRef: + """ + Retrieve the URIRef of a term from the OntoUML vocabulary. + + Given a term name as a string, this method retrieves its associated URIRef from the OntoUML vocabulary. The + method ensures compliance with OntoUML vocabulary version 1.1.0. If the term is unavailable or nonexistent, + an exception is raised. + + :param str_term: The name of the OntoUML term to retrieve. + :type str_term: str + :return: URIRef associated with the specified OntoUML term. + :rtype: URIRef + :raises OUUnavailableOUTerm: If the term is not available in the OntoUML vocabulary. + + Example: + ``` + from ontouml_namespace import OntoUML + + try: + my_term = OntoUML.get_term('Class') + # Result: rdflib.term.URIRef('https://w3id.org/ontouml#Class') + except OUUnavailableOUTerm: + print("Specified term is not available in OntoUML vocabulary.") + ``` + """ + for term in dir(cls): + if str_term == term.fragment: + return term + else: + raise OUUnavailableTerm(str_term) diff --git a/ontouml_vocabulary_lib/ouexception.py b/ontouml_vocabulary_lib/ouexception.py new file mode 100644 index 0000000..e2c0be5 --- /dev/null +++ b/ontouml_vocabulary_lib/ouexception.py @@ -0,0 +1,23 @@ +"""Module for handling custom exceptions related to OntoUML. + +This module provides a set of custom exceptions designed for handling various error scenarios +that may occur in the manipulation and management of OntoUML graphs and related operations. +Each exception is designed to provide clear, user-friendly error messages to assist in +debugging and issue resolution. +""" + + +class OUUnavailableTerm(ValueError): + """Custom exception for handling cases where an OUTerm is unavailable in the OntoUML Vocabulary. + + This exception is raised when a given OUTerm does not exist or is not found in the OntoUML + Vocabulary, providing clear feedback that the requested term is unavailable or incorrectly + specified. + + :param ou_term: The OUTerm that does not exist in the OntoUML Vocabulary. + :type ou_term: str + """ + + def __init__(self, ou_term: str) -> None: + message = f"The OUTerm '{ou_term}' does not exist in the OntoUML Vocabulary." + super().__init__(message) diff --git a/ontouml_vocabulary_lib/tests/__init__.py b/ontouml_vocabulary_lib/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ontouml_vocabulary_lib/tests/test_constants/__init__.py b/ontouml_vocabulary_lib/tests/test_constants/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ontouml_vocabulary_lib/tests/test_constants/fixtures_test_constants.py b/ontouml_vocabulary_lib/tests/test_constants/fixtures_test_constants.py new file mode 100644 index 0000000..75f91d8 --- /dev/null +++ b/ontouml_vocabulary_lib/tests/test_constants/fixtures_test_constants.py @@ -0,0 +1,94 @@ +from ontouml_vocabulary_lib.constants.constants_classes import ONTOUML_BASE_SORTAL_CLASS_STEREOTYPES, \ + ONTOUML_ULTIMATE_SORTAL_CLASS_STEREOTYPES, ONTOUML_SORTAL_CLASS_STEREOTYPES, ONTOUML_NON_SORTAL_CLASS_STEREOTYPES, \ + ONTOUML_ABSTRACT_CLASS_STEREOTYPES, ONTOUML_RIGID_CLASS_STEREOTYPES, ONTOUML_ANTI_RIGID_CLASS_STEREOTYPES, \ + ONTOUML_SEMI_RIGID_CLASS_STEREOTYPES, ONTOUML_CLASS_STEREOTYPES +from ontouml_vocabulary_lib.constants.constants_misc import ONTOUML_RELATION_STEREOTYPES, ONTOUML_PROPERTY_STEREOTYPES, \ + ONTOUML_AGGREGATION_KINDS, ONTOUML_ONTOLOGICAL_NATURES, ONTOUML_ABSTRACT_ELEMENTS, ONTOUML_CONCRETE_ELEMENTS + +DICT_TUPLES_SIZE = { + ONTOUML_BASE_SORTAL_CLASS_STEREOTYPES: 4, + ONTOUML_ULTIMATE_SORTAL_CLASS_STEREOTYPES: 7, + ONTOUML_SORTAL_CLASS_STEREOTYPES: 11, + ONTOUML_NON_SORTAL_CLASS_STEREOTYPES: 5, + ONTOUML_ABSTRACT_CLASS_STEREOTYPES: 3, + ONTOUML_RIGID_CLASS_STEREOTYPES: 8, + ONTOUML_ANTI_RIGID_CLASS_STEREOTYPES: 6, + ONTOUML_SEMI_RIGID_CLASS_STEREOTYPES: 1, + ONTOUML_CLASS_STEREOTYPES: 21, + ONTOUML_RELATION_STEREOTYPES: 19, + ONTOUML_PROPERTY_STEREOTYPES: 2, + ONTOUML_AGGREGATION_KINDS: 3, + ONTOUML_ONTOLOGICAL_NATURES: 11, + ONTOUML_ABSTRACT_ELEMENTS: 11, + ONTOUML_CONCRETE_ELEMENTS: 10, +} + + +def swap_first_two(original: tuple) -> tuple: + """Swaps the first two elements of a given tuple. + + :param original: The original tuple. + :type original: tuple + :return: A new tuple with the first two elements swapped. If the tuple has fewer than two elements, returns the + original tuple unmodified. + :rtype: tuple + """ + mutant = list(original) + if len(mutant) > 1: + mutant[0], mutant[1] = mutant[1], mutant[0] + return tuple(mutant) + + +def remove_first(original: tuple) -> tuple: + """Removes the first element from a given tuple. + + :param original: The original tuple. + :type original: tuple + :return: A new tuple with the first element removed. + :rtype: tuple + """ + return original[1:] + + +def add_element(original: tuple) -> tuple: + """Adds a None element to the end of a given tuple. + + :param original: The original tuple. + :type original: tuple + :return: A new tuple with a None element added at the end. + :rtype: tuple + """ + return original + (None,) + + +def replace_last(original: tuple) -> tuple: + """Replaces the last element of a given tuple with the string "REPLACEMENT". + + :param original: The original tuple. + :type original: tuple + :return: A new tuple with the last element replaced by "REPLACEMENT". + :rtype: tuple + """ + return original[:-1] + ("REPLACEMENT",) + + +def reverse_order(original: tuple) -> tuple: + """Reverses the order of elements in a given tuple. + + :param original: The original tuple. + :type original: tuple + :return: A new tuple with the order of elements reversed. + :rtype: tuple + """ + return tuple(reversed(original)) + + +# List of mutation functions +MUTATIONS = [swap_first_two, remove_first, add_element, replace_last, reverse_order] + + +# Extract tuples from your dictionary +TUPLES = DICT_TUPLES_SIZE.keys() + +# Generate mutants +MUTANTS = [(original, mutation, mutation(original)) for original in TUPLES for mutation in MUTATIONS] diff --git a/ontouml_vocabulary_lib/tests/test_constants/test_constants.py b/ontouml_vocabulary_lib/tests/test_constants/test_constants.py new file mode 100644 index 0000000..5294c6d --- /dev/null +++ b/ontouml_vocabulary_lib/tests/test_constants/test_constants.py @@ -0,0 +1,99 @@ +"""Testing Module for OntoUML Tuple Validation + +This module carries out tests to validate tuples utilized in the OntoUML model, ensuring that elements contained +within them adhere to the defined criteria (e.g., non-null, type adherence, etc.) and overall tuple integrity +(e.g., non-emptiness, expected size, etc.). +""" +import pytest +from rdflib import URIRef + +from ontouml_vocabulary_lib.tests.test_constants.fixtures_test_constants import DICT_TUPLES_SIZE, MUTANTS + + +@pytest.mark.parametrize("in_tuple", DICT_TUPLES_SIZE.keys()) +def test_elements_are_non_null_and_of_type_URIRef(in_tuple: tuple) -> None: + """Ensure each element within the tuple is non-null and an instance of URIRef. + + :param in_tuple: The tuple whose elements are to be checked for non-nullity and type URIRef. + :type in_tuple: tuple + """ + for elem in in_tuple: + assert elem is not None + assert isinstance(elem, URIRef) + + +@pytest.mark.parametrize("in_tuple", DICT_TUPLES_SIZE.keys()) +def test_first_element_of_tuple_is_truthy(in_tuple: tuple) -> None: + """Assert the first element of the tuple evaluates to True. + + :param in_tuple: The tuple whose first element is to be asserted as truthy. + :type in_tuple: tuple + """ + assert in_tuple[0] + + +@pytest.mark.parametrize("in_tuple", DICT_TUPLES_SIZE.keys()) +def test_tuple_is_not_empty(in_tuple: tuple) -> None: + """Ensure the provided tuple is not empty. + + :param in_tuple: The tuple to be checked for non-emptiness. + :type in_tuple: tuple + """ + assert len(in_tuple) > 0 + + +@pytest.mark.parametrize("in_tuple, size", DICT_TUPLES_SIZE.items()) +def test_tuple_size_matches_expected(in_tuple: tuple, size: int) -> None: + """Confirm the provided tuple's size matches the expected size. + + :param in_tuple: The tuple whose size is to be checked. + :type in_tuple: tuple + :param size: The expected size of the tuple. + :type size: int + """ + assert len(in_tuple) == size + + + + + +@pytest.mark.parametrize("in_tuple", DICT_TUPLES_SIZE.keys()) +def test_elements_are_unique(in_tuple: tuple) -> None: + """Test to ensure that all elements within a tuple are unique. + + :param in_tuple: The input tuple whose elements are to be verified for uniqueness. + :type in_tuple: tuple + """ + assert len(in_tuple) == len(set(in_tuple)), "Duplicate elements found in tuple." + + + +@pytest.mark.parametrize("in_tuple", DICT_TUPLES_SIZE.keys()) +def test_elements_are_in_alphabetical_order(in_tuple: tuple) -> None: + """Test to ensure that all elements within a tuple are in alphabetical order. + + :param in_tuple: The input tuple whose elements are to be verified for order. + :type in_tuple: tuple + """ + # Extracting the string representations of the elements in the tuple + string_representations = [str(elem) for elem in in_tuple] + + # Comparing the tuple with its sorted version + assert string_representations == sorted(string_representations), f"Tuple {in_tuple} is not in alphabetical order." + + +@pytest.mark.parametrize("original,mutation_func,mutant", MUTANTS) +def test_tuple_mutation(original: tuple, mutation_func, mutant: tuple) -> None: + """ + Mutation test to ensure that tests can detect alterations in the original tuple. + + :param original: The original tuple. + :type original: tuple + :param mutation_func: Function that applies mutation. + :param mutant: A mutated version of the original tuple. + :type mutant: tuple + """ + if len(original) > 1: + assert ( + original != mutant + ), f"Mutant {mutant} (created by {mutation_func.__name__}) is not detected as different from the original {original}." diff --git a/ontouml_vocabulary_lib/tests/test_ontouml/__init__.py b/ontouml_vocabulary_lib/tests/test_ontouml/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ontouml_vocabulary_lib/tests/test_ontouml/fixtures_test_ontouml.py b/ontouml_vocabulary_lib/tests/test_ontouml/fixtures_test_ontouml.py new file mode 100644 index 0000000..686b6aa --- /dev/null +++ b/ontouml_vocabulary_lib/tests/test_ontouml/fixtures_test_ontouml.py @@ -0,0 +1,311 @@ +ALL_TERMS_FRAGMENT = [ + "abstract", + "abstractNature", + "aggregationKind", + "AggregationKind", + "attribute", + "begin", + "bringsAbout", + "cardinality", + "Cardinality", + "cardinalityValue", + "categorizer", + "category", + "characterization", + "Class", + "Classifier", + "ClassStereotype", + "ClassView", + "collective", + "collectiveNature", + "comparative", + "componentOf", + "composite", + "ConnectorView", + "containsModelElement", + "containsView", + "creation", + "datatype", + "DecoratableElement", + "derivation", + "description", + "diagram", + "Diagram", + "DiagramElement", + "ElementView", + "end", + "enumeration", + "event", + "eventNature", + "externalDependence", + "extrinsicModeNature", + "functionalComplexNature", + "general", + "generalization", + "Generalization", + "GeneralizationSet", + "GeneralizationSetView", + "GeneralizationView", + "height", + "historicalDependence", + "historicalRole", + "historicalRoleMixin", + "instantiation", + "intrinsicModeNature", + "isAbstract", + "isComplete", + "isDerived", + "isDisjoint", + "isExtensional", + "isOrdered", + "isPowertype", + "isReadOnly", + "isViewOf", + "kind", + "literal", + "Literal", + "lowerBound", + "manifestation", + "material", + "mediation", + "memberOf", + "mixin", + "mode", + "model", + "ModelElement", + "name", + "NodeView", + "none", + "Note", + "NoteView", + "OntologicalNature", + "OntoumlElement", + "order", + "owner", + "Package", + "PackageView", + "participation", + "participational", + "Path", + "phase", + "phaseMixin", + "point", + "Point", + "project", + "Project", + "property", + "Property", + "PropertyStereotype", + "propertyType", + "quality", + "qualityNature", + "quantity", + "quantityNature", + "Rectangle", + "RectangularShape", + "redefinesProperty", + "Relation", + "relationEnd", + "RelationStereotype", + "RelationView", + "relator", + "relatorNature", + "restrictedTo", + "role", + "roleMixin", + "shape", + "Shape", + "shared", + "situation", + "situationNature", + "sourceEnd", + "sourceView", + "specific", + "stereotype", + "Stereotype", + "subCollectionOf", + "subkind", + "subQuantityOf", + "subsetsProperty", + "targetEnd", + "targetView", + "termination", + "text", + "Text", + "topLeftPosition", + "triggers", + "type", + "typeNature", + "upperBound", + "width", + "xCoordinate", + "yCoordinate", +] + +ALL_TERMS_STR = [ + "https://w3id.org/ontouml#abstract", + "https://w3id.org/ontouml#abstractNature", + "https://w3id.org/ontouml#aggregationKind", + "https://w3id.org/ontouml#AggregationKind", + "https://w3id.org/ontouml#attribute", + "https://w3id.org/ontouml#begin", + "https://w3id.org/ontouml#bringsAbout", + "https://w3id.org/ontouml#cardinality", + "https://w3id.org/ontouml#Cardinality", + "https://w3id.org/ontouml#cardinalityValue", + "https://w3id.org/ontouml#categorizer", + "https://w3id.org/ontouml#category", + "https://w3id.org/ontouml#characterization", + "https://w3id.org/ontouml#Class", + "https://w3id.org/ontouml#Classifier", + "https://w3id.org/ontouml#ClassStereotype", + "https://w3id.org/ontouml#ClassView", + "https://w3id.org/ontouml#collective", + "https://w3id.org/ontouml#collectiveNature", + "https://w3id.org/ontouml#comparative", + "https://w3id.org/ontouml#componentOf", + "https://w3id.org/ontouml#composite", + "https://w3id.org/ontouml#ConnectorView", + "https://w3id.org/ontouml#containsModelElement", + "https://w3id.org/ontouml#containsView", + "https://w3id.org/ontouml#creation", + "https://w3id.org/ontouml#datatype", + "https://w3id.org/ontouml#DecoratableElement", + "https://w3id.org/ontouml#derivation", + "https://w3id.org/ontouml#description", + "https://w3id.org/ontouml#diagram", + "https://w3id.org/ontouml#Diagram", + "https://w3id.org/ontouml#DiagramElement", + "https://w3id.org/ontouml#ElementView", + "https://w3id.org/ontouml#end", + "https://w3id.org/ontouml#enumeration", + "https://w3id.org/ontouml#event", + "https://w3id.org/ontouml#eventNature", + "https://w3id.org/ontouml#externalDependence", + "https://w3id.org/ontouml#extrinsicModeNature", + "https://w3id.org/ontouml#functionalComplexNature", + "https://w3id.org/ontouml#general", + "https://w3id.org/ontouml#generalization", + "https://w3id.org/ontouml#Generalization", + "https://w3id.org/ontouml#GeneralizationSet", + "https://w3id.org/ontouml#GeneralizationSetView", + "https://w3id.org/ontouml#GeneralizationView", + "https://w3id.org/ontouml#height", + "https://w3id.org/ontouml#historicalDependence", + "https://w3id.org/ontouml#historicalRole", + "https://w3id.org/ontouml#historicalRoleMixin", + "https://w3id.org/ontouml#instantiation", + "https://w3id.org/ontouml#intrinsicModeNature", + "https://w3id.org/ontouml#isAbstract", + "https://w3id.org/ontouml#isComplete", + "https://w3id.org/ontouml#isDerived", + "https://w3id.org/ontouml#isDisjoint", + "https://w3id.org/ontouml#isExtensional", + "https://w3id.org/ontouml#isOrdered", + "https://w3id.org/ontouml#isPowertype", + "https://w3id.org/ontouml#isReadOnly", + "https://w3id.org/ontouml#isViewOf", + "https://w3id.org/ontouml#kind", + "https://w3id.org/ontouml#literal", + "https://w3id.org/ontouml#Literal", + "https://w3id.org/ontouml#lowerBound", + "https://w3id.org/ontouml#manifestation", + "https://w3id.org/ontouml#material", + "https://w3id.org/ontouml#mediation", + "https://w3id.org/ontouml#memberOf", + "https://w3id.org/ontouml#mixin", + "https://w3id.org/ontouml#mode", + "https://w3id.org/ontouml#model", + "https://w3id.org/ontouml#ModelElement", + "https://w3id.org/ontouml#name", + "https://w3id.org/ontouml#NodeView", + "https://w3id.org/ontouml#none", + "https://w3id.org/ontouml#Note", + "https://w3id.org/ontouml#NoteView", + "https://w3id.org/ontouml#OntologicalNature", + "https://w3id.org/ontouml#OntoumlElement", + "https://w3id.org/ontouml#order", + "https://w3id.org/ontouml#owner", + "https://w3id.org/ontouml#Package", + "https://w3id.org/ontouml#PackageView", + "https://w3id.org/ontouml#participation", + "https://w3id.org/ontouml#participational", + "https://w3id.org/ontouml#Path", + "https://w3id.org/ontouml#phase", + "https://w3id.org/ontouml#phaseMixin", + "https://w3id.org/ontouml#point", + "https://w3id.org/ontouml#Point", + "https://w3id.org/ontouml#project", + "https://w3id.org/ontouml#Project", + "https://w3id.org/ontouml#property", + "https://w3id.org/ontouml#Property", + "https://w3id.org/ontouml#PropertyStereotype", + "https://w3id.org/ontouml#propertyType", + "https://w3id.org/ontouml#quality", + "https://w3id.org/ontouml#qualityNature", + "https://w3id.org/ontouml#quantity", + "https://w3id.org/ontouml#quantityNature", + "https://w3id.org/ontouml#Rectangle", + "https://w3id.org/ontouml#RectangularShape", + "https://w3id.org/ontouml#redefinesProperty", + "https://w3id.org/ontouml#Relation", + "https://w3id.org/ontouml#relationEnd", + "https://w3id.org/ontouml#RelationStereotype", + "https://w3id.org/ontouml#RelationView", + "https://w3id.org/ontouml#relator", + "https://w3id.org/ontouml#relatorNature", + "https://w3id.org/ontouml#restrictedTo", + "https://w3id.org/ontouml#role", + "https://w3id.org/ontouml#roleMixin", + "https://w3id.org/ontouml#shape", + "https://w3id.org/ontouml#Shape", + "https://w3id.org/ontouml#shared", + "https://w3id.org/ontouml#situation", + "https://w3id.org/ontouml#situationNature", + "https://w3id.org/ontouml#sourceEnd", + "https://w3id.org/ontouml#sourceView", + "https://w3id.org/ontouml#specific", + "https://w3id.org/ontouml#stereotype", + "https://w3id.org/ontouml#Stereotype", + "https://w3id.org/ontouml#subCollectionOf", + "https://w3id.org/ontouml#subkind", + "https://w3id.org/ontouml#subQuantityOf", + "https://w3id.org/ontouml#subsetsProperty", + "https://w3id.org/ontouml#targetEnd", + "https://w3id.org/ontouml#targetView", + "https://w3id.org/ontouml#termination", + "https://w3id.org/ontouml#text", + "https://w3id.org/ontouml#Text", + "https://w3id.org/ontouml#topLeftPosition", + "https://w3id.org/ontouml#triggers", + "https://w3id.org/ontouml#type", + "https://w3id.org/ontouml#typeNature", + "https://w3id.org/ontouml#upperBound", + "https://w3id.org/ontouml#width", + "https://w3id.org/ontouml#xCoordinate", + "https://w3id.org/ontouml#yCoordinate", +] + + +OK_BASE_URI = "https://w3id.org/ontouml#" +NOK_BASE_URI = "https://example.org/" + +INVALID_INPUTS = [ + " ", + "str", + b"some_byte_string", + 10, + 10.1, + None, + False, + True, + "_", + (10, 2), + [1, 2], + {}, + {"key": "value"}, + 1 + 2j, + bytearray([65, 66, 67]), + {1, 2, 3}, + frozenset([1, 2, 3]), +] diff --git a/ontouml_vocabulary_lib/tests/test_ontouml/test_ontouml_attributes.py b/ontouml_vocabulary_lib/tests/test_ontouml/test_ontouml_attributes.py new file mode 100644 index 0000000..ccc1975 --- /dev/null +++ b/ontouml_vocabulary_lib/tests/test_ontouml/test_ontouml_attributes.py @@ -0,0 +1,116 @@ +"""Module for testing exclusively the attributes of the OntoUML class.""" +import pytest +from rdflib import URIRef + +from ontouml_vocabulary_lib.ontouml import OntoUML +from ontouml_vocabulary_lib.tests.test_ontouml.fixtures_test_ontouml import ALL_TERMS_STR, OK_BASE_URI, \ + ALL_TERMS_FRAGMENT, NOK_BASE_URI + + +def test_internal_term_presence_and_type() -> None: + """ + Ensure internal terms in OntoUML are present in ALL_TERMS_STR and are instances of URIRef. + + This function loops through every internal term of the OntoUML class, converts it to a Python datatype if possible, + and then verifies: + - Its presence in the predefined ALL_TERMS_STR list. + - Its instantiation from the rdflib.URIRef class. + """ + for internal_term in dir(OntoUML): + assert internal_term.toPython() in ALL_TERMS_STR + assert isinstance(internal_term, URIRef) + + +@pytest.mark.parametrize("term", ALL_TERMS_STR) +def test_ontouml_terms_as_uri_references(term: str) -> None: + """Check if OntoUML terms can be recognized as valid URI references. + + :param term: A term from the ALL_TERMS_STR list, representing an OntoUML term. + :type term: str + :raises AssertionError: If the URIRef of the term is not found in OntoUML's directory. + """ + list_valid_terms = dir(OntoUML) + assert URIRef(term) in list_valid_terms + + +@pytest.mark.parametrize("term", ALL_TERMS_STR) +def test_ontouml_term_matching_uri(term: str) -> None: + """Verify that accessing an OntoUML term provides a URI combining OK_BASE_URI and the term itself. + + :param term: A term from the ALL_TERMS_STR list, representing an OntoUML term. + :type term: str + :raises AssertionError: If the assembled URI does not match the expected format. + """ + called_term = str(getattr(OntoUML, term)) + assert called_term == OK_BASE_URI + term + + +@pytest.mark.parametrize("term", ALL_TERMS_FRAGMENT) +def test_ontouml_has_valid_attributes(term: str) -> None: + """Verifies that OntoUML possesses the specified attributes. + + :param term: The attribute name to be checked. + :type term: str + """ + + assert hasattr(OntoUML, term) + + +@pytest.mark.parametrize("term", ALL_TERMS_STR) +def test_ontouml_term_excludes_invalid_variations(term: str) -> None: + """Assert that called_term doesn't match any of the undesired/invalid variations. + + :param term: A term from the ALL_TERMS_STR list, representing an OntoUML term. + :type term: str + :raises AssertionError: If called_term matches any pattern in the invalid_variations list. + """ + called_term = str(getattr(OntoUML, term)) + + # Creating a list of all the variations to be checked + invalid_variations = [ + OK_BASE_URI, + OK_BASE_URI + term.upper(), + OK_BASE_URI + " " + term, + " " + OK_BASE_URI + term, + OK_BASE_URI + term + " ", + NOK_BASE_URI + term, + "", + ] + + # Asserting that called_term is not any of the invalid variations + assert called_term not in invalid_variations + + +@pytest.mark.parametrize("term", ALL_TERMS_STR) +def test_ontouml_term_accessibility(term: str) -> None: + """Verify that each term from ALL_TERMS_STR in OntoUML can be accessed without exceptions and is not None. + + Using getattr, the function dynamically retrieves OntoUML class attributes using terms from ALL_TERMS_STR, ensuring: + - The accessed attribute is not None. + - No exceptions are raised during access. If an exception occurs, the function fails and provides an error message with + the exception’s details. + + :param term: A term from the ALL_TERMS_STR list, representing an OntoUML term to be accessed. + :type term: str + :raises pytest.fail: If accessing the term raises any Exception, detailing the exception in the error message. + """ + try: + result = getattr(OntoUML, term) + assert result is not None + except Exception as e: + pytest.fail(f"Unexpected error accessing OntoUML.Class: {str(e)}") + + +@pytest.mark.parametrize( + "input_string", ["", "abc", "123", "!!!", " ", "!1a", "ab12/#", "class", " Class", "Class ", "cLass", "CLASS"] +) +def test_invalid_ontouml_term_access(input_string: str) -> None: + """Ensure that attempting to access invalid OntoUML terms raises AttributeError. + + :param input_string: A string input intended to represent an invalid OntoUML term. + :type input_string: str + """ + with pytest.raises(AttributeError): + getattr(OntoUML, input_string) + + assert not (hasattr(OntoUML, input_string)) diff --git a/ontouml_vocabulary_lib/tests/test_ontouml/test_ontouml_get_namespace.py b/ontouml_vocabulary_lib/tests/test_ontouml/test_ontouml_get_namespace.py new file mode 100644 index 0000000..8e86c80 --- /dev/null +++ b/ontouml_vocabulary_lib/tests/test_ontouml/test_ontouml_get_namespace.py @@ -0,0 +1,46 @@ +"""Module for testing exclusively the method get_namespace()of the OntoUML class.""" +import pytest + +from ontouml_vocabulary_lib.ontouml import OntoUML +from ontouml_vocabulary_lib.tests.test_ontouml.fixtures_test_ontouml import OK_BASE_URI, NOK_BASE_URI, INVALID_INPUTS + + +def test_get_namespace_equals_ns_attribute_and_ok_base_uri() -> None: + """Ensure get_namespace returns the expected namespace URI. + + Validates that the OntoUML.get_namespace method returns a namespace URI equivalent to the '_NS' class attribute + and matches the predefined OK_BASE_URI. + """ + assert OntoUML.get_namespace() == (str(OntoUML._NS)) + assert OntoUML.get_namespace() == OK_BASE_URI + + +def test_get_namespace_is_not_none_and_differs_from_nok_base_uri() -> None: + """Verify get_namespace returns a valid namespace. + + Ensures the namespace returned by OntoUML.get_namespace is not None and does not equate to the undesired NOK_BASE_URI. + """ + assert OntoUML.get_namespace() is not None + assert OntoUML.get_namespace() != NOK_BASE_URI + + +def test_get_namespace_returns_string_type() -> None: + """Check the return type of get_namespace. + + Ensures the namespace URI returned by OntoUML.get_namespace is a string, validating its format for URI representation. + """ + assert isinstance(OntoUML.get_namespace(), str) + + +@pytest.mark.parametrize( + "in_arg", + INVALID_INPUTS, +) +def test_get_namespace_raises_typeerror_with_argument(in_arg) -> None: + """Confirm get_namespace raises TypeError when provided an argument. + + Tests various input types to ensure that the OntoUML.get_namespace method raises a TypeError when it is provided + with an argument, affirming its designed usage without parameters. + """ + with pytest.raises(TypeError): + OntoUML.get_namespace(in_arg) diff --git a/ontouml_vocabulary_lib/tests/test_ontouml/test_ontouml_get_term.py b/ontouml_vocabulary_lib/tests/test_ontouml/test_ontouml_get_term.py new file mode 100644 index 0000000..fa4c358 --- /dev/null +++ b/ontouml_vocabulary_lib/tests/test_ontouml/test_ontouml_get_term.py @@ -0,0 +1,86 @@ +"""Module for testing exclusively the method get_namespace of the OntoUML class.""" +import pytest +from rdflib import URIRef + +from ontouml_vocabulary_lib.ontouml import OntoUML +from ontouml_vocabulary_lib.ouexception import OUUnavailableTerm +from ontouml_vocabulary_lib.tests.test_ontouml.fixtures_test_ontouml import ALL_TERMS_FRAGMENT, ALL_TERMS_STR, \ + INVALID_INPUTS + + +@pytest.mark.parametrize( + "in_arg", + ALL_TERMS_FRAGMENT, +) +def test_string_representation_validity(in_arg) -> None: + """Test if the string representation of a retrieved OntoUML term is valid. + + This test ensures that when provided with a valid term fragment as input, the `get_term` method returns + a term whose string representation is found within the predefined list of all term strings (ALL_TERMS_STR). + + :param in_arg: A fragment of an OntoUML term used as input to retrieve the term. + :type in_arg: str + """ + assert str(OntoUML.get_term(in_arg)) in ALL_TERMS_STR + + +@pytest.mark.parametrize( + "in_arg", + ALL_TERMS_FRAGMENT, +) +def test_term_retrieval_inclusion(in_arg) -> None: + """Test if a retrieved OntoUML term is included in the list of all OntoUML terms. + + This test ensures that when provided with a valid term fragment as input, the `get_term` method returns + a term that is found within the predefined list of all OntoUML terms obtained through the `get_list_all` method. + + :param in_arg: A fragment of an OntoUML term used as input to retrieve the term. + :type in_arg: str + """ + assert OntoUML.get_term(in_arg) in OntoUML.get_list_all() + + +@pytest.mark.parametrize( + "in_arg", + ALL_TERMS_FRAGMENT, +) +def test_get_term_type(in_arg) -> None: + """Test if `get_term` returns instances of URIRef for valid term fragments. + + The test ensures that when provided with a valid term fragment as input, the `get_term` method: + - Does not return None. + - Returns an instance of `URIRef`. + + :param in_arg: A fragment of an OntoUML term. + :type in_arg: str + """ + term = OntoUML.get_term(in_arg) + assert term is not None + assert isinstance(term, URIRef) + + +@pytest.mark.parametrize( + "in_arg", + INVALID_INPUTS, +) +def test_get_term_with_invalid_inputs(in_arg) -> None: + """Test if `get_term` raises OUUnavailableTerm for invalid inputs. + + This test ensures that when provided with an invalid input (that does not correspond to any OntoUML term), + the `get_term` method raises an exception of type `OUUnavailableTerm`. + + :param in_arg: An invalid OntoUML term fragment. + :type in_arg: str + """ + with pytest.raises(OUUnavailableTerm): + OntoUML.get_term(in_arg) + + +def test_get_term_with_no_argument() -> None: + """Test if `get_term` raises a TypeError when called without arguments. + + This test ensures that when the `get_term` method is called without providing any input arguments, + it raises an exception of type `TypeError`. + """ + with pytest.raises(TypeError): + OntoUML.get_term() diff --git a/ontouml_vocabulary_lib/tests/test_ontouml/test_ontouml_list_all.py b/ontouml_vocabulary_lib/tests/test_ontouml/test_ontouml_list_all.py new file mode 100644 index 0000000..6bc6850 --- /dev/null +++ b/ontouml_vocabulary_lib/tests/test_ontouml/test_ontouml_list_all.py @@ -0,0 +1,83 @@ +"""Module for testing exclusively the method list_all of the OntoUML class.""" +import pytest +from rdflib import URIRef + +from ontouml_vocabulary_lib.ontouml import OntoUML +from ontouml_vocabulary_lib.tests.test_ontouml.fixtures_test_ontouml import ALL_TERMS_STR, OK_BASE_URI, NOK_BASE_URI, \ + INVALID_INPUTS + + +def test_get_list_all_method_valid_ontouml_attribute_existence() -> None: + """Verifies if the list_all method returns valid terms and that these terms have the expected type.""" + + list_terms = OntoUML.get_list_all() + + for term in list_terms: + assert hasattr(OntoUML, term) + + +@pytest.mark.parametrize("term", ALL_TERMS_STR) +def test_valid_term_in_ontouml_list(term: str) -> None: + """Verifies if a term exists in the list returned by OntoUML.get_list_all(). + + :param term: A term to be checked. + :type term: str + """ + list_terms = OntoUML.get_list_all() + + assert URIRef(term) in list_terms + + +def test_get_list_all_returns_correct_number_of_items() -> None: + """Verifies if OntoUML.get_list_all() returns the same number of items as both the class attribute number + and the length of ALL_TERMS_STR. + """ + assert len(dir(OntoUML)) == len(OntoUML.get_list_all()) + assert len(ALL_TERMS_STR) == len(OntoUML.get_list_all()) + + +def test_get_list_all_returns_valid_uriref_list() -> None: + """ + Validates that OntoUML.get_list_all() returns a valid list of URIRef objects. + + Ensures that: + - The return type from OntoUML.list_all() is a list. + - The returned list is not None. + - Every term in the list is an instance of URIRef. + - No term in the list is None. + """ + list_terms = OntoUML.get_list_all() + assert isinstance(list_terms, list) + assert list_terms is not None + + for term in list_terms: + assert isinstance(term, URIRef) + assert term is not None + + +def test_get_list_all_terms_include_correct_base_uri() -> None: + """Ensures each term in OntoUML.get_list_all() contains the correct base URI and avoids incorrect ones. + + Verifies that: + - Each term contains OK_BASE_URI. + - NOK_BASE_URI is not contained in any term. + """ + list_terms = OntoUML.get_list_all() + + for term in list_terms: + assert OK_BASE_URI in term + assert NOK_BASE_URI not in term + + +@pytest.mark.parametrize( + "in_arg", + INVALID_INPUTS, +) +def test_get_list_all_raises_typeerror_with_arguments(in_arg) -> None: + """Verifies that OntoUML.get_list_all() raises a TypeError when provided with an argument. + + :param in_arg: An argument to be passed to OntoUML.list_all(). + :type in_arg: Any + """ + with pytest.raises(TypeError): + OntoUML.get_list_all(in_arg)