diff --git a/ontoScorer/ontology_axioms.py b/ontoScorer/ontology_axioms.py new file mode 100644 index 0000000000000000000000000000000000000000..f09efd25e3d7d4b54993664842dbcd3838050db3 --- /dev/null +++ b/ontoScorer/ontology_axioms.py @@ -0,0 +1,70 @@ +#!/usr/bin/python3.10 +# -*-coding:Utf-8 -* + +#============================================================================== +# Ontology Axioms +#------------------------------------------------------------------------------ +# Represents various logical axioms within an ontology, including restrictions, +# class equivalences, disjointness, and others. +#============================================================================== + +from rdflib import URIRef, BNode + +class Axiom: + def __str__(self) -> str: + """String representation of the axiom for display or debugging.""" + raise NotImplementedError("Subclasses must implement this method.") + +#-------------------------------------------------------------------------- +# Restriction Axioms +#-------------------------------------------------------------------------- +class RestrictionAxiom(Axiom): + def __init__(self, restriction_node: (URIRef, BNode)): + self.restriction_node = restriction_node + + def __str__(self): + return f"Restriction on: {self.restriction_node}" + + +class SomeValuesFromRestrictionAxiom(RestrictionAxiom): + def __init__(self, restriction_node: (URIRef, BNode), on_property: URIRef, some_values_from: (URIRef, BNode)): + super().__init__(restriction_node) + self.on_property = on_property + self.some_values_from = some_values_from + + def __str__(self): + return f"Restriction on: {self.restriction_node} via property: {self.on_property} with some values from: {self.some_values_from}" + + +# There are different kinds of restriction axioms, e.g., someValueFrom, allValuesFrom, etc. +# Each type can be implemented as a subclass of RestrictionAxiom if needed. + + +#-------------------------------------------------------------------------- +# Equivalence Axioms +#-------------------------------------------------------------------------- +class ClassEquivalenceAxiom(Axiom): + def __init__(self, classes: [URIRef]): + self.classes = classes + # Further methods to handle class equivalences can be added here. + + +#-------------------------------------------------------------------------- +# Disjointness Axioms +#-------------------------------------------------------------------------- +class ClassDisjointnessAxiom(Axiom): + def __init__(self, classes: [URIRef]): + self.classes = classes + # Further methods to handle class disjointness can be added here. + + +#-------------------------------------------------------------------------- +# Property Characteristics (e.g., Functional, InverseFunctional, Symmetric) +#-------------------------------------------------------------------------- +class PropertyCharacteristicAxiom(Axiom): + def __init__(self, property_ref: URIRef, characteristic_type: URIRef): + self.property_ref = property_ref + self.characteristic_type = characteristic_type + # Further methods to handle property characteristics can be added here. + +# Additional axioms, such as subproperty axioms, inverse properties, etc., can be added as needed. diff --git a/ontoScorer/ontology_nontaxonomic_relations.py b/ontoScorer/ontology_nontaxonomic_relations.py new file mode 100644 index 0000000000000000000000000000000000000000..874ca7225478bd94954d50e04bf12ebd8dc1aae8 --- /dev/null +++ b/ontoScorer/ontology_nontaxonomic_relations.py @@ -0,0 +1,58 @@ +#!/usr/bin/python3.10 +# -*-coding:Utf-8 -* + +#============================================================================== +# Ontology Non-Taxonomic Relations +#------------------------------------------------------------------------------ +# Represents non-taxonomic relations (object/data properties, domains, ranges, +# etc.) within an ontology. +#============================================================================== + +from rdflib import URIRef + +#-------------------------------------------------------------------------- +# Base Class for Non-Taxonomic Relations +#-------------------------------------------------------------------------- +class NonTaxonomicRelation: + def __init__(self, source, target, graph): + """ + Initialize a non-taxonomic relation between source and target entities. + + :param source: The starting entity (typically class or property). + :param target: The target entity (typically class or datatype). + :param graph: The RDF graph from which the relation is extracted. + """ + self.source = source + self.target = target + self.graph = graph + + def __str__(self): + return f"{self.source} -> {self.target}" + +#-------------------------------------------------------------------------- +# Object and Data Property Relations +#-------------------------------------------------------------------------- +class ObjectPropertyRelation(NonTaxonomicRelation): + """Represents an 'object property' relation.""" + def __str__(self): + return f"{self.source} has object property {self.target}" + +class DataPropertyRelation(NonTaxonomicRelation): + """Represents a 'data property' relation.""" + def __str__(self): + return f"{self.source} has data property {self.target}" + +#-------------------------------------------------------------------------- +# Domain and Range Relations +#-------------------------------------------------------------------------- +class DomainRelation(NonTaxonomicRelation): + """Represents a 'domain' relation.""" + def __str__(self): + return f"{self.source} has domain {self.target}" + +class RangeRelation(NonTaxonomicRelation): + """Represents a 'range' relation.""" + def __str__(self): + return f"{self.source} has range {self.target}" + +# Additional relations (like subproperties, inverse properties, etc.) can be added below as needed. diff --git a/ontoScorer/ontology_taxonomic_relations.py b/ontoScorer/ontology_taxonomic_relations.py new file mode 100644 index 0000000000000000000000000000000000000000..a8177eff188cbce431802f67d206aaceca17d818 --- /dev/null +++ b/ontoScorer/ontology_taxonomic_relations.py @@ -0,0 +1,56 @@ +#!/usr/bin/python3.10 +# -*-coding:Utf-8 -* + +#============================================================================== +# Ontology Taxonomic Relations +#------------------------------------------------------------------------------ +# Represents taxonomic relations (subclass, subproperty, instance of) +# within an ontology. +#============================================================================== + +from rdflib import URIRef + +#-------------------------------------------------------------------------- +# Base Class for Taxonomic Relations +#-------------------------------------------------------------------------- +class TaxonomicRelation: + def __init__(self, source, target, graph): + """ + Initialize a taxonomic relation between source and target entities. + + :param source: The starting entity (subclass/individual/property). + :param target: The target entity (superclass/class/property). + :param graph: The RDF graph from which the relation is extracted. + """ + self.source = source + self.target = target + self.graph = graph + + def __str__(self): + return f"{self.source} -> {self.target}" + +#-------------------------------------------------------------------------- +# Subclass Relations +#-------------------------------------------------------------------------- +class SubclassOfRelation(TaxonomicRelation): + """Represents a 'subclass of' relation.""" + def __str__(self): + return f"{self.source} is a subclass of {self.target}" + +#-------------------------------------------------------------------------- +# Subproperty Relations +#-------------------------------------------------------------------------- +class SubpropertyOfRelation(TaxonomicRelation): + """Represents a 'subproperty of' relation.""" + def __str__(self): + return f"{self.source} is a subproperty of {self.target}" + +#-------------------------------------------------------------------------- +# Instance Relations +#-------------------------------------------------------------------------- +class InstanceOfRelation(TaxonomicRelation): + """Represents an 'instance of' relation.""" + def __str__(self): + return f"{self.source} is an instance of {self.target}" + +# Additional taxonomic relations can be added below as needed. diff --git a/tests/test_metrics.py b/tests/test_metrics.py index 9dc65011609dc3e73c66cfba0a52ac1402f00440..61327bc3bf9840ebcd9f0d2d2a17ad6ca523f64f 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -13,6 +13,9 @@ from context import ontoScorer from ontoScorer.ontology import Ontology from ontoScorer.metrics import Metrics +#-------------------------------------------------------------------------- +# Metrics Test +#-------------------------------------------------------------------------- class TestMetrics(unittest.TestCase): def setUp(self): @@ -36,5 +39,8 @@ class TestMetrics(unittest.TestCase): self.metrics.print_scores() +#-------------------------------------------------------------------------- +# Main Unit Test Run +#-------------------------------------------------------------------------- if __name__ == "__main__": unittest.main() diff --git a/tests/test_ontology_submodules.py b/tests/test_ontology_submodules.py new file mode 100644 index 0000000000000000000000000000000000000000..d18896d2727b10231fedac97bf3b3e142365f356 --- /dev/null +++ b/tests/test_ontology_submodules.py @@ -0,0 +1,83 @@ +#!/usr/bin/python3.10 +# -*-coding:Utf-8 -* + +#============================================================================== +# test_ontology_modules: Ontology Modules Testing +#------------------------------------------------------------------------------ +# Contains tests for verifying functionality of the ontology submodules. +#============================================================================== + +import unittest +from rdflib import URIRef + +# Importing necessary modules from the ontoScorer package for testing. +from context import ontoScorer +from ontoScorer.ontology_axioms import * +from ontoScorer.ontology_nontaxonomic_relations import * +from ontoScorer.ontology_taxonomic_relations import * + +#-------------------------------------------------------------------------- +# Ontology Taxonomic Relations Test +#-------------------------------------------------------------------------- +class TestOntologyTaxonomicRelations(unittest.TestCase): + + def setUp(self): + # Any necessary setup can be done here... + pass + + def test_subclass_of_relation(self): + relation = SubclassOfRelation(URIRef("http://example.com#Subclass"), URIRef("http://example.com#Superclass"), None) + self.assertEqual(str(relation), "http://example.com#Subclass is a subclass of http://example.com#Superclass") + + # Additional test methods for other taxonomic relations can be added here... + + + +#-------------------------------------------------------------------------- +# Ontology Non-Taxonomic Relations Test +#-------------------------------------------------------------------------- +class TestOntologyNonTaxonomicRelations(unittest.TestCase): + + def setUp(self): + # Any necessary setup can be done here... + pass + + def test_object_property_relation(self): + relation = ObjectPropertyRelation(URIRef("http://example.com#Source"), URIRef("http://example.com#Target"), None) + self.assertEqual(str(relation), "http://example.com#Source has object property http://example.com#Target") + + # Additional test methods for other non-taxonomic relations can be added here... + + + +#-------------------------------------------------------------------------- +# Ontology Axioms Test +#-------------------------------------------------------------------------- +class TestOntologyAxioms(unittest.TestCase): + + def setUp(self): + # Any necessary setup can be done here... + pass + + def test_restriction_axiom(self): + axiom = RestrictionAxiom(URIRef("http://example.com#SomeClass")) + self.assertEqual(str(axiom), "Restriction on: http://example.com#SomeClass") + + def test_some_values_from_restriction(self): + restriction_node = BNode() + on_property = URIRef("http://example.org/hasPart") + some_values_from = URIRef("http://example.org/Hand") + restriction = SomeValuesFromRestrictionAxiom(restriction_node, on_property, some_values_from) + self.assertEqual(str(restriction), "Restriction on: {} via property: http://example.org/hasPart with some values from: http://example.org/Hand".format(restriction_node)) + + # Additional test methods for other axiom types can be added here... + + + + +#-------------------------------------------------------------------------- +# Main Unit Test Run +#-------------------------------------------------------------------------- +if __name__ == "__main__": + unittest.main() +