generate an urn/iri/uri dynamically

2019-07-02 00:33发布

问题:

I need to generate dynamically the name of a graph depending on the time. I've tough that some think like

select ?g where { 
    bind(concat("<urn:myNewGraph_",str(now()),">") as ?g)
}

would have done the trick, but with Stardog I get a null result.

If instead I run this

select ?g where { 
    bind(concat("urn:myNewGraph_",str(now())) as ?g)
}

i get urn:myNewGraph_2015-05-28T09:37:11.823Z

Any Ideas?

moreover I'm not sure that even if i can get somehow a string like <urn:myNewGraph_2015-05-28T09:37:11.823Z> would have worked as a valid argument for a graph name as can be seen from this not-working test:

INSERT {graph ?g {<urn:s> <urn:p> <urn:o>}
where { 
    ?g="<rn:myNewGraph_2015-05-28T09:37:11.823Z>"
}

is there a proper way to generate an urn/iri/uri dynamically?

回答1:

Your original query looks correct, and produces a valid result when I execute it using a different SPARQL engine (Sesame), so I guess that you might want to report this to the Stardog developers as a possible bug.

However, to be able to use the value thus obtained it needs to be an actual URI (or IRI) - whereas what you're producing is a literal string.

You need to change two things: first of all, get rid of the enclosing < and > (these brackets are not actually part of the IRI) - so actually your second query is better. Second, use the IRI function to convert your string value to an IRI:

INSERT {GRAPH ?g {<urn:s> <urn:p> <urn:o>} }
WHERE { 
     BIND( IRI(CONCAT("urn:myNewGraph_",STR(NOW()))) as ?g)
}

Not sure it's necessary in your case, but in general you may need to use the ENCODE_FOR_URI function in there somewhere, to make sure that any special characters in your string are properly encoded/escaped before turning it into an IRI.



标签: sparql