这里我的sparql查询代码是找到最小公共子类和查询的输出必须是'A'。我得到一个空白的结果。我认为我的查询是正确的,但似乎是Apache耶拿的"过滤器不存在"不工作。我也和其他的例子核对过了。是否有"过滤器不存在"的替代解决方案,或者我是否需要为此修改我的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代码/strong>
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();
}
}
}
猫头鹰代码:
<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>
filter not exists
工作正常
查询中的filter not exists
工作得很好。问题是推理模型中存在的数据比原始模型中存在的数据多,因此在原始模型中不存在的 事物比在推理模型中更多。
不需要推理模型
期望答案为:P
。如果您使用(稍作修改的)上一个问题的答案,如何使用SPARQL查询获得本体中最少公共子类?,您可以使用Jena的sparql
命令行工具获得预期的结果:
prefix : <http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?lcs where {
?lcs ^(rdf:type/rdfs:subClassOf*) :p1, :m1 ;
a owl:Class .
filter not exists {
?llcs ^(rdf:type/rdfs:subClassOf*) :p1, :m1 ;
a owl:Class ;
rdfs:subClassOf+ ?lcs .
}
}
-------
| lcs |
=======
| :P |
-------
这打破了推理模型
查询在非推理模型中工作,但在推理模型中中断,因此推理模型必须有一些使filter not exists
失败的额外数据。什么会改变?在filter not exists
中,我们检查没有匹配:
?llcs rdfs:subClassOf+ ?lcs .
在原始数据中,没有三重:P rdfs:subClassOf :P
,所以我们不从结果中过滤掉:P
。然而,对于推理模型,我们对每个类X都有x rdfs:subClassOf x
,因此我们有有:P rdfs:subClassOf :P
,因此您没有得到结果。这意味着你的永远不会得到的结果,所以你需要添加另一个过滤器来确保?llcs
和?lcs
不一样:
filter not exists {
?llcs ^(rdf:type/rdfs:subClassOf*) :p1, :m1 ;
a owl:Class ;
rdfs:subClassOf+ ?lcs .
filter ( ?llcs != ?lcs )
}
您运行的是最新版本吗?
"空白资源"是什么意思?
+ "SELECT ?lcs"
+ "WHERE {"
将成为
"SELECT ?lcsWHERE {"
:
soln.getResource("Disease_Observation_List");
为空(没有变量" disease - observation_list ")。