如何在SPARQL中使用CONSTRUCT或DESCRIBE隔离RDF文件中的URI列表? [英] How to isolate a list of URIs in a RDF file using CONSTRUCT or DESCRIBE in SPARQL?

查看:259
本文介绍了如何在SPARQL中使用CONSTRUCT或DESCRIBE隔离RDF文件中的URI列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试仅获取RDF中的URI列表,而不是三元组列表:

I'm trying to get only a list of URIs in RDF instead of a list of triples:

PREFIX gr: <http://purl.org/goodrelations/v1#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

DESCRIBE  ?product 
WHERE
{
  ?product rdfs:subClassOf gr:ProductOrService .
}

使用SELECT,而不是DESCRIBE,我仅接收主题(我想要的),但不接收RDF,而是接收带有绑定,变量等的SPARQL结果.

Using SELECT, instead of DESCRIBE I receive only the subject (which I want), but not as an RDF but like a SPARQL Result with binds, vars, etc.

使用CONSTRUCT,我不能仅指定?product,如上所述,所以我能得到的最接近的是:

Using CONSTRUCT, I can't specify only the ?product, as above, so the closest I can get is:

PREFIX gr: <http://purl.org/goodrelations/v1#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

CONSTRUCT
WHERE
{
  ?product rdfs:subClassOf gr:ProductOrService .
}

哪个返回的RDF具有三乘不同的乘积,但具有相同的属性和对象.

Which returns an RDF with triples of different products, but the same properties and objects.

推荐答案

将SPARQL SELECT查询的结果表示为RDF列表是一个工具问题-通常,这不是SPARQL可以解决的问题.

Representing the result of a SPARQL SELECT query as an RDF List is a tooling issue - it is not something that can be solved in SPARQL in general.

某些SPARQL工具可能具有支持将查询结果呈现为RDF列表的方法.但这不能通过仅以不同的方式表达查询来解决,您将需要使用工具来(以编程方式)格式化结果.

Some SPARQL tools may have ways to support rendering the query result as an RDF list. But it's not something you can fix by just formulating your query differently, you'll need to use a tool to (programmatically) format the result.

在Java中,使用Eclipse RDF4J,您可以按以下步骤进行操作(未经测试,因此您可能需要对其进行调整以使其正常工作,但应该可以为您提供一个总体思路):

In Java, using Eclipse RDF4J, you can do it as follows (untested so you may need to tweak it to work properly, but it should give you a general idea):

String query = "SELECT ?product WHERE {  ?product rdfs:subClassOf gr:ProductOrService . }";

// do the query on repo and convert to a Java list of URI objects     
List<URI> results = Repositories.tupleQuery(
  repo, 
  query, 
  r -> QueryResults.stream(r).map(bs -> (URI)bs.getValue("product")).collect(Collectors.toList()
);

// create a resource (bnode or URI) for the start of the rdf:List
Resource head = SimpleValueFactory.getInstance().createBNode();

// convert the Java list of results to an rdf:list and add it 
// to a newly-created RDF Model
Model m = RDFCollections.asRDF(results, head, new LinkedHashModel());

将结果作为RDF模型后,可以使用任何现有的 RDF4J API 来将其写入文件或存储在三元组中.

Once you have your result as an RDF model you can use any of the existing RDF4J APIs to write it to file or to store it in a triplestore.

现在,我并没有声称这是一个好主意-我从未见过类似此类的用例.但是,如果需要的话,这就是我会做的.

Now, I am not claiming that any of this is a good idea - I have never seen a use case for something like this. But this is how I would do it if it were necessary.

这篇关于如何在SPARQL中使用CONSTRUCT或DESCRIBE隔离RDF文件中的URI列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