[英]How to isolate a list of URIs in a RDF file using CONSTRUCT or DESCRIBE in SPARQL?
我正在嘗試僅獲取RDF中的URI列表,而不是三元組列表:
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結果。
使用CONSTRUCT
,如上所述,我不能僅指定?product
,所以最接近的是:
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,該RDF具有三乘不同的乘積,但具有相同的屬性和對象。
我找到了解決方案。 我使用了SELECT,但是沒有綁定和變量,而是輸出了“逗號分隔值(帶有N-Triples語法的字段)” CSV文件:
<http://www.productontology.org/id/Real_estate>
<http://www.productontology.org/id/Automobile>
<http://www.productontology.org/id/Auction>
<http://www.productontology.org/id/Video_game>
<http://www.productontology.org/id/Campsite>
<http://www.productontology.org/id/Car>
<http://www.productontology.org/id/Audiobook>
<http://www.productontology.org/id/Browser_game>
...
將SPARQL SELECT查詢的結果表示為RDF列表是一個工具問題-通常,這不是SPARQL可以解決的問題。
一些SPARQL工具可能具有支持將查詢結果呈現為RDF列表的方式。 但是,僅通過以不同的方式表達查詢就無法解決該問題,您需要使用一種工具來(以編程方式)格式化結果。
在Java中,可以使用Eclipse RDF4J進行以下操作(未經測試,因此您可能需要對其進行調整以使其正常工作,但應該可以為您提供一個總體思路):
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將其寫入文件或將其存儲在Triplestore中。
現在,我並沒有聲稱這是個好主意-我從未見過類似這樣的用例。 但是,如果需要的話,這就是我會做的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.