Search code examples
owl-api

Replace current super class of an individual with the inferred superclass


Greetings Professor Ignazio Palmisano,

Kindly i have a question in regards to owl-API V5

Scenario:

I have an owl ontology that contains some individuals and some defined classes (has equivalent axioms).

i add a new individual "X" as a subclass of Thing, and i add the individual properties.

i initialise the reasoner and precomputeInferences()

Goal:

if the new individual "X" was classified under an existing defined class, i want to retrieve this inferred class and replace the current individual superclass Thing with the inferred superclass.

Main question:

1) Kindly what is the best way to do this?

Sub question:

2) do i have to save the new inferred ontology and then deal with it as asserted, in order to retrieve that class? i do not like to do this since my interest is only to replace the current individual superclass with the inferred superclass.

i was trying to find how to do this, i came across: EntitySearcher.getTypes(OWLIndividual, OWLOntology), however this retrieves only the original asserted superclass not the inferred.

Thanks for your time.

Sincere regards.


Solution

  • In order to replace the asserted class with the inferred one (or ones - there might be more than one), you need to do the following:

    • Retrieve the inferred types
    • Remove asserted types
    • Add inferred types

    So, called r the OWLReasoner instance, i the OWLIndividual, t its original type and T the inferred type:

    OWLOntology o = ...
    OWLReasoner r = ...
    // This returns the node of direct types - i.e., the set of equivalent classes that are the most specific named types including i among their instances
    // This will always be a Node even for a single class. If the reasoner can infer that there are equivalent classes, they all will appear in the Node
    Node<OWLClass> types = r.getTypes(i, true);
    
    // remove existing type assertions
    o.removeAxioms(o.getClassAssertionAxioms(i));
    
    // add the new ones
    OWLDataFactory df = ...
    Stream<OWLAxiom> axiomsToAdd = types.entities().map(T->df.getOWLClassAssertionAxiom(T, i));
    o.addAxioms(OWLAPIStreamUtils.asList(axiomsToAdd));
    

    Now o contains the new assertions in place of the old ones. Save or further elaborate the ontology as needed.