2015-02-05 27 views
1

我有以下代碼:從列表中輸入值SPARQL查詢

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;  
} 

我想補充給予(荷蘭國際集團)到查詢列表中的每一個元素。因此,可以說我有這樣一個名單:荷蘭國際集團= [西紅柿,黃瓜,鹽]我想建立這樣的查詢:

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

反正有做到這一點?有任何想法嗎?

+0

這聽起來像一個最近的問題的重複...(搜索) – 2015-02-05 17:56:31

+0

哪些問題?你能把鏈接放在這裏嗎? – SomeAnonymousPerson 2015-02-05 17:57:54

+1

這就是爲什麼我說「搜索」。我還沒有找到它。無論如何,我正在考慮[SPARQL(耶拿)更新可以通過文字集合(而不是文字)參數化?](http://stackoverflow.com/q/27822769/1281433) – 2015-02-05 17:58:53

回答

1

假設您的列表是一個簡單的字符串列表,這就變成了基本的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) + ")" 
    + " }" 
    + "}";