【发布时间】:2014-07-01 18:23:26
【问题描述】:
这里我的 sparql 查询代码是查找 Least Common Subsumer,查询的输出必须是“A”。我在这里得到空白结果。我认为我的查询是正确的,但似乎 Apache Jena 的“过滤器不存在”不起作用。我还检查了其他示例。是否有任何“过滤器不存在”的替代解决方案,或者我需要为此修改我的 java 代码吗?
OWL 文件结构
Thing:
|
A
|_P(p1,p2)
|_M(m1,m2)
|
B
这里的 A,P,M 和 B 是概念。 p1,p2,m1 和 m2 是实例。 M 是 P 的子类,P 是 A 的子类。A 和 B 是 Thing 的子类。
JAVA 代码
import org.apache.jena.iri.impl.Main;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.reasoner.Reasoner;
import com.hp.hpl.jena.reasoner.ReasonerRegistry;
import com.hp.hpl.jena.util.FileManager;
public class SPARQLReasoner {
public static void main(String args[]) {
sparqlTest();
}
public static void sparqlTest() {
FileManager.get().addLocatorClassLoader(Main.class.getClassLoader());
Model model;
model = FileManager.get().loadModel("C:\\Users\\Chetan\\Desktop\\test.owl");
Reasoner reasoner=ReasonerRegistry.getOWLReasoner();
reasoner = reasoner.bindSchema(model);
InfModel infmodel = ModelFactory.createInfModel(reasoner, model);
String queryString;
queryString = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
+ "PREFIX owl: <http://www.w3.org/2002/07/owl#> "
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> "
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "
+ "PREFIX : <http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#> "
+"SELECT ?lcs WHERE{ "
+"?lcs ^(rdf:type/(rdfs:subClassOf)*) :p1 . "
+"?lcs ^(rdf:type/(rdfs:subClassOf)*) :m1 . "
+"?lcs rdf:type owl:Class . "
+"FILTER NOT EXISTS {?llcs ^(rdf:type/(rdfs:subClassOf)*) :p1 . "
+"?llcs ^(rdf:type/(rdfs:subClassOf)*) :m1 . "
+"?llcs rdf:type owl:Class . "
+"?llcs (rdfs:subClassOf)+ ?lcs . "
+"} "
+"}";
Query query= QueryFactory.create(queryString);
QueryExecution qexec= QueryExecutionFactory.create(query, infmodel);
try{
ResultSet results=qexec.execSelect();
while(results.hasNext()){
QuerySolution soln=results.nextSolution();
Resource r;
r=soln.getResource("lcs");
System.out.println(r);
}
}finally{
qexec.close();
}
}
}
OWL 代码:
<rdf:RDF xmlns="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#"
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.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11"/>
<owl:Class rdf:about="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#A"/>
<owl:Class rdf:about="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#B"/>
<owl:Class rdf:about="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#M">
<rdfs:subClassOf rdf:resource="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#P"/>
</owl:Class>
<owl:Class rdf:about="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#P">
<rdfs:subClassOf rdf:resource="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#A"/>
</owl:Class>
<owl:NamedIndividual rdf:about="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#m1">
<rdf:type rdf:resource="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#M"/>
</owl:NamedIndividual>
<owl:NamedIndividual rdf:about="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#m2">
<rdf:type rdf:resource="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#M"/>
</owl:NamedIndividual>
<owl:NamedIndividual rdf:about="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#p1">
<rdf:type rdf:resource="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#P"/>
</owl:NamedIndividual>
<owl:NamedIndividual rdf:about="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#p2">
<rdf:type rdf:resource="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#P"/>
</owl:NamedIndividual>
</rdf:RDF>
请在这里发表你的看法。
【问题讨论】:
-
您是否尝试过使用sparql.org/query-validator.html 验证查询?
-
我在我的查询中做了一些更改,并在我验证了它之后 sparql.org/query-validator.html。它显示正确。我刚刚更新了我的 sparql 代码
-
@Joshua 但是如果我想执行一些基于推理的查询,那么我不能使用 sparql 工具。
-
@Chetan 啊,我想我找到了(并添加了答案)。问题是当
?llcs = ?lcs = :P时,filter not exists { ?llcs rdfs:subClassOf+ ?lcs }对于原始模型和推理模型的行为不同。在原始模型中,没有三元组:P rdfs:subClassOf :P,但在推理模型中有这样一个三元组。请参阅我的答案以了解如何解决此问题。 -
@Joshua Taylor 我得到了预期的结果。谢谢你:)