gpt4 book ai didi

java - 如何在剧中显示 SPARQL 查询的输出!看法?

转载 作者:行者123 更新时间:2023-11-30 04:41:49 27 4
gpt4 key购买 nike

我正在编写一个应用程序,用户可以在其中上传 .rdf 文件,然后对其执行 SPARQL 查询。现在我陷入了如何格式化查询结果的困境。例如:ASK-查询输出 boolean 值,SELECT-查询返回结果集。 CONSTRUCTDESCRIBE 返回一个新的 RDF-Graph。
如何以表单形式获取给定查询的结果并将其传递到我的 View ?我希望在那里打印出一个 HTML 表格。

为了更好地理解我的问题,我创建了这个简约的代码示例:

import java.io.InputStream;

import com.hp.hpl.jena.*;

public class playground {

public static void main(String[] args) {

InputStream in = FileManager.get().open("vc-db-1.rdf");
Model model = ModelFactory.createDefaultModel();
model.read(in, null);

String queryStringSelect = "SELECT ?subject ?predicate ?object WHERE { ?subject ?predicate ?object }";
String queryStringAsk = "ASK WHERE { ?subject ?predicate ?object }";
String queryStringDescribe = "DESCRIBE * WHERE { ?subject ?predicate ?object }";
String queryStringConstruct = "CONSTRUCT { ?subject ?predicate ?object } WHERE { ?subject ?predicate ?object }";

QueryExecution qe = QueryExecutionFactory.create(queryStringDescribe,
model);
Query q = QueryFactory.create(queryStringDescribe);

int queryType = q.getQueryType();
switch (queryType) {
case Query.QueryTypeAsk:
boolean b = qe.execAsk(); // Result that has to be formatted
ResultSetFormatter.outputAsTSV(System.out, b);
break;
case Query.QueryTypeConstruct:
model = qe.execConstruct(); // Result that has to be formatted
model.write(System.out);
break;
case Query.QueryTypeDescribe:
model = qe.execDescribe(); // Result that has to be formatted
model.write(System.out);
break;
case Query.QueryTypeSelect:
ResultSet results = qe.execSelect(); // Result that has to be
// formatted
ResultSetFormatter.outputAsTSV(System.out, results);
break;
}

model.close();
qe.close();

}

}

mentionend .rdf 文件可以在这里下载:http://jena.apache.org/tutorials/sparql_data/vc-db-1.rdf

最佳答案

我终于找到答案了:

我采用 ByteArrayOutputStream 并使用各种 model.write() 函数完成其余操作。对于 SELECT-Query 我使用文本表示,因为提供的 ResultSetFormatter.outputAsRDF() 给了我很多废话。无论如何,这是相关代码

ByteArrayOutputStream baos = new ByteArrayOutputStream();
...
int queryType = q.getQueryType();
switch (queryType) {
case Query.QueryTypeAsk:
boolean b = qe.execAsk();
ResultSetFormatter.outputAsRDF(baos, notation, b);
retval = baos.toString();
break;
case Query.QueryTypeConstruct:
model = qe.execConstruct();
model.write(baos, notation);
retval = baos.toString();
break;
case Query.QueryTypeDescribe:
model = qe.execDescribe();
model.write(baos, notation);
retval = baos.toString();
break;
case Query.QueryTypeSelect:
ResultSet results = qe.execSelect();
ResultSetFormatter.out(baos, results);
retval = baos.toString();
break;
}

关于java - 如何在剧中显示 SPARQL 查询的输出!看法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12123270/

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