net.nprod.rdf4k / org.eclipse.rdf4j.model.util.ModelBuilder / namedGraph

namedGraph

fun ModelBuilder.namedGraph(graphName: String, block: ModelBuilder.() -> ModelBuilder): ModelBuilder (source)

Adds a named graph to a given ModelBuilder

/**
 * RDF Tutorial example 12: Building a Model with named graphs
 *
 * In this example, we show how you can use the context mechanism to add statements separate named
 * graphs within the same Model.
 *
 * @author Jeen Broekstra
 * @author Jonathan Bisson
 */
object Example12BuildModelWithNamedGraphs {

    @Throws(IOException::class)
    @JvmStatic
    fun main(args: Array<String>) {
        // We'll use a ModelBuilder to create two named graphs, one containing data about
        // picasso, the other about Van Gogh.
        val builder = modelBuilder {
            namespace("ex", "http://example.org/")

            // in named graph 1, we add info about Picasso
            namedGraph("ex:namedGraph1") {
                subject("ex:Picasso") {
                    add(RDF.TYPE, EX.ARTIST)
                    add(FOAF.FIRST_NAME, "Pablo")
                }
            }

            // in named graph2, we add info about Van Gogh.
            namedGraph("ex:namedGraph2") {
                subject("ex:VanGogh") {
                    add(RDF.TYPE, EX.ARTIST)
                    add(FOAF.FIRST_NAME, "Vincent")
                }
            }
        }

        // We're done building, create our Model
        val model = builder.build()

        // each named graph is stored as a separate context in our Model
        model.contexts().map { context ->
            println("Named graph $context contains: ")

            // write _only_ the statements in the current named graph to the console, in N-Triples format
            Rio.write(model.filter(null, null, null, context), System.out, RDFFormat.NTRIPLES)
            println()
        }
    }
}

Author
Jonathan Bisson