Skip to content
Snippets Groups Projects
Commit a582ae25 authored by Aurélien Lamercerie's avatar Aurélien Lamercerie
Browse files

New classes: (ontology) Metadata and Element

parent a92f647f
Branches
No related tags found
No related merge requests found
......@@ -43,8 +43,8 @@ class Metrics:
methods = [
("class", "get_classes"),
("object_property", "get_object_properties"),
("data_property", "get_data_properties"),
("restriction", "get_restrictions"),
#("data_property", "get_data_properties"),
#("restriction", "get_restrictions"),
("individual", "get_individuals"),
#("annotation", "get_annotations")
]
......
......@@ -13,47 +13,8 @@ from rdflib import Graph, RDF, RDFS, OWL
from rdflib import URIRef, BNode
from rdflib.namespace import split_uri
#==============================================================================
# Classes: Element, NamedElement and BlankElement
#==============================================================================
class Element:
def __init__(self, reference, graph):
self.reference = reference
self.graph = graph
def properties(self):
"""Retrieve properties associated with the element."""
return list(self.graph.predicate_objects(subject=self.reference))
def name(self):
raise NotImplementedError("The method name() must be implemented by subclasses.")
class NamedElement(Element):
def __init__(self, uri, graph):
super().__init__(uri, graph)
self.uri = uri
def __str__(self):
return str(self.uri)
def name(self):
_, element_name = split_uri(self.uri)
return element_name
class BlankElement(Element):
def __init__(self, bnode, graph):
super().__init__(bnode, graph)
self.id = str(bnode)
def __str__(self):
return f"BNode: {self.id}"
def name(self):
return self.id
from ontology_metadata import Metadata
from ontology_element import Element, NamedElement, BlankElement
#==============================================================================
# Class: Ontology
......@@ -94,7 +55,30 @@ class Ontology:
#--------------------------------------------------------------------------
# Extracting Method(s)
# Metadata and Annotation Extraction
#--------------------------------------------------------------------------
def extract_metadata(self):
"""Extract metadata from the ontology and return a Metadata object."""
version_info = self._get_version_info()
namespaces = list(self.graph.namespaces())
return Metadata(version_info, namespaces)
def _get_version_info(self):
pass # TODO
return -1
def get_annotations(self):
"""Extract all annotation comments from the ontology."""
annotations = set()
for _, _, o in self.graph.triples((None, RDFS.label, None)):
annotations.add(str(o))
return annotations
#--------------------------------------------------------------------------
# Element Extraction
#--------------------------------------------------------------------------
def get_classes(self):
......@@ -109,10 +93,6 @@ class Ontology:
"""Extract all data properties from the ontology."""
return self._get_elements_of_type(OWL.DatatypeProperty)
def get_restrictions(self):
"""Extract all restrictons from the ontology."""
return self._get_elements_of_type(OWL.Restriction)
def get_individuals(self) -> list:
"""Extract all individuals from the ontology, including elements explicitly typed as owl:Individual,
owl:NamedIndividual or any class."""
......@@ -130,12 +110,20 @@ class Ontology:
return list(set(individuals)) # Ensuring uniqueness
def get_annotations(self):
"""Extract all annotation comments from the ontology."""
annotations = set()
for _, _, o in self.graph.triples((None, RDFS.label, None)):
annotations.add(str(o))
return annotations
#--------------------------------------------------------------------------
# Taxonomic Relation Extraction (Hierarchical Structure)
#--------------------------------------------------------------------------
def get_taxonomic_relations(self):
return []
#--------------------------------------------------------------------------
# Non-Taxonomic Relation Extraction
#--------------------------------------------------------------------------
def get_restrictions(self):
"""Extract all restrictons from the ontology."""
return self._get_elements_of_type(OWL.Restriction)
#--------------------------------------------------------------------------
......
#!/usr/bin/python3.10
# -*-coding:Utf-8 -*
#==============================================================================
# Ontology Elements
#------------------------------------------------------------------------------
# Represents various elements (Classes, Object Properties, Data Properties,
# Individuals) within an ontology.
#==============================================================================
from rdflib import URIRef, BNode
from rdflib.namespace import split_uri
class Element:
def __init__(self, reference, graph):
self.reference = reference
self.graph = graph
def properties(self):
"""Retrieve properties associated with the element."""
return list(self.graph.predicate_objects(subject=self.reference))
def name(self):
raise NotImplementedError("The method name() must be implemented by subclasses.")
class NamedElement(Element):
def __init__(self, uri, graph):
super().__init__(uri, graph)
self.uri = uri
def __str__(self):
return str(self.uri)
def name(self):
_, element_name = split_uri(self.uri)
return element_name
class BlankElement(Element):
def __init__(self, bnode, graph):
super().__init__(bnode, graph)
self.id = str(bnode)
def __str__(self):
return f"BNode: {self.id}"
def name(self):
return self.id
#!/usr/bin/python3.10
# -*-coding:Utf-8 -*
#==============================================================================
# Metadata Container
#------------------------------------------------------------------------------
# Holds metadata (Imports, Versions, Namespaces) from RDF/OWL ontologies.
#==============================================================================
class Metadata:
def __init__(self, version_info, namespaces):
"""Initialize with metadata information."""
self.version_info = version_info
self.namespaces = namespaces
# TODO: add methods to print or manage metadata, if needed.
......@@ -57,9 +57,10 @@ class TestOntology(unittest.TestCase):
self.assertIn("orbit", object_properties_names)
def test_get_data_properties(self):
data_properties_names = {dp.name() for dp in self.onto1.get_data_properties()}
self.assertEqual(len(data_properties_names), 0)
data_properties_names_1 = {dp.name() for dp in self.onto1.get_data_properties()}
data_properties_names_2 = {dp.name() for dp in self.onto2.get_data_properties()}
self.assertEqual(len(data_properties_names_1), 0)
self.assertEqual(len(data_properties_names_2), 0)
def test_get_restrictions(self):
restrictions = self.onto1.get_restrictions()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment