Question

I want to query an rdf file with sparql in jena. the rdf file is as follow:


<?xml version="1.0"?>
<rdf:RDF xmlns="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#"
     xml:base="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73"
     xmlns:swp2="http://semwebprogramming.org/2009/ont/chp2#"
     xmlns:untitled-ontology-73="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#"
     xmlns:protege="http://protege.stanford.edu/plugins/owl/protege#"
     xmlns:foaf="http://xmlns.com/foaf/0.1/"
     xmlns:xsp="http://www.owl-ontologies.com/2005/08/07/xsp.owl#"
     xmlns:ns66675="http://www.w3.org/2004/02/skos/core#"
     xmlns:cmp="http://ld.thomsonreuters.com/ContentMarketplace/2013-10-15/#"
     xmlns:ns36343="http://bioportal.bioontology.org/ontologies/umls/"
     xmlns:Ontology1395931632451="http://www.semanticweb.org/ontologies/2014/2/Ontology1395931632451.owl#"
     xmlns:ns20477="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:xml="http://www.w3.org/XML/1998/namespace"
     xmlns:ns28717="http://purl.bioontology.org/ontology/HL7/"
     xmlns:ns91299="http://www.w3.org/2002/07/owl#"
     xmlns:schema="http://schema.org/"
     xmlns:swrl="http://www.w3.org/2003/11/swrl#"
     xmlns:owl2xml="http://www.w3.org/2006/12/owl2-xml#"
     xmlns:dct="http://purl.org/dc/terms/"
     xmlns:admin="http://webn.net/mvcb/"
     xmlns:mds="http://ld.thomsonreuters.com/MetadataSystems/2013-10-15/#"
     xmlns:swrlb="http://www.w3.org/2003/11/swrlb#"
     xmlns:base="http://ld.thomsonreuters.com/MetadataSystems/2013-10-15/"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
    <ns91299:Ontology rdf:about="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73"/>



    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Object Properties
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->




    <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#knowns -->

    <ns91299:ObjectProperty rdf:about="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#knowns">
        <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#ReflexiveProperty"/>
        <ns20477:range rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person"/>
        <ns20477:domain rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person"/>
    </ns91299:ObjectProperty>



    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Data properties
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->




    <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#name -->

    <ns91299:DatatypeProperty rdf:about="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#name">
        <ns20477:domain rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person"/>
        <ns20477:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
    </ns91299:DatatypeProperty>



    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Classes
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->




    <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person -->

    <ns91299:Class rdf:about="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person">
        <ns20477:subClassOf>
            <ns91299:Restriction>
                <ns91299:onProperty rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#knowns"/>
                <ns91299:someValuesFrom rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person"/>
            </ns91299:Restriction>
        </ns20477:subClassOf>
    </ns91299:Class>



    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Individuals
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->




    <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#amina -->

    <ns91299:NamedIndividual rdf:about="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#amina">
        <rdf:type rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person"/>
        <name>amina</name>
        <knowns rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#george"/>
        <knowns rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#john"/>
    </ns91299:NamedIndividual>



    <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#george -->

    <ns91299:NamedIndividual rdf:about="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#george">
        <rdf:type rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person"/>
        <name>george</name>
        <knowns rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#amina"/>
        <knowns rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#nacira"/>
    </ns91299:NamedIndividual>



    <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#john -->

    <ns91299:NamedIndividual rdf:about="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#john">
        <rdf:type rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person"/>
        <name>john</name>
    </ns91299:NamedIndividual>



    <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#nacira -->

    <ns91299:NamedIndividual rdf:about="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#nacira">
        <rdf:type rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person"/>
        <name>nacira</name>
    </ns91299:NamedIndividual>
</rdf:RDF>



<!-- Generated by the OWL API (version 3.4.5-SNAPSHOT) http://owlapi.sourceforge.net -->

The java code is as follow:

package firstProject;

import java.util.Iterator;
//import com.hp.hpl.jena.ontology.OntModel;
//import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.util.FileManager;

public class main {
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    sparqltest();}

static void sparqltest()
{

    FileManager.get().addLocatorClassLoader(main.class.getClassLoader());
    Model model= FileManager.get().loadModel("C:/Users/acer/workspace/firstProject/src/firstProject/onto1.rdf");

String queryString=
       "PREFIX rdf:<http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#>"+
             "SELECT * WHERE {"+
      "?person foaf:name ?x. "+
          "?person foaf:knows ?person2."+
          "?person2 foaf:name ?y."+
       "FILTER( ?y = \"john\")"+
       "}";
Query query= QueryFactory.create(queryString);
QueryExecution qexec=QueryExecutionFactory.create(query, model);
try {
    ResultSet results = qexec.execSelect();while ( results.hasNext()){
        QuerySolution soln = results.nextSolution();
        Literal name = soln.getLiteral("x");
        System.out.println(name);
    }
} finally {
qexec.close();
    }}}

I can't execute this query beacause there is an a lot of error: log4j:WARN No appenders could be found for logger (org.apache.jena.riot.stream.JenaIOEnvironment). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

I hope someone will help me on this.

Thx?

Was it helpful?

Solution

In your RDF file, the knows property uses the default namespace, which is set to http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73# in that file's prefix declarations. In your query, however, you are using the property foaf:knows. In your example code this prefix is not declared in your query (and therefore the query is incorrect), but I see in the comments that you've added a prefix declaration for it later (tip: update your question).

So, you have properties http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#knows in your database, but you query for http://xmlns.com/foaf/0.1/knows. See the problem? Everything works fine, you're just querying for the wrong property.

On a more general note: simply copy-pasting code from the web is not really the best way to learn how to use these tools. I suggest you work your way through one of the Jena tutorials which teach you a bit more about the how and why. This will make your life a lot easier in the longer run.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top