Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

To write a query SPARQL in Java code using strstarts filter

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/>
" +
                "prefix dbpedia: <http://dbpedia.org/resource/>
" +
                "prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
" +
                "
" +  
                "select ?resource where {
" +
                "?mat rdf:type ?resource
" +
                "filter strstarts(str(?resource), http://dbpedia.org/ontology)
" +
                "}" );


        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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/>
" +
                "prefix dbpedia: <http://dbpedia.org/resource/>
" +
                "prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
" +
                "
" +  
                "select ?resource where {
" +
                "?mat rdf:type ?resource
" +
                "filter strstarts(str(?resource), dbpediaont:)
" +
                "}" );


        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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...