1

I am trying to query the DBPedia using sparql from a javacode For some query it works perfectly anf for another one it doesn't work. I don't think that I have an error in my query because I already tested it in the DBPedia aparql endpoint. Here is my java code:

package ja1;

import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.util.*;

    public class Q_DBP_Online {

        public static void main(String[]args)
            {
                sparqlTest();
            }

    public static void sparqlTest()
       {             
         /*String queryString = "SELECT ?o WHERE {"+
                                "?s ?p ?o ."+
                                "} LIMIT 10";*/
         String str="Obama";
         String queryString = "PREFIX pr:<http://xmlns.com/foaf/0.1/>" +
                              "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+ 
                              "SELECT DISTINCT ?s ?label WHERE {" +                                        
                              "?s rdfs:label ?label . "+
                              "?s a pr:Person."+
                              "FILTER (lang(?label) = 'en'). "+
                              "?label bif:contains"+str+" ."+
                              "}";

         Query query = QueryFactory.create(queryString);        
         QueryExecution qexec =         QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);
         try
          {
            ResultSet results = qexec.execSelect();
            while(results.hasNext()){
           QuerySolution soln = results.nextSolution();
           //Literal name = soln.getLiteral("x");
           System.out.println(soln);
                }
             }
         finally{
              qexec.close();
          }

    }
}

So the first query which is commented runs perfectly and the second one doesn't run and I get this message in netbeans:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/rodwan/Desktop/Th_Pr/apache-jena-2.12.1/lib/slf4j-log4j12-1.7.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/rodwan/Desktop/Th_Pr/pellet-2.3.1/lib/jena/slf4j-log4j12-1.6.4.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]

Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " "(" "( "" at line 1, column 169.
Was expecting one of:
    "values" ...
    "graph" ...
    "optional" ...
    "minus" ...
    "bind" ...
    "service" ...
    "filter" ...
    "{" ...
    "}" ...
    ";" ...
    "," ...
    "." ...

    at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.perform(ParserSPARQL11.java:102)
    at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.parse$(ParserSPARQL11.java:53)
    at com.hp.hpl.jena.sparql.lang.SPARQLParser.parse(SPARQLParser.java:37)
    at com.hp.hpl.jena.query.QueryFactory.parse(QueryFactory.java:148)
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:80)
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:53)
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:41)
    at ja1.Q_DBP_Online.sparqlTest(Q_DBP_Online.java:38)
    at ja1.Q_DBP_Online.main(Q_DBP_Online.java:18)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

----------------------------------
ByteHamster
  • 4,884
  • 9
  • 38
  • 53
Rodwan Bakkar
  • 474
  • 3
  • 17

2 Answers2

4

I have tried your example, but it seems there are some missing spaces. The following was working for me on DBPedia:

PREFIX pr:<http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?s ?label WHERE {?s rdfs:label ?label . ?s a pr:Person . FILTER (lang(?label) = 'en') . ?label bif:contains "Obama" .}

The translation to Java would look like this:

String queryString = "PREFIX pr:<http://xmlns.com/foaf/0.1/>\n" +
                              "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"+
                              "SELECT DISTINCT ?s ?label WHERE {" +                              "?s rdfs:label ?label . "+
                              "?s a pr:Person . "+
                              "FILTER (lang(?label) = 'en') . "+
                              "?label bif:contains \""+str+"\" ."+
                              "}";

Hope this helps.

Gábor Bakos
  • 8,982
  • 52
  • 35
  • 52
  • That solved the problem :) Thank you very much. But I got a new error: Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Line 3, column 112: Unresolved prefixed name: bif:contains – Rodwan Bakkar Apr 12 '15 at 18:35
  • It is considering the bif as a namespace, I don't know why!! – Rodwan Bakkar Apr 12 '15 at 18:36
  • For your second problem http://stackoverflow.com/questions/15876916/bifcontain-error-occurs-when-try-to-query-dbpedia-through-my-java-program might give an answer. – Gábor Bakos Apr 12 '15 at 18:39
  • Thank you very much Mr. Gábor Bakos :) As this page directed me to see the link: http://answers.semanticweb.com/questions/22052/change-these-sparql-query-to-jena-codejava-to-make-http-request-to-dbpedia I found the solution which is just to use instead of bif:contains and it worked, I don't know why but it worked :) – Rodwan Bakkar Apr 12 '15 at 18:52
  • You need to post your answer here or close this question. – Artemis Apr 12 '15 at 18:58
  • Sorry, but you mean that I have to post my code here ? – Rodwan Bakkar Apr 12 '15 at 19:02
0

The problem is that the query parser strictly checks whether you have all used prefixed declared in the beginning of the query, and that's obviously not the case for bif:contains - which is in fact a built-in property of Virtuoso.

You already provided one solution with the usage of angle braces, i.e. <bif:contains> .

Another way would be to use the class QueryEngineHttp by something like

QueryEngineHttp qe = new QueryEngineHttp(queryString);

and just send the query string to the endpoint instead of parsing it into a Query object before.

UninformedUser
  • 8,397
  • 1
  • 14
  • 23