将列表中的值输入到 sparql 查询中

Enter values from a list into a sparql query

我有以下代码:

 public static List getSomeReceipes(List ing){
    List list = new ArrayList();

    String log4jConfPath = "C:/Users/Karen/workspace/Jena/src/Tutorial/log4j.properties";
    PropertyConfigurator.configure(log4jConfPath);
    String ingre = " ";

    try {
        //opening owl file
        Model model = ModelFactory.createDefaultModel();
        model.read(new FileInputStream("C:/Users/Karen/Desktop/Proyecto/bbdd.owl"), null, "TTL");
        //System.out.println(model);
        for(int i=0; i<ing.size(); i++){
            ingre = (String) ing.get(i);
            System.out.println(ingre);
        }
        //create a new query
                 String queryString
                ="PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
                +"PREFIX owl: <http://www.w3.org/2002/07/owl#>"
                +"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
                +"PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>"         
                +"PREFIX rec:<http://www.receta.org#>"
                +"SELECT reduced ?r WHERE { "
                +"  ?x rdf:type rec:Receta . "
                +"  ?x rdfs:label ?r."
                +"  filter not exists {"
                +"    ?x rec:Ingrediente ?i"
                +"    filter( ?i not in (rec:" + ing + "))"
                +"}"
                +"}";
        com.hp.hpl.jena.query.Query q = QueryFactory.create(queryString);

        //execute the query and obtain results

        QueryExecution qe = QueryExecutionFactory.create(q, model);
        ResultSet results = qe.execSelect();

        //print query results
        while (results.hasNext()) {
            //System.out.println(results.getResourceModel());
            //ResultSetFormatter.out(System.out,results, q);
            list.add(results.next());
        }
    } catch (java.lang.NullPointerException e) {
        System.out.println(e);
    } catch (Exception e) {
        //System.out.println("Query Failed !");
    }
     System.out.println(list.toString() + "\n");
        return list;      
}

我想将给定 (ing) 列表的每个元素添加到查询中。假设我有一个列表,如:ing=[Tomato, Cucumber, Salt] 我想创建一个这样的查询:

                 String queryString
                ="PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
                +"PREFIX owl: <http://www.w3.org/2002/07/owl#>"
                +"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
                +"PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>"         
                +"PREFIX rec:<http://www.receta.org#>"
                +"SELECT reduced ?r WHERE { "
                +"  ?x rdf:type rec:Receta . "
                +"  ?x rdfs:label ?r."
                +"  filter not exists {"
                +"    ?x rec:Ingrediente ?i"
                +"    filter( ?i not in (rec:" + Tomato + ", rec:" + Cucumber + ", rec:" + Salt + "))"
                +"}"
                +"}";

有办法吗?有什么想法吗?

假设您的列表是一个简单的字符串列表,这只是基本的 Java 编程,您可以通过编写一个将 List<String> 作为输入和输出的方法来轻松地做到这一点该列表作为 SPARQL 语法中的字符串。例如:

String convertToSPARQLList(List<String> list) {
    StringBuffer sb = new StringBuffer();
    sb.append("(");
    for(String item: list) {
         sb.append("rec:" + item);
         sb.append(", ");
    }
    sb.setLength(sb.length() - 2); // remove last comma and whitespace
    sb.append(")");
    return sb.toString();
 }

然后在您的查询构造中使用该方法:

 String queryString = 
         ....
   + "filter( ?i not in " + convertToSPARQLList(in) + ")"
   + " }" 
   + "}";