' FILTER NOT EXISTS'运营商不与apache jena合作?

时间:2014-07-01 18:23:27

标签: sparql jena

这里我的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>

请在此提出您的意见。

2 个答案:

答案 0 :(得分:2)

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 )
  }

答案 1 :(得分:1)

您是否正在运行最新版本?

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

            + "SELECT ?lcs" 
            + "WHERE {"

将成为

"SELECT ?lcsWHERE {" 

部分:

soln.getResource("Disease_Observation_List");

为null(无变量“Disease_Observation_List”)。

相关问题