带有可选零件的SPARQL更新 [英] SPARQL update with optional parts
问题描述
请考虑以下SPARQL更新:
Consider the following SPARQL update:
INSERT {
?performance
mo:performer ?performer ; # optional
mo:singer ?singer ; # optional
mo:performance_of [
dc:title ?title ; # mandatory
mo:composed_in [ a mo:Composition ;
mo:composer ?composer # optional
]
]
}
WHERE {}
如果我不提供值(例如,在Jena的ParameterizedSparqlString.setIri()
中的?performer
,?singer
或?composer
,则此更新不会按预期插入带有相应对象的语句.
If I do not provide values (e.g. in Jena's ParameterizedSparqlString.setIri()
for ?performer
, ?singer
, or ?composer
, this update won't insert statements with the corresponding objects, which is as intended.
但是如果缺少?composer
,我又如何抑制[] a mo:Composition
.在第二个INSERT中创建它(在ISIRI(?composer)
上使用WHERE过滤器)似乎不是一个选择,因为INSERT不会知道第一个已经创建的空白节点.
But how can I suppress [] a mo:Composition
as well if ?composer
is missing. Creating it in a second INSERT whose WHERE filters on ISIRI(?composer)
doesn't seem to be an option because that INSERT won't know the blank node that has already been created by the first one.
那么,如何在单个SPARQL更新中支持这种可选参数?例如,是否有任何方法可以存储"两个INSERT之间的空白节点?
So how can I support this kind of optional parameters in a single SPARQL update? E.g., is there any means for "storing" the blank node between two INSERTs?
推荐答案
当且仅当调用方将?composer
设置为IRI时,调用方才将composition
设置为空白节点时,以下内容似乎起作用.
The following seems to work, when the caller sets composition
to a blank node if and only if it sets ?composer
to an IRI.
if (composer != null) {
parameterizedSparqlString.setIri ("composer" , composer);
parameterizedSparqlString.setParam("composition", NodeFactory.createAnon());
}
INSERT {
?performance
mo:performer ?performer ; # optional
mo:singer ?singer ; # optional
mo:performance_of [
dc:title ?title ; # mandatory
mo:composed_in ?composition ] . # optional
?composition a mo:Composition ;
mo:composer ?composer .
}
WHERE {}
到@Joshua Taylor领队.
Hats off to @Joshua Taylor for the lead.
如果可能的话,我仍然希望使用不包含附加参数?composition
的自包含版本(即,无需对调用方提出附加要求即可工作).
I'd still prefer a self-contained version that does not require the additional parameter ?composition
(i.e. works without making additional demands on the caller), if that's possible at all.
这篇关于带有可选零件的SPARQL更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!