Message Boards Message Boards

Translating SPARQL usage example to Wolfram Language?

Posted 3 years ago

Hello. I've been learning how to access SPARQL endpoints using the GraphStore` functions and Wikidata. I've been translating their usage examples into Wolfram Language code and I ran into this piece:

BIND(geof:distance(?beLoc, ?shLoc) as ?dist)

I don't know how to translate that. I checked the SPARQL documentation, the GraphStore` documentation, and other examples to establish some basis of comparison to translate the SPARQL expression, and I tried some guesses, but was unable to construct a proper query. I don't know how to express the argument within the parenthesis.

Here are coordinates:

SPARQLExecute[
 "https://query.wikidata.org/sparql",
 SPARQLSelect[{
   RDFTriple[wd["Q39671"], wdt["P625"], SPARQLVariable["shLoc"]],
   RDFTriple[wd["Q64"], wdt["P625"], SPARQLVariable["beLoc"]]
   }]]

Thank you for your assistance.

POSTED BY: James Wey
Posted 3 years ago

I found a new function: ImportString [ string query here , "SPARQLQuery" ].

ImportString["SELECT * WHERE {
wd:Q64 wdt:P625 ?berlinLoc .
wd:Q1384 wdt:P625 ?newyorkLoc 
BIND(geof:distance(?berlinLoc, ?newyorkLoc) as ?dist)
}", "SPARQLQuery"]

returns

SPARQLSelect[{
RDFTriple[URL[{"wd", "Q64"}], URL[{"wdt", "P625"}], SPARQLVariable["berlinLoc"]], 
RDFTriple[URL[{"wd", "Q1384"}], URL[{"wdt", "P625"}], SPARQLVariable["newyorkLoc"]], 
"dist" -> SPARQLEvaluation[URL[{"geof", "distance"}]][SPARQLVariable["berlinLoc"], SPARQLVariable["newyorkLoc"]]
}]

Neat! Very nice function.

POSTED BY: James Wey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract