Skip to content

Commit

Permalink
generalization.py implemented with tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
pedropaulofb committed Dec 3, 2023
1 parent ac42bdf commit 62b179e
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@

.. py:module:: ontouml_py.classes.concrete_classes.generalization
.. autoapi-nested-parse::

This module defines the `Generalization` class, a component of the ontouml_py library. The `Generalization` class
represents a generalization relationship in an ontological model, linking two classifiers in a hierarchy where one
(classifier) is a generalization of the other. This module includes the necessary validations to ensure the integrity
of the generalization relationship, such as preventing a classifier from being a generalization of itself.



Module Contents
---------------
Expand All @@ -20,28 +28,41 @@ Classes
.. py:class:: Generalization(**data)
Bases: :py:obj:`ontouml_py.classes.abstract_classes.namedelement.NamedElement`
Bases: :py:obj:`ontouml_py.classes.abstract_classes.modelelement.ModelElement`

Represent a generalization relationship between two classifiers in an ontological model.

This class extends `ModelElement` and is used to define a generalization, where one classifier (the general) is
a generalization of another classifier (the specific).

:ivar general: The general classifier in the generalization relationship.
:vartype general: Classifier
:ivar specific: The specific classifier in the generalization relationship.
:vartype specific: Classifier
:cvar model_config: Configuration settings for the Pydantic model.
:vartype model_config: Dict[str, Any]

.. py:attribute:: general
:type: ontouml_py.classes.abstract_classes.classifier.Classifier

An abstract class representing a named element within an OntoUML model, extending the OntoumlElement class.

This class provides functionality for managing named elements, including their preferred name, alternative names,
descriptions, editorial notes, as well as lists of creators and contributors.
.. py:attribute:: specific
:type: ontouml_py.classes.abstract_classes.classifier.Classifier

:ivar names: The preferred names of the element, represented as a list of LangStrings.
:vartype names: list[LangString]
:ivar alt_names: A list of alternative names for the element, each represented as a LangString object.
:vartype alt_names: list[LangString]
:ivar description: A LangString object representing the description of the element.
:vartype description: Optional[LangString]
:ivar editorial_notes: A list of LangString objects containing editorial notes associated with the element.
:vartype editorial_notes: list[LangString]
:ivar creators: A list of URIs represented as strings identifying the creators of the element.
:vartype creators: list[str]
:ivar contributors: A list of URIs represented as strings identifying the contributors to the element.
:vartype contributors: list[str]


.. py:attribute:: model_config
.. py:method:: ensure_irreflexive()
Validate that the generalization relationship is irreflexive.

Ensures that the 'general' and 'specific' classifiers are not the same, as a classifier cannot generalize itself.

:raises ValueError: If 'general' and 'specific' classifiers are the same.



Original file line number Diff line number Diff line change
Expand Up @@ -91,44 +91,67 @@

