Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting ranges of object property using Hermit Reasoner 1.4.1.513 fails #789

Closed
karanthpallavi opened this issue Sep 16, 2018 · 10 comments
Closed
Labels

Comments

@karanthpallavi
Copy link

Hello,
Have used the following code to get object property ranges,

for(String eachProp: propertyNames)
{
int instancesWithValueForProp = 0;
int[] NumberOfInstancesWithValueForPropForEachClass = new int[numOfClasses];
OWLObjectProperty prop = dataFactory.getOWLObjectProperty(IRI.create(base+eachProp));
Set subProperties = reasoner.getSubObjectProperties(prop).getFlattened();
System.out.println("For Property : "+prop);
for(int index = 0; index < setOfInstancesOfEachClass.size(); index++)
{
Set instancesOfClass = setOfInstancesOfEachClass.get(index);
for(OWLNamedIndividual indiv: instancesOfClass)
{
OWLClass classOfThisInstance = namesOfClasses.get(index);
System.out.println("Individual "+indiv+ " Class "+classOfThisInstance);
Set objectPropertyRanges = reasoner.getObjectPropertyRanges(prop).getFlattened();
for(OWLClass eachRangeClass : objectPropertyRanges)
{
System.out.println("Property "+prop + " Range "+eachRangeClass);
}
}
}
For property hasCast,
The domain is : Movie
The range is: allValuesFrom Cast

But, the result is:
Property http://www.semanticweb.org/drkm/ontologies/2018/5/untitled-ontology-42#hasCast Range owl:Thing

Kindly help,

@ignazio1977
Copy link
Contributor

Hi, can you add the ontology snippet that shows the declaration of hasCast?

@karanthpallavi
Copy link
Author

This is the part of inferred ontology file used as input for my application,

http://www.semanticweb.org/drkm/ontologies/2018/5/untitled-ontology-42#hasCast http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2002/07/owl#ObjectProperty .
http://www.semanticweb.org/drkm/ontologies/2018/5/untitled-ontology-42#hasCast http://www.w3.org/2000/01/rdf-schema#subPropertyOf http://www.w3.org/2002/07/owl#topObjectProperty .
http://www.semanticweb.org/drkm/ontologies/2018/5/untitled-ontology-42#hasCast http://www.w3.org/2002/07/owl#inverseOf _:genid1 .
_:genid1 http://www.w3.org/2002/07/owl#inverseOf http://www.semanticweb.org/drkm/ontologies/2018/5/untitled-ontology-42#hasCast .
http://www.semanticweb.org/drkm/ontologies/2018/5/untitled-ontology-42#hasCast http://www.w3.org/2000/01/rdf-schema#domain http://www.semanticweb.org/drkm/ontologies/2018/5/untitled-ontology-42#Movie .
http://www.semanticweb.org/drkm/ontologies/2018/5/untitled-ontology-42#hasCast http://www.w3.org/2000/01/rdf-schema#range _:genid2 .
_:genid2 http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2002/07/owl#Restriction .
_:genid2 http://www.w3.org/2002/07/owl#onProperty http://www.semanticweb.org/drkm/ontologies/2018/5/untitled-ontology-42#hasCast .
_:genid2 http://www.w3.org/2002/07/owl#allValuesFrom http://www.semanticweb.org/drkm/ontologies/2018/5/untitled-ontology-42#Cast .

This inferred file is generated by another application which uses OWLReasoner,
Original file before inference contains the declaration as:










@karanthpallavi
Copy link
Author

No description provided.

@ignazio1977
Copy link
Contributor

http://www.semanticweb.org/drkm/ontologies/2018/5/untitled-ontology-42#hasCast http://www.w3.org/2000/01/rdf-schema#range _:genid2 .
_:genid2 http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2002/07/owl#Restriction .

That's where the problem is. The range is an anonymous expression, but the reasoner cannot return non named expressions as results of its operations (this is part of the design of OWLReasoner. To access these you need to use the OWLOntology interface and do the selection manually. You can use the Searcher and EntitySearcher classes to simplify some of the coding.

@ignazio1977
Copy link
Contributor

note also that hasCast is declared to be the inverse of the inverse of hasCast. That's a tautology and can be removed from your input.

@karanthpallavi
Copy link
Author

The anonymous expressions were generated when i tried to infer the ontology file using OWLReasoner, After all inferencing, the saved inferred ontology file has all tautologies and anonymous expressions,
It was not manually added,
I think its better to use the original ontology to do current operations, Thanks much for your response
Reasoner.ReasonerFactory rf = new Reasoner.ReasonerFactory() {
@OverRide
public OWLReasoner createReasoner(OWLOntology ontology,
OWLReasonerConfiguration config) {
Configuration configuration = new Configuration();
configuration.ignoreUnsupportedDatatypes = true;
return super.createReasoner(ontology, configuration);
}
};

    Reasoner reasoner=(Reasoner)rf.createReasoner(ontology);
    boolean consistencyCheck = reasoner.isConsistent();
    if(consistencyCheck)
    {
        reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY, InferenceType.CLASS_ASSERTIONS, InferenceType.OBJECT_PROPERTY_HIERARCHY, InferenceType.DATA_PROPERTY_HIERARCHY, InferenceType.OBJECT_PROPERTY_ASSERTIONS);
    // We now have to decide which kinds of inferences we want to compute. For different types 
    // there are different InferredAxiomGenerator implementations available in the OWL API and 
    // we use the InferredSubClassAxiomGenerator and the InferredClassAssertionAxiomGenerator 
    // here. The different generators are added to a list that is then passed to an 
    // InferredOntologyGenerator. 
        List<InferredAxiomGenerator<? extends OWLAxiom>> generators=new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
        generators.add(new InferredSubClassAxiomGenerator());
        generators.add(new InferredClassAssertionAxiomGenerator());
        generators.add(new InferredDataPropertyCharacteristicAxiomGenerator());
generators.add(new InferredEquivalentClassAxiomGenerator());
generators.add(new InferredEquivalentDataPropertiesAxiomGenerator());
generators.add(new InferredEquivalentObjectPropertyAxiomGenerator());
generators.add(new InferredInverseObjectPropertiesAxiomGenerator());
generators.add(new InferredObjectPropertyCharacteristicAxiomGenerator());

// NOTE: InferredPropertyAssertionGenerator significantly slows down
// inference computation
generators.add(new org.semanticweb.owlapi.util.InferredPropertyAssertionGenerator());

generators.add(new InferredSubClassAxiomGenerator());
generators.add(new InferredSubDataPropertyAxiomGenerator());
generators.add(new InferredSubObjectPropertyAxiomGenerator());
List<InferredIndividualAxiomGenerator<? extends OWLIndividualAxiom>> individualAxioms= new ArrayList<InferredIndividualAxiomGenerator<? extends OWLIndividualAxiom>>();
	  generators.addAll(individualAxioms);
	  
    // We dynamically overwrite the default disjoint classes generator since it tries to 
    // encode the reasoning problem itself instead of using the appropriate methods in the 
    // reasoner. That bypasses all our optimisations and means there is not progress report :-( 
    // We don't want that!
    generators.add(new InferredDisjointClassesAxiomGenerator() {
        boolean precomputed=false;
        protected void addAxioms(OWLClass entity, OWLReasoner reasoner, OWLDataFactory dataFactory, Set<OWLDisjointClassesAxiom> result) {
            if (!precomputed) {
                reasoner.precomputeInferences(InferenceType.DISJOINT_CLASSES);
                precomputed=true;
            }
            for (OWLClass cls : reasoner.getDisjointClasses(entity).getFlattened()) {
                result.add(dataFactory.getOWLDisjointClassesAxiom(entity, cls));
            }
        }
    });
    // We can now create an instance of InferredOntologyGenerator. 
    InferredOntologyGenerator iog=new InferredOntologyGenerator(reasoner,generators);
    // Before we actually generate the axioms into an ontology, we first have to create that ontology. 
    // The manager creates the for now empty ontology for the inferred axioms for us. 
    OWLOntology inferredAxiomsOntology=manager.createOntology();
    OWLOntology inferredTriplesOntology = manager.createOntology();
    // Now we use the inferred ontology generator to fill the ontology. That might take some 
    // time since it involves possibly a lot of calls to the reasoner.    
    iog.fillOntology( df, inferredAxiomsOntology);
    // To add original axioms to the inferred ontology
    manager.addAxioms(inferredAxiomsOntology, ontology.getAxioms());
    iog.fillOntology(df,inferredTriplesOntology);
    manager.addAxioms(inferredTriplesOntology, ontology.getAxioms());
    // Now the axioms are computed and added to the ontology, but we still have to save 
    // the ontology into a file. Since we cannot write to relative files, we have to resolve the 
    // relative path to an absolute one in an OS independent form. We do this by (virtually) creating a 
    // file with a relative path from which we get the absolute file.  
    File inferredOntologyFile=new File(inferredFileName);
    if (!inferredOntologyFile.exists())
        inferredOntologyFile.createNewFile();
    inferredOntologyFile=inferredOntologyFile.getAbsoluteFile();
    
    File inferredTriplesFile = new File(inferredfileTriples);
    if(!inferredTriplesFile.exists())
        inferredTriplesFile.createNewFile();
    inferredTriplesFile=inferredTriplesFile.getAbsoluteFile();
    // Now we create a stream since the ontology manager can then write to that stream. 
    OutputStream outputStream=new FileOutputStream(inferredOntologyFile);
    // We use the same format as for the input ontology.
    manager.saveOntology(inferredAxiomsOntology, manager.getOntologyFormat(ontology), outputStream);
    OutputStream outputStreamTriples=new FileOutputStream(inferredTriplesFile);
    NTriplesDocumentFormat nt = new NTriplesDocumentFormat();
    manager.saveOntology(inferredAxiomsOntology,nt , outputStreamTriples);

@ignazio1977
Copy link
Contributor

There's a bug in HermiT about inverses - declaring an inverse with an anonymous expression, such as x inverseOf inverse(x) causes incorrect inferences. In this case, you're feeding the HermiT output back in, so these axioms are very common. I would avoid using InferredInverseObjectPropertiesAxiomGenerator while the bug gets sorted out. Very little of use is generated from that anyway.

Note that feeding the inferred axioms back in is not a very common practice and it doesn't work well with many reasoners. You end up doing the reasoning twice. See #786 for another discussion where similar issues were mentioned.

@karanthpallavi
Copy link
Author

karanthpallavi commented Sep 24, 2018 via email

@ignazio1977
Copy link
Contributor

Bug fixed in HermiT, will be available in the next release

@ignazio1977
Copy link
Contributor

owlcs/hermit-reasoner#3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants