【问题标题】:'FILTER NOT EXISTS ' operator is not working with apache jena?'FILTER NOT EXISTS' 运算符不使用 apache jena?
【发布时间】: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 我得到了预期的结果。谢谢你:)

标签: sparql jena


【解决方案1】:

filter not exists 工作正常

您查询中的filter not exists 工作正常。问题是推理模型中存在的数据多于原始模型中的数据,因此原始模型中不存在的事物比推理模型中的更多 .

这无需推理模型即可工作

预期的答案是:P。如果您使用上一个问题 (How to get Least common subsumer in ontology using SPARQL Query?) 中的(稍作修改)答案,您可以使用 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 )
  }

【讨论】:

    【解决方案2】:

    您运行的是最新版本吗?

    “空白资源”是什么意思?

                + "SELECT ?lcs" 
                + "WHERE {"
    

    会变成

    "SELECT ?lcsWHERE {" 
    

    部分:

    soln.getResource("Disease_Observation_List");
    

    为空(无变量“Disease_Observation_List”)。

    【讨论】:

    • 我正在使用 apache-jena-2.11.2.zip。我已根据您的建议修改了我的代码。结果仍然是空白。
    猜你喜欢
    • 2020-09-03
    • 2012-11-15
    • 2022-01-20
    • 2017-04-28
    • 2016-02-11
    • 1970-01-01
    • 2015-02-17
    • 2016-05-01
    • 2012-12-07
    相关资源
    最近更新 更多