<section id="module-ontouml_py.classes.concrete_classes.generalization">
<span id="ontouml-py-classes-concrete-classes-generalization"></span><h1><a class="reference internal" href="#module-ontouml_py.classes.concrete_classes.generalization" title="ontouml_py.classes.concrete_classes.generalization"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ontouml_py.classes.concrete_classes.generalization</span></code></a><a class="headerlink" href="#module-ontouml_py.classes.concrete_classes.generalization" title="Link to this heading"></a></h1>
<p>This module defines the <cite>Generalization</cite> class, a component of the ontouml_py library. The <cite>Generalization</cite> class
represents a generalization relationship in an ontological model, linking two classifiers in a hierarchy where one
(classifier) is a generalization of the other. This module includes the necessary validations to ensure the integrity
of the generalization relationship, such as preventing a classifier from being a generalization of itself.</p>
<section id="module-contents">
<h2>Module Contents<a class="headerlink" href="#module-contents" title="Link to this heading"></a></h2>
<section id="classes">
<h3>Classes<a class="headerlink" href="#classes" title="Link to this heading"></a></h3>
<table class="autosummary longtable docutils align-default">
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="#ontouml_py.classes.concrete_classes.generalization.Generalization" title="ontouml_py.classes.concrete_classes.generalization.Generalization"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Generalization</span></code></a></p></td>
<td><p>An abstract class representing a named element within an OntoUML model, extending the OntoumlElement class.</p></td>
<td><p>Represent a generalization relationship between two classifiers in an ontological model.</p></td>
</tr>
</tbody>
</table>
<dl class="py class">
<dt class="sig sig-object py" id="ontouml_py.classes.concrete_classes.generalization.Generalization">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ontouml_py.classes.concrete_classes.generalization.</span></span><span class="sig-name descname"><span class="pre">Generalization</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">data</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ontouml_py.classes.concrete_classes.generalization.Generalization" title="Link to this definition"></a></dt>
<dd><p>Bases: <a class="reference internal" href="../../abstract_classes/namedelement/index.html#ontouml_py.classes.abstract_classes.namedelement.NamedElement" title="ontouml_py.classes.abstract_classes.namedelement.NamedElement"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ontouml_py.classes.abstract_classes.namedelement.NamedElement</span></code></a></p>
<p>An abstract class representing a named element within an OntoUML model, extending the OntoumlElement class.</p>
<p>This class provides functionality for managing named elements, including their preferred name, alternative names,
descriptions, editorial notes, as well as lists of creators and contributors.</p>
<dd><p>Bases: <a class="reference internal" href="../../abstract_classes/modelelement/index.html#ontouml_py.classes.abstract_classes.modelelement.ModelElement" title="ontouml_py.classes.abstract_classes.modelelement.ModelElement"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ontouml_py.classes.abstract_classes.modelelement.ModelElement</span></code></a></p>
<p>Represent a generalization relationship between two classifiers in an ontological model.</p>
<p>This class extends <cite>ModelElement</cite> and is used to define a generalization, where one classifier (the general) is
a generalization of another classifier (the specific).</p>
<dl class="field-list simple">
<dt class="field-odd">Variables<span class="colon">:</span></dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>names</strong> (<em>list</em><em>[</em><em>LangString</em><em>]</em>) – The preferred names of the element, represented as a list of LangStrings.</p></li>
<li><p><strong>alt_names</strong> (<em>list</em><em>[</em><em>LangString</em><em>]</em>) – A list of alternative names for the element, each represented as a LangString object.</p></li>
<li><p><strong>description</strong> (<em>Optional</em><em>[</em><em>LangString</em><em>]</em>) – A LangString object representing the description of the element.</p></li>
<li><p><strong>editorial_notes</strong> (<em>list</em><em>[</em><em>LangString</em><em>]</em>) – A list of LangString objects containing editorial notes associated with the element.</p></li>
<li><p><strong>creators</strong> (<em>list</em><em>[</em><em>str</em><em>]</em>) – A list of URIs represented as strings identifying the creators of the element.</p></li>
<li><p><strong>contributors</strong> (<em>list</em><em>[</em><em>str</em><em>]</em>) – A list of URIs represented as strings identifying the contributors to the element.</p></li>
<li><p><strong>general</strong> (<a class="reference internal" href="../../abstract_classes/classifier/index.html#ontouml_py.classes.abstract_classes.classifier.Classifier" title="ontouml_py.classes.abstract_classes.classifier.Classifier"><em>Classifier</em></a>) – The general classifier in the generalization relationship.</p></li>
<li><p><strong>specific</strong> (<a class="reference internal" href="../../abstract_classes/classifier/index.html#ontouml_py.classes.abstract_classes.classifier.Classifier" title="ontouml_py.classes.abstract_classes.classifier.Classifier"><em>Classifier</em></a>) – The specific classifier in the generalization relationship.</p></li>
<li><p><strong>model_config</strong> (<em>Dict</em><em>[</em><em>str</em><em>, </em><em>Any</em><em>]</em>) – Configuration settings for the Pydantic model.</p></li>
</ul>
</dd>
<dt class="field-even">Parameters<span class="colon">:</span></dt>
<dd class="field-even"><p><strong>data</strong> (<em>dict</em><em>[</em><em>str</em><em>, </em><em>Any</em><em>]</em>) – </p>
</dd>
</dl>
<dl class="py attribute">
<dt class="sig sig-object py" id="ontouml_py.classes.concrete_classes.generalization.Generalization.general">
<span class="sig-name descname"><span class="pre">general</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="../../abstract_classes/classifier/index.html#ontouml_py.classes.abstract_classes.classifier.Classifier" title="ontouml_py.classes.abstract_classes.classifier.Classifier"><span class="pre">ontouml_py.classes.abstract_classes.classifier.Classifier</span></a></em><a class="headerlink" href="#ontouml_py.classes.concrete_classes.generalization.Generalization.general" title="Link to this definition"></a></dt>
<dd></dd></dl>

