参数化SPARQL查询

时间:2017-03-23 14:10:52

标签: sparql jena apache-jena

我想在Java Jena中编写一个参数化的SPARQL查询,其中一个三重查询被注入

所以在下面的代码中我需要将值作为字符串注入到类

的传递中

但是,SPARQL查询是正确的,所以当我用类名替换“value”时,我得到了正确的结果

我尝试了两个非工作代码没有结果或运行时错误

第一个代码:

package ontology;

import org.apache.jena.iri.impl.Main;
import com.hp.hpl.jena.query.ParameterizedSparqlString;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.util.FileManager;

public class SPARQL {
     public static void sparqlTest( String str)
     {
FileManager.get().addLocatorClassLoader(Main.class.getClassLoader());
Model model=FileManager.get().loadModel("ASO.owl");
    String queryString=

                "PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>"+ 
                "PREFIX rdf:< http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+
                "PREFIX HASO:< http://www.semanticweb.org/rabaa006/ontologies/2014/4/HASO#>"+

                 "SELECT  ?x  "+

                 "WHERE"+ 

                 "      {?x rdfs:subClassOf  HASO:Affective_State}";


   ParameterizedSparqlString queryStr = new   ParameterizedSparqlString(queryString);
   queryStr.setLiteral("value", str);

   Query  query=QueryFactory.create(queryStr.toString());

   QueryExecution qexec = QueryExecutionFactory.create(query,model);

   try {
   ResultSet results = qexec.execSelect();

   while ( results.hasNext()){
           QuerySolution soln = results.nextSolution();
           String strg=soln.getResource("?x").toString();
           //System.out.println(strg);
             String number = strg.substring(strg.lastIndexOf("#") + 1);
             System.out.println(number);

   }}

   finally{
   qexec.close();}

}
}

第二个代码:

package ontology;

import org.apache.jena.iri.impl.Main;
import com.hp.hpl.jena.query.ParameterizedSparqlString;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.util.FileManager;

public class SPARQL {
public static void sparqlTest( String str)
     {

FileManager.get().addLocatorClassLoader(Main.class.getClassLoader());
Model model=FileManager.get().loadModel("ASO.owl");

   ParameterizedSparqlString pss = new ParameterizedSparqlString();
   pss.setCommandText (
                       "PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>"+ 

                       "PREFIX rdf:< http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+

                       "PREFIX HASO:< http://www.semanticweb.org/rabaa006/ontologies/2014/4/HASO#>"+


                 "SELECT  ?x  "+
                  "WHERE"+ 
    "      {?x rdfs:subClassOf  HASO:valuee}");
    pss.setLiteral("value", str);

   Query  query=QueryFactory.create(pss.toString());
   QueryExecution qexec = QueryExecutionFactory.create(query,model);

   try {
    ResultSet results = qexec.execSelect();

   while ( results.hasNext()){
           QuerySolution soln = results.nextSolution();
           String strg=soln.getResource("?x").toString();
           //System.out.println(strg);
             String number = strg.substring(strg.lastIndexOf("#") + 1);
             System.out.println(number);

   }}

  finally{

 qexec.close();}
 }
}

2 个答案:

答案 0 :(得分:0)

由于我没有使用SPARQL,已经有一段时间了,但我不知道你是如何在代码中实际注入值参数的。它应该是这样的:

   pss.setCommandText (
        "PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>"+ 
        "PREFIX rdf:< http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+
        "PREFIX HASO:< http://www.semanticweb.org/rabaa006/ontologies/2014/4/HASO#>"+


             "SELECT  ?x  "+
              "WHERE"+ 
                   "{?x HASO:value ?value}");
                    pss.setLiteral("value", str);

也许你会使用其他一些谓词,但为了注入价值,你的查询中应该有?值作为对象。

答案 1 :(得分:0)

使用setLitral()的问题是将String注入到由双引号括起来的查询中,如:

String queryString
            = "PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
            + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"
            + "PREFIX HASO: <http://www.semanticweb.org/rabaa006/ontologies/2014/4/HASO#>"
            + "SELECT ?x WHERE {\n"
            + "  ?x rdfs:subClassOf ?clazz \n"
            + "}\n";

    ParameterizedSparqlString queryStr = new ParameterizedSparqlString(queryString);

    queryStr.setLiteral("clazz", "HASO:Affective_State");

    System.out.println(queryStr.toString());

Query的结果如下:

PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX HASO: <http://www.semanticweb.org/rabaa006/ontologies/2014/4/HASO#>SELECT ?x WHERE {
  ?x rdfs:subClassOf "HASO:Affective_State" 
}

如上所示?x rdfs:subClassOf&#34; HASO:Affective_State&#34;其中&#34; HASO:Affective_State&#34; 。是错误的类名,因为它包含&#34;&#34;因此,如果执行该查询,则不会返回任何结果。 解决方案 使用String.format而不是ParameterizedSparqlString :: setLiteral:

String q
            = "PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
            + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"
            + "PREFIX HASO: <http://www.semanticweb.org/rabaa006/ontologies/2014/4/HASO#>"
            + "SELECT ?x WHERE {\n"
            + "  ?x rdfs:subClassOf %s \n"
            + "}\n"
            + "LIMIT %d";

    q = String.format(q, "HASO:Affective_State", 2);
    System.out.println(q);

结果:

PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX HASO: <http://www.semanticweb.org/rabaa006/ontologies/2014/4/HASO#>SELECT ?x WHERE {
  ?x rdfs:subClassOf HASO:Affective_State 
}
LIMIT 2

如上所示HASO:Affective_State和2已正确注入,因此您可以使用String.format注入尽可能多的参数[%s表示字符串,%d表示数字,依此类推..]

完整示例:

public static void main(String[] args) {

    JenaSystem.init();
    UpdateFactory.create();

    Model model = FileManager.get().loadModel("ASO.owl");

    String q
            = "PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
            + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"
            + "PREFIX HASO: <http://www.semanticweb.org/rabaa006/ontologies/2014/4/HASO#>"
            + "SELECT ?x WHERE {\n"
            + "  ?x rdfs:subClassOf %s \n"
            + "}\n"
            + "LIMIT %d";

    q = String.format(q, "HASO:Affective_State", 2);
    System.out.println(q);
    Query query = QueryFactory.create(q);
    QueryExecution qexec = QueryExecutionFactory.create(query, model);
    ResultSet resSet = qexec.execSelect();

    while (resSet.hasNext()) {

        QuerySolution soln = resSet.nextSolution();
        String strg = soln.getResource("?x").toString();
        System.out.println(">>>>>>> " + strg);
        String number = strg.substring(strg.lastIndexOf("#") + 1);
        System.out.println("<<<<<< " + number);

    }


}
相关问题