本文介绍了使用Jena API绑定SPARQL中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android上使用jena向DBpedia发送查询.这是我的查询:

I use jena for android to send a query to DBpedia. This is my query:

    protected Vector<List> doInBackground(String... keyword) {
                VecDBpedia=new Vector();
                vecurl= new ArrayList();
                 int i=0;
                 ResultSet results,re = null ;
                 QueryExecution exec = null ;
    try{           
              do{
               if(i!=VectorKeyWords.size())
                 {System.out.println(keyword[i]);
                String sparqlQuery=

                        "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
                            "PREFIX dbo:     <http://dbpedia.org/ontology/>\n" +
                            "PREFIX rdfs:   <http://www.w3.org/2000/01/rdf-schema#> \n" +
                            "prefix foaf: <http://xmlns.com/foaf/0.1/> \n"+
                            "PREFIX dbpprop: <http://dbpedia.org/property/>\n"+


                            "select distinct ?nbr ?Nom ?resource ?url where {\n" +
                            "?resource rdfs:label ?Nom.\n" +
                                "?resource foaf:isPrimaryTopicOf ?url.\n" +
                                 "?resource dbo:abstract ?resume.\n"+
                                "FILTER  langMatches( lang(?Nom), \"EN\") .\n" +
                                "FILTER langMatches( lang(?resume), \"EN\" )\n"+
                                "?Nom <bif:contains> \"Apple\".\n"+
                                "bind(strlen(replace(replace(Lcase(?resume), \"jobs\", \"_\"),\"[^_]\", \"\")) as ?nbr )\n"+
                                "filter (?nbr >= 1)\n"+
                                "}" ;

            String service=  "http://dbpedia.org/sparql";

            Query qur=QueryFactory.create(sparqlQuery,Syntax.syntaxARQ);
            System.out.print("sparqlQuery"+sparqlQuery);

             exec =QueryExecutionFactory.sparqlService(service,qur );
             System.out.print("qur"+qur);
              re =exec.execSelect();
              System.out.print("re"+re);

            i++;
            System.out.println(re.hasNext());}  

              } while(re.hasNext()==false && i<VectorKeyWords.size());
          if(i==VectorKeyWords.size())  //si aucun des mot clé ne se trouve dans DBpedia
     { System.out.println("Aucun mot clé ne correspond à une entrée en DBpedia");
     cancel(true);// A vérifier
     finish();}


     results = ResultSetFactory.copyResults( re );
     ResultSetFormatter.out( results );
     while ( results.hasNext() ) {
                ArrayList<String>   listDBpediaResource=new ArrayList();
                QuerySolution node=  results.next();

                RDFNode colonne1=node.get( "Nom" );
                RDFNode colonne2=node.get( "resource" );
                RDFNode colonne3=node.get( "url" );

                // RDFNode s2= results.next().get( "url" );
                 listDBpediaResource.add(colonne2.toString());
                 listDBpediaResource.add(colonne3.toString());
                 listDBpediaResource.add(colonne1.toString());

                 VecDBpedia.add(listDBpediaResource);
                 vecurl.add(colonne3.toString());//creer un vecteur contenant les url de chaque ressource condidat de dbpedia

                }


            }  catch (Exception  e) {
                 e.printStackTrace();
            }
            finally{exec.close();}

            return VecDBpedia;
        }
         ;

此查询可在SPARQL端点以及Java应用程序中运行,但不适用于android应用程序.

this query works at SPARQL endpoint, and also in a java application, but does'nt work in android application.

我在执行代码时收到此消息:

I get this message while executing the code:

W/System.err(8123): com.hp.hpl.jena.query.QueryParseException: Lexical error at line 13, column 5.  Encountered: "(" (40), after : "bind"
W/System.err(8123):     at com.hp.hpl.jena.sparql.lang.ParserARQ.perform(ParserARQ.java:95)
W/System.err(8123):at com.hp.hpl.jena.sparql.lang.ParserARQ.parse(ParserARQ.java:39)
W/System.err(8123):     at com.hp.hpl.jena.query.QueryFactory.parse(QueryFactory.java:129)
W/System.err(8123):     at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:72)
W/System.err(8123):     at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:43)
W/System.err(8123):     at com.example.testtvprg.MainActivity$AsyncDBpedia.doInBackground(MainActivity.java:426)
W/System.err(8123):     at com.example.testtvprg.MainActivity$AsyncDBpedia.doInBackground(MainActivity.java:1)
W/System.err(8123):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
W/System.err(8123):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
W/System.err(8123):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
 W/System.err(8123):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
W/System.err(8123):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
W/System.err(8123):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
W/System.err(8123):     at java.lang.Thread.run(Thread.java:856)

我认为这是耶拿或arq版本的问题,但我不知道如何纠正它.任何人都可以帮助我.谢谢

I think that it's a problem of jena or arq version, but i don't know how to correct it.Any one can help me please.Thanks

推荐答案

所有想帮助我的人都想一下.我最终决定更改查询的功能BIND,因为它不适用于QueryExecutionFactory.sparqlService也不适用于QueryEngineHttp.这是一个临时解决方案,因为可能是我需要具有相同问题的其他功能.如果有人知道解决方案,我将不胜感激.

Thinks to all those wanted to help me. I finally decide to change the function BIND of the query, because it doesn't work with QueryExecutionFactory.sparqlService neither with QueryEngineHttp. It's a temporary solution, because may be i would need to other functions that will have the same problem. If anyone knows a solution , i will be grateful.

这篇关于使用Jena API绑定SPARQL中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 22:52