问题
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 {}
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.
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.
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?
回答1:
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 {}
Hats off to @Joshua Taylor for the lead.
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.
来源:https://stackoverflow.com/questions/27743990/sparql-update-with-optional-parts