-
Notifications
You must be signed in to change notification settings - Fork 315
Delete axioms from any ontology
ignazio1977 edited this page Nov 6, 2013
·
1 revision
Deleting axioms does not appear to be as intuitive as it should, so I have written a small example to try and make things clearer.
Let us suppose that we have two ontologies, koala
and country
, and koala
imports country
.
koala
has a class, University
, that we wish to be rid of. country
, too, has a class, Chunk
, that we do not wish to use. So, how do we go about removing both classes?
The following example will do it. Note: it does not save any file, so all changes are strictly in memory. For convenience and self containment, the ontologies are shortened to very small sizes and inlined in strings. The code works equally well with ontologies loaded from remote sites.
import java.util.HashSet;
import java.util.Set;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.StringDocumentSource;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyManager;
public class DeleteTest {
private final static String koala_owl = "Prefix(owl:=<http://www.w3.org/2002/07/owl#>)\n"
+ "Prefix(rdf:=<http://www.w3.org/1999/02/22-rdf-syntax-ns#>)\n"
+ "Prefix(xml:=<http://www.w3.org/XML/1998/namespace>)\n"
+ "Prefix(xsd:=<http://www.w3.org/2001/XMLSchema#>)\n"
+ "Prefix(rdfs:=<http://www.w3.org/2000/01/rdf-schema#>)\n"
+ "Ontology(<urn:koala.owl>\n"
+ "Import(<urn:country.owl>)\n"
+ "Declaration(Class(<urn:koala.owl#University>))\n"
+ "SubClassOf(<urn:koala.owl#TasmanianDevil> <urn:koala.owl#Marsupials>)\n"
+ "SubClassOf(<urn:koala.owl#University> <urn:koala.owl#Habitat>)\n"
+ "ObjectPropertyDomain(<urn:koala.owl#hasHabitat> <urn:koala.owl#Animal>)\n"
+ "ObjectPropertyRange(<urn:koala.owl#hasHabitat> <urn:koala.owl#Habitat>)\n"
+ "ClassAssertion(<urn:koala.owl#Degree> <urn:koala.owl#PhD>))";
private final static String country_owl = "Prefix(owl:=<http://www.w3.org/2002/07/owl#>)\n"
+ "Prefix(rdf:=<http://www.w3.org/1999/02/22-rdf-syntax-ns#>)\n"
+ "Prefix(xml:=<http://www.w3.org/XML/1998/namespace>)\n"
+ "Prefix(xsd:=<http://www.w3.org/2001/XMLSchema#>)\n"
+ "Prefix(rdfs:=<http://www.w3.org/2000/01/rdf-schema#>)\n"
+ "Ontology(<urn:country.owl>\n"
+ "Declaration(Class(<urn:country.owl#Chunk>))\n"
+ "Declaration(Class(<urn:country.owl#Fragment>))\n"
+ "Declaration(DataProperty(<urn:country.owl#size>))\n"
+ "Declaration(NamedIndividual(<urn:country.owl#Asinara>))\n"
+ "SubClassOf(<urn:country.owl#BoundaryFragment> <urn:country.owl#Fragment>)\n"
+ "EquivalentClasses(<urn:country.owl#Chunk> <urn:country.owl#Fragment>)\n"
+ "SubClassOf(<urn:country.owl#LandBoundaryFragment> <urn:country.owl#BoundaryFragment>)\n"
+ "ClassAssertion(<urn:country.owl#Country> <urn:country.owl#Asinara>))";
public static void main(String[] args) throws Exception {
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
// load the ontology to be imported
OWLOntology country = manager.loadOntologyFromOntologyDocument(new StringDocumentSource(country_owl));
// load the importing ontology
OWLOntology koala = manager.loadOntologyFromOntologyDocument(new StringDocumentSource(koala_owl));
// we wish to delete University from the importing ontology
OWLClass university = manager.getOWLDataFactory().getOWLClass(IRI.create("urn:koala.owl#University"));
// select all axioms that mention University
Set<OWLAxiom> axiomsToRemove = new HashSet<OWLAxiom>();
for (OWLAxiom ax : koala.getAxioms()) {
if (ax.getSignature().contains(university)) {
axiomsToRemove.add(ax);
System.out.println("to remove from " + koala.getOntologyID().getOntologyIRI() + ": " + ax);
}
}
System.out.println("Before: " + koala.getAxiomCount());
manager.removeAxioms(koala, axiomsToRemove);
System.out.println("After: " + koala.getAxiomCount());
// Now save your ontology, and no University will be mentioned in it
// Suppose the entity to remove is in the imported ontology...
// there is no getAxioms(include import closure)
OWLClass chunk = manager.getOWLDataFactory().getOWLClass(IRI.create("urn:country.owl#Chunk"));
// cycle over the imports closure: all the ontologies, including koala
// and all the imported ontologies, and the ontologies imported by the
// imported ontologies recursively
for (OWLOntology o : koala.getImportsClosure()) {
axiomsToRemove = new HashSet<OWLAxiom>();
for (OWLAxiom ax : o.getAxioms()) {
if (ax.getSignature().contains(chunk)) {
axiomsToRemove.add(ax);
System.out.println("to remove from " + o.getOntologyID().getOntologyIRI() + ": " + ax);
}
}
System.out.println("Before: " + o.getAxiomCount());
manager.removeAxioms(o, axiomsToRemove);
System.out.println("After: " + o.getAxiomCount());
}
//if you want to save the changes to the imported ontologies, you'll need to save each one individually
}
}
And the output for this code looks like this:
to remove from urn:koala.owl: SubClassOf(<urn:koala.owl#University> <urn:koala.owl#Habitat>)
to remove from urn:koala.owl: Declaration(Class(<urn:koala.owl#University>))
Before: 6
After: 4
to remove from urn:country.owl: Declaration(Class(<urn:country.owl#Chunk>))
to remove from urn:country.owl: EquivalentClasses(<urn:country.owl#Chunk> <urn:country.owl#Fragment> )
Before: 8
After: 6