'FILTER NOT EXISTS'运算符不能与apache jena一起使用? [英] 'FILTER NOT EXISTS ' operator is not working with apache jena?

查看:81
本文介绍了'FILTER NOT EXISTS'运算符不能与apache jena一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我的sparql查询代码是找到最小公有使用者,查询的输出必须为'A'.我在这里得到空白结果.我认为我的查询是正确的,但似乎是Apache Jena的过滤器不存在"无法正常工作.我也检查了另一个例子.有没有过滤器不存在"的任何替代解决方案,还是我需要为此修改我的Java代码?

Here my sparql query code is to find Least Common Subsumer and output of query must be 'A'. I am getting blank result here. I think my query is right but seems to be Apache Jena's "filter not exists" is not working. I have also checked with other example. Is there any alternative solution of "filter not exists" or shall i need to modify my java code for that?

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的子类.

Here A,P,M and B are the concepts. p1,p2,m1 and m2 are the instances. M is subclass of P and P is subclass of A. A and B is the subclass of 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>

请在这里发表您的看法.

Please give your views here.

推荐答案

filter not exists正常工作

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

filter not exists works just fine

The filter not exists in your query is working just fine. The problem is that more data exists in the inference model than in the raw model, so there are more things that don't exist in the raw model than in the inference model.

期望的答案是:P.如果您使用上一个问题的(略作修改的)答案,请如何使用SPARQL查询获得本体中最少的普通使用者? ,您可以使用Jena的sparql命令行工具获得预期的结果:

The expected answer is :P. If you use the (slightly modified) answer from your previous question, How to get Least common subsumer in ontology using SPARQL Query?, you get the expected results using Jena's sparql command line tool:

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中,我们检查是否没有匹配项:

This breaks with an inference model

The query works with a non-inference model, but breaks with an inference model, so the inference model must have some additional data that makes the filter not exists fail. What could change? In the filter not exists, we check that there's no match for:

?llcs rdfs:subClassOf+ ?lcs .

在原始数据中,没有三元组:P rdfs:subClassOf :P,因此我们不会从结果中过滤出:P.但是,使用推理模型,每个类X都有x rdfs:subClassOf x,因此我们 do :P rdfs:subClassOf :P,所以您没有结果.这将意味着您永远不会获得结果,因此您需要添加另一个过滤器以确保?llcs?lcs不相同:

In the raw data, there's no triple :P rdfs:subClassOf :P, so we don't filter out :P from the results. However, with the inference model, we have x rdfs:subClassOf x for every class X, and thus we do have :P rdfs:subClassOf :P, so you get no results. This will mean that you never get a result, so you need to add another filter to make sure that ?llcs is not the same as ?lcs:

  filter not exists { 
    ?llcs ^(rdf:type/rdfs:subClassOf*) :p1, :m1 ;
          a owl:Class ;
          rdfs:subClassOf+ ?lcs .
    filter ( ?llcs != ?lcs )
  }

这篇关于'FILTER NOT EXISTS'运算符不能与apache jena一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