使用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推理器,第二种是使用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());
}
}
}
使用弹丸:这会产生以下输出:
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的结果?
与其枚举域和范围,不如尝试询问属性是否具有给定的域或范围:
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: %sn", theClass);
if (prop.hasDomain(theClass) System.out.printf("DOMAIN: %sn", theClass);
}
我想你会发现,颗粒会像预期的那样报告范围和域。这是推理机工作方式的不同:jena的内置推理机是一个在rdf上工作的混合规则引擎,而pellet是一个OWL推理机。在实践中,这意味着推断出的三元组并不明显存在。
Pellet有一个常见问题解答,解释了其中的一些差异,以及如何提取所有推论。