I have two questions:
OWLReasonerFactory reasoner_factory = new ReasonerFactory();
OWLReasoner reasoner = reasoner_factory.createReasoner(ontology);
Which reasoner is called?
owl:topObjectProperty(a,b)
, which is true, but also redundant, and explodes the size of the KB.So how can I prevent this from happening?
The method I used is as follows:
public static Ontology materialization(Ontology ontology, File inferredOntologyFile) throws OWLOntologyStorageException, IOException, OWLOntologyCreationException {
Configuration config=new Configuration();
OntologyManager manager= OntManagers.createManager();
Ontology inf_ontology = manager.loadOntologyFromOntologyDocument(inferredOntologyFile);
OutputStream outputStream=new FileOutputStream(inferredOntologyFile);
OWLDataFactory data_factory = ontology.getOWLOntologyManager().getOWLDataFactory();
OWLReasonerFactory reasoner_factory = new ReasonerFactory();
OWLReasoner reasoner = reasoner_factory.createReasoner(ontology);
if (reasoner.isConsistent() == false) {
System.out.println("INCONSISTENT ONTOLOGY!");
}
InferredOntologyGenerator inference = new InferredOntologyGenerator(reasoner);
inference.fillOntology(data_factory, ontology);
manager.saveOntology(ontology, new NTriplesDocumentFormat(), outputStream);
return ontology;
}
owlapi-distribution version: 5.1.14 Hermit version: 1.3.8.510
Also a very simple test ontology after materialization is here: https://drive.google.com/file/d/1EKm8uD1zuMfIjstCAZMperZFFv4-kCtD/view?usp=sharing
or you can see it below:
<http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Ontology> .
<http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#c> <http://www.w3.org/2002/07/owl#topObjectProperty> <http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#c> .
<http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#c> <http://www.w3.org/2002/07/owl#topObjectProperty> <http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#b> .
<http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#c> <http://www.w3.org/2002/07/owl#topObjectProperty> <http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#a> .
<http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#c> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#c> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Thing> .
<http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#r> <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> <http://www.w3.org/2002/07/owl#topObjectProperty> .
<http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#r> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#a> <http://www.w3.org/2002/07/owl#topObjectProperty> <http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#b> .
<http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#a> <http://www.w3.org/2002/07/owl#topObjectProperty> <http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#a> .
<http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#a> <http://www.w3.org/2002/07/owl#topObjectProperty> <http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#c> .
<http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#a> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#a> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Thing> .
<http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#A> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://www.w3.org/2002/07/owl#Thing> .
<http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#A> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
<http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#b> <http://www.w3.org/2002/07/owl#topObjectProperty> <http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#c> .
<http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#b> <http://www.w3.org/2002/07/owl#topObjectProperty> <http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#b> .
<http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#b> <http://www.w3.org/2002/07/owl#topObjectProperty> <http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#a> .
<http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#b> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#b> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Thing> .
<http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#B> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://www.w3.org/2002/07/owl#Thing> .
<http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#B> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
<http://www.w3.org/2002/07/owl#topObjectProperty> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ReflexiveProperty> .
<http://www.w3.org/2002/07/owl#topObjectProperty> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#SymmetricProperty> .
<http://www.w3.org/2002/07/owl#topObjectProperty> <http://www.w3.org/2002/07/owl#inverseOf> <http://www.w3.org/2002/07/owl#topObjectProperty> .
<http://www.w3.org/2002/07/owl#topObjectProperty> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#q> <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> <http://www.w3.org/2002/07/owl#topObjectProperty> .
<http://www.semanticweb.org/savtr/ontologies/2023/6/untitled-ontology-9#q> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
Two items: I recommend updating the versions you're using because they are old, and it seems there is an issue in your input ontology.
Suggested version updates: use OWLAPI 5.1.20 and HermiT 1.4.5.519 (they don't directly affect this issue but they're more recent versions than the ones you're using.
Example Maven config:
<dependencies>
<dependency>
<groupId>net.sourceforge.owlapi</groupId>
<artifactId>owlapi-distribution</artifactId>
<version>5.1.20</version>
</dependency>
<dependency>
<groupId>net.sourceforge.owlapi</groupId>
<artifactId>org.semanticweb.hermit</artifactId>
<version>1.4.5.519</version>
<exclusions>
<exclusion>
<groupId>net.sourceforge.owlapi</groupId>
<artifactId>owlapi-distribution</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
Regarding your input ontology, if I feed it as is in the code you show (after amending to remove ONTAPI references, which are not relevant to the problem and in which I'm not an expert), I have the following code replicating the problem:
package teststack;
import java.io.*;
import org.semanticweb.HermiT.ReasonerFactory;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.formats.NTriplesDocumentFormat;
import org.semanticweb.owlapi.io.StringDocumentSource;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.reasoner.*;
import org.semanticweb.owlapi.util.*;
public class Check {
static OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
static OWLDataFactory data_factory = manager.getOWLDataFactory();
public static void main(String[] args)
throws OWLOntologyCreationException, OWLOntologyStorageException, IOException {
String notWorking="<urn:test:onto> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Ontology> .\n"
+ "<urn:test:onto#c> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .\n"
+ "<urn:test:onto#c> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Thing> .\n"
+ "<urn:test:onto#r> <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> <http://www.w3.org/2002/07/owl#topObjectProperty> .\n"
+ "<urn:test:onto#r> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .\n"
+ "<urn:test:onto#a> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .\n"
+ "<urn:test:onto#a> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Thing> .\n"
+ "<urn:test:onto#A> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://www.w3.org/2002/07/owl#Thing> .\n"
+ "<urn:test:onto#A> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .\n"
+ "<urn:test:onto#b> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .\n"
+ "<urn:test:onto#b> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Thing> .\n"
+ "<urn:test:onto#B> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://www.w3.org/2002/07/owl#Thing> .\n"
+ "<urn:test:onto#B> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .\n"
+ "<urn:test:onto#q> <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> <http://www.w3.org/2002/07/owl#topObjectProperty> .\n"
+ "<urn:test:onto#q> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .\n";
OWLOntology o = manager.loadOntologyFromOntologyDocument(new StringDocumentSource(
notWorking,
IRI.create("urn:test:onto"), new NTriplesDocumentFormat(), null));
materialization(o, new File("result.txt"));
}
public static OWLOntology materialization(OWLOntology ontology, File inferredOntologyFile)
throws OWLOntologyStorageException, IOException, OWLOntologyCreationException {
OutputStream outputStream = new FileOutputStream(inferredOntologyFile);
OWLReasonerFactory reasoner_factory = new ReasonerFactory();
OWLReasoner reasoner = reasoner_factory.createReasoner(ontology);
if (reasoner.isConsistent() == false) {
System.out.println("INCONSISTENT ONTOLOGY!");
}
InferredOntologyGenerator inference = new InferredOntologyGenerator(reasoner);
inference.fillOntology(data_factory, ontology);
manager.saveOntology(ontology, new NTriplesDocumentFormat(), outputStream);
return ontology;
}
}
Note: I've removed all axioms relating individuals with topObjectProperty from this ontology. These axioms are replicated by the code.
However, removing the type assertions that state the properties are subproperties of topObjectProperties, the problem goes away. As the declarations are redundant, it looks like them being in the ontology tricks the reasoner into generating those axioms. Such unnecessary declarations should be ignored, so this might be a bug. However, workaround for you is to filter out any declaration axioms where the type is topObjectProperty from your input file.
<urn:test:onto> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Ontology> .
<urn:test:onto#c> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<urn:test:onto#c> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Thing> .
<urn:test:onto#r> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<urn:test:onto#a> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<urn:test:onto#a> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Thing> .
<urn:test:onto#A> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://www.w3.org/2002/07/owl#Thing> .
<urn:test:onto#A> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
<urn:test:onto#b> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<urn:test:onto#b> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Thing> .
<urn:test:onto#B> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://www.w3.org/2002/07/owl#Thing> .
<urn:test:onto#B> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
<urn:test:onto#q> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .