SPARQL update with optional parts

喜你入骨 提交于 2019-12-11 12:07:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!