<dl class="py attribute">
<dt class="sig sig-object py" id="ontouml_py.classes.concrete_classes.generalization.Generalization.specific">
<span class="sig-name descname"><span class="pre">specific</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="../../abstract_classes/classifier/index.html#ontouml_py.classes.abstract_classes.classifier.Classifier" title="ontouml_py.classes.abstract_classes.classifier.Classifier"><span class="pre">ontouml_py.classes.abstract_classes.classifier.Classifier</span></a></em><a class="headerlink" href="#ontouml_py.classes.concrete_classes.generalization.Generalization.specific" title="Link to this definition"></a></dt>
<dd></dd></dl>

<dl class="py attribute">
<dt class="sig sig-object py" id="ontouml_py.classes.concrete_classes.generalization.Generalization.model_config">
<span class="sig-name descname"><span class="pre">model_config</span></span><a class="headerlink" href="#ontouml_py.classes.concrete_classes.generalization.Generalization.model_config" title="Link to this definition"></a></dt>
<dd></dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="ontouml_py.classes.concrete_classes.generalization.Generalization.ensure_irreflexive">
<span class="sig-name descname"><span class="pre">ensure_irreflexive</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ontouml_py.classes.concrete_classes.generalization.Generalization.ensure_irreflexive" title="Link to this definition"></a></dt>
<dd><p>Validate that the generalization relationship is irreflexive.</p>
<p>Ensures that the ‘general’ and ‘specific’ classifiers are not the same, as a classifier cannot generalize itself.</p>
<dl class="field-list simple">
<dt class="field-odd">Raises<span class="colon">:</span></dt>
<dd class="field-odd"><p><strong>ValueError</strong> – If ‘general’ and ‘specific’ classifiers are the same.</p>
</dd>
</dl>
</dd></dl>

</dd></dl>

</section>
Expand Down
6 changes: 6 additions & 0 deletions docs/genindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ <h2 id="E">E</h2>
<li><a href="autoapi/ontouml_py/classes/concrete_classes/project/index.html#ontouml_py.classes.concrete_classes.project.Project.elements">elements (ontouml_py.classes.concrete_classes.project.Project property)</a>
</li>
<li><a href="autoapi/ontouml_py/classes/enumerations/propertystereotype/index.html#ontouml_py.classes.enumerations.propertystereotype.PropertyStereotype.END">END (ontouml_py.classes.enumerations.propertystereotype.PropertyStereotype attribute)</a>
</li>
<li><a href="autoapi/ontouml_py/classes/concrete_classes/generalization/index.html#ontouml_py.classes.concrete_classes.generalization.Generalization.ensure_irreflexive">ensure_irreflexive() (ontouml_py.classes.concrete_classes.generalization.Generalization method)</a>
</li>
<li><a href="autoapi/ontouml_py/classes/enumerations/ontouml_enum/index.html#ontouml_py.classes.enumerations.ontouml_enum.EnumABCMeta">EnumABCMeta (class in ontouml_py.classes.enumerations.ontouml_enum)</a>
</li>
Expand Down Expand Up @@ -305,6 +307,8 @@ <h2 id="F">F</h2>
<h2 id="G">G</h2>
<table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="autoapi/ontouml_py/classes/concrete_classes/generalization/index.html#ontouml_py.classes.concrete_classes.generalization.Generalization.general">general (ontouml_py.classes.concrete_classes.generalization.Generalization attribute)</a>
</li>
<li><a href="autoapi/ontouml_py/classes/concrete_classes/generalization/index.html#ontouml_py.classes.concrete_classes.generalization.Generalization">Generalization (class in ontouml_py.classes.concrete_classes.generalization)</a>
</li>
</ul></td>
Expand Down Expand Up @@ -906,6 +910,8 @@ <h2 id="S">S</h2>
<li><a href="autoapi/ontouml_py/classes/enumerations/ontologicalnature/index.html#ontouml_py.classes.enumerations.ontologicalnature.OntologicalNature.SITUATION_NATURE">SITUATION_NATURE (ontouml_py.classes.enumerations.ontologicalnature.OntologicalNature attribute)</a>
</li>
<li><a href="autoapi/ontouml_py/classes/concrete_classes/project/index.html#ontouml_py.classes.concrete_classes.project.Project.sources">sources (ontouml_py.classes.concrete_classes.project.Project attribute)</a>
</li>
<li><a href="autoapi/ontouml_py/classes/concrete_classes/generalization/index.html#ontouml_py.classes.concrete_classes.generalization.Generalization.specific">specific (ontouml_py.classes.concrete_classes.generalization.Generalization attribute)</a>
</li>
<li><a href="autoapi/ontouml_py/classes/abstract_classes/relation/index.html#ontouml_py.classes.abstract_classes.relation.Relation.stereotype">stereotype (ontouml_py.classes.abstract_classes.relation.Relation attribute)</a>

