Skip to content

Commit 00b39d8

Browse files
committed
added section on ModelBuilder API
Signed-off-by: Jeen Broekstra <jeen.broekstra@gmail.com>
1 parent 28bdc1e commit 00b39d8

2 files changed

Lines changed: 54 additions & 18 deletions

File tree

doc/index.adoc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
include::header.adoc[]
77

88
:numbered!:
9-
== Eclipse RDF4j Documentation
9+
== Eclipse RDF4J Documentation
1010

11-
== Tutorials
11+
=== Tutorials
1212

1313
- link:getting-started[Getting Started with RDF4J, Maven, and Eclipse]
1414

15-
== Reference Documentation
15+
=== Reference Documentation
1616

1717
- link:migration[Sesame to RDF4J Migration Guide]
1818
- link:programming[Programming with RDF4J]
1919
- link:server-workbench-console[RDF4J Server, Workbench, and Console]
2020

21-
== Javadoc
21+
=== Javadoc
2222

2323
- link:/javadoc/latest[RDF4J API Javadoc]
2424
- older versions: [link:/javadoc/1.0[1.0]]

doc/programming/02-model-api.adoc

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@ The RDF Model API is the core of the RDF4J framework. It provides the basic buil
44

55
== RDF Building Blocks: IRIs, literals, blank nodes and statements
66

7-
The core of the RDF4J framework is the RDF Model API (see the Model API Javadoc). This API defines how the building blocks of RDF (statements, IRIs, blank nodes, literals, and models) are represented.
7+
The core of the RDF4J framework is the RDF Model API (see the link:/javadoc/latest/?org/eclipse/rdf4j/model/package-summary.html[Model API Javadoc]). This API defines how the building blocks of RDF (statements, IRIs, blank nodes, literals, and models) are represented.
88

9-
RDF statements are represented by the org.eclipse.rdf4j.model.Statement interface. Each Statement has a subject, predicate, object and (optionally) a context (more about contexts below, in the section about the Repository API). Each of these 4 items is a org.eclipse.rdf4j.model.Value. The Value interface is further specialized into org.eclipse.rdf4j.model.Resource, and org.eclipse.rdf4j.model.Literal. Resource represents any RDF value that is either a blank node or a IRI (in fact, it specializes further into org.eclipse.rdf4j.model.IRI and org.eclipse.rdf4j.model.BNode). Literal
9+
RDF statements are represented by the `org.eclipse.rdf4j.model.Statement` interface. Each `Statement` has a subject, predicate, object and (optionally) a context (more about contexts below, in the section about the Repository API). Each of these 4 items is a `org.eclipse.rdf4j.model.Value`. The `Value` interface is further specialized into `org.eclipse.rdf4j.model.Resource`, and `org.eclipse.rdf4j.model.Literal`. `Resource` represents any RDF value that is either a blank node or a IRI (in fact, it specializes further into `org.eclipse.rdf4j.model.IRI` and `org.eclipse.rdf4j.model.BNode`). `Literal`
1010
represents RDF literal values (strings, dates, integer numbers, and so on).
1111

12-
To create new values and statements, we can use a org.eclipse.rdf4j.model.ValueFactory. You can use a default ValueFactory implementation called org.eclipse.rdf4j.model.impl.SimpleValueFactory:
12+
To create new values and statements, we can use a `org.eclipse.rdf4j.model.ValueFactory`. You can use a default ValueFactory implementation called `org.eclipse.rdf4j.model.impl.SimpleValueFactory`:
1313

14-
ValueFactory factory = SimpleValueFactory.getInstance();
14+
[source,java]
15+
----
16+
ValueFactory factory = SimpleValueFactory.getInstance();
17+
----
1518

16-
You can also obtain a ValueFactory from the Repository you are working with, and in fact, this is the recommend approach. More about that in the next section.
19+
You can also obtain a `ValueFactory` from the `Repository` you are working with, and in fact, this is the recommend approach. More about that in the next section.
1720

18-
Regardless of how you obtain your ValueFactory, once you have it, you can use it to create new URIs, Literals, and Statements:
21+
Regardless of how you obtain your `ValueFactory`, once you have it, you can use it to create new URIs, Literals, and Statements:
1922

2023
[source,java]
2124
----
@@ -25,7 +28,7 @@ Literal bobsName = factory.createLiteral("Bob");
2528
Statement nameStatement = factory.createStatement(bob, name, bobsName);
2629
----
2730

28-
The Model API also provides pre-defined IRIs for several well-known vocabularies, such as RDF, RDFS, OWL, DC (Dublin Core), FOAF (Friend-of-a-Friend), and more. These constants can all be found in the org.eclipse.rdf4j.model.vocabulary package, and can be quite handy in quick creation of RDF statements (or in querying a Repository, as we shall see later):
31+
The Model API also provides pre-defined IRIs for several well-known vocabularies, such as RDF, RDFS, OWL, DC (Dublin Core), FOAF (Friend-of-a-Friend), and more. These constants can all be found in the `org.eclipse.rdf4j.model.vocabulary` package, and can be quite handy in quick creation of RDF statements (or in querying a Repository, as we shall see later):
2932

3033
[source,java]
3134
----
@@ -34,9 +37,9 @@ Statement typeStatement = factory.createStatement(bob, RDF.TYPE, FOAF.PERSON);
3437

