OWL API: Replacing entities in an OWLClassExpression (or OWLAxiom)

One of the problems when generating ontologies using the OWLAPI is that we might want to simply take an existing axiom or class expression as a template, and generate a new expression based on their structure, but with different classes or properties as fillers. For example, we might have an expression such as

A and R some (P some C)

wishing to rewrite this into

A and R some (P some D)

Handcrafting a function that does that can be extremely tedious; fortunately, the OWLAPI provides the OWLObjectDuplicator, which can be used for just that purpose. Here is an example of a method that takes in an OWLObject, for example, an OWLClassExpression or an OWLAxiom, and rewrites it while replacing one entity (replace) with another (replacement). The method is implemented using OWL API 4.5.1.

private OWLClassExpression replaceClassInClassExpression(OWLObject ce, OWLEntity replace, OWLEntity replacement) {
    Map<IRI, IRI> iriMap = new HashMap();
    iriMap.put(replace.getIRI(), replacement.getIRI()); // one or more of these at once
    OWLObjectDuplicator replacer = new OWLObjectDuplicator(m.getOWLDataFactory(), iriMap);
    return replacer.duplicateObject(ce);

It should be noted that the iriMap can take any number of IRI mappings, all of which would be applied during duplicateObject().

Be the first to comment

Leave a Reply

Your email address will not be published.