Expand Down
Binary file modified docs/objects.inv
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/searchindex.js

Large diffs are not rendered by default.

57 changes: 53 additions & 4 deletions ontouml_py/classes/concrete_classes/generalization.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,61 @@
from ontouml_py.classes.abstract_classes.namedelement import NamedElement
"""This module defines the `Generalization` class, a component of the ontouml_py library. The `Generalization` class
represents a generalization relationship in an ontological model, linking two classifiers in a hierarchy where one
(classifier) is a generalization of the other. This module includes the necessary validations to ensure the integrity
of the generalization relationship, such as preventing a classifier from being a generalization of itself.
"""
from typing import Any

from pydantic import Field, model_validator

class Generalization(NamedElement):
# Configuration settings for the Project model using Pydantic.
from ontouml_py.classes.abstract_classes.classifier import Classifier
from ontouml_py.classes.abstract_classes.modelelement import ModelElement


class Generalization(ModelElement):
"""Represent a generalization relationship between two classifiers in an ontological model.
This class extends `ModelElement` and is used to define a generalization, where one classifier (the general) is
a generalization of another classifier (the specific).
:ivar general: The general classifier in the generalization relationship.
:vartype general: Classifier
:ivar specific: The specific classifier in the generalization relationship.
:vartype specific: Classifier
:cvar model_config: Configuration settings for the Pydantic model.
:vartype model_config: Dict[str, Any]
"""

general: Classifier = Field()
specific: Classifier = Field()

# Pydantic's configuration settings for the class.
model_config = { # noqa (vulture)
"arbitrary_types_allowed": True,
"validate_assignment": True,
"validate_default": True,
"extra": "forbid",
"str_strip_whitespace": True,
"validate_default": True,
}

@model_validator(mode="after")
def ensure_irreflexive(self):
"""Validate that the generalization relationship is irreflexive.
Ensures that the 'general' and 'specific' classifiers are not the same, as a classifier cannot generalize itself.
:raises ValueError: If 'general' and 'specific' classifiers are the same.
"""
if self.general == self.specific:
raise ValueError("A generalization must relate different 'general' and 'specific' Classifiers.")

def __init__(self, **data: dict[str, Any]) -> None:
"""Initialize a new instance of the Generalization class.
This constructor initializes a Generalization instance with specified attributes. It ensures that the
generalization relationship is correctly established between the 'general' and 'specific' classifiers.
:param data: A dictionary containing the data to initialize the Generalization instance. Expected keys are
'general' and 'specific', corresponding to the classifiers involved in the generalization.
:type data: dict[str, Any]
"""
super().__init__(**data)
8 changes: 4 additions & 4 deletions tests/abstract_classes/test_decoratable.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self, **data):


@pytest.fixture
def default_decoratable():
def default_decoratable() -> Property:
"""Fixture to create a default instance of Property."""
return Property()

Expand All @@ -44,7 +44,7 @@ def test_decoratable_is_derived_attribute(default_decoratable: Property):
), "Setting 'is_derived' to True should be reflected in the Decoratable instance."


def test_decoratable_invalid_subclass_initialization():
def test_decoratable_invalid_subclass_initialization() -> None:
"""Test the initialization of a Decoratable instance with an invalid subclass.
:raises TypeError: If an invalid subclass is used to initialize a Decoratable instance.
Expand All @@ -53,7 +53,7 @@ def test_decoratable_invalid_subclass_initialization():
Decoratable() # Abstract class should not be instantiated directly


def test_decoratable_valid_subclass_initialization():
def test_decoratable_valid_subclass_initialization() -> None:
"""Test the initialization of a Decoratable instance with valid subclasses.
:return: None
Expand All @@ -64,7 +64,7 @@ def test_decoratable_valid_subclass_initialization():
assert isinstance(property_instance, Decoratable), "Property should be a valid subclass of Decoratable."


def test_decoratable_is_derived_mutability():
def test_decoratable_is_derived_mutability() -> None:
"""Test the mutability of the `is_derived` attribute in a `Decoratable` subclass.
This test verifies that the `is_derived` attribute of a `Decoratable` subclass can be changed after initialization,
Expand Down
Loading

0 comments on commit 62b179e

Please sign in to comment.