3538
== The Model interface
3639

37-
The above interfaces and classes show how we can create the individual building blocks that make up an RDF model. However, an actual collection of RDF data is just that: a collection. In order to deal with collections of RDF statements, we can use the org.eclipse.rdf4j.model.Model interface.
40+
The above interfaces and classes show how we can create the individual building blocks that make up an RDF model. However, an actual collection of RDF data is just that: a collection. In order to deal with collections of RDF statements, we can use the `org.eclipse.rdf4j.model.Model` interface.
3841

39-
org.eclipse.rdf4j.model.Model is an extension of the default Java Collection class `java.util.Set<Statement>`. This means that you can use a Model like any other Java collection in your code:
42+
`org.eclipse.rdf4j.model.Model` is an extension of the default Java Collection class `java.util.Set<Statement>`. This means that you can use a `Model` like any other Java collection in your code:
4043

4144
[source,java]
4245
----
@@ -53,7 +56,7 @@ for (Statement statement: model) {
5356
}
5457
----
5558

56-
In addition, however, Model offers a number of useful methods to quickly get subsets of statements and otherwise search/filter your collection of statements. For example, to quickly iterate over all statements that make a resource an instance of the class foaf:Person, you can do:
59+
In addition, however, `Model` offers a number of useful methods to quickly get subsets of statements and otherwise search/filter your collection of statements. For example, to quickly iterate over all statements that make a resource an instance of the class `foaf:Person`, you can do:
5760

5861
[source,java]
5962
----
@@ -62,8 +65,7 @@ for (Statement typeStatement: model.filter(null, RDF.TYPE, FOAF.PERSON)) {
6265
}
6366
----
6467

65-
Even more convenient is that you can quickly retrieve the building blocks that make up the statements. For example, to immediately iterate over all subject-resources that are of type foaf:Person and then retrieve each person’s name, you can do something like the following:
66-
68+
Even more convenient is that you can quickly retrieve the building blocks that make up the statements. For example, to immediately iterate over all subject-resources that are of type `foaf:Person` and then retrieve each person’s name, you can do something like the following:
6769

6870
[source,java]
6971
----
@@ -73,9 +75,43 @@ for (Resource person: model.filter(null, RDF.TYPE, FOAF.PERSON).subjects()) {
7375
}
7476
----
7577

76-
The filter() method returns a Model again. However, the Model returned by this method is still backed by the original Model. Thus, changes that you make to this returned Model will automatically be reflected in the original Model as well.
78+
The `filter()` method returns a `Model` again. However, the `Model` returned by this method is still backed by the original `Model`. Thus, changes that you make to this returned `Model` will automatically be reflected in the original `Model` as well.
79+
80+
RDF4J provides two default implementations of the `Model` interface: `org.eclipse.rdf4j.model.impl.LinkedHashModel`, and `org.eclipse.rdf4j.model.impl.TreeModel`. The difference between the two is in their performance for different kinds of lookups and insertion patterns (see their respective javadoc entries for details). These differences are only really noticable when dealing with quite large collections of statements, however.
81+
82+
== Building RDF Models with the ModelBuilder
83+
84+
Since version 2.1, RDF4J provides a `ModelBuilder` utility. The ModelBuilder provides a fluent API to quickly and efficiently create RDF models programmatically.
85+
86+
Here’s a simple code example that demonstrates how to quickly create an RDF graph with some FOAF data:
87+
88+
[source,java]
89+
----
90+
ModelBuilder builder = new ModelBuilder();
91+
92+
// set some namespaces
93+
builder.setNamespace("ex", "http://example.org/").setNamespace(FOAF.NS);
94+
95+
builder.namedGraph("ex:graph1") // add a new named graph to the model
96+
.subject("ex:john") // add several statements about resource ex:john
97+
.add(FOAF.NAME, "John") // add the triple (ex:john, foaf:name "John") to the named graph
98+
.add(FOAF.AGE, 42)
99+
.add(FOAF.MBOX, "john@example.org");
100+
101+
// add a triple to the default graph
102+
builder.defaultGraph().add("ex:graph1", RDF.TYPE, "ex:Graph");
103+
104+
// return the Model object
105+
Model m = builder.build();
106+
----
107+
108+
The ModelBuilder offers several conveniences:
109+
110+
- you can specify a subject/predicate IRI as a prefixed name string (for example “ex:john”), so you don’t have to use a ValueFactory to create an IRI object first.
111+
- you can add a literal object as a String, an int, or several other supported Java primitive types.
112+
- the subject() method make it easier to take a resource-centric view when building an RDF Model.
77113

78-
RDF4J provides two default implementations of the Model interface: org.eclipse.rdf4j.model.impl.LinkedHashModel , and org.eclipse.rdf4j.model.impl.TreeModel . The difference between the two is in their performance for different kinds of lookups and insertion patterns (see their respective javadoc entries for details). These differences are only really noticable when dealing with quite large collections of statements, however.
114+
For more details, see the link:/javadoc/latest/?org/eclipse/rdf4j/model/util/ModelBuilder.html[ModelBuilder API Javadoc].
79115

80116
== RDF Collections
81117

0 commit comments

Comments
 (0)