是否可以轻松地发出CONSTRUCT
请求,在该请求中,我可以检查不同图形中的数据并按"图形偏好"对其进行排序?
比方说我卖产品。对于每种产品,我可能有不同的供应商,所以我的设置看起来像这样:
<http://data.experiment.com/product/1> <http://purl.org/goodrelations/v1#hasCurrencyValue> "10" <http://data.experiment.com/graph/supplier/1> .
<http://data.experiment.com/product/1> <http://purl.org/goodrelations/v1#hasCurrencyValue> "8" <http://data.experiment.com/graph/supplier/2> .
<http://data.experiment.com/product/2> <http://purl.org/goodrelations/v1#hasCurrencyValue> "5" <http://data.experiment.com/graph/supplier/2> .
对于每个产品规格,我希望从<http://data.experiment.com/graph/supplier/1>
获得,如果在<http://data.experiment.com/graph/supplier/1>
中找不到,则从<http://data.experiment.com/graph/supplier/2>
获得。
这就是我想到的:
CONSTRUCT
{
<http://data.experiment.com/product/1> ?p ?o .
}
WHERE
{
GRAPH <http://data.experiment.com/graph/supplier/1>
{
OPTIONAL
{
<http://data.experiment.com/product/1> ?p1 ?o1 .
}
}
GRAPH <http://data.experiment.com/graph/supplier/2>
{
OPTIONAL
{
<http://data.experiment.com/product/1> ?p2 ?o2 .
}
}
BIND (IF (BOUND(?p1), ?p1, IF (BOUND(?p2), ?p2, UNDEF)) AS ?p)
BIND (IF (BOUND(?o1), ?o1, IF (BOUND(?o2), ?o2, UNDEF)) AS ?o)
}
如果我知道我在找什么,它确实很好用。现在,如果我考虑:
CONSTRUCT
{
<http://data.experiment.com/product/1> ?p ?o . ?o ?cp ?co
}
WHERE
{
GRAPH <http://data.experiment.com/graph/supplier/1>
{
OPTIONAL
{
<http://data.experiment.com/product/1> ?p1 ?o1 .
OPTIONAL { ?o1 ?cp1 ?co1 . }
}
}
GRAPH <http://data.experiment.com/graph/supplier/2>
{
OPTIONAL
{
<http://data.experiment.com/product/1> ?p2 ?o2 .
OPTIONAL { ?o2 ?cp2 ?co2 . }
}
}
BIND (IF (BOUND(?p1), ?p1,IF (BOUND(?p2), ?p2, UNDEF)) AS ?p)
BIND (IF (BOUND(?o1), ?o1,IF (BOUND(?o2), ?o2, UNDEF)) AS ?o)
BIND (IF (BOUND(?cp1), ?cp1,IF (BOUND(?cp2), ?cp2, UNDEF)) AS ?cp)
BIND (IF (BOUND(?co1), ?co1,IF (BOUND(?co2), ?co2, UNDEF)) AS ?co)
}
有时它不起作用,因为我明确地BIND
?o
,而?o
可能不是对象——
Virtuoso RDF01错误CONSTRUCT中的变量值错误:"1532610063"(标记189框标记0(不是有效的主题,只有三元组的对象可以是文字
我似乎没有发现有人试图通过"图表"对数据进行排序,我正在努力寻找一种"简单"的方法。我已经尝试过使用SELECT
和FROM NAMED
,但您仍然需要从所需的图中手动选择数据。
如果有人能提供帮助,那将是非常受欢迎的。
谢谢。
我上一篇文章的更新。给定账簿的每个供应商都存储在一个"默认"图形中。
# Named graph : http://data.books.com/default
@prefix book: <http://data.books.com/resource/Book/>
@prefix ns: <http://data.books.com/ns#>
book:8780953608758 ns:hasSupplier <http://data.books.com/supplier/Alpha> .
book:8780953608758 ns:hasSupplier <http://data.books.com/supplier/Beta> .
# Named graph : http://data.books.com/supplier/Alpha
@prefix book: <http://data.books.com/resource/Book/>
@prefix price: <http://data.books.com/resource/Price/>
@prefix gr: <http://purl.org/goodrelations/v1#>
@prefix dc: <http://purl.org/dc/terms/>
book:8780953608758 gr:hasPriceSpecification price:8780953608758_FR_EUR .
price:8780953608758_FR_EUR gr:hasCurrencyValue "10" .
book:8780953608758 dc:available 1447632000 .
# Named graph : http://data.books.com/supplier/Beta
@prefix book: <http://data.books.com/resource/Book/>
@prefix price: <http://data.books.com/resource/Price/>
@prefix gr: <http://purl.org/goodrelations/v1#>
@prefix dc: <http://purl.org/dc/terms/>
book:8780953608758 gr:hasPriceSpecification price:8780953608758_FR_USD .
price:8780953608758_FR_USD gr:hasCurrencyValue "8" .
book:8780953608758 dc:available 1547632000 .
以下查询中的第一个子查询使用图形http://data.books.com/default查找并排序8780953608758这本书的所有供应商图表。然后,它将另一个模式与该图相匹配。
PREFIX book: <http://data.bookeen.com/resource/Book/>
CONSTRUCT
{
book:8780953608758 ?p ?o . ?o ?cp ?co .
}
WHERE
{
{
SELECT ?supplier
FROM <http://data.books.com/default>
WHERE
{
VALUES (?supplier ?priority)
{
(<http://data.books.com/supplier/Beta> 1)
(<http://data.books.com/supplier/Alpha> 2)
}
book:8780953608758 <http://data.books.com/ns/hasSupplier> ?supplier.
}
ORDER BY ?priority
LIMIT 1
}
GRAPH ?supplier
{
book:8780953608758 ?p ?o .
OPTIONAL { ?o ?cp ?co . }
}
}