Apache Commons RDF logo

Implementations

The Commons RDF API must be used with one or more implementations.

The Apache Commons RDF distribution includes bindings for the implementations:

In addition there can be External implementations which are released separately by their respective projects.

One goal of the Commons RDF API is to enable runtime cross-compatibility of its implementations, therefore it is perfectly valid to combine them and for instance do:

  • Copy triples from a Jena Model to an RDF4J Repository (e.g. copying between two Common RDF Graphs)
  • Create an RDF4J-backed Quad that use a Jena-backed BlankNode
  • Read an RDF file with Jena’s parsers into an RDF4J-backed Dataset

Commons RDF Simple

org.apache.commons.rdf.simple is part of Commons RDF, and its main purpose is to verify and clarify the test harness. It is backed by simple (if not naive) in-memory POJO objects and have no external dependencies.

Note that although this module fully implements the commons-rdf API, it should not be considered as a reference implementation. It is not thread-safe and probably not scalable, however it may be useful for testing and simple usage (e.g. prototyping and creating graph fragments).

Usage:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-rdf-simple</artifactId>
    <version>0.5.0</version>
</dependency>
import org.apache.commons.rdf.api.Graph;
import org.apache.commons.rdf.api.RDF;
import org.apache.commons.rdf.simple.SimpleRDF;

RDF rdf = new SimpleRDF();
Graph graph = rdf.createGraph();

Apache Jena

org.apache.commons.rdf.jena is an implementation of the Commons RDF API backed by Apache Jena, including converters from/to Jena and Commons RDF.

Usage:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-rdf-jena</artifactId>
    <version>0.5.0</version>
</dependency>
import org.apache.commons.rdf.api.Graph;
import org.apache.commons.rdf.api.RDFTermFactory;
import org.apache.commons.rdf.jena.JenaRDF;

RDF rdf = new JenaRDF();
Graph graph = rdf.createGraph();

Objects created with JenaRDF implement interfaces like JenaQuad and JenaLiteral which give access to the underlying Jena objects through methods like asJenaNode() and asJenaGraph().

JenaRDF includes additional methods for converting from/to Apache Jena and Commons RDF, like asRDFTerm(Node) and asJenaNode(RDFTerm).

Generalized RDF

Apache Jena can support generalized RDF, e.g.:

A generalized RDF triple is a triple having a subject, a predicate, and object, where each can be an IRI, a blank node or a literal.

Within Commons RDF it is possible to create generalized triples and quads using JenaRDF - however note that the returned JenaGeneralizedTripleLike and JenaGeneralizedQuadLike do not have the equality semantics of Triple or Quad and thus can’t be used with the regular Graph or Dataset methods.

The generalized triples/quads can be accessed as org.apache.jena.graph.Triple and org.apache.jena.sparql.core.Quad - but can’t currently be used with an equivalent generalized graph or generalized dataset within Commons RDF (see COMMONSRDF-42).

Eclipse RDF4J

org.apache.commons.rdf.rdf4j is an implementation of the Commons RDF API backed by Eclispe RDF4J 2.0 (formerly Sesame), including converters from/to RDF4J and Commons RDF.

Usage:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-rdf-rdf4j</artifactId>
    <version>0.5.0</version>
</dependency>
import org.apache.commons.rdf.api.Graph;
import org.apache.commons.rdf.api.RDF;
import org.apache.commons.rdf.rdf4j.RDF4J;

RDF rdf = new RDF4J();
Graph graph = rdf.createGraph();

Objects created with RDF4J implement interfaces like RDF4JTerm and RDF4JGraph which give access to the underlying Jena objects through methods like asValue() and asRepository().

RDF4J includes additional methods for converting from/to RDF4J and Commons RDF, like asTriple(Statement) and asRDFTerm(Value).

Closing RDF4J resources

When using RDF4J with an RDF4J Repository, e.g. from asRDFTermGraph(Repository), care must be taken to close underlying resources when using the methods stream() and iterate() for both Graphs and Datasets.

This can generally achieved using a try-with-resources block, e.g.:

int subjects;
try (Stream<RDF4JTriple> s : graph.stream(s,p,o)) {
  subjects = s.map(RDF4JTriple::getSubject).distinct().count()
}

This will ensure that the underlying RDF4J RepositoryConnection and RepositoryResult are closed after use.

Methods that return directly, like Graph.add() and Dataset.size() will use and close separate transactions per method calls and therefore do not need any special handling; however this will come with a performance hit when doing multiple graph/dataset modifications. (See COMMONSRDF-45)

Java’s java.util.Iteratable and java.util.Iterator does not extend AutoClosable, and as there are many ways that a for-each loop may not run to exhaustion, Commons RDF introduces ClosableIterable, which can be used with RDF4J as:

RDF4JGraph graph; // ...
try (ClosableIterable<Triple> s : graph.iterate()) {
 for (Triple t : triples) {
     return t; // OK to terminate for-loop early
 }
}

JSONLD-Java

org.apache.commons.rdf.jsonld is an implementation of the Commons RDF API backed by JSON-LD-Java.

This is primarily intended to support JSON-LD parsing and writing.

Usage:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-rdf-jsonld</artifactId>
    <version>0.5.0</version>
</dependency>
import org.apache.commons.rdf.api.Graph;
import org.apache.commons.rdf.api.RDFTermFactory;
import org.apache.commons.rdf.jsonld.JsonLdFactory;

RDF rdf = new JsonLdRDF();
Graph graph = rdfTermFactory.createGraph();

External implementations

OWL API

OWL API 5 extends Commons RDF directly for its family of RDFNode implementations. It is a partial compatibility implementation without its own RDFTermFactory, Graph or Dataset.

Related implementations

Apache Clerezza

Apache Clerezza is aligning its RDF core module with Commons RDF.