我正在评估与模型 API相关的 Jena 查询功能,但我遇到了一个问题。首先,我测试有关限制的查询。实际上,Jena 是允许查询推断模型的可用 API 之一。此外,我需要从数据中拆分模式,因此,我使用 Protégé 创建了两个具有两个不同名称空间的单独 RDF 文件。
在第一个命名空间中http://www.test.com/schema#
,对于模式,有一个类:Woman
; 一个对象属性:hasSpouse
; 和一个对hasSpouse : Husband
.
<?xml version="1.0"?>
<!DOCTYPE rdf:RDF [
<!ENTITY owl "http://www.w3.org/2002/07/owl#" >
<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
<!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
]>
<rdf:RDF xmlns="http://www.test.com/schema#"
xml:base="http://www.test.com/schema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<!--<owl:Ontology rdf:about="http://www.test.com/schema"/>-->
<owl:ObjectProperty rdf:about="http://www.test.com/schema#hasSpouse">
<rdfs:range rdf:resource="http://www.test.com/schema#Woman"/>
</owl:ObjectProperty>
<owl:Class rdf:about="http://www.test.com/schema#Husband">
<owl:equivalentClass>
<owl:Restriction>
<owl:onProperty rdf:resource="http://www.test.com/schema#hasSpouse"/>
<owl:someValuesFrom rdf:resource="http://www.test.com/schema#Woman"/>
</owl:Restriction>
</owl:equivalentClass>
</owl:Class>
<owl:Class rdf:about="http://www.test.com/schema#Woman"/>
</rdf:RDF>
在第二个命名空间 中http://www.test.com/data#
,有两个个体:john
和janette
。janette
是Woman
和john
的 配偶 是janette
.
<?xml version="1.0"?>
<!DOCTYPE rdf:RDF [
<!ENTITY owl "http://www.w3.org/2002/07/owl#" >
<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
<!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
<!ENTITY schema "http://www.test.com/schema#">
]>
<rdf:RDF xmlns="http://www.test.com/data#"
xml:base="http://www.test.com/data#"
xmlns:schema="http://www.test.com/schema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<owl:NamedIndividual rdf:about="http://www.test.com/data#janette">
<rdf:type rdf:resource="http://www.test.com/schema#Woman"/>
</owl:NamedIndividual>
<owl:NamedIndividual rdf:about="http://www.test.com/data#john">
<schema:hasSpouse rdf:resource="http://www.test.com/data#janette"/>
</owl:NamedIndividual>
</rdf:RDF>
我的测试查询正在查找husband
数据中的每一个,我希望得到john
. 这是查询:
PREFIX schema: <http://www.test.com/schema#>
select ?subject where {?subject a schema:Husband}
使用下面的代码一切正常
System.out.println("QUERY ON LOADED RESTRICTION");
String path = "....";
Model schema = FileManager.get().loadModel("file:"+path+"married_schema_ns.xml");
schema.write(System.out, "RDF/XML-ABBREV");
Model data = FileManager.get().loadModel("file:"+path+"married_data_ns.xml");
data.write(System.out, "RDF/XML-ABBREV");
Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
reasoner = reasoner.bindSchema(schema);
InfModel inf_model = ModelFactory.createInfModel(reasoner, data);
String query_string = "PREFIX schema: <http://www.test.com/schema#>\r\n";
query_string += "select ?subject where {?subject a schema:Husband}";
Query query = QueryFactory.create(query_string);
query.serialize(new IndentedWriter(System.out));
QueryExecution execution = QueryExecutionFactory.create(query,model);
ResultSet results = execution.execSelect();
while (results.hasNext()) {
QuerySolution solution = results.nextSolution();
RDFNode node = solution.get("subject");
System.out.println("subject="+node);
}
System.out.println("END ....");
subject=http://www.test.com/data#john
系统按预期回复。我尝试从头开始构建完全相同的Model
内容,但之后查询不再起作用。
System.out.println("QUERY ON BUILT RESTRICTION");
OntModel ontology = ModelFactory.createOntologyModel();
String ns_ontology="http://www.test.com/schema#";
String pr_ontology = "schema";
ontology.setNsPrefix("", ns_ontology);
ObjectProperty has_spouse = ontology.createObjectProperty(ns_ontology+"hasSpouse");
OntClass woman = ontology.createClass(ns_ontology+"Woman");
has_spouse.setRange(woman);
OntClass husband = ontology.createClass(ns_ontology+"Husband");
SomeValuesFromRestriction restriction = ontology.createSomeValuesFromRestriction(null, has_spouse, woman);
husband.addEquivalentClass(restriction);
String ns_facts = "http://www.test.com/data#";
String pr_facts = "data";
OntModel facts = ModelFactory.createOntologyModel();
facts.setNsPrefix("", ns_facts);
facts.setNsPrefix(pr_ontology, ns_ontology);
Resource r = facts.getResource("http://www.w3.org/2002/07/owl#NamedIndividual");
Individual john = facts.createIndividual(ns_facts+"john",r);
Individual janette = facts.createIndividual(ns_facts+"janette",r);
janette.addProperty(RDF.type, woman);
john.addProperty(has_spouse, janette);
Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
reasoner.bindSchema(ontology);
Model inf_model = ModelFactory.createInfModel(reasoner, facts);
String query_string = "PREFIX schema: <"+ns_ontology+">\r\n";
query_string += "select ?subject where {?subject a schema:Husband}";
Query query = QueryFactory.create(query_string);
query.serialize(new IndentedWriter(System.out));
QueryExecution execution = QueryExecutionFactory.create(query,model);
ResultSet results = execution.execSelect();
while (results.hasNext()) {
QuerySolution solution = results.nextSolution();
RDFNode node = solution.get("subject");
System.out.println("subject="+node);
}
System.out.println("END ...");
我不明白为什么。两个版本的 RDF/XML-ABBREV 序列化完美匹配。
此外,当我在第一个版本中加载构建的模式/数据序列化时,查询再次起作用。
如果有人可以帮助我理解这一点!