'FILTER NOT EXISTS ' operator is not worki

2019-09-03 07:44发布

问题:

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 file structure

 Thing:
    |
    A 
    |_P(p1,p2)
      |_M(m1,m2) 
    |
    B

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 Code

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 Code:

<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.

回答1:

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.

This works without an inference model

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

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 .

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


回答2:

Are you running the latest release?

What do you mean by "blank resource"?

            + "SELECT ?lcs" 
            + "WHERE {"

will become

"SELECT ?lcsWHERE {" 

The part:

soln.getResource("Disease_Observation_List");

is null (no variable "Disease_Observation_List").



标签: sparql jena