# A tour of the functions a TARQL query can call beyond SPARQL's own, on the bookshops:
# Jena's property functions (apf:) and functions (afn:), as TARQL proper has them, and
# HOLOS's libraries: XPath (fn:), SPIN (spif:) and GeoSPARQL (geof:).
#
# It writes a small guide to the trail in a namespace of its own, guide:, not the trail's
# ontology: these values illustrate the functions, and are not facts the ontology records.
#
# tarka run -q tarql/functions-tour.rq -i data/bookshops.csv --test 3
PREFIX bt:
PREFIX guide:
PREFIX xsd:
PREFIX geo:
PREFIX apf:
PREFIX afn:
PREFIX fn:
PREFIX spif:
PREFIX geof:
PREFIX uom:
CONSTRUCT {
?shop_iri guide:namespace ?namespace ; # apf:splitIRI
guide:shortName ?local ; # afn:localname
guide:welcome ?welcome ; # apf:concat
guide:card ?card ; # afn:strjoin
guide:sign ?sign ; # fn:upper-case
guide:key ?key ; # afn:sha1sum, afn:substr
guide:specialism ?specialism_name ; # spif:titleCase
guide:areaPerPerson ?per_person ; # afn:max
guide:circleRadius ?radius ; # afn:sqrt, afn:pi
guide:kmFromWigtown ?km . # geof:distance
}
WHERE {
BIND(IRI(CONCAT(STR(bt:), "shop-", ?shop)) AS ?shop_iri)
# apf:splitIRI: an IRI split where Jena splits it, before the XML name at its end
?shop_iri apf:splitIRI (?namespace ?local_part)
# afn:localname gives the same local name as a string
BIND(afn:localname(?shop_iri) AS ?local)
# apf:concat binds its subject to the concatenation, as CONCAT() would
?welcome apf:concat ("Welcome to " ?name)
# spif:titleCase: "literary-fiction" -> "Literary Fiction"
BIND(spif:titleCase(REPLACE(?specialism, "-", " ")) AS ?specialism_name)
# afn:strjoin: a separator, then the strings (after the BIND above: a BIND sees only what
# is bound before it in the group)
BIND(afn:strjoin(" ยท ", ?name, ?specialism_name, CONCAT("since ", ?founded)) AS ?card)
# the name painted over the door: XPath's fn:upper-case
BIND(fn:upper-case(?name) AS ?sign)
# a short, stable key: the first eight characters of a SHA-1 (afn:substr counts from 0,
# as Java does; SPARQL's SUBSTR counts from 1)
BIND(afn:substr(afn:sha1sum(?shop), 0, 8) AS ?key)
# floor area per member of staff; afn:max keeps a staff of 0 from dividing by zero
BIND(xsd:decimal(?floor_area) / afn:max(xsd:integer(?staff), 1) AS ?per_person)
# the radius of a round shop with the same floor area: r = sqrt(A / pi)
BIND(ROUND(afn:sqrt(xsd:decimal(?floor_area) / afn:pi()) * 10) / 10 AS ?radius)
# how far the shop is from Wigtown, Scotland's National Book Town, as the crow flies
BIND(STRDT(CONCAT(" POINT(", ?long, " ", ?lat, ")"), geo:wktLiteral) AS ?here)
BIND(ROUND(geof:distance(?here, " POINT(-4.44577 54.86733)"^^geo:wktLiteral, uom:metre) / 1000) AS ?km)
}