使用strstarts过滤器以Java代码编写查询SPARQL [英] To write a query SPARQL in Java code using strstarts filter

查看:120
本文介绍了使用strstarts过滤器以Java代码编写查询SPARQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用耶拿(Jena)用Java编写此SPARQL查询:

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")
}

我正在使用以下代码:

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;

    }

我收到此错误:

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)

该错误出现在strstarts FILTER中的" http://dbpedia.org/ontology 上,因为它必须在"之间,对不对?如何用Java编写此代码?如果我在"之间写" http://dbpedia.org/ontology ",则从代码中可以看到第一个关闭查询.

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.

我在做什么错了?

推荐答案

我已使用以下代码解决了我的问题:

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;

    }

尤其是在FILTER中,作为第二个参数,我写了"dbpediaont"标签.

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:

dbpediaont:

str()

str()

因为它是一个IRI,而不是字符串:

since it's an IRI, not a string:

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

后一个问题报告在我希望这个答案可以对某人有所帮助.

I hope this answer can help someone.

这篇关于使用strstarts过滤器以Java代码编写查询SPARQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