简体   繁体   中英

Jena; Domains and Ranges for a property differs upon selected Reasoner

Using Jena, I am trying to get domains and ranges for a property.

Let's consider the following ontology

 @prefix : <http://www.semanticweb.org/eng.medianhilal/ontologies/2015/2/untitled-ontology-82#> .
 @prefix owl: <http://www.w3.org/2002/07/owl#> .
 @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
 @prefix xml: <http://www.w3.org/XML/1998/namespace> .
 @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
 @base <http://www.semanticweb.org/eng.medianhilal/ontologies/2015/2/untitled-ontology-82> .

<http://www.semanticweb.org/eng.medianhilal/ontologies/2015/2/untitled-ontology-82> rdf:type owl:Ontology .

:P rdf:type owl:ObjectProperty ;   
   rdfs:domain :A1 ;   
   rdfs:range :B1 .

:A rdf:type owl:Class .

:A1 rdf:type owl:Class ;    
    rdfs:subClassOf :A .

:A2 rdf:type owl:Class ;    
    rdfs:subClassOf :A1 .

:B rdf:type owl:Class .

:B1 rdf:type owl:Class ;
    rdfs:subClassOf :B .

:B2 rdf:type owl:Class ;    
    rdfs:subClassOf :B1 .

As we can see, A1 is the domain of P and B1 is its range. According to OWL semantics we can infer that A is also a domain for P and B is also a range for it see here .

However, using Jena with a reasoner don't always give the expected behavior. Let's differentiate two situations, the first is using Pellet reasoner, and the second is using OWL_DL_MEM_RULE_INF

Code

import org.mindswap.pellet.jena.PelletReasonerFactory;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;

public class Test{
    public static void main (String [] args)
{                       

    OntModel ontModel = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
    /*OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RULE_INF);*/
    ontModel.read("path_to_ontology",  "RDF/XML");                              
    ontModel.setStrictMode(false); 


    String myNS = ontModel.getNsPrefixURI("");              

Resource r = ontModel.getResource(myNS + "P" );     
    OntProperty prop = (OntProperty) r.as( OntProperty.class);
    ExtendedIterator <OntClass> opDomains = (ExtendedIterator <OntClass>) prop.listDomain();                        
    while(opDomains.hasNext()){
        OntClass domain = opDomains.next();
        System.out.println("DOMAIN: " + domain.getURI());
    }       

    ExtendedIterator <OntClass> opRanges = (ExtendedIterator <OntClass>) prop.listRange();
    while(opRanges.hasNext()){
        OntClass ran = opRanges.next();
        System.out.println("RANGE: " + ran.getURI());
    }
}
}

Using Pellet: this gives the following output:

DOMAIN: http://www.semanticweb.org/eng.medianhilal/ontologies/2015/2/untitled-ontology-82#A1
RANGE: http://www.semanticweb.org/eng.medianhilal/ontologies/2015/2/untitled-ontology-82#B1

Using OWL_DL_MEM_RULE_INF: this gives the following output:

DOMAIN: http://www.w3.org/2002/07/owl#Thing
DOMAIN: http://www.semanticweb.org/eng.medianhilal/ontologies/2015/2/untitled-ontology-82#A1
DOMAIN: http://www.w3.org/2000/01/rdf-schema#Resource
DOMAIN: http://www.semanticweb.org/eng.medianhilal/ontologies/2015/2/untitled-ontology-82#A
RANGE: http://www.w3.org/2002/07/owl#Thing
RANGE: http://www.semanticweb.org/eng.medianhilal/ontologies/2015/2/untitled-ontology-82#B1
RANGE: http://www.w3.org/2000/01/rdf-schema#Resource
RANGE: http://www.semanticweb.org/eng.medianhilal/ontologies/2015/2/untitled-ontology-82#B   

Question:

1- Why is this difference happenning?

2- What are the correct conclusions?

3- Is there some way to enforce Pellet to give results similar to OWL_DL_MEM_RULE_INF?

Rather than enumerating domain and range try asking whether a property has a given domain or range:

import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
...
Collection<Resource> classes = Arrays.asList(
    createResource("http://www.semanticweb.org/eng.medianhilal/ontologies/2015/2/untitled-ontology-82#A"),
    createResource("http://www.semanticweb.org/eng.medianhilal/ontologies/2015/2/untitled-ontology-82#B")
);
...
for (Resource theClass: classes) {
    if (prop.hasRange(theClass) System.out.printf("RANGE: %s\n", theClass);
    if (prop.hasDomain(theClass) System.out.printf("DOMAIN: %s\n", theClass);
}

What I think you'll find is that pellet will report ranges and domains as expected. This is a difference in the way reasoners work: jena's built in reasoner is a hybrid rule engine working over rdf, whereas pellet is an OWL reasoner. In practice what this means is that the inferred triples are not obviously present.

Pellet has an FAQ which explains some of the differences, and how to extract all inferences.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM