使用 Jena,我正在尝试获取属性的域和范围。
让我们考虑以下本体
@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 .
我们可以看到,A1 是 P 的域,B1 是它的范围。根据 OWL 语义,我们可以推断 A 也是 P 的域,B 也是它的范围,请参见此处。
但是,将 Jena 与推理器一起使用并不总是给出预期的行为。我们区分两种情况,第一种是使用 Pellet reasoner,第二种是使用 OWL_DL_MEM_RULE_INF
代码
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());
}
}
}
使用 Pellet:这将提供以下输出:
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
使用 OWL_DL_MEM_RULE_INF: 这给出以下输出:
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
问题:
1-为什么会发生这种差异?
2-正确的结论是什么?
3- 有没有办法强制 Pellet 给出类似于 OWL_DL_MEM_RULE_INF 的结果?