net.nprod.rdf4k / org.eclipse.rdf4j.query.QueryResult / map

map

inline fun <T, R> QueryResult<T>.map(transform: (T) -> R): List<R> (source)

Iterates over the TupleQueryResult's BindingSet elements using block

/**
 * RDF Tutorial example 15: executing a simple SPARQL query on the database
 *
 * @author Jeen Broekstra
 * @author Jonathan Bisson
 */
object Example15SimpleSPARQLQuery {

    @Throws(IOException::class)
    @JvmStatic
    fun main(args: Array<String>) {
        // Create a new Repository.
        val db = SailRepository(MemoryStore())
        db.initialize()

        // Open a connection to the database
        try {
            db.connection.use { conn ->
                val filename = "example-data-artists.ttl"

                // add the RDF data from the inputstream directly to our database
                conn.add(javaClass.getResourceAsStream("/$filename"), "", RDFFormat.TURTLE)

                // We do a simple SPARQL SELECT-query that retrieves all resources of type `ex:Artist`,
                // and their first names.
                val queryString = """
                    |PREFIX ex: <http://example.org/>
                    |PREFIX foaf: <${FOAF.NAMESPACE}>
                    |SELECT ?s ?n
                    |WHERE {
                    |    ?s a ex:Artist;
                    |       foaf:firstName ?n .
                    |}""".trimMargin()

                val query = conn.prepareTupleQuery(queryString)

                // A QueryResult is also an AutoCloseable resource, so make sure it gets closed when done.
                // we just iterate over all solutions in the result...
                query.evaluate().map {
                    // ... and print out the value of the variable binding for ?s and ?n
                    println("?s = " + it["s"])
                    println("?n = " + it["n"])
                }
            }
        } finally {
            // Before our program exits, make sure the database is properly shut down.
            db.shutDown()
        }
    }
}
/**
 * RDF Tutorial example 16: executing a SPARQL CONSTRUCT query on the database
 *
 * @author Jeen Broekstra
 * @author Jonathan Bisson
 */
object Example16SPARQLConstructQuery {

    @Throws(IOException::class)
    @JvmStatic
    fun main(args: Array<String>) {
        // Create a new Repository.
        val db = SailRepository(MemoryStore())
        db.initialize()

        // Open a connection to the database
        try {
            db.connection.use { conn ->
                val filename = "example-data-artists.ttl"
                // add the RDF data from the inputstream directly to our database
                conn.add(javaClass.getResourceAsStream("/$filename"),
                    "",
                    RDFFormat.TURTLE)

                // We do a simple SPARQL CONSTRUCT-query that retrieves all statements about artists,
                // and their first names.
                val queryString = """
                    |PREFIX ex: <http://example.org/>
                    |PREFIX foaf: <${FOAF.NAMESPACE}>
                    |CONSTRUCT
                    |WHERE {
                    |    ?s a ex:Artist;
                    |       foaf:firstName ?n .
                    |}""".trimMargin()

                val query = conn.prepareGraphQuery(queryString)

                val turtleWriter = Rio.createWriter(RDFFormat.TURTLE, System.out)
                query.evaluate(turtleWriter)

                // A QueryResult is also an AutoCloseable resource, so make sure it gets closed when done.
                // This was needed explicitly in Java, but not necessary in Kotlin
                query.evaluate().map { st ->
                        // ... and print them out
                        println(st)
                }
            }
        } finally {
            // Before our program exits, make sure the database is properly shut down.
            db.shutDown()
        }
    }
}

Parameters

block - is a lambda handling each bindingSet

Author
Jonathan Bisson