-1

When I run this code in java I am getting error, I think the error occurred because of "String values". I am not sure about adding it but I got this idea from my previous question's answer which I asked in this site Query DBpedia to get abstract for different inputs

    public static void DbpediaResultSparql() { 
    String values = "New York";
    String service = "http://dbpedia.org/sparql";

    String sparqlQueryString2 = "PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+
           "PREFIX  dbpedia-owl: <http://dbpedia.org/ontology/>"+
           "PREFIX  dbpedia: <http://dbpedia.org/resource/>"+

               "SELECT DISTINCT  ?abstract"+
               "WHERE"+
                 "{ _:b0 rdfs:label ?name ."+
                   "_:b0 dbpedia-owl:abstract ?abstract"+
                   "FILTER langMatches(lang(?abstract), 'en')"+
                  "?name { " + values +" @en }"+
                "}" ;                 

    Query query = QueryFactory.create(sparqlQueryString2);
    ARQ.getContext().setTrue(ARQ.useSAX);
    // Executing SPARQL Query and pointing to the DBpedia SPARQL Endpoint
    QueryExecution qexec = QueryExecutionFactory.sparqlService(
            "http://DBpedia.org/sparql", query);
    // Retrieving the SPARQL Query results
    ResultSet results = qexec.execSelect();
    // Iterating over the SPARQL Query results
    while (results.hasNext()) {
        QuerySolution soln = results.nextSolution();
        // Printing DBpedia entries' abstract.
        System.out.println(soln.get("?abstract"));
    }
    qexec.close();
 }
Community
  • 1
  • 1
  • -1 This shows no debugging attempts at all. Printing the query before sending it off to DBpedia would have revealed a number of problems immediately. See [DBpedia Jena Query returning null](http://stackoverflow.com/q/15663510/1281433), which describes some of the problems in this code. – Joshua Taylor Sep 24 '14 at 01:30

1 Answers1

2

You're not going to get useful answers with code like

"SELECT DISTINCT  ?abstract"+ "WHERE"

"_:b0 dbpedia-owl:abstract ?abstract"+ "FILTER langMatches(lang(?abstract), 'en')"

because it turns into

SELECT DISTINCT ?abstractWHERE

_:b0 dbpedia-owl:abstract ?abstractFILTER

and you don't want variables named ?abstractWHERE or ?abstractFILTER.

This doesn't make any sense either:

String values = "New York";
"?name { " + values +" @en }"

You'd end up with

?name { New York@en }

I expect that what you wanted was

values ?name { "New York"@en }

I'd suggest you take a look into ParameterizedSparqlStrings, and be sure to put terminating newlines, or at least whitespace, in your code. If you had just printed out the query, you could drop it into sparql.org's query validator and you'd have seen the problem right away.

You can write the query like this:

select distinct ?abstract where {
  values ?name { "New York"@en }
  [ rdfs:label ?name ;
    dbpedia-owl:abstract ?abstract ]
  filter langMatches(lang(?abstract),'en')
}

SPARQL Results

If you only have the one value for the ?name and you're not selecting that variable, you can just write it in the query:

select distinct ?abstract where {
  [ rdfs:label "New York"@en ;
    dbpedia-owl:abstract ?abstract ]
  filter langMatches(lang(?abstract),'en')
}
Community
  • 1
  • 1
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • When I run this query it gives result but when I keep this query in java code its saying error: Syntax error, insert ";" to complete LocalVariableDeclarationStatement at line: values ?name { "New York"@en } – user2022416 Sep 24 '14 at 01:39
  • So I need some changes to my knowlage... :( – user2022416 Sep 24 '14 at 01:40
  • The query in your code does not have the text `values` in it; you just have `?name { New York@en }`. You need something like `"values ?name { \"" + values + "\" }"`. Those quotation marks are important. If you print your query before you send it to DBpedia, you can easily see where it's different from what you're typing in DBpedia's web interface. – Joshua Taylor Sep 24 '14 at 01:41
  • Ok got you let me try it... Thank you – user2022416 Sep 24 '14 at 01:56
  • Query is: PREFIX rdfs: PREFIX dbpedia-owl: PREFIX dbpedia: SELECT DISTINCT ?abstract WHERE { _:b0 rdfs:label \"" + New York + "\"@en . _:b0 dbpedia-owl:abstract ?abstract FILTER langMatches(lang(?abstract), "en") } – user2022416 Sep 24 '14 at 02:14
  • @user2022416 Code is unreadable in comments, especially when it contains URLs. I can't make sense of the previous comment, and what I can see doesn't look right at all. E.g., there shouldn't be semicolons between prefix declarations. At any rate, I'm glad this ended up working for you. – Joshua Taylor Sep 24 '14 at 02:24
  • There are no semicolons, I think in comment it got added but the query is working – user2022416 Sep 24 '14 at 02:55