Skip to content

Commit

Permalink
HermiT is default reasoner. (#671)
Browse files Browse the repository at this point in the history
* HermiT is deafault reasoner.

Ontoconvert used to accept only FaCT++ because
owlready2.reasoning._apply_reasoning_results() was broken.
It added the inferred
rdfs:subClassOf relations as rdf:type relations.

* Moved tests for reasoner into separate file.

Created new tests for all three reasoners.
which check that the new relation is inferred.

* Added new ontology to test reasoning
  • Loading branch information
francescalb authored Nov 4, 2023
1 parent acc7b5d commit c50115b
Show file tree
Hide file tree
Showing 8 changed files with 316 additions and 127 deletions.
10 changes: 5 additions & 5 deletions ontopy/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -1230,15 +1230,15 @@ def rename_entities(
break

def sync_reasoner(
self, reasoner="FaCT++", include_imported=False, **kwargs
self, reasoner="HermiT", include_imported=False, **kwargs
):
"""Update current ontology by running the given reasoner.
Supported values for `reasoner` are 'Pellet', 'HermiT' and 'FaCT++'.
Supported values for `reasoner` are 'HermiT' (default), Pellet
and 'FaCT++'.
If `include_imported` is true, the reasoner will also reason
over imported ontologies. Note that this may be **very** slow
with Pellet and HermiT.
over imported ontologies. Note that this may be **very** slow.
Keyword arguments are passed to the underlying owlready2 function.
"""
Expand All @@ -1250,7 +1250,7 @@ def sync_reasoner(
sync = owlready2.sync_reasoner_hermit
else:
raise ValueError(
f"unknown reasoner '{reasoner}'. Supported reasoners "
f"Unknown reasoner '{reasoner}'. Supported reasoners "
"are 'Pellet', 'HermiT' and 'FaCT++'."
)

Expand Down
57 changes: 57 additions & 0 deletions tests/ontopy_tests/test_reasoner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from typing import TYPE_CHECKING
import pytest

if TYPE_CHECKING:
from pathlib import Path

from ontopy.ontology import Ontology


def test_sync_reasoner_hermit(repo_dir: "Path") -> None:
"""Test `ontopy:Ontology.sync_reasoner()` with HermiT."""
from ontopy import get_ontology

testonto = get_ontology(
repo_dir / "tests" / "testonto" / "testonto_for_reasoner.ttl"
).load()
assert testonto.Avocado
assert not isinstance(testonto.Avocado, testonto.NaturalDye)
testonto.sync_reasoner(reasoner="HermiT")
assert isinstance(testonto.Avocado, testonto.NaturalDye)


def test_sync_reasoner_pellet(repo_dir: "Path") -> None:
"""Test `ontopy:Ontology.sync_reasoner()` with Pellet"""
from ontopy import get_ontology

testonto = get_ontology(
repo_dir / "tests" / "testonto" / "testonto_for_reasoner.ttl"
).load()

assert testonto.Avocado
assert not isinstance(testonto.Avocado, testonto.NaturalDye)
testonto.sync_reasoner(reasoner="Pellet")
assert isinstance(testonto.Avocado, testonto.NaturalDye)


def test_sync_reasoner_fact(repo_dir: "Path") -> None:
"""Test `ontopy:Ontology.sync_reasoner()` with FaCT++"""
from ontopy import get_ontology

testonto = get_ontology(
repo_dir / "tests" / "testonto" / "testonto_for_reasoner.ttl"
).load()
assert testonto.Avocado
assert not isinstance(testonto.Avocado, testonto.NaturalDye)
testonto.sync_reasoner(reasoner="FaCT++")
assert isinstance(testonto.Avocado, testonto.NaturalDye)


def test_sync_reasoner_wrong(testonto: "Ontology") -> None:
"""Test `ontopy:Ontology.sync_reasoner()` with wrong reasoner."""
with pytest.raises(ValueError) as excinfo:
testonto.sync_reasoner(reasoner="NonExistingReasoner")
assert str(excinfo.value) == (
"Unknown reasoner 'NonExistingReasoner'. "
"Supported reasoners are 'Pellet', 'HermiT' and 'FaCT++'."
)
6 changes: 0 additions & 6 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,3 @@ class H2O(emmo.Molecule):
onto.sync_attributes(name_policy="uuid", name_prefix=name_prefix)
assert synced_uuid != water.name
assert water.name.startswith("onto_")


def test_sync_reasoner(testonto: "Ontology") -> None:
"""Test `ontopy:Ontology.sync_reasoner()`."""

testonto.sync_reasoner()
125 changes: 125 additions & 0 deletions tests/testonto/testonto_for_reasoner.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
@prefix : <http://emmo.info/testontology#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@base <http://emmo.info/testontology> .

<http://emmo.info/testontology> rdf:type owl:Ontology ;
owl:versionIRI <http://emmo.info/testonto/0.1.0/testontology> .

#################################################################
# Annotation properties
#################################################################

### http://www.w3.org/2004/02/skos/core#altLabel
skos:altLabel rdf:type owl:AnnotationProperty .


### http://www.w3.org/2004/02/skos/core#prefLabel
skos:prefLabel rdf:type owl:AnnotationProperty .


#################################################################
# Object Properties
#################################################################

### http://emmo.info/testontology#hasColour
:hasColour rdf:type owl:ObjectProperty ;
rdfs:domain :Dye ;
rdfs:range :Colour .


### http://emmo.info/testontology#hasNaturePart
:hasNaturePart rdf:type owl:ObjectProperty ;
rdfs:subPropertyOf :hasPart ;
rdfs:domain :Nature ;
rdfs:range :Nature .


### http://emmo.info/testontology#hasPart
:hasPart rdf:type owl:ObjectProperty .


#################################################################
# Classes
#################################################################

### http://emmo.info/testontology#Avocado
:Avocado rdf:type owl:Class ;
rdfs:subClassOf :EvergreenTree ,
[ rdf:type owl:Restriction ;
owl:onProperty :hasColour ;
owl:qualifiedCardinality "1"^^xsd:nonNegativeInteger ;
owl:onClass :Colour
] ;
skos:prefLabel "Avocado"@en .


### http://emmo.info/testontology#Colour
:Colour rdf:type owl:Class .


### http://emmo.info/testontology#DesiduousTree
:DesiduousTree rdf:type owl:Class ;
rdfs:subClassOf :Tree ;
skos:prefLabel "DesiduousTree"@en .


### http://emmo.info/testontology#Dye
:Dye rdf:type owl:Class .


### http://emmo.info/testontology#EngelmannSpruce
:EngelmannSpruce rdf:type owl:Class ;
rdfs:subClassOf :Spruce ;
skos:prefLabel "EngelmannSpruce"@en .


### http://emmo.info/testontology#EvergreenTree
:EvergreenTree rdf:type owl:Class ;
rdfs:subClassOf :Tree ;
skos:prefLabel "EvergreenTree"@en .


### http://emmo.info/testontology#NaturalDye
:NaturalDye rdf:type owl:Class ;
owl:equivalentClass [ owl:intersectionOf ( :Dye
:Nature
) ;
rdf:type owl:Class
] ;
skos:prefLabel "NaturalDye"@en .


### http://emmo.info/testontology#Nature
:Nature rdf:type owl:Class .


### http://emmo.info/testontology#NorwaySpruce
:NorwaySpruce rdf:type owl:Class ;
rdfs:subClassOf :Spruce ;
skos:prefLabel "NorwaySpruce"@en .


### http://emmo.info/testontology#ShingledHedgehogMushroom
:ShingledHedgehogMushroom rdf:type owl:Class ;
rdfs:subClassOf :NaturalDye ;
skos:prefLabel "ShingledHedgehogMushroom"@en .


### http://emmo.info/testontology#Spruce
:Spruce rdf:type owl:Class ;
rdfs:subClassOf :EvergreenTree ;
skos:prefLabel "Spruce"@en .


### http://emmo.info/testontology#Tree
:Tree rdf:type owl:Class ;
rdfs:subClassOf :Nature ;
skos:prefLabel "Tree"@en .


### Generated by the OWL API (version 4.5.9.2019-02-01T07:24:44Z) https://github.com/owlcs/owlapi
105 changes: 105 additions & 0 deletions tests/testonto/testonto_for_reasoner_inferred.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
@prefix : <http://emmo.info/testonto/0.1.0/testontology#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@base <http://emmo.info/testonto/0.1.0/testontology> .

<http://emmo.info/testonto/0.1.0/testontology> rdf:type owl:Ontology .

#################################################################
# Object Properties
#################################################################

### http://emmo.info/testontology#hasColour
<http://emmo.info/testontology#hasColour> rdf:type owl:ObjectProperty ;
rdfs:domain <http://emmo.info/testontology#Dye> ;
rdfs:range <http://emmo.info/testontology#Colour> .


### http://emmo.info/testontology#hasNaturePart
<http://emmo.info/testontology#hasNaturePart> rdf:type owl:ObjectProperty ;
rdfs:subPropertyOf <http://emmo.info/testontology#hasPart> ;
rdfs:domain <http://emmo.info/testontology#Nature> ;
rdfs:range <http://emmo.info/testontology#Nature> .


### http://emmo.info/testontology#hasPart
<http://emmo.info/testontology#hasPart> rdf:type owl:ObjectProperty .


#################################################################
# Classes
#################################################################

### http://emmo.info/testontology#Avocado
<http://emmo.info/testontology#Avocado> rdf:type owl:Class ;
rdfs:subClassOf <http://emmo.info/testontology#EvergreenTree> ,
<http://emmo.info/testontology#NaturalDye> ,
[ rdf:type owl:Restriction ;
owl:onProperty <http://emmo.info/testontology#hasColour> ;
owl:qualifiedCardinality "1"^^xsd:nonNegativeInteger ;
owl:onClass <http://emmo.info/testontology#Colour>
] .


### http://emmo.info/testontology#Colour
<http://emmo.info/testontology#Colour> rdf:type owl:Class .


### http://emmo.info/testontology#DesiduousTree
<http://emmo.info/testontology#DesiduousTree> rdf:type owl:Class ;
rdfs:subClassOf <http://emmo.info/testontology#Tree> .


### http://emmo.info/testontology#Dye
<http://emmo.info/testontology#Dye> rdf:type owl:Class .


### http://emmo.info/testontology#EngelmannSpruce
<http://emmo.info/testontology#EngelmannSpruce> rdf:type owl:Class ;
rdfs:subClassOf <http://emmo.info/testontology#Spruce> .


### http://emmo.info/testontology#EvergreenTree
<http://emmo.info/testontology#EvergreenTree> rdf:type owl:Class ;
rdfs:subClassOf <http://emmo.info/testontology#Tree> .


### http://emmo.info/testontology#NaturalDye
<http://emmo.info/testontology#NaturalDye> rdf:type owl:Class ;
owl:equivalentClass [ owl:intersectionOf ( <http://emmo.info/testontology#Dye>
<http://emmo.info/testontology#Nature>
) ;
rdf:type owl:Class
] ;
rdfs:subClassOf <http://emmo.info/testontology#Dye> ,
<http://emmo.info/testontology#Nature> .


### http://emmo.info/testontology#Nature
<http://emmo.info/testontology#Nature> rdf:type owl:Class .


### http://emmo.info/testontology#NorwaySpruce
<http://emmo.info/testontology#NorwaySpruce> rdf:type owl:Class ;
rdfs:subClassOf <http://emmo.info/testontology#Spruce> .


### http://emmo.info/testontology#ShingledHedgehogMushroom
<http://emmo.info/testontology#ShingledHedgehogMushroom> rdf:type owl:Class ;
rdfs:subClassOf <http://emmo.info/testontology#NaturalDye> .


### http://emmo.info/testontology#Spruce
<http://emmo.info/testontology#Spruce> rdf:type owl:Class ;
rdfs:subClassOf <http://emmo.info/testontology#EvergreenTree> .


### http://emmo.info/testontology#Tree
<http://emmo.info/testontology#Tree> rdf:type owl:Class ;
rdfs:subClassOf <http://emmo.info/testontology#Nature> .


### Generated by the OWL API (version 4.5.9.2019-02-01T07:24:44Z) https://github.com/owlcs/owlapi
Loading

0 comments on commit c50115b

Please sign in to comment.