使用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也是它的范围see here。
然而,使用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());
}
}
}
使用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的结果?
答案 0 :(得分:0)
不是枚举域和范围,而是尝试询问属性是否具有给定的域或范围:
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);
}
我认为您会发现,pellet会按预期报告范围和域。这是reasoners工作方式的不同之处:jena的内置推理器是一个在rdf上工作的混合规则引擎,而pellet是一个OWL推理器。在实践中,这意味着推断的三元组显然不存在。
Pellet有an FAQ,它解释了一些差异,以及如何提取所有推论。