Annabelle
Annabelle

Reputation: 754

OWL API changing IRIs of entities in axioms

I am using OWL API. My question is how can I modify IRIs of all Entities in all OWLAxiom axioms in my OWLOntology read from file. For example I want to modify all "http://xxx" to "http://yyy" in all axioms in my ontology. The selected example axiom in this ontology is:

SubClassOf(<http://xxx#A> <http://xxx#B>)

I need to receive e.g.:

SubClassOf(<http://yyy#A> <http://yyy#B>)

My question especially implies to changing the default IRI, therefore, I tried to specify:

PrefixOWLOntologyFormat prefix = (PrefixOWLOntologyFormat) manager.getOntologyFormat(ontology);
prefix.setDefaultPrefix(...new...);

but it didn't change the IRIs in axioms.

Upvotes: 3

Views: 562

Answers (1)

Galigator
Galigator

Reputation: 10791

One solution is to use the OWLEntityRenamer class.

one basic usage is the following one :

OWLEntityRenamer renamer = new OWLEntityRenamer(manager, Collections.singleton(ontology));
Map<OWLEntity, IRI> entity2IRIMap = new HashMap<>();
...
ont.applyChanges(renamer.changeIRI(entity2IRIMap));

All individuals targeted by your Map<> will be rename. If you want to rename all yours individual by a pattern, you have to queries a complet list of your individuals to build the Map<>.

Here a complete small example using OWLEntityRenamer :

    final OWLOntologyManager m = OWLManager.createOWLOntologyManager();
    final OWLOntology o = m.createOntology();

    o.add(OWL.classAssertion(OWL.Individual("xxx:1"), OWL.Class("b:y")));
    o.add(OWL.classAssertion(OWL.Individual("xxx:2"), OWL.Class("b:y")));
    o.add(OWL.classAssertion(OWL.Individual("xxx:3"), OWL.Class("b:y")));
    o.individualsInSignature().forEach(System.out::println);

    final OWLEntityRenamer renamer = new OWLEntityRenamer(m, Collections.singleton(o));
    final Map<OWLEntity, IRI> entity2IRIMap = new HashMap<>();

    o.individualsInSignature().forEach(toRename ->
    {
        final IRI iri = toRename.getIRI();
        entity2IRIMap.put(toRename, IRI.create(iri.toString().replaceFirst("xxx", "yyy")));
    });

    o.applyChanges(renamer.changeIRI(entity2IRIMap));
    o.individualsInSignature().forEach(System.out::println);

Should print :

<xxx:3>
<xxx:2>
<xxx:1>
<yyy:3>
<yyy:2>
<yyy:1>

Upvotes: 4

Related Questions