gpt4 book ai didi

java - 将列表中的值输入 sparql 查询

转载 作者:太空宇宙 更新时间:2023-11-04 06:13:51 25 4
gpt4 key购买 nike

我有以下代码:

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

关于java - 将列表中的值输入 sparql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28350732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com