如何使用Java变量过滤SPARQL查询

时间:2016-11-02 11:23:46

标签: sparql jena semantic-web

我有测验游戏,用户必须回答以下问题:什么是德国首都城市名称?选项包括:柏林马德里伦敦数据在我的猫头鹰文件中。

我还有另一个本体论,其中我有rdfs:资源评论(测验问题的选项),如rdfs:柏林,伦敦的评论等。我想如果用户选择错误的选项,即伦敦,rdfs:评论权利选项在这种情况下,应该向用户显示柏林。

  The right (correct) option I have is in java variable 'correct' and if user click wrong option, I want to show the rdfs:comment of correct option as:

        "SELECT  * " +
                    " WHERE {   ?x rdfs:comment ?y   "  +  "FILTER regex( ?x ,'"+correct+"' ) " +

              "}";
        System.out.println(queryString);
                     Query query = QueryFactory.create(queryString);
            QueryExecution qexec = QueryExecutionFactory.create(query, model) ;
            ResultSet results = qexec.execSelect() ;
                while (results.hasNext())
            {
                QuerySolution binding = results.nextSolution();

                        Literal l=binding.getLiteral("y");
                        String s=l.toString();

        The problem is that it does not show me the filtered value rdfs:comment. When I remove Filter, it shows all rdfs:comment of resources but with Filter value, it simply does not work.

        When I print the query, it shows the options values but does not display the rdfs:comment of that resources. 

         PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> prefix dbr: <http://dbpedia.org/page/>PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT  *  WHERE {   ?x rdfs:comment ?y   FILTER regex( ?x ,"Berlin" ) }

        PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> prefix dbr: <http://dbpedia.org/page/>PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT  *  WHERE {   ?x rdfs:comment ?y   FILTER regex( ?x ,"Rome" ) }

    My owl for rdfs:comment is like: Berlin--> rdfs:comment
    Rome--> rdfs:comment etc

1 个答案:

答案 0 :(得分:2)

不确定您的数据结构,但我认为x是URI

要使过滤器正常工作,您应该这样做:

FILTER正则表达式( str(?x),“柏林”)