Saturday, February 28, 2009

SPARQL for the Pragmatic Ontologist

SPARQL

  • SQL of RDF

  • XSLT of RDF

  • JavaDoc of RDF

  • Lint of RDF

  • Unit Test of RDF



SPARQL is READ-ONLY!

Jena ARQ Extensions (SPARQL 2 Candidates)

1st Query: Find the country names
SELECT ?country ?name
WHERE {
?country rdf:type country:Country .
?country country:name ?name
}

OR

SELECT ?country ?name
WHERE {
?country a country:Country .
?country country:name ?name
}

OR

SELECT ?country ?name {
?country rdf:type country:Country ;
country:name ?name
}

OR

SELECT ?country ?name {
?country country:name ?name
}

2nd Query: Find the states and their cities sorted by ascending state, then city name, then by descending city name
SELECT ?state ?city
WHERE {
?cityR city:name ?city ;
Uscity:state ?stateR .
?stateR state:name ?state
} ORDER BY ASC(?state) DESC(?city)

Compactified Expression
SELECT ?state ?city
WHERE {
?cityR city:name ?city;
uscity:state [state:name ?state]
} ORDER BY ASC(?state) DESC(?city)

SELECT ?state ?capital ?airport
WHERE {
?stateR state:name ?state ;
state:capital ?capitalR .
?capitalR city:name ?capital
OPTIONAL {
?airportR airports:city ?capital ;
airports:airport ?airport
}
}

3rd Query: Select with UNION
SELECT ?state
WHERE {
{
?stateR state:borderstate usstate:AL, usstate:TN .
?stateR state:name ?state .
}
UNION
{
?stateR state:borderstate usstate:ID .
?stateR state:name ?state .
}
}

4th Query: Find all states that do NOT border another state

SELECT ?state
WHERE {
?stateR state:name ?state .
OPTIONAL {
?stateR state:borderstate ?borderState .
}
FILTER(!bound(?borderState))
}

FILTER means "keep".
With !bound() we effectively look for the absense of a resource.

5th Query: Find all states, and cities, where the city name begins with the letter 'Y'

SELECT ?state ?city
WHERE {
?cityR uscity:state ?stateR
?cityR city:name ?city .
?stateR state:name ?state
FILTER(regex(xsd:string(?city), "^Y"))
}

Cast untyped literals into datatype needed by function.

Filter early, near the top of an expression
-if a condition is not met the pattern match aborts immediately.

User OPTIONAL sparingly, they will slow down a query, sometimes drastically.

Use ASK when you do need the results of the match.

DISTINCT and ORDERED BY may also be slow over large result sets.

USE LIMIT and OFFSET with large result sets.

6th Query: DISTINCT avoids duplicate result sets

SELECT DISTINCT ?state ?city
WHERE {
?person uscity:address ?address .
?address uscity:city ?city .
?city uscity:state ?state
}

7th Query: CONSTRUCT Query
CONSTRUCT allows us to specify a graph to return.
CONSTRUCTed graphs are not inserted into the queried graph - but some tools allow the constructed graph to be asserted.

CONSTRUCT {
?countryR owl:sameAs ?nationR
}
WHERE {

}

Start comments anywhere with "#".
Use LET() to assign variables.

Use INSERT before DELETE to move data.
The graph can also be a variable.

TopBraid Suite Advanced Product Training
SPARQLMotion
SPIN - SPARQL Inferencing Notation (SPROC)

No comments: