-2

I would like to write this SPARQL query in Java using Jena:

prefix dbpediaont: <http://dbpedia.org/ontology/>
prefix dbpedia: <http://dbpedia.org/resource/>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

select ?resource where {
  dbpedia:Fred_Guy rdf:type ?resource
  filter strstarts(str(?resource), "http://dbpedia.org/ontology")
}

I'm using this code:

public QueryExecution query(){

        String stringa = "http://dbpedia.org/resource/Fred_Guy";

        ParameterizedSparqlString qs = new ParameterizedSparqlString( "" +
                "prefix dbpediaont: <http://dbpedia.org/ontology/>\n" +
                "prefix dbpedia: <http://dbpedia.org/resource/>\n" +
                "prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
                "\n" +  
                "select ?resource where {\n" +
                "?mat rdf:type ?resource\n" +
                "filter strstarts(str(?resource), http://dbpedia.org/ontology)\n" +
                "}" );


        Resource risorsa = ResourceFactory.createResource(stringa);
        qs.setParam( "mat", risorsa );

        //System.out.println( qs );

        QueryExecution exec = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", qs.asQuery() );


        ResultSet results = ResultSetFactory.copyResults( exec.execSelect() );

        while ( results.hasNext() ) {

            System.out.println( results.next().get( "resource" ));
        }

        // A simpler way of printing the results.
        ResultSetFormatter.out( results );

        return exec;

    }

I get this error:

Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Line 7, column 34: Unresolved prefixed name: http:
    at com.hp.hpl.jena.sparql.lang.ParserBase.throwParseException(ParserBase.java:661)
    at com.hp.hpl.jena.sparql.lang.ParserBase.resolvePName(ParserBase.java:274)
    at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.PrefixedName(SPARQLParser11.java:4892)
    at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.iri(SPARQLParser11.java:4872)
    at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.iriOrFunction(SPARQLParser11.java:4674)
    at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.PrimaryExpression(SPARQLParser11.java:3887)
    at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.UnaryExpression(SPARQLParser11.java:3802)
    at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.MultiplicativeExpression(SPARQLParser11.java:3669)
    at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.AdditiveExpression(SPARQLParser11.java:3567)
    at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.NumericExpression(SPARQLParser11.java:3560)
    at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.RelationalExpression(SPARQLParser11.java:3492)
    at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.ValueLogical(SPARQLParser11.java:3485)
    at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.ConditionalAndExpression(SPARQLParser11.java:3464)
    at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.ConditionalOrExpression(SPARQLParser11.java:3443)
    at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.Expression(SPARQLParser11.java:3436)
    at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.BuiltInCall(SPARQLParser11.java:4108)
    at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.Constraint(SPARQLParser11.java:2283)
    at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.Filter(SPARQLParser11.java:2211)
    at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.GraphPatternNotTriples(SPARQLParser11.java:1888)
    at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.GroupGraphPatternSub(SPARQLParser11.java:1765)
    at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.GroupGraphPattern(SPARQLParser11.java:1702)
    at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.WhereClause(SPARQLParser11.java:446)
    at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.SelectQuery(SPARQLParser11.java:134)
    at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.Query(SPARQLParser11.java:50)
    at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.QueryUnit(SPARQLParser11.java:41)
    at com.hp.hpl.jena.sparql.lang.ParserSPARQL11$1.exec(ParserSPARQL11.java:49)
    at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.perform(ParserSPARQL11.java:98)
    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:139)
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:79)
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:52)
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:40)
    at com.hp.hpl.jena.query.ParameterizedSparqlString.asQuery(ParameterizedSparqlString.java:1384)
    at MyPackage.Test.query(Test.java:769)

The error is on "http://dbpedia.org/ontology" in strstarts FILTER, because it because it must be between "", right? How I can write this code in Java? If I write "http://dbpedia.org/ontology" between "", the first " is seen from code as closing of the query.

What am I doing wrong here?

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Musich87
  • 562
  • 1
  • 12
  • 31
  • 1
    possible duplicate of [In Java, is there a way to write a string literal without having to escape quotes?](http://stackoverflow.com/questions/3034186/in-java-is-there-a-way-to-write-a-string-literal-without-having-to-escape-quote) – Joshua Taylor Jul 04 '14 at 17:38
  • 3
    This question doesn't have anything to do with strstarts, or jena, or sparql, or dbpedia. It's just about including double quotation marks in a string in Java, and it's as simple as `String foo = "John says, \"Just escape them with a slash.\"";` – Joshua Taylor Jul 04 '14 at 17:39

1 Answers1

-2

I have resolved my problem using this code:

public QueryExecution query(){

        String stringa = "http://dbpedia.org/resource/Fred_Guy";

        ParameterizedSparqlString qs = new ParameterizedSparqlString( "" +
                "prefix dbpediaont: <http://dbpedia.org/ontology/>\n" +
                "prefix dbpedia: <http://dbpedia.org/resource/>\n" +
                "prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
                "\n" +  
                "select ?resource where {\n" +
                "?mat rdf:type ?resource\n" +
                "filter strstarts(str(?resource), dbpediaont:)\n" +
                "}" );


        Resource risorsa = ResourceFactory.createResource(stringa);
        qs.setParam( "mat", risorsa );

        //System.out.println( qs );

        QueryExecution exec = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", qs.asQuery() );


        ResultSet results = ResultSetFactory.copyResults( exec.execSelect() );

        while ( results.hasNext() ) {

            System.out.println( results.next().get( "resource" ));
        }

        // A simpler way of printing the results.
        ResultSetFormatter.out( results );

        return exec;

    }

In particular, in FILTER as the second arguments, I have written the "dbpediaont" label.

This code, however, still produces an error:

Exception in thread "main" HttpException: 500
    at com.hp.hpl.jena.sparql.engine.http.HttpQuery.execGet(HttpQuery.java:340)
    at com.hp.hpl.jena.sparql.engine.http.HttpQuery.exec(HttpQuery.java:276)
    at com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP.execSelect(QueryEngineHTTP.java:345)

This error is due to the fact that the code need to write

dbpediaont:

with

str()

since it's an IRI, not a string:

filter strstarts(str(?resource), str(dbpediaont:))

The latter problem is reported on HttpException error when I call SPARQL query (on DBPedia) in Java Code

I hope this answer can help someone.

Community
  • 1
  • 1
Musich87
  • 562
  • 1
  • 12
  • 31
  • This is one way to do it. The other is `String query = "...\"inner string\"..."`. – Joshua Taylor Jul 04 '14 at 17:37
  • 2
    The other surprising thing here is that you've already shown that you're familiar with ParameterizedSparqlStrings, so you could have done `filter strstarts( str(?resource), ?prefix )` and `setParam("prefix", "http://…" );`. – Joshua Taylor Jul 04 '14 at 18:27
  • If this resolved your problem, then how come you have this [follow-on question](http://stackoverflow.com/q/24606539/1281433). If `strstarts(str(?resource),dbpediaont:)` doesn't work in that one, it's hard to see how it could work in this one. – Joshua Taylor Jul 07 '14 at 16:08
  • With this answer the above error is resolved, the link "http://stackoverflow.com/questions/24606539/httpexception-error-when-i-call-sparql-query-on-dbpedia-in-java-code" is another problem. Since "StackOverflow" advised me to open new posts for new questions, then I opened a new post. The answer solves that problem, so it does not deserve the negative vote. – Musich87 Jul 08 '14 at 07:07
  • You're right, in the sense that the code above doesn't produce the same error that's in the question. It still produces an error though, so it's rather unhelpful if anyone finds the question and tries to use this answer, **especially** since the answer doesn't suggest that any other problems exist with the code. – Joshua Taylor Jul 08 '14 at 13:01
  • While this solves your problem, it does so because you use a library to compose your query string. You are missing the actual problem and question which was noted on comments to your question. – Eric Tobias Jan 30 '15 at 14:57