fun SelectQuery.prefix(name: String, iri: Iri): SelectQuery
(source)
Allows to insert a prefix in a SPARQL Query using Kotlin's DSL
/**
* RDF Tutorial example K02: Using the Query Builder as a DSL
*
* Refer to that documentation: [http://docs.rdf4j.org/sparqlbuilder/]
*
*
* @author Jonathan Bisson
*/
object ExampleK02QueryBuilder {
@JvmStatic
fun main(args: Array<String>) {
// We are using the adapted Kotlin DSL here
val model = modelBuilder {
namespace("ex", "http://example.org/")
subject("ex:a") {
add(FOAF.NAME, "Katherine Johnson")
add(RDF.TYPE, FOAF.PERSON)
}
subject("ex:b") {
add(FOAF.NAME, "Grace Hopper")
add(RDF.TYPE, FOAF.PERSON)
}
}.build()
val foafPrefix = sparqlPrefix("foaf", iri(FOAF.NAMESPACE))
val name = sparqlVariable("name")
val x = sparqlVariable("x")
val selectQuery = (selectQuery {
prefix(foafPrefix)
// Could also have used directly
// prefix("foaf", iri("http://xmlns.com/foaf/0.1/"))
select(name) {
where(
x.has(foafPrefix.iri("name"), name),
x.isA(foafPrefix.iri("Person"))
)
}
} orderBy name limit 5 offset 0 ).queryString
println(selectQuery)
val db = SailRepository(MemoryStore())
db.initialize()
// Open a connection to the database
try {
db.connection.apply {
// add the model
add(model)
val query = prepareTupleQuery(selectQuery)
query.evaluate().map {
// ... and print out the value of the variable binding for ?name
println("name = ${it["name"]}")
}
}
} finally {
// before our program exits, make sure the database is properly shut down.
db.shutDown()
}
}
}
Author
Jonathan Bisson