ontouml_models_lib.model
The model module provides the Model class, a specialized extension of the QueryableElement class, designed for managing and interacting with individual ontology models within the OntoUML/UFO catalog.
This module facilitates the loading, querying, and management of RDF graphs and associated metadata for ontology models, ensuring compliance with the metadata schema specified in Appendix A.
Overview
The Model class represents a single ontology model, encapsulating its RDF graph and metadata, and provides methods for querying and interacting with this data. The metadata attributes, such as title, keyword, acronym, language, and others, are populated based on a YAML file and follow the standards defined in the OntoUML/UFO catalog’s metadata schema. The class is built to support operations on ontology models stored in RDF formats, such as Turtle, and accompanied by metadata in YAML format.
Usage
Example 1: Loading a Model and Accessing Metadata
>>> from ontouml_models_lib import Model
>>> model = Model('/path/to/ontology_model_folder')
>>> print(model.title)
# Output: "Example Ontology Title"
>>> print(model.keyword)
# Output: ["ontology", "example"]
Example 2: Executing a Query on the Model
>>> from ontouml_models_lib import Model
>>> from ontouml_models_lib import Query
>>> model = Model('/path/to/ontology_model_folder')
>>> query = Query('/path/to/query.sparql')
>>> results = model.execute_query(query, '/path/to/results')
>>> print(results)
# Output: [{'subject': 'ExampleSubject', 'predicate': 'ExamplePredicate', 'object': 'ExampleObject'}]
Dependencies
rdflib: For RDF graph operations and SPARQL query execution.
yaml: For parsing YAML files containing metadata.
loguru: For logging operations and debugging information.
References
For additional details on the OntoUML/UFO catalog, refer to the official OntoUML repository: https://github.com/OntoUML/ontouml-models
Classes
Represents an individual ontology model within the OntoUML/UFO catalog. |
Module Contents
- class ontouml_models_lib.model.Model(model_path)
Bases:
ontouml_models_lib.utils.queryable_element.QueryableElement
Represents an individual ontology model within the OntoUML/UFO catalog.
The Model class extends the QueryableElement class to manage and interact with RDF graphs representing ontology models. It provides methods for loading RDF graphs, extracting metadata from associated YAML files, and executing SPARQL queries. This class ensures that ontology data is consistently managed and that metadata attributes are easily accessible.
- Variables:
title (str) – The title of the ontology model, as determined by the dct:title property. There must be at most one title per language.
keyword (list[str]) – A list of keywords associated with the ontology model, aiding in the categorization and searchability of the model.
acronym (Optional[str]) – An optional acronym for the ontology model, providing a shorthand identifier.
source (Optional[str]) – The source or origin of the ontology model, typically a publication, organization, or project. It is recommended to use persistent and resolvable identifiers, such as DOIs or DBLP URIs, to refer to these resources.
language (Optional[str]) – The language in which the lexical labels of the ontology model are written. The use of values from the IANA Language Sub Tag Registry (e.g., “en”, “pt”) is required.
designedForTask (list[OntologyPurpose]) – A list of standardized purposes for which the ontology model was designed, categorized using the OntologyPurpose enumeration. Examples include Conceptual Clarification, Data Publication, and Decision Support Systems.
context (list[OntologyDevelopmentContext]) – The development context of the ontology model, classified under the OntologyDevelopmentContext enumeration. Examples include Research, Industry, and Classroom.
representationStyle (Optional[OntologyRepresentationStyle]) – The representation style of the ontology model, categorized under the OntologyRepresentationStyle enumeration. Examples include OntoumlStyle and UfoStyle.
ontologyType (Optional[OntologyType]) – The type of ontology, categorized under the OntologyType enumeration. Examples include Core, Domain, and Application.
theme (Optional[str]) – The central theme of the ontology model, identified according to a theme taxonomy such as the Library of Congress Classification (LCC).
contributor (Optional[str]) – An optional contributor to the ontology model, typically a person or organization that contributed to its development.
editorialNote (Optional[str]) – An optional editorial note providing additional context or comments regarding the ontology model.
issued (Optional[int]) – The year the ontology model was issued or published, represented as an integer.
landingPage (Optional[str]) – A URL representing the landing page or home page for the ontology model.
license (Optional[str]) – The license under which the ontology model is distributed. It is recommended to use a standard license identifier, such as those from SPDX (e.g., “CC-BY-4.0”).
modified (Optional[int]) – The year the ontology model was last modified, represented as an integer.
- Parameters:
model_path (Union[pathlib.Path, str])
Example:
>>> from ontouml_models_lib import Model >>> model = Model('/path/to/ontology_model_folder') >>> print(model.title) # Output: "Example Ontology Title" >>> print(model.keyword) # Output: ["ontology", "example"]
- acronym: str | None = None
- context: list[ontouml_models_lib.enumerations.OntologyDevelopmentContext] = []
- contributor: str | None = None
- designedForTask: list[ontouml_models_lib.enumerations.OntologyPurpose] = []
- editorialNote: str | None = None
- issued: int | None = None
- keyword: list[str] = []
- landingPage: str | None = None
- language: str | None = None
- license: str | None = None
- modified: int | None = None
- ontologyType: ontouml_models_lib.enumerations.OntologyType | None = None
- representationStyle: ontouml_models_lib.enumerations.OntologyRepresentationStyle | None = None
- source: str | None = None
- theme: str | None = None
- title: str = ''
- model_path: pathlib.Path
- path_model_graph
- path_metadata_graph
- path_metadata_yaml
- _compute_consistent_hash(graph)
Compute a consistent hash value for an RDFLib graph.
This method generates a SHA-256 hash for an RDFLib graph by first serializing it to a canonical format (N-Triples), sorting the serialized triples, and then encoding the sorted serialization to UTF-8. The resulting hash value is used to ensure consistency and integrity of the graph’s content.
- Parameters:
graph (Graph) – The RDFLib graph to be hashed.
- Returns:
The computed hash value for the RDFLib graph.
- Return type:
int
- _load_graph_safely(ontology_file)
Safely load an RDFLib graph from a file.
This method loads an RDFLib graph from a specified ontology file, ensuring that the file exists and is correctly parsed. It determines the file format based on its extension and returns the loaded graph.
- Parameters:
ontology_file (Path) – The path to the ontology file to be loaded.
- Returns:
The loaded RDFLib graph.
- Return type:
Graph
- Raises:
FileNotFoundError – If the ontology file does not exist.
OSError – If an error occurs during the parsing of the ontology file.
- _populate_attributes(yaml_file)
Populate the model’s attributes from a YAML metadata file.
This method reads a YAML file containing metadata and assigns the corresponding values to the model’s attrs. It handles enumerations by matching the string values in the YAML file to the appropriate enumeration members. The method supports both single-value and list-value attributes.
- Parameters:
yaml_file (Path) – The path to the YAML file containing the metadata.
- Raises:
FileNotFoundError – If the YAML metadata file does not exist.
ValueError – If an invalid value is encountered for an enumeration attribute.
- Return type:
None