blob: 90a5b852faaa9a8b274db0d8d0997d62c9014835 [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<title>Apache Jena - An Introduction to RDF and the Jena RDF API</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="/css/bootstrap-icons.css" rel="stylesheet" media="screen"><link rel="stylesheet" type="text/css" href="https://jena.apache.org/sass/jena.1b17c39a117e22b46db4c66f6395dc27c134a60377d87d2d5745b8600eb69722.css" integrity="sha256-GxfDmhF&#43;IrRttMZvY5XcJ8E0pgN32H0tV0W4YA62lyI=">
<link rel="shortcut icon" href="/images/favicon.ico" />
</head>
<body>
<nav class="navbar navbar-expand-lg bg-body-tertiary" role="navigation">
<div class="container">
<div class="navbar-header">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="/index.html">
<img class="logo-menu" src="/images/jena-logo/jena-logo-notext-small.png" alt="jena logo">Apache Jena</a>
</div>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li id="homepage" class="nav-item"><a class="nav-link" href="/index.html"><span class="bi-house"></span> Home</a></li>
<li id="download" class="nav-item"><a class="nav-link" href="/download/index.cgi"><span class="bi-download"></span> Download</a></li>
<li class="nav-item dropdown">
<a href="#" class="nav-link dropdown-toggle" role="button" data-bs-toggle="dropdown" aria-expanded="false"><span class="bi-journal"></span> Learn <b class="caret"></b></a>
<ul class="dropdown-menu">
<li class="dropdown-header">Tutorials</li>
<li><a class="dropdown-item" href="/tutorials/index.html">Overview</a></li>
<li><a class="dropdown-item" href="/documentation/fuseki2/index.html">Fuseki Triplestore</a></li>
<li><a class="dropdown-item" href="/documentation/notes/index.html">How-To's</a></li>
<li><a class="dropdown-item" href="/documentation/query/manipulating_sparql_using_arq.html">Manipulating SPARQL using ARQ</a></li>
<li><a class="dropdown-item" href="/tutorials/rdf_api.html">RDF core API tutorial</a></li>
<li><a class="dropdown-item" href="/tutorials/sparql.html">SPARQL tutorial</a></li>
<li><a class="dropdown-item" href="/tutorials/using_jena_with_eclipse.html">Using Jena with Eclipse</a></li>
<li class="dropdown-divider"></li>
<li class="dropdown-header">References</li>
<li><a class="dropdown-item" href="/documentation/index.html">Overview</a></li>
<li><a class="dropdown-item" href="/documentation/query/index.html">ARQ (SPARQL)</a></li>
<li><a class="dropdown-item" href="/documentation/io/">RDF I/O</a></li>
<li><a class="dropdown-item" href="/documentation/assembler/index.html">Assembler</a></li>
<li><a class="dropdown-item" href="/documentation/tools/index.html">Command-line tools</a></li>
<li><a class="dropdown-item" href="/documentation/rdfs/">Data with RDFS Inferencing</a></li>
<li><a class="dropdown-item" href="/documentation/geosparql/index.html">GeoSPARQL</a></li>
<li><a class="dropdown-item" href="/documentation/inference/index.html">Inference API</a></li>
<li><a class="dropdown-item" href="/documentation/ontology/">Ontology API</a></li>
<li><a class="dropdown-item" href="/documentation/permissions/index.html">Permissions</a></li>
<li><a class="dropdown-item" href="/documentation/extras/querybuilder/index.html">Query Builder</a></li>
<li><a class="dropdown-item" href="/documentation/rdf/index.html">RDF API</a></li>
<li><a class="dropdown-item" href="/documentation/rdfconnection/">RDF Connection - SPARQL API</a></li>
<li><a class="dropdown-item" href="/documentation/rdfstar/index.html">RDF-star</a></li>
<li><a class="dropdown-item" href="/documentation/shacl/index.html">SHACL</a></li>
<li><a class="dropdown-item" href="/documentation/shex/index.html">ShEx</a></li>
<li><a class="dropdown-item" href="/documentation/tdb/index.html">TDB</a></li>
<li><a class="dropdown-item" href="/documentation/tdb2/index.html">TDB2</a></li>
<li><a class="dropdown-item" href="/documentation/query/text-query.html">Text Search</a></li>
</ul>
</li>
<li class="nav-item dropdown">
<a href="#" class="nav-link dropdown-toggle" role="button" data-bs-toggle="dropdown" aria-expanded="false"><span class="bi-journal-code"></span> Javadoc <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="/documentation/javadoc.html">All Javadoc</a></li>
<li><a class="dropdown-item" href="/documentation/javadoc/arq/">ARQ</a></li>
<li><a class="dropdown-item" href="/documentation/javadoc/fuseki2/">Fuseki</a></li>
<li><a class="dropdown-item" href="/documentation/javadoc/geosparql/">GeoSPARQL</a></li>
<li><a class="dropdown-item" href="/documentation/javadoc/jena/">Jena Core</a></li>
<li><a class="dropdown-item" href="/documentation/javadoc/permissions/">Permissions</a></li>
<li><a class="dropdown-item" href="/documentation/javadoc/extras/querybuilder/">Query Builder</a></li>
<li><a class="dropdown-item" href="/documentation/javadoc/shacl/">SHACL</a></li>
<li><a class="dropdown-item" href="/documentation/javadoc/tdb/">TDB</a></li>
<li><a class="dropdown-item" href="/documentation/javadoc/text/">Text Search</a></li>
</ul>
</li>
</ul>
<form class="d-flex" role="search" action="/search" method="GET">
<div class="input-group">
<input class="form-control border-end-0 border m-0" type="search" name="q" id="search-query" placeholder="Search...." aria-label="Search" style="width: 10rem;">
<button class="btn btn-outline-secondary border-start-0 border" type="submit">
<i class="bi-search"></i>
</button>
</div>
</form>
<ul class="navbar-nav">
<li id="ask" class="nav-item"><a class="nav-link" href="/help_and_support/index.html" title="Ask"><span class="bi-patch-question"></span><span class="text-body d-none d-xxl-inline"> Ask</span></a></li>
<li class="nav-item dropdown">
<a href="#" title="Get involved" class="nav-link dropdown-toggle" role="button" data-bs-toggle="dropdown" aria-expanded="false"><span class="bi-megaphone"></span><span class="text-body d-none d-xxl-inline"> Get involved </span><b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="/getting_involved/index.html">Contribute</a></li>
<li><a class="dropdown-item" href="/help_and_support/bugs_and_suggestions.html">Report a bug</a></li>
<li class="dropdown-divider"></li>
<li class="dropdown-header">Project</li>
<li><a class="dropdown-item" href="/about_jena/about.html">About Jena</a></li>
<li><a class="dropdown-item" href="/about_jena/architecture.html">Architecture</a></li>
<li><a class="dropdown-item" href="/about_jena/citing.html">Citing</a></li>
<li><a class="dropdown-item" href="/about_jena/team.html">Project team</a></li>
<li><a class="dropdown-item" href="/about_jena/contributions.html">Related projects</a></li>
<li><a class="dropdown-item" href="/about_jena/roadmap.html">Roadmap</a></li>
<li><a class="dropdown-item" href="/about_jena/security-advisories.html">Security Advisories</a></li>
<li class="dropdown-divider"></li>
<li class="dropdown-header">ASF</li>
<li><a class="dropdown-item" href="https://www.apache.org/">Apache Software Foundation</a></li>
<li><a class="dropdown-item" href="https://www.apache.org/foundation/sponsorship.html">Become a Sponsor</a></li>
<li><a class="dropdown-item" href="https://www.apache.org/licenses/LICENSE-2.0">License</a></li>
<li><a class="dropdown-item" href="https://www.apache.org/security/">Security</a></li>
<li><a class="dropdown-item" href="https://www.apache.org/foundation/thanks.html">Thanks</a></li>
</ul>
</li>
<li class="nav-item" id="edit"><a class="nav-link" href="https://github.com/apache/jena-site/edit/main/source/tutorials/rdf_api.md" title="Edit this page on GitHub"><span class="bi-pencil-square"></span><span class="text-body d-none d-xxl-inline"> Edit this page</span></a></li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-md-12">
<div id="breadcrumbs">
<ol class="breadcrumb mt-4 p-2 bg-body-tertiary">
<li class="breadcrumb-item"><a href='/tutorials'>TUTORIALS</a></li>
<li class="breadcrumb-item active">RDF API</li>
</ol>
</div>
<h1 class="title">An Introduction to RDF and the Jena RDF API</h1>
<main class="d-flex flex-xl-row flex-column">
<aside class="text-muted align-self-start mb-3 p-0 d-xl-none d-block">
<h2 class="h6 sticky-top m-0 p-2 bg-body-tertiary">On this page</h2>
<nav id="TableOfContents">
<ul>
<li><a href="#preface">Preface</a></li>
<li><a href="#ch-Introduction">Introduction</a></li>
<li><a href="#ch-Statements">Statements</a></li>
<li><a href="#ch-Writing-RDF">Writing RDF</a></li>
<li><a href="#ch-Reading-RDF">Reading RDF</a></li>
<li><a href="#ch-Prefixes">Controlling Prefixes</a>
<ul>
<li><a href="#explicit-prefix-definitions">Explicit prefix definitions</a></li>
</ul>
</li>
<li><a href="#ch-Jena-RDF-Packages">Jena RDF Packages</a></li>
<li><a href="#ch-Navigating-a-Model">Navigating a Model</a></li>
<li><a href="#ch-Querying-a-Model">Querying a Model</a></li>
<li><a href="#ch-Operations-on-Models">Operations on Models</a></li>
<li><a href="#ch-Containers">Containers</a></li>
<li><a href="#ch-More-about-Literals-and-Datatypes">More about Literals and Datatypes</a></li>
<li><a href="#ch-Glossary">Glossary</a></li>
<li><a href="#footnotes">Footnotes</a></li>
</ul>
</nav>
</aside>
<article class="flex-column me-lg-4">
<h2 id="preface">Preface</h2>
<p>This is a tutorial introduction to both W3C&rsquo;s Resource Description Framework
(RDF) and Jena, a Java API for RDF. It is written for the programmer who is
unfamiliar with RDF and who learns best by prototyping, or, for other
reasons, wishes to move quickly to implementation. Some familiarity
with both XML and Java is assumed.</p>
<p>Implementing too quickly, without first understanding the RDF data model,
leads to frustration and disappointment. Yet studying the data model
alone is dry stuff and often leads to tortuous metaphysical conundrums. It
is better to approach understanding both the data model and how to use it in
parallel. Learn a bit of the data model and try it out. Then
learn a bit more and try that out. Then the theory informs the practice
and the practice the theory. The data model is quite simple, so this
approach does not take long.</p>
<p>RDF has an XML syntax and many who are familiar with XML will think of RDF
in terms of that syntax. This is a mistake. RDF should be
understood in terms of its data model. RDF data can be represented in
XML, but understanding the syntax is secondary to understanding the data
model.</p>
<p>An implementation of the Jena API, including the working source code for
all the examples used in this tutorial can be downloaded from
<a href="//jena.apache.org/download/index.cgi"><code>jena.apache.org/download/index.cgi</code></a>.</p>
<h2 id="ch-Introduction">Introduction</h2>
<p>The Resource Description Framework (RDF) is a standard (technically a W3C
Recommendation) for describing resources. What is a resource? That is
rather a deep question and the precise definition is still the subject of
debate. For our purposes we can think of it as anything we can identify.
You are a resource, as is your home page, this tutorial, the number one and
the great white whale in Moby Dick.</p>
<p>Our examples in this tutorial will be about people. They use an <a
href="https://www.w3.org/TR/vcard-rdf/">RDF representation of VCARDS</a>. RDF
is best thought of in the form of node and arc diagrams. A simple vcard
might look like this in RDF:</p>
<p align="center">
<img border="0" src="figures/fig1.png" alt="figure 1" width="240" height="180"></p>
<p>The <a href="#glos-Resource"><i>resource</i></a>, John Smith, is shown as
an ellipse and is identified by a Uniform Resource Identifier (URI)<font
size="1"><sup><a href="#fn-01">1</a></sup></font>, in this case
"http://.../JohnSmith". If you try to access that resource using your
browser, you are unlikely to be successful; April the first jokes not
withstanding, you would be rather surprised if your browser were able to
deliver John Smith to your desk top. If you are unfamiliar with URI's,
think of them simply as rather strange looking names.</p>
<p>Resources have <a href="#glos-Property"><i>properties</i></a>. In these
examples we are interested in the sort of properties that would appear on
John Smith's business card. Figure 1 shows only one property, John
Smith's full name. A property is represented by an arc, labeled with the
name of a property. The name of a property is also a URI, but as URI's are
rather long and cumbersome, the diagram shows it in XML qname form. The
part before the ':' is called a namespace prefix and represents a
namespace. The part after the ':' is called a local name and represents
a name in that namespace. Properties are usually represented in this
qname form when written as RDF XML and it is a convenient shorthand for
representing them in diagrams and in text. Strictly, however,
properties are identified by a URI. The nsprefix:localname form is a
shorthand for the URI of the namespace concatenated with the localname.
There is no requirement that the URI of a property resolve to anything when
accessed by a browser.</p>
<p>Each property has a value. In this case the value is a <a
href="#glos-Literal"><i>literal</i></a>, which for now we can think of as a
strings of characters<font size="1"><sup><a href="#fn-02">2</a></sup></font>.
Literals are shown in rectangles.</p>
<p>Jena is a Java API which can be used to create and manipulate RDF graphs
like this one. Jena has object classes to represent graphs, resources,
properties and literals. The interfaces representing resources,
properties and literals are called Resource, Property and Literal
respectively. In Jena, a graph is called a model and is represented by the Model
interface.</p>
<p>The code to create this graph, or model, is simple:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#080;font-style:italic">// some definitions
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span><span style="color:#a2f;font-weight:bold">static</span> String personURI <span style="color:#666">=</span> <span style="color:#b44">&#34;http://somewhere/JohnSmith&#34;</span><span style="color:#666">;</span>
</span></span><span style="display:flex;"><span><span style="color:#a2f;font-weight:bold">static</span> String fullName <span style="color:#666">=</span> <span style="color:#b44">&#34;John Smith&#34;</span><span style="color:#666">;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic">// create an empty Model
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>Model model <span style="color:#666">=</span> ModelFactory<span style="color:#666">.</span><span style="color:#b44">createDefaultModel</span><span style="color:#666">();</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic">// create the resource
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>Resource johnSmith <span style="color:#666">=</span> model<span style="color:#666">.</span><span style="color:#b44">createResource</span><span style="color:#666">(</span>personURI<span style="color:#666">);</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic">// add the property
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>johnSmith<span style="color:#666">.</span><span style="color:#b44">addProperty</span><span style="color:#666">(</span>VCARD<span style="color:#666">.</span><span style="color:#b44">FN</span><span style="color:#666">,</span> fullName<span style="color:#666">);</span>
</span></span></code></pre></div><p>It begins with some constant definitions and then creates an empty Model
or model, using the <code>ModelFactory</code> method <code>createDefaultModel()</code>
to create a memory-based model. Jena contains other implementations
of the Model interface, e.g one which uses a relational database: these
types of Model are also available from ModelFactory.</p>
<p>The John Smith resource is then created and a property added to it.
The property is provided by a "constant" class VCARD which holds objects
representing all the definitions in the VCARD schema. Jena provides constant
classes for other well known schemas, such as RDF and RDF schema themselves,
Dublin Core and OWL.</p>
<p>The working code for this example can be found in the /src-examples directory of
the Jena distribution as
<a href="https://github.com/apache/jena/tree/main/jena-core/src-examples/jena/examples/rdf/Tutorial01.java">tutorial
1</a>.
As an exercise, take this code and modify it to create a simple VCARD for
yourself.</p>
<p>The code to create the resource and add the property, can be more
compactly written in a cascading style:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>Resource johnSmith <span style="color:#666">=</span>
</span></span><span style="display:flex;"><span> model<span style="color:#666">.</span><span style="color:#b44">createResource</span><span style="color:#666">(</span>personURI<span style="color:#666">)</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">.</span><span style="color:#b44">addProperty</span><span style="color:#666">(</span>VCARD<span style="color:#666">.</span><span style="color:#b44">FN</span><span style="color:#666">,</span> fullName<span style="color:#666">);</span>
</span></span></code></pre></div><p>Now let's add some more detail to the vcard, exploring some more features
of RDF and Jena.</p>
<p>In the first example, the property value was a literal. RDF
properties can also take other resources as their value. Using a common RDF
technique, this example shows how to represent the different parts of John
Smith's name:</p>
<p align="center">
<img border="0" src="figures/fig2.png" alt="figure 2" width="360" height="240"></p>
<p>Here we have added a new property, vcard:N, to represent the structure of
John Smith's name. There are several things of interest about this
Model. Note that the vcard:N property takes a resource as its value.
Note also that the ellipse representing the compound name has no URI.
It is known as an <i><a href="#glos-blank node">blank Node</a>.</i></p>
<p>The Jena code to construct this example, is again very simple. First
some declarations and the creation of the empty model.</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#080;font-style:italic">// some definitions
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>String personURI <span style="color:#666">=</span> <span style="color:#b44">&#34;http://somewhere/JohnSmith&#34;</span><span style="color:#666">;</span>
</span></span><span style="display:flex;"><span>String givenName <span style="color:#666">=</span> <span style="color:#b44">&#34;John&#34;</span><span style="color:#666">;</span>
</span></span><span style="display:flex;"><span>String familyName <span style="color:#666">=</span> <span style="color:#b44">&#34;Smith&#34;</span><span style="color:#666">;</span>
</span></span><span style="display:flex;"><span>String fullName <span style="color:#666">=</span> givenName <span style="color:#666">+</span> <span style="color:#b44">&#34; &#34;</span> <span style="color:#666">+</span> familyName<span style="color:#666">;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic">// create an empty Model
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>Model model <span style="color:#666">=</span> ModelFactory<span style="color:#666">.</span><span style="color:#b44">createDefaultModel</span><span style="color:#666">();</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic">// create the resource
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic">// and add the properties cascading style
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>Resource johnSmith
</span></span><span style="display:flex;"><span> <span style="color:#666">=</span> model<span style="color:#666">.</span><span style="color:#b44">createResource</span><span style="color:#666">(</span>personURI<span style="color:#666">)</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">.</span><span style="color:#b44">addProperty</span><span style="color:#666">(</span>VCARD<span style="color:#666">.</span><span style="color:#b44">FN</span><span style="color:#666">,</span> fullName<span style="color:#666">)</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">.</span><span style="color:#b44">addProperty</span><span style="color:#666">(</span>VCARD<span style="color:#666">.</span><span style="color:#b44">N</span><span style="color:#666">,</span>
</span></span><span style="display:flex;"><span> model<span style="color:#666">.</span><span style="color:#b44">createResource</span><span style="color:#666">()</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">.</span><span style="color:#b44">addProperty</span><span style="color:#666">(</span>VCARD<span style="color:#666">.</span><span style="color:#b44">Given</span><span style="color:#666">,</span> givenName<span style="color:#666">)</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">.</span><span style="color:#b44">addProperty</span><span style="color:#666">(</span>VCARD<span style="color:#666">.</span><span style="color:#b44">Family</span><span style="color:#666">,</span> familyName<span style="color:#666">));</span>
</span></span></code></pre></div><p>The working code for this example can be found as <a
href="https://github.com/apache/jena/tree/main/jena-core/src-examples/jena/examples/rdf/Tutorial02.java">tutorial 2</a> in the /src-examples directory
of the Jena distribution.</p>
<h2 id="ch-Statements">Statements</h2>
<p>Each arc in an RDF Model is called a <i><a
href="#glos-Statement">statement</a></i>. Each statement asserts a fact
about a resource. A statement has three parts:</p>
<ul>
<li>the <i><a href="#glos-Subject"> subject</a></i> is the resource from
which the arc leaves</li>
<li>the <i><a href="#glos-Predicate"> predicate</a></i> is the property
that labels the arc</li>
<li>the <i><a href="#glos-Object"> object</a></i> is the resource or
literal pointed to by the arc</li>
</ul>
<p>A statement is sometimes called a <a href="#glos-Triple">triple</a>,
because of its three parts.</p>
<p>An RDF Model is represented as a <i>set</i> of statements. Each call of
<code>addProperty</code> in tutorial2 added another statement to the Model.
(Because a Model is set of statements, adding a duplicate of a statement has no
effect.) The Jena model interface defines a <code>listStatements()</code>
method which returns an <code>StmtIterator</code>, a subtype of Java's
<code>Iterator</code> over all the statements in a Model.
<code>StmtIterator</code> has a method <code>nextStatement()</code>
which returns the next statement from the iterator (the same one that
<code>next()</code> would deliver, already cast to <code>Statement</code>).
The <code>Statement</code> interface provides accessor
methods to the subject, predicate and object of a statement.</p>
<p>Now we will use that interface to extend tutorial2 to list all the
statements created and print them out. The complete code for this can be
found in <a href="https://github.com/apache/jena/tree/main/jena-core/src-examples/jena/examples/rdf/Tutorial03.java">tutorial 3</a>.</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#080;font-style:italic">// list the statements in the Model
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>StmtIterator iter <span style="color:#666">=</span> model<span style="color:#666">.</span><span style="color:#b44">listStatements</span><span style="color:#666">();</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic">// print out the predicate, subject and object of each statement
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span><span style="color:#a2f;font-weight:bold">while</span> <span style="color:#666">(</span>iter<span style="color:#666">.</span><span style="color:#b44">hasNext</span><span style="color:#666">())</span> <span style="color:#666">{</span>
</span></span><span style="display:flex;"><span> Statement stmt <span style="color:#666">=</span> iter<span style="color:#666">.</span><span style="color:#b44">nextStatement</span><span style="color:#666">();</span> <span style="color:#080;font-style:italic">// get next statement
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span> Resource subject <span style="color:#666">=</span> stmt<span style="color:#666">.</span><span style="color:#b44">getSubject</span><span style="color:#666">();</span> <span style="color:#080;font-style:italic">// get the subject
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span> Property predicate <span style="color:#666">=</span> stmt<span style="color:#666">.</span><span style="color:#b44">getPredicate</span><span style="color:#666">();</span> <span style="color:#080;font-style:italic">// get the predicate
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span> RDFNode object <span style="color:#666">=</span> stmt<span style="color:#666">.</span><span style="color:#b44">getObject</span><span style="color:#666">();</span> <span style="color:#080;font-style:italic">// get the object
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>
</span></span><span style="display:flex;"><span> System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">.</span><span style="color:#b44">print</span><span style="color:#666">(</span>subject<span style="color:#666">.</span><span style="color:#b44">toString</span><span style="color:#666">());</span>
</span></span><span style="display:flex;"><span> System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">.</span><span style="color:#b44">print</span><span style="color:#666">(</span><span style="color:#b44">&#34; &#34;</span> <span style="color:#666">+</span> predicate<span style="color:#666">.</span><span style="color:#b44">toString</span><span style="color:#666">()</span> <span style="color:#666">+</span> <span style="color:#b44">&#34; &#34;</span><span style="color:#666">);</span>
</span></span><span style="display:flex;"><span> <span style="color:#a2f;font-weight:bold">if</span> <span style="color:#666">(</span>object <span style="color:#a2f;font-weight:bold">instanceof</span> Resource<span style="color:#666">)</span> <span style="color:#666">{</span>
</span></span><span style="display:flex;"><span> System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">.</span><span style="color:#b44">print</span><span style="color:#666">(</span>object<span style="color:#666">.</span><span style="color:#b44">toString</span><span style="color:#666">());</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">}</span> <span style="color:#a2f;font-weight:bold">else</span> <span style="color:#666">{</span>
</span></span><span style="display:flex;"><span> <span style="color:#080;font-style:italic">// object is a literal
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span> System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">.</span><span style="color:#b44">print</span><span style="color:#666">(</span><span style="color:#b44">&#34; \&#34;&#34;</span> <span style="color:#666">+</span> object<span style="color:#666">.</span><span style="color:#b44">toString</span><span style="color:#666">()</span> <span style="color:#666">+</span> <span style="color:#b44">&#34;\&#34;&#34;</span><span style="color:#666">);</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">}</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span> System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">.</span><span style="color:#b44">println</span><span style="color:#666">(</span><span style="color:#b44">&#34; .&#34;</span><span style="color:#666">);</span>
</span></span><span style="display:flex;"><span><span style="color:#666">}</span>
</span></span></code></pre></div><p>Since the object of a statement can be either a resource or a literal, the
<code>getObject()</code> method returns an
object typed as <code>RDFNode</code>, which is a
common superclass of both <code>Resource</code>
and <code>Literal</code>. The underlying object
is of the appropriate type, so the code uses <code>instanceof</code> to
determine which and
processes it accordingly.</p>
<p>When run, this program should produce output resembling:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-turtle" data-lang="turtle"><span style="display:flex;"><span><span style="">http</span>:<span style="">//somewhere/JohnSmith</span><span style="color:#bbb"> </span><span style="">http</span>:<span style="">//www</span>.<span style="">w</span><span style="color:#666">3</span>.<span style="">org/</span><span style="color:#666">2001</span><span style="">/vcard-rdf/</span><span style="color:#666">3.0</span><span style="color:#080;font-style:italic">#N 413f6415-c3b0-4259-b74d-4bd6e757eb60 .</span><span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span><span style="color:#666">413</span><span style="">f</span><span style="color:#666">6415</span><span style="">-c</span><span style="color:#666">3</span><span style="">b</span><span style="color:#666">0-4259</span><span style="">-b</span><span style="color:#666">74</span><span style="">d</span><span style="color:#666">-4</span><span style="">bd</span><span style="color:#666">6e757</span><span style="">eb</span><span style="color:#666">60</span><span style="color:#bbb"> </span><span style="">http</span>:<span style="">//www</span>.<span style="">w</span><span style="color:#666">3</span>.<span style="">org/</span><span style="color:#666">2001</span><span style="">/vcard-rdf/</span><span style="color:#666">3.0</span><span style="color:#080;font-style:italic">#Family &#34;Smith&#34; .</span><span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span><span style="color:#666">413</span><span style="">f</span><span style="color:#666">6415</span><span style="">-c</span><span style="color:#666">3</span><span style="">b</span><span style="color:#666">0-4259</span><span style="">-b</span><span style="color:#666">74</span><span style="">d</span><span style="color:#666">-4</span><span style="">bd</span><span style="color:#666">6e757</span><span style="">eb</span><span style="color:#666">60</span><span style="color:#bbb"> </span><span style="">http</span>:<span style="">//www</span>.<span style="">w</span><span style="color:#666">3</span>.<span style="">org/</span><span style="color:#666">2001</span><span style="">/vcard-rdf/</span><span style="color:#666">3.0</span><span style="color:#080;font-style:italic">#Given &#34;John&#34; .</span><span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span><span style="">http</span>:<span style="">//somewhere/JohnSmith</span><span style="color:#bbb"> </span><span style="">http</span>:<span style="">//www</span>.<span style="">w</span><span style="color:#666">3</span>.<span style="">org/</span><span style="color:#666">2001</span><span style="">/vcard-rdf/</span><span style="color:#666">3.0</span><span style="color:#080;font-style:italic">#FN &#34;John Smith&#34; .</span><span style="color:#bbb">
</span></span></span></code></pre></div><p></p>
<p>Now you know why it is clearer to draw Models. If you look carefully, you
will see that each line consists of three fields representing the subject,
predicate and object of each statement. There are four arcs in the Model, so
there are four statements. The "14df86:ecc3dee17b:-7fff" is an internal
identifier generated by Jena. It is not a URI and should not be confused
with one. It is simply an internal label used by the Jena implementation.</p>
<p>The W3C <a href="https://www.w3.org/2001/sw/RDFCore/">RDFCore Working
Group</a> have defined a similar simple notation called <a
href="https://www.w3.org/TR/rdf-testcases/#ntriples">N-Triples</a>. The name
means "triple notation". We will see in the next section that Jena has an
N-Triples writer built in.</p>
<h2 id="ch-Writing-RDF">Writing RDF</h2>
<p>Jena has methods for reading and writing RDF as XML. These can be
used to save an RDF model to a file and later read it back in again.</p>
<p>Tutorial 3 created a model and wrote it out in triple form. <a
href="https://github.com/apache/jena/tree/main/jena-core/src-examples/jena/examples/rdf/Tutorial04.java">Tutorial 4</a> modifies tutorial 3 to write the
model in RDF XML form to the standard output stream. The code again, is
very simple: <code>model.write</code> can take an <code>OutputStream</code>
argument.</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#080;font-style:italic">// now write the model in XML form to a file
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>model<span style="color:#666">.</span><span style="color:#b44">write</span><span style="color:#666">(</span>System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">);</span>
</span></span></code></pre></div><p>The output should look something like this:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">&lt;rdf:RDF</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">xmlns:rdf=</span><span style="color:#b44">&#39;http://www.w3.org/1999/02/22-rdf-syntax-ns#&#39;</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">xmlns:vcard=</span><span style="color:#b44">&#39;http://www.w3.org/2001/vcard-rdf/3.0#&#39;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdf:Description</span> <span style="color:#b44">rdf:about=</span><span style="color:#b44">&#39;http://somewhere/JohnSmith&#39;</span><span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:FN&gt;</span>John Smith<span style="color:#008000;font-weight:bold">&lt;/vcard:FN&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:N</span> <span style="color:#b44">rdf:nodeID=</span><span style="color:#b44">&#34;A0&#34;</span><span style="color:#008000;font-weight:bold">/&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;/rdf:Description&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdf:Description</span> <span style="color:#b44">rdf:nodeID=</span><span style="color:#b44">&#34;A0&#34;</span><span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:Given&gt;</span>John<span style="color:#008000;font-weight:bold">&lt;/vcard:Given&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:Family&gt;</span>Smith<span style="color:#008000;font-weight:bold">&lt;/vcard:Family&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;/rdf:Description&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">&lt;/rdf:RDF&gt;</span>
</span></span></code></pre></div><p>The RDF specifications specify how to represent RDF as XML. The RDF
XML syntax is quite complex. The reader is referred to the <a
href="https://www.w3.org/TR/rdf-primer/">primer</a> being developed by the
RDFCore WG for a more detailed introduction. However, let's take a quick look
at how to interpret the above.</p>
<p>RDF is usually embedded in an &lt;rdf:RDF&gt; element. The element is
optional if there are other ways of knowing that some XML is RDF, but it is
usually present. The RDF element defines the two namespaces used in the
document. There is then an &lt;rdf:Description&gt; element which describes
the resource whose URI is "http://somewhere/JohnSmith". If the rdf:about
attribute was missing, this element would represent a blank node.</p>
<p>The &lt;vcard:FN&gt; element describes a property of the resource. The
property name is the "FN" in the vcard namespace. RDF converts this to a URI
reference by concatenating the URI reference for the namespace prefix and
"FN", the local name part of the name. This gives a URI reference of
"http://www.w3.org/2001/vcard-rdf/3.0#FN". The value of the property
is the literal "John Smith".</p>
<p>The &lt;vcard:N&gt; element is a resource. In this case the resource is
represented by a relative URI reference. RDF converts this to an absolute
URI reference by concatenating it with the base URI of the current
document.</p>
<p>There is an error in this RDF XML; it does not exactly represent the Model
we created. The blank node in the Model has been given a URI reference. It
is no longer blank. The RDF/XML syntax is not capable of representing all
RDF Models; for example it cannot represent a blank node which is the object
of two statements. The 'dumb' writer we used to write this RDF/XML makes no
attempt to write correctly the subset of Models which can be written
correctly. It gives a URI to each blank node, making it no longer blank.</p>
<p>Jena has an extensible interface which allows new writers for different
serialization languages for RDF to be easily plugged in. The above call
invoked the standard 'dumb' writer. Jena also includes a more sophisticated
RDF/XML writer which can be invoked by using
<code>RDFDataMgr.write</code> function call:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#080;font-style:italic">// now write the model in a pretty form
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>RDFDataMgr<span style="color:#666">.</span><span style="color:#b44">write</span><span style="color:#666">(</span>System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">,</span> model<span style="color:#666">,</span> Lang<span style="color:#666">.</span><span style="color:#b44">RDFXML</span><span style="color:#666">);</span>
</span></span></code></pre></div><p>This writer, the so called PrettyWriter, takes advantage of features of
the RDF/XML abbreviated syntax to write a Model more compactly. It is also
able to preserve blank nodes where that is possible. It is however, not
suitable for writing very large Models, as its performance is unlikely to be
acceptable. To write large files and preserve blank nodes, write in
N-Triples format:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#080;font-style:italic">// now write the model in N-TRIPLES form
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>RDFDataMgr<span style="color:#666">.</span><span style="color:#b44">write</span><span style="color:#666">(</span>System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">,</span> model<span style="color:#666">,</span> Lang<span style="color:#666">.</span><span style="color:#b44">NTRIPLES</span><span style="color:#666">);</span>
</span></span></code></pre></div><p>This will produce output similar to that of tutorial 3 which conforms to
the N-Triples specification.</p>
<h2 id="ch-Reading-RDF">Reading RDF</h2>
<p><a href="https://github.com/apache/jena/tree/main/jena-core/src-examples/jena/examples/rdf/Tutorial05.java">Tutorial 5</a> demonstrates reading the
statements recorded in RDF XML form into a model. With this tutorial,
we have provided <a href="sparql_data/vc-db-1.rdf">a small database of vcards in RDF/XML form</a>. The following
code will read it in and write it out. <em>Note that for this application to
run, the input file must be in the current directory.</em></p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#080;font-style:italic">// create an empty model
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>Model model <span style="color:#666">=</span> ModelFactory<span style="color:#666">.</span><span style="color:#b44">createDefaultModel</span><span style="color:#666">();</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic">// use the RDFDataMgr to find the input file
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>InputStream in <span style="color:#666">=</span> RDFDataMgr<span style="color:#666">.</span><span style="color:#b44">open</span><span style="color:#666">(</span> inputFileName <span style="color:#666">);</span>
</span></span><span style="display:flex;"><span><span style="color:#a2f;font-weight:bold">if</span> <span style="color:#666">(</span>in <span style="color:#666">==</span> <span style="color:#a2f;font-weight:bold">null</span><span style="color:#666">)</span> <span style="color:#666">{</span>
</span></span><span style="display:flex;"><span> <span style="color:#a2f;font-weight:bold">throw</span> <span style="color:#a2f;font-weight:bold">new</span> IllegalArgumentException<span style="color:#666">(</span><span style="color:#b44">&#34;File: &#34;</span> <span style="color:#666">+</span> inputFileName <span style="color:#666">+</span> <span style="color:#b44">&#34; not found&#34;</span><span style="color:#666">);</span>
</span></span><span style="display:flex;"><span><span style="color:#666">}</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic">// read the RDF/XML file
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>model<span style="color:#666">.</span><span style="color:#b44">read</span><span style="color:#666">(</span>in<span style="color:#666">,</span> <span style="color:#a2f;font-weight:bold">null</span><span style="color:#666">);</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic">// write it to standard out
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>model<span style="color:#666">.</span><span style="color:#b44">write</span><span style="color:#666">(</span>System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">);</span>
</span></span></code></pre></div><p>The second argument to the <code>read()</code> method call is the URI which will
be used for resolving relative URI's. As there are no relative URI
references in the test file, it is allowed to be empty. When run, <a
href="https://github.com/apache/jena/tree/main/jena-core/src-examples/jena/examples/rdf/Tutorial05.java"> tutorial 5</a> will produce XML output which
looks like:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">&lt;rdf:RDF</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">xmlns:rdf=</span><span style="color:#b44">&#39;http://www.w3.org/1999/02/22-rdf-syntax-ns#&#39;</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">xmlns:vcard=</span><span style="color:#b44">&#39;http://www.w3.org/2001/vcard-rdf/3.0#&#39;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdf:Description</span> <span style="color:#b44">rdf:nodeID=</span><span style="color:#b44">&#34;A0&#34;</span><span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:Family&gt;</span>Smith<span style="color:#008000;font-weight:bold">&lt;/vcard:Family&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:Given&gt;</span>John<span style="color:#008000;font-weight:bold">&lt;/vcard:Given&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;/rdf:Description&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdf:Description</span> <span style="color:#b44">rdf:about=</span><span style="color:#b44">&#39;http://somewhere/JohnSmith/&#39;</span><span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:FN&gt;</span>John Smith<span style="color:#008000;font-weight:bold">&lt;/vcard:FN&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:N</span> <span style="color:#b44">rdf:nodeID=</span><span style="color:#b44">&#34;A0&#34;</span><span style="color:#008000;font-weight:bold">/&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;/rdf:Description&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdf:Description</span> <span style="color:#b44">rdf:about=</span><span style="color:#b44">&#39;http://somewhere/SarahJones/&#39;</span><span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:FN&gt;</span>Sarah Jones<span style="color:#008000;font-weight:bold">&lt;/vcard:FN&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:N</span> <span style="color:#b44">rdf:nodeID=</span><span style="color:#b44">&#34;A1&#34;</span><span style="color:#008000;font-weight:bold">/&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;/rdf:Description&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdf:Description</span> <span style="color:#b44">rdf:about=</span><span style="color:#b44">&#39;http://somewhere/MattJones/&#39;</span><span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:FN&gt;</span>Matt Jones<span style="color:#008000;font-weight:bold">&lt;/vcard:FN&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:N</span> <span style="color:#b44">rdf:nodeID=</span><span style="color:#b44">&#34;A2&#34;</span><span style="color:#008000;font-weight:bold">/&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;/rdf:Description&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdf:Description</span> <span style="color:#b44">rdf:nodeID=</span><span style="color:#b44">&#34;A3&#34;</span><span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:Family&gt;</span>Smith<span style="color:#008000;font-weight:bold">&lt;/vcard:Family&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:Given&gt;</span>Rebecca<span style="color:#008000;font-weight:bold">&lt;/vcard:Given&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;/rdf:Description&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdf:Description</span> <span style="color:#b44">rdf:nodeID=</span><span style="color:#b44">&#34;A1&#34;</span><span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:Family&gt;</span>Jones<span style="color:#008000;font-weight:bold">&lt;/vcard:Family&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:Given&gt;</span>Sarah<span style="color:#008000;font-weight:bold">&lt;/vcard:Given&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;/rdf:Description&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdf:Description</span> <span style="color:#b44">rdf:nodeID=</span><span style="color:#b44">&#34;A2&#34;</span><span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:Family&gt;</span>Jones<span style="color:#008000;font-weight:bold">&lt;/vcard:Family&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:Given&gt;</span>Matthew<span style="color:#008000;font-weight:bold">&lt;/vcard:Given&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;/rdf:Description&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdf:Description</span> <span style="color:#b44">rdf:about=</span><span style="color:#b44">&#39;http://somewhere/RebeccaSmith/&#39;</span><span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:FN&gt;</span>Becky Smith<span style="color:#008000;font-weight:bold">&lt;/vcard:FN&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:N</span> <span style="color:#b44">rdf:nodeID=</span><span style="color:#b44">&#34;A3&#34;</span><span style="color:#008000;font-weight:bold">/&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;/rdf:Description&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">&lt;/rdf:RDF&gt;</span>
</span></span></code></pre></div><h2 id="ch-Prefixes">Controlling Prefixes</h2>
<h3 id="explicit-prefix-definitions">Explicit prefix definitions</h3>
<p>In the previous section, we saw that the output XML declared a namespace
prefix <code>vcard</code> and used that prefix to abbreviate URIs. While RDF
uses only the full URIs, and not this shortened form, Jena provides ways
of controlling the namespaces used on output with its <i>prefix mappings</i>.
Here&rsquo;s a simple example.</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>Model m <span style="color:#666">=</span> ModelFactory<span style="color:#666">.</span><span style="color:#b44">createDefaultModel</span><span style="color:#666">();</span>
</span></span><span style="display:flex;"><span>String nsA <span style="color:#666">=</span> <span style="color:#b44">&#34;http://somewhere/else#&#34;</span><span style="color:#666">;</span>
</span></span><span style="display:flex;"><span>String nsB <span style="color:#666">=</span> <span style="color:#b44">&#34;http://nowhere/else#&#34;</span><span style="color:#666">;</span>
</span></span><span style="display:flex;"><span>Resource root <span style="color:#666">=</span> m<span style="color:#666">.</span><span style="color:#b44">createResource</span><span style="color:#666">(</span> nsA <span style="color:#666">+</span> <span style="color:#b44">&#34;root&#34;</span> <span style="color:#666">);</span>
</span></span><span style="display:flex;"><span>Property P <span style="color:#666">=</span> m<span style="color:#666">.</span><span style="color:#b44">createProperty</span><span style="color:#666">(</span> nsA <span style="color:#666">+</span> <span style="color:#b44">&#34;P&#34;</span> <span style="color:#666">);</span>
</span></span><span style="display:flex;"><span>Property Q <span style="color:#666">=</span> m<span style="color:#666">.</span><span style="color:#b44">createProperty</span><span style="color:#666">(</span> nsB <span style="color:#666">+</span> <span style="color:#b44">&#34;Q&#34;</span> <span style="color:#666">);</span>
</span></span><span style="display:flex;"><span>Resource x <span style="color:#666">=</span> m<span style="color:#666">.</span><span style="color:#b44">createResource</span><span style="color:#666">(</span> nsA <span style="color:#666">+</span> <span style="color:#b44">&#34;x&#34;</span> <span style="color:#666">);</span>
</span></span><span style="display:flex;"><span>Resource y <span style="color:#666">=</span> m<span style="color:#666">.</span><span style="color:#b44">createResource</span><span style="color:#666">(</span> nsA <span style="color:#666">+</span> <span style="color:#b44">&#34;y&#34;</span> <span style="color:#666">);</span>
</span></span><span style="display:flex;"><span>Resource z <span style="color:#666">=</span> m<span style="color:#666">.</span><span style="color:#b44">createResource</span><span style="color:#666">(</span> nsA <span style="color:#666">+</span> <span style="color:#b44">&#34;z&#34;</span> <span style="color:#666">);</span>
</span></span><span style="display:flex;"><span>m<span style="color:#666">.</span><span style="color:#b44">add</span><span style="color:#666">(</span> root<span style="color:#666">,</span> P<span style="color:#666">,</span> x <span style="color:#666">).</span><span style="color:#b44">add</span><span style="color:#666">(</span> root<span style="color:#666">,</span> P<span style="color:#666">,</span> y <span style="color:#666">).</span><span style="color:#b44">add</span><span style="color:#666">(</span> y<span style="color:#666">,</span> Q<span style="color:#666">,</span> z <span style="color:#666">);</span>
</span></span><span style="display:flex;"><span>System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">.</span><span style="color:#b44">println</span><span style="color:#666">(</span> <span style="color:#b44">&#34;# -- no special prefixes defined&#34;</span> <span style="color:#666">);</span>
</span></span><span style="display:flex;"><span>m<span style="color:#666">.</span><span style="color:#b44">write</span><span style="color:#666">(</span> System<span style="color:#666">.</span><span style="color:#b44">out</span> <span style="color:#666">);</span>
</span></span><span style="display:flex;"><span>System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">.</span><span style="color:#b44">println</span><span style="color:#666">(</span> <span style="color:#b44">&#34;# -- nsA defined&#34;</span> <span style="color:#666">);</span>
</span></span><span style="display:flex;"><span>m<span style="color:#666">.</span><span style="color:#b44">setNsPrefix</span><span style="color:#666">(</span> <span style="color:#b44">&#34;nsA&#34;</span><span style="color:#666">,</span> nsA <span style="color:#666">);</span>
</span></span><span style="display:flex;"><span>m<span style="color:#666">.</span><span style="color:#b44">write</span><span style="color:#666">(</span> System<span style="color:#666">.</span><span style="color:#b44">out</span> <span style="color:#666">);</span>
</span></span><span style="display:flex;"><span>System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">.</span><span style="color:#b44">println</span><span style="color:#666">(</span> <span style="color:#b44">&#34;# -- nsA and cat defined&#34;</span> <span style="color:#666">);</span>
</span></span><span style="display:flex;"><span>m<span style="color:#666">.</span><span style="color:#b44">setNsPrefix</span><span style="color:#666">(</span> <span style="color:#b44">&#34;cat&#34;</span><span style="color:#666">,</span> nsB <span style="color:#666">);</span>
</span></span><span style="display:flex;"><span>m<span style="color:#666">.</span><span style="color:#b44">write</span><span style="color:#666">(</span> System<span style="color:#666">.</span><span style="color:#b44">out</span> <span style="color:#666">);</span>
</span></span></code></pre></div><p>The output from this fragment is lots of RDF/XML, with
three different prefix mappings. First the default, with no
prefixes other than the standard ones:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span># -- no special prefixes defined
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">&lt;rdf:RDF</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">xmlns:j.0=</span><span style="color:#b44">&#34;http://nowhere/else#&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">xmlns:rdf=</span><span style="color:#b44">&#34;http://www.w3.org/1999/02/22-rdf-syntax-ns#&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">xmlns:j.1=</span><span style="color:#b44">&#34;http://somewhere/else#&#34;</span> <span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdf:Description</span> <span style="color:#b44">rdf:about=</span><span style="color:#b44">&#34;http://somewhere/else#root&#34;</span><span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;j.1:P</span> <span style="color:#b44">rdf:resource=</span><span style="color:#b44">&#34;http://somewhere/else#x&#34;</span><span style="color:#008000;font-weight:bold">/&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;j.1:P</span> <span style="color:#b44">rdf:resource=</span><span style="color:#b44">&#34;http://somewhere/else#y&#34;</span><span style="color:#008000;font-weight:bold">/&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;/rdf:Description&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdf:Description</span> <span style="color:#b44">rdf:about=</span><span style="color:#b44">&#34;http://somewhere/else#y&#34;</span><span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;j.0:Q</span> <span style="color:#b44">rdf:resource=</span><span style="color:#b44">&#34;http://somewhere/else#z&#34;</span><span style="color:#008000;font-weight:bold">/&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;/rdf:Description&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">&lt;/rdf:RDF&gt;</span>
</span></span></code></pre></div><p>We see that the rdf namespace is declared automatically, since it
is required for tags such as <code>&lt;rdf:RDF&gt;</code> and
<code>&lt;rdf:resource&gt;</code>. XML namespace declarations are also
needed for using the two properties P and Q, but since their
prefixes have not been introduced to the model in this example,
they get invented namespace names: <code>j.0</code> and <code>j.1</code>.</p>
<p>The method <code>setNsPrefix(String prefix, String URI)</code>
declares that the namespace <code>URI</code> may be abbreviated
by <code>prefix</code>. Jena requires that <code>prefix</code> be
a legal XML namespace name, and that <code>URI</code> ends with a
non-name character. The RDF/XML writer will turn these prefix
declarations into XML namespace declarations and use them in its
output:
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span># -- nsA defined
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">&lt;rdf:RDF</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">xmlns:j.0=</span><span style="color:#b44">&#34;http://nowhere/else#&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">xmlns:rdf=</span><span style="color:#b44">&#34;http://www.w3.org/1999/02/22-rdf-syntax-ns#&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">xmlns:nsA=</span><span style="color:#b44">&#34;http://somewhere/else#&#34;</span> <span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdf:Description</span> <span style="color:#b44">rdf:about=</span><span style="color:#b44">&#34;http://somewhere/else#root&#34;</span><span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;nsA:P</span> <span style="color:#b44">rdf:resource=</span><span style="color:#b44">&#34;http://somewhere/else#x&#34;</span><span style="color:#008000;font-weight:bold">/&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;nsA:P</span> <span style="color:#b44">rdf:resource=</span><span style="color:#b44">&#34;http://somewhere/else#y&#34;</span><span style="color:#008000;font-weight:bold">/&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;/rdf:Description&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdf:Description</span> <span style="color:#b44">rdf:about=</span><span style="color:#b44">&#34;http://somewhere/else#y&#34;</span><span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;j.0:Q</span> <span style="color:#b44">rdf:resource=</span><span style="color:#b44">&#34;http://somewhere/else#z&#34;</span><span style="color:#008000;font-weight:bold">/&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;/rdf:Description&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">&lt;/rdf:RDF&gt;</span>
</span></span></code></pre></div><p>The other namespace still gets the constructed name, but the nsA name
is now used in the property tags. There&rsquo;s no need for the prefix name
to have anything to do with the variables in the Jena code:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span># -- nsA and cat defined
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">&lt;rdf:RDF</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">xmlns:cat=</span><span style="color:#b44">&#34;http://nowhere/else#&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">xmlns:rdf=</span><span style="color:#b44">&#34;http://www.w3.org/1999/02/22-rdf-syntax-ns#&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">xmlns:nsA=</span><span style="color:#b44">&#34;http://somewhere/else#&#34;</span> <span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdf:Description</span> <span style="color:#b44">rdf:about=</span><span style="color:#b44">&#34;http://somewhere/else#root&#34;</span><span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;nsA:P</span> <span style="color:#b44">rdf:resource=</span><span style="color:#b44">&#34;http://somewhere/else#x&#34;</span><span style="color:#008000;font-weight:bold">/&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;nsA:P</span> <span style="color:#b44">rdf:resource=</span><span style="color:#b44">&#34;http://somewhere/else#y&#34;</span><span style="color:#008000;font-weight:bold">/&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;/rdf:Description&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdf:Description</span> <span style="color:#b44">rdf:about=</span><span style="color:#b44">&#34;http://somewhere/else#y&#34;</span><span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;cat:Q</span> <span style="color:#b44">rdf:resource=</span><span style="color:#b44">&#34;http://somewhere/else#z&#34;</span><span style="color:#008000;font-weight:bold">/&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;/rdf:Description&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">&lt;/rdf:RDF&gt;</span>
</span></span></code></pre></div><p>Both prefixes are used for output, and no generated prefixes are needed.</p>
<h3>Implicit prefix definitions</h3>
<p>As well as prefix declarations provided by calls to <code>setNsPrefix</code>,
Jena will remember the prefixes that were used in input to
<code>model.read()</code>.</p>
<p>Take the output produced by the previous fragment, and paste it into
some file, with URL <b>file:/tmp/fragment.rdf</b> say. Then run the
code:
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>Model m2 <span style="color:#666">=</span> ModelFactory<span style="color:#666">.</span><span style="color:#b44">createDefaultModel</span><span style="color:#666">();</span>
</span></span><span style="display:flex;"><span>m2<span style="color:#666">.</span><span style="color:#b44">read</span><span style="color:#666">(</span> <span style="color:#b44">&#34;file:/tmp/fragment.rdf&#34;</span> <span style="color:#666">);</span>
</span></span><span style="display:flex;"><span>m2<span style="color:#666">.</span><span style="color:#b44">write</span><span style="color:#666">(</span> System<span style="color:#666">.</span><span style="color:#b44">out</span> <span style="color:#666">);</span>
</span></span></code></pre></div><p>You&rsquo;ll see that the prefixes from the input are preserved in the output.
All the prefixes are written, even if they&rsquo;re not used anywhere. You can
remove a prefix with <code>removeNsPrefix(String prefix)</code> if you
don&rsquo;t want it in the output.</p>
<p>Since NTriples doesn't have any short way of writing URIs, it takes
no notice of prefixes on output and doesn't provide any on input. The
notation <b>N3</b>, also supported by Jena, does have short prefixed names,
and records them on input and uses them on output.
<p>Jena has further operations on the prefix mappings that a model holds,
such as extracting a Java <code>Map</code> of the exiting mappings, or
adding a whole group of mappings at once; see the documentation for
<code>PrefixMapping</code> for details.
<h2 id="ch-Jena-RDF-Packages">Jena RDF Packages</h2>
<p>Jena is a Java API for semantic web applications. The key RDF package for
the application developer is
<code>org.apache.jena.rdf.model</code>. The API has been defined
in terms of interfaces so that application code can work with different
implementations without change. This package contains interfaces for
representing models, resources, properties, literals, statements and all the
other key concepts of RDF, and a ModelFactory for creating models. So that
application code remains independent of
the implementation, it is best if it uses interfaces wherever possible, not
specific class implementations.</p>
<p>The <code>org.apache.jena.tutorial</code> package contains the
working source code for all the examples used in this tutorial.</p>
<p>The <code>org.apache.jena...impl</code> packages contains
implementation classes which may be common to many implementations. For
example, they defines classes <code>ResourceImpl</code>,
<code>PropertyImpl</code>, and <code>LiteralImpl</code> which may be
used directly or subclassed by different implementations. Applications
should rarely, if ever, use these classes directly. For example, rather
than creating a new instance of <code>ResourceImpl</code>, it is better to
use the <code>createResource</code> method of whatever model is being
used. That way, if the model implementation has used an optimized
implementation of <code>Resource</code>, then no conversions between the two
types will be necessary.</p>
<h2 id="ch-Navigating-a-Model">Navigating a Model</h2>
<p>So far, this tutorial has dealt mainly with creating, reading and writing
RDF Models. It is now time to deal with accessing information held in a
Model.</p>
<p>Given the URI of a resource, the resource object can be retrieved from a
model using the <code>Model.getResource(String uri)</code> method. This
method is defined to return a Resource object if one exists in the model, or
otherwise to create a new one. For example, to retrieve the John Smith
resource from the model read in from the file in tutorial 5:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#080;font-style:italic">// retrieve the John Smith vcard resource from the model
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>Resource vcard <span style="color:#666">=</span> model<span style="color:#666">.</span><span style="color:#b44">getResource</span><span style="color:#666">(</span>johnSmithURI<span style="color:#666">);</span>
</span></span></code></pre></div><p>The Resource interface defines a number of methods for accessing the
properties of a resource. The <code>Resource.getProperty(Property
p)</code> method accesses a property of the resource. This method does
not follow the usual Java accessor convention in that the type of the object
returned is <code>Statement,</code> not the <code>Property</code> that you
might have expected. Returning the whole statement allows the application to
access the value of the property using one of its accessor methods which
return the object of the statement. For example to retrieve the
resource which is the value of the <code>vcard:N</code> property:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#080;font-style:italic">// retrieve the value of the N property
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>Resource name <span style="color:#666">=</span> <span style="color:#666">(</span>Resource<span style="color:#666">)</span> vcard<span style="color:#666">.</span><span style="color:#b44">getProperty</span><span style="color:#666">(</span>VCARD<span style="color:#666">.</span><span style="color:#b44">N</span><span style="color:#666">)</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">.</span><span style="color:#b44">getObject</span><span style="color:#666">();</span>
</span></span></code></pre></div><p>In general, the object of a statement could be a resource or a literal, so
the application code, knowing the value must be a resource, casts the
returned object. One of the things that Jena tries to do is to provide
type specific methods so the application does not have to cast and type
checking can be done at compile time. The code fragment above, can be
more conveniently written:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#080;font-style:italic">// retrieve the value of the N property
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>Resource name <span style="color:#666">=</span> vcard<span style="color:#666">.</span><span style="color:#b44">getProperty</span><span style="color:#666">(</span>VCARD<span style="color:#666">.</span><span style="color:#b44">N</span><span style="color:#666">)</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">.</span><span style="color:#b44">getResource</span><span style="color:#666">();</span>
</span></span></code></pre></div><p>Similarly, the literal value of a property can be retrieved:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java//" data-lang="java//"><span style="display:flex;"><span>String fullName <span style="color:#666">=</span> vcard<span style="color:#666">.</span><span style="color:#b44">getProperty</span><span style="color:#666">(</span>VCARD<span style="color:#666">.</span><span style="color:#b44">FN</span><span style="color:#666">)</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">.</span><span style="color:#b44">getString</span><span style="color:#666">();</span>
</span></span></code></pre></div><p>In this example, the vcard resource has only one <code>vcard:FN</code> and
one <code>vcard:N</code> property. RDF permits a resource to repeat a
property; for example Adam might have more than one nickname. Let's give him
two:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#080;font-style:italic">// add two nickname properties to vcard
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>vcard<span style="color:#666">.</span><span style="color:#b44">addProperty</span><span style="color:#666">(</span>VCARD<span style="color:#666">.</span><span style="color:#b44">NICKNAME</span><span style="color:#666">,</span> <span style="color:#b44">&#34;Smithy&#34;</span><span style="color:#666">)</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">.</span><span style="color:#b44">addProperty</span><span style="color:#666">(</span>VCARD<span style="color:#666">.</span><span style="color:#b44">NICKNAME</span><span style="color:#666">,</span> <span style="color:#b44">&#34;Adman&#34;</span><span style="color:#666">);</span>
</span></span></code></pre></div><p>As noted before, Jena represents an RDF Model as <em>set </em>of
statements, so adding a statement with the subject, predicate and object as
one already in the Model will have no effect. Jena does not define which of
the two nicknames present in the Model will be returned. The result of
calling <code>vcard.getProperty(VCARD.NICKNAME)</code> is indeterminate. Jena
will return one of the values, but there is no guarantee even that two
consecutive calls will return the same value.</p>
<p>If it is possible that a property may occur more than once, then the
Resource.listProperties(Property p) method can be used to return an iterator
which will list them all. This method returns an iterator which returns
objects of type <code>Statement</code>. We can list the nicknames like
this:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#080;font-style:italic">// set up the output
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">.</span><span style="color:#b44">println</span><span style="color:#666">(</span><span style="color:#b44">&#34;The nicknames of \&#34;&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">+</span> fullName <span style="color:#666">+</span> <span style="color:#b44">&#34;\&#34; are:&#34;</span><span style="color:#666">);</span>
</span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic">// list the nicknames
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>StmtIterator iter <span style="color:#666">=</span> vcard<span style="color:#666">.</span><span style="color:#b44">listProperties</span><span style="color:#666">(</span>VCARD<span style="color:#666">.</span><span style="color:#b44">NICKNAME</span><span style="color:#666">);</span>
</span></span><span style="display:flex;"><span><span style="color:#a2f;font-weight:bold">while</span> <span style="color:#666">(</span>iter<span style="color:#666">.</span><span style="color:#b44">hasNext</span><span style="color:#666">())</span> <span style="color:#666">{</span>
</span></span><span style="display:flex;"><span> System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">.</span><span style="color:#b44">println</span><span style="color:#666">(</span><span style="color:#b44">&#34; &#34;</span> <span style="color:#666">+</span> iter<span style="color:#666">.</span><span style="color:#b44">nextStatement</span><span style="color:#666">()</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">.</span><span style="color:#b44">getObject</span><span style="color:#666">()</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">.</span><span style="color:#b44">toString</span><span style="color:#666">());</span>
</span></span><span style="display:flex;"><span><span style="color:#666">}</span>
</span></span></code></pre></div><p>This code can be found in <a href="https://github.com/apache/jena/tree/main/jena-core/src-examples/jena/examples/rdf/Tutorial06.java"> tutorial 6</a>.
The statement iterator <code>iter</code> produces each and every statement
with subject <code>vcard</code> and predicate <code>VCARD.NICKNAME</code>,
so looping over it allows us to fetch each statement by using
<code>nextStatement()</code>, get the object field, and convert it to
a string.
The code produces the following output when run:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>The nicknames of <span style="color:#b44">&#34;John Smith&#34;</span> are:
</span></span><span style="display:flex;"><span> Smithy
</span></span><span style="display:flex;"><span> Adman
</span></span></code></pre></div><p>All the properties of a resource can be listed by using the
<code>listProperties()</code> method without an argument.
</p>
<h2 id="ch-Querying-a-Model">Querying a Model</h2>
<p>The previous section dealt with the case of navigating a model from a
resource with a known URI. This section deals with searching a
model. The core Jena API supports only a limited query primitive. The
more powerful query facilities of SPARQL are described elsewhere.</p>
<p>The <code>Model.listStatements()</code> method, which lists all the
statements in a model, is perhaps the crudest way of querying a model.
Its use is not recommended on very large Models.
<code>Model.listSubjects()</code> is similar, but returns an iterator over
all resources that have properties, <i>ie</i> are the subject of some
statement.</p>
<p><code>Model.listSubjectsWithProperty(Property p, RDFNode
o)</code><code></code> will return an iterator over all the resources which
have property <code>p</code> with value <code>o</code>. If we assume that
only vcard resources
will have <code>vcard:FN</code> property, and that in our data, all such
resources have such a property, then we can find all the vcards like this:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#080;font-style:italic">// list vcards
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>ResIterator iter <span style="color:#666">=</span> model<span style="color:#666">.</span><span style="color:#b44">listSubjectsWithProperty</span><span style="color:#666">(</span>VCARD<span style="color:#666">.</span><span style="color:#b44">FN</span><span style="color:#666">);</span>
</span></span><span style="display:flex;"><span><span style="color:#a2f;font-weight:bold">while</span> <span style="color:#666">(</span>iter<span style="color:#666">.</span><span style="color:#b44">hasNext</span><span style="color:#666">())</span> <span style="color:#666">{</span>
</span></span><span style="display:flex;"><span> Resource r <span style="color:#666">=</span> iter<span style="color:#666">.</span><span style="color:#b44">nextResource</span><span style="color:#666">();</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">...</span>
</span></span><span style="display:flex;"><span><span style="color:#666">}</span>
</span></span></code></pre></div><p>All these query methods are simply syntactic sugar over a primitive query
method <code>model.listStatements(Selector s)</code>. This method returns an
iterator over all the statements in the model 'selected' by <code>s</code>.
The selector interface is designed to be extensible, but for now, there is
only one implementation of it, the class <code>SimpleSelector</code> from the
package <code>org.apache.jena.rdf.model</code>. Using
<code>SimpleSelector </code>is one of the rare occasions in Jena when it is
necessary to use a specific class rather than an interface. The
<code>SimpleSelector</code> constructor takes three arguments:</p>
<pre>Selector selector = new SimpleSelector(subject, predicate, object);</pre>
<p>This selector will select all statements with a subject that matches
<code>subject</code>, a predicate that matches <code>predicate</code> and an
object that matches <code>object</code>. If a <code>null</code> is supplied
in any of the positions, it matches anything; otherwise they match corresponding
equal resources or literals. (Two resources are equal if they have equal URIs
or are the same blank node; two literals are the same if all their components
are equal.) Thus:</p>
<pre>Selector selector = new SimpleSelector(null, null, null);</pre>
<p>will select all the statements in a Model.</p>
<pre>Selector selector = new SimpleSelector(null, VCARD.FN, null);</pre>
<p>will select all the statements with VCARD.FN as their predicate, whatever
the subject or object. As a special shorthand,
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>listStatements<span style="color:#666">(</span> S<span style="color:#666">,</span> P<span style="color:#666">,</span> O <span style="color:#666">)</span>
</span></span></code></pre></div><p>is equivalent to</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>listStatements<span style="color:#666">(</span> <span style="color:#a2f;font-weight:bold">new</span> SimpleSelector<span style="color:#666">(</span> S<span style="color:#666">,</span> P<span style="color:#666">,</span> O <span style="color:#666">)</span> <span style="color:#666">)</span>
</span></span></code></pre></div><p>
The following code, which can be found in full in <a
href="https://github.com/apache/jena/tree/main/jena-core/src-examples/jena/examples/rdf/Tutorial07.java">tutorial 7</a> lists the full names on all the
vcards in the database.</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#080;font-style:italic">// select all the resources with a VCARD.FN property
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>ResIterator iter <span style="color:#666">=</span> model<span style="color:#666">.</span><span style="color:#b44">listSubjectsWithProperty</span><span style="color:#666">(</span>VCARD<span style="color:#666">.</span><span style="color:#b44">FN</span><span style="color:#666">);</span>
</span></span><span style="display:flex;"><span><span style="color:#a2f;font-weight:bold">if</span> <span style="color:#666">(</span>iter<span style="color:#666">.</span><span style="color:#b44">hasNext</span><span style="color:#666">())</span> <span style="color:#666">{</span>
</span></span><span style="display:flex;"><span> System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">.</span><span style="color:#b44">println</span><span style="color:#666">(</span><span style="color:#b44">&#34;The database contains vcards for:&#34;</span><span style="color:#666">);</span>
</span></span><span style="display:flex;"><span> <span style="color:#a2f;font-weight:bold">while</span> <span style="color:#666">(</span>iter<span style="color:#666">.</span><span style="color:#b44">hasNext</span><span style="color:#666">())</span> <span style="color:#666">{</span>
</span></span><span style="display:flex;"><span> System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">.</span><span style="color:#b44">println</span><span style="color:#666">(</span><span style="color:#b44">&#34; &#34;</span> <span style="color:#666">+</span> iter<span style="color:#666">.</span><span style="color:#b44">nextResource</span><span style="color:#666">()</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">.</span><span style="color:#b44">getProperty</span><span style="color:#666">(</span>VCARD<span style="color:#666">.</span><span style="color:#b44">FN</span><span style="color:#666">)</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">.</span><span style="color:#b44">getString</span><span style="color:#666">());</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">}</span>
</span></span><span style="display:flex;"><span><span style="color:#666">}</span> <span style="color:#a2f;font-weight:bold">else</span> <span style="color:#666">{</span>
</span></span><span style="display:flex;"><span> System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">.</span><span style="color:#b44">println</span><span style="color:#666">(</span><span style="color:#b44">&#34;No vcards were found in the database&#34;</span><span style="color:#666">);</span>
</span></span><span style="display:flex;"><span><span style="color:#666">}</span>
</span></span></code></pre></div><p>This should produce output similar to the following:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>The database contains vcards <span style="color:#a2f;font-weight:bold">for</span>:
</span></span><span style="display:flex;"><span> Sarah Jones
</span></span><span style="display:flex;"><span> John Smith
</span></span><span style="display:flex;"><span> Matt Jones
</span></span><span style="display:flex;"><span> Becky Smith
</span></span></code></pre></div><p>Your next exercise is to modify this code to use <code>SimpleSelector
</code>instead of <code>listSubjectsWithProperty</code>.</p>
<p>Lets see how to implement some finer control over the statements selected.
<code>SimpleSelector</code> can be subclassed and its selects method modified
to perform further filtering:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#080;font-style:italic">// select all the resources with a VCARD.FN property
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic">// whose value ends with &#34;Smith&#34;
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>StmtIterator iter <span style="color:#666">=</span> model<span style="color:#666">.</span><span style="color:#b44">listStatements</span><span style="color:#666">(</span>
</span></span><span style="display:flex;"><span> <span style="color:#a2f;font-weight:bold">new</span> SimpleSelector<span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">null</span><span style="color:#666">,</span> VCARD<span style="color:#666">.</span><span style="color:#b44">FN</span><span style="color:#666">,</span> <span style="color:#666">(</span>RDFNode<span style="color:#666">)</span> <span style="color:#a2f;font-weight:bold">null</span><span style="color:#666">)</span> <span style="color:#666">{</span>
</span></span><span style="display:flex;"><span> <span style="color:#a2f;font-weight:bold">public</span> <span style="color:#0b0;font-weight:bold">boolean</span> <span style="color:#00a000">selects</span><span style="color:#666">(</span>Statement s<span style="color:#666">)</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">{</span><span style="color:#a2f;font-weight:bold">return</span> s<span style="color:#666">.</span><span style="color:#b44">getString</span><span style="color:#666">().</span><span style="color:#b44">endsWith</span><span style="color:#666">(</span><span style="color:#b44">&#34;Smith&#34;</span><span style="color:#666">);}</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">});</span>
</span></span></code></pre></div><p>This sample code uses a neat Java technique of overriding a method
definition inline when creating an instance of the class. Here the
<code>selects(...)</code> method checks to ensure that the full name ends
with "Smith". It is important to note that filtering based on the subject,
predicate and object arguments takes place before the
<code>selects(...)</code> method is called, so the extra test will only be
applied to matching statements.</p>
<p>The full code can be found in <a href="https://github.com/apache/jena/tree/main/jena-core/src-examples/jena/examples/rdf/Tutorial08.java">tutorial
8</a> and produces output like this:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>The database contains vcards <span style="color:#a2f;font-weight:bold">for</span>:
</span></span><span style="display:flex;"><span> John Smith
</span></span><span style="display:flex;"><span> Becky Smith
</span></span></code></pre></div><p>You might think that:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#080;font-style:italic">// do all filtering in the selects method
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>StmtIterator iter <span style="color:#666">=</span> model<span style="color:#666">.</span><span style="color:#b44">listStatements</span><span style="color:#666">(</span>
</span></span><span style="display:flex;"><span> <span style="color:#a2f;font-weight:bold">new</span>
</span></span><span style="display:flex;"><span> SimpleSelector<span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">null</span><span style="color:#666">,</span> <span style="color:#a2f;font-weight:bold">null</span><span style="color:#666">,</span> <span style="color:#666">(</span>RDFNode<span style="color:#666">)</span> <span style="color:#a2f;font-weight:bold">null</span><span style="color:#666">)</span> <span style="color:#666">{</span>
</span></span><span style="display:flex;"><span> <span style="color:#a2f;font-weight:bold">public</span> <span style="color:#0b0;font-weight:bold">boolean</span> <span style="color:#00a000">selects</span><span style="color:#666">(</span>Statement s<span style="color:#666">)</span> <span style="color:#666">{</span>
</span></span><span style="display:flex;"><span> <span style="color:#a2f;font-weight:bold">return</span> <span style="color:#666">(</span>subject <span style="color:#666">==</span> <span style="color:#a2f;font-weight:bold">null</span> <span style="color:#666">||</span> s<span style="color:#666">.</span><span style="color:#b44">getSubject</span><span style="color:#666">().</span><span style="color:#b44">equals</span><span style="color:#666">(</span>subject<span style="color:#666">))</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">&amp;</span>amp<span style="color:#666">;&amp;</span>amp<span style="color:#666">;</span> <span style="color:#666">(</span>predicate <span style="color:#666">==</span> <span style="color:#a2f;font-weight:bold">null</span> <span style="color:#666">||</span> s<span style="color:#666">.</span><span style="color:#b44">getPredicate</span><span style="color:#666">().</span><span style="color:#b44">equals</span><span style="color:#666">(</span>predicate<span style="color:#666">))</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">&amp;</span>amp<span style="color:#666">;&amp;</span>amp<span style="color:#666">;</span> <span style="color:#666">(</span>object <span style="color:#666">==</span> <span style="color:#a2f;font-weight:bold">null</span> <span style="color:#666">||</span> s<span style="color:#666">.</span><span style="color:#b44">getObject</span><span style="color:#666">().</span><span style="color:#b44">equals</span><span style="color:#666">(</span>object<span style="color:#666">))</span> <span style="color:#666">;</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">}</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">}</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">});</span>
</span></span></code></pre></div><p>is equivalent to:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>StmtIterator iter <span style="color:#666">=</span>
</span></span><span style="display:flex;"><span> model<span style="color:#666">.</span><span style="color:#b44">listStatements</span><span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">new</span> SimpleSelector<span style="color:#666">(</span>subject<span style="color:#666">,</span> predicate<span style="color:#666">,</span> object<span style="color:#666">)</span>
</span></span></code></pre></div><p>Whilst functionally they may be equivalent, the first form will list all
the statements in the Model and test each one individually, whilst the second
allows indexes maintained by the implementation to improve performance. Try
it on a large Model and see for yourself, but make a cup of coffee first.</p>
<h2 id="ch-Operations-on-Models">Operations on Models</h2>
<p>Jena provides three operations for manipulating Models as a whole. These
are the common set operations of union, intersection and difference.</p>
<p>The union of two Models is the union of the sets of statements which
represent each Model. This is one of the key operations that the design of
RDF supports. It enables data from disparate data sources to be merged.
Consider the following two Models:</p>
<p style="text-align: center">
<img alt="figure 4" src="figures/fig4.png" width="240" height="240">and
<img alt="figure 5" src="figures/fig5.png" width="240" height="240"></p>
<p>When these are merged, the two http://...JohnSmith nodes are merged into
one and the duplicate <code>vcard:FN</code> arc is dropped to produce:</p>
<p style="text-align: center">
<img alt="figure 6" src="figures/fig6.png" width="540" height="240"></p>
<p>Lets look at the code to do this (the full code is in <a
href="https://github.com/apache/jena/tree/main/jena-core/src-examples/jena/examples/rdf/Tutorial09.java">tutorial 9</a>) and see what happens.</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#080;font-style:italic">// read the RDF/XML files
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>model1<span style="color:#666">.</span><span style="color:#b44">read</span><span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">new</span> InputStreamReader<span style="color:#666">(</span>in1<span style="color:#666">),</span> <span style="color:#b44">&#34;&#34;</span><span style="color:#666">);</span>
</span></span><span style="display:flex;"><span>model2<span style="color:#666">.</span><span style="color:#b44">read</span><span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">new</span> InputStreamReader<span style="color:#666">(</span>in2<span style="color:#666">),</span> <span style="color:#b44">&#34;&#34;</span><span style="color:#666">);</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic">// merge the Models
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>Model model <span style="color:#666">=</span> model1<span style="color:#666">.</span><span style="color:#b44">union</span><span style="color:#666">(</span>model2<span style="color:#666">);</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic">// print the Model as RDF/XML
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>model<span style="color:#666">.</span><span style="color:#b44">write</span><span style="color:#666">(</span>system<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">,</span> <span style="color:#b44">&#34;RDF/XML-ABBREV&#34;</span><span style="color:#666">);</span>
</span></span></code></pre></div><p>The output produced by the pretty writer looks like this:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">&lt;rdf:RDF</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">xmlns:rdf=</span><span style="color:#b44">&#34;&lt;a href=&#34;</span><span style="">http://www.w3.org/1999/02/22-rdf-syntax-ns#&#34;</span><span style="color:#008000;font-weight:bold">&gt;</span>http://www.w3.org/1999/02/22-rdf-syntax-ns#<span style="color:#008000;font-weight:bold">&lt;/a&gt;</span>&#34;
</span></span><span style="display:flex;"><span> xmlns:vcard=&#34;http://www.w3.org/2001/vcard-rdf/3.0#&#34;&gt;
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdf:Description</span> <span style="color:#b44">rdf:about=</span><span style="color:#b44">&#34;http://somewhere/JohnSmith/&#34;</span><span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:EMAIL&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:internet&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdf:value&gt;</span>John@somewhere.com<span style="color:#008000;font-weight:bold">&lt;/rdf:value&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;/vcard:internet&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;/vcard:EMAIL&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:N</span> <span style="color:#b44">rdf:parseType=</span><span style="color:#b44">&#34;Resource&#34;</span><span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:Given&gt;</span>John<span style="color:#008000;font-weight:bold">&lt;/vcard:Given&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:Family&gt;</span>Smith<span style="color:#008000;font-weight:bold">&lt;/vcard:Family&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;/vcard:N&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;vcard:FN&gt;</span>John Smith<span style="color:#008000;font-weight:bold">&lt;/vcard:FN&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;/rdf:Description&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">&lt;/rdf:RDF&gt;</span>
</span></span></code></pre></div><p>Even if you are unfamiliar with the details of the RDF/XML syntax, it
should be reasonably clear that the Models have merged as expected. The
intersection and difference of the Models can be computed in a similar
manner, using the methods <code>.intersection(Model)</code> and
<code>.difference(Model)</code>; see the
<a href="/documentation/javadoc/jena/org.apache.jena.core/org/apache/jena/rdf/model/Model.html#difference(org.apache.jena.rdf.model.Model)">difference</a>
and
<a href="/documentation/javadoc/jena/org.apache.jena.core/org/apache/jena/rdf/model/Model.html#intersection(org.apache.jena.rdf.model.Model)">intersection</a>
Javadocs for more details.
</p>
<h2 id="ch-Containers">Containers</h2>
<p>RDF defines a special kind of resources for representing collections of
things. These resources are called <i>containers</i>. The members of a
container can be either literals or resources. There are three kinds of
container:</p>
<ul>
<li>a BAG is an unordered collection</li>
<li>an ALT is an unordered collection intended to represent
alternatives</li>
<li>a SEQ is an ordered collection</li>
</ul>
<p>A container is represented by a resource. That resource will have an
rdf:type property whose value should be one of rdf:Bag, rdf:Alt or rdf:Seq,
or a subclass of one of these, depending on the type of the container.
The first member of the container is the value of the container's rdf:_1
property; the second member of the container is the value of the container's
rdf:_2 property and so on. The rdf:_nnn properties are known as the
<i>ordinal properties</i>.</p>
<p>For example, the Model for a simple bag containing the vcards of the
Smith's might look like this:</p>
<p align="center">
<img border="0" src="figures/fig3.png" alt="figure 3" width="720" height="420"></p>
<p align="left">Whilst the members of the bag are represented by the
properties rdf:_1, rdf:_2 etc the ordering of the properties is not
significant. We could switch the values of the rdf:_1 and rdf:_2
properties and the resulting Model would represent the same information.</p>
<p align="left">Alt's are intended to represent alternatives. For
example, lets say a resource represented a software product. It might
have a property to indicate where it might be obtained from. The value
of that property might be an Alt collection containing various sites from
which it could be downloaded. Alt's are unordered except that the
rdf:_1 property has special significance. It represents the default
choice.</p>
<p align="left">Whilst containers can be handled using the basic machinery of
resources and properties, Jena has explicit interfaces and implementation
classes to handle them. It is not a good idea to have an object
manipulating a container, and at the same time to modify the state of that
container using the lower level methods.</p>
<p align="left">Let's modify tutorial 8 to create this bag:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#080;font-style:italic">// create a bag
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>Bag smiths <span style="color:#666">=</span> model<span style="color:#666">.</span><span style="color:#b44">createBag</span><span style="color:#666">();</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic">// select all the resources with a VCARD.FN property
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic">// whose value ends with &#34;Smith&#34;
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>StmtIterator iter <span style="color:#666">=</span> model<span style="color:#666">.</span><span style="color:#b44">listStatements</span><span style="color:#666">(</span>
</span></span><span style="display:flex;"><span> <span style="color:#a2f;font-weight:bold">new</span> SimpleSelector<span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">null</span><span style="color:#666">,</span> VCARD<span style="color:#666">.</span><span style="color:#b44">FN</span><span style="color:#666">,</span> <span style="color:#666">(</span>RDFNode<span style="color:#666">)</span> <span style="color:#a2f;font-weight:bold">null</span><span style="color:#666">)</span> <span style="color:#666">{</span>
</span></span><span style="display:flex;"><span> <span style="color:#a2f;font-weight:bold">public</span> <span style="color:#0b0;font-weight:bold">boolean</span> <span style="color:#00a000">selects</span><span style="color:#666">(</span>Statement s<span style="color:#666">)</span> <span style="color:#666">{</span>
</span></span><span style="display:flex;"><span> <span style="color:#a2f;font-weight:bold">return</span> s<span style="color:#666">.</span><span style="color:#b44">getString</span><span style="color:#666">().</span><span style="color:#b44">endsWith</span><span style="color:#666">(</span><span style="color:#b44">&#34;Smith&#34;</span><span style="color:#666">);</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">}</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">});</span>
</span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic">// add the Smith&#39;s to the bag
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span><span style="color:#a2f;font-weight:bold">while</span> <span style="color:#666">(</span>iter<span style="color:#666">.</span><span style="color:#b44">hasNext</span><span style="color:#666">())</span> <span style="color:#666">{</span>
</span></span><span style="display:flex;"><span> smiths<span style="color:#666">.</span><span style="color:#b44">add</span><span style="color:#666">(</span>iter<span style="color:#666">.</span><span style="color:#b44">nextStatement</span><span style="color:#666">().</span><span style="color:#b44">getSubject</span><span style="color:#666">());</span>
</span></span><span style="display:flex;"><span><span style="color:#666">}</span>
</span></span></code></pre></div><p>If we write out this Model, it contains something like the following:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">&lt;rdf:RDF</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">xmlns:rdf=</span><span style="color:#b44">&#39;http://www.w3.org/1999/02/22-rdf-syntax-ns#&#39;</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">xmlns:vcard=</span><span style="color:#b44">&#39;http://www.w3.org/2001/vcard-rdf/3.0#&#39;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span>...
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdf:Description</span> <span style="color:#b44">rdf:nodeID=</span><span style="color:#b44">&#34;A3&#34;</span><span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdf:type</span> <span style="color:#b44">rdf:resource=</span><span style="color:#b44">&#39;http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag&#39;</span><span style="color:#008000;font-weight:bold">/&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdf:_1</span> <span style="color:#b44">rdf:resource=</span><span style="color:#b44">&#39;http://somewhere/JohnSmith/&#39;</span><span style="color:#008000;font-weight:bold">/&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdf:_2</span> <span style="color:#b44">rdf:resource=</span><span style="color:#b44">&#39;http://somewhere/RebeccaSmith/&#39;</span><span style="color:#008000;font-weight:bold">/&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;/rdf:Description&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">&lt;/rdf:RDF&gt;</span>
</span></span></code></pre></div><p>which represents the Bag resource.</p>
<p>The container interface provides an iterator to list the contents of a
container:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#080;font-style:italic">// print out the members of the bag
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>NodeIterator iter2 <span style="color:#666">=</span> smiths<span style="color:#666">.</span><span style="color:#b44">iterator</span><span style="color:#666">();</span>
</span></span><span style="display:flex;"><span><span style="color:#a2f;font-weight:bold">if</span> <span style="color:#666">(</span>iter2<span style="color:#666">.</span><span style="color:#b44">hasNext</span><span style="color:#666">())</span> <span style="color:#666">{</span>
</span></span><span style="display:flex;"><span> System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">.</span><span style="color:#b44">println</span><span style="color:#666">(</span><span style="color:#b44">&#34;The bag contains:&#34;</span><span style="color:#666">);</span>
</span></span><span style="display:flex;"><span> <span style="color:#a2f;font-weight:bold">while</span> <span style="color:#666">(</span>iter2<span style="color:#666">.</span><span style="color:#b44">hasNext</span><span style="color:#666">())</span> <span style="color:#666">{</span>
</span></span><span style="display:flex;"><span> System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">.</span><span style="color:#b44">println</span><span style="color:#666">(</span><span style="color:#b44">&#34; &#34;</span> <span style="color:#666">+</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">((</span>Resource<span style="color:#666">)</span> iter2<span style="color:#666">.</span><span style="color:#b44">next</span><span style="color:#666">())</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">.</span><span style="color:#b44">getProperty</span><span style="color:#666">(</span>VCARD<span style="color:#666">.</span><span style="color:#b44">FN</span><span style="color:#666">)</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">.</span><span style="color:#b44">getString</span><span style="color:#666">());</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">}</span>
</span></span><span style="display:flex;"><span><span style="color:#666">}</span> <span style="color:#a2f;font-weight:bold">else</span> <span style="color:#666">{</span>
</span></span><span style="display:flex;"><span> System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">.</span><span style="color:#b44">println</span><span style="color:#666">(</span><span style="color:#b44">&#34;The bag is empty&#34;</span><span style="color:#666">);</span>
</span></span><span style="display:flex;"><span><span style="color:#666">}</span>
</span></span></code></pre></div><p>which produces the following output:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>The bag contains:
</span></span><span style="display:flex;"><span> John Smith
</span></span><span style="display:flex;"><span> Becky Smith
</span></span></code></pre></div><p>Executable example code can be found in <a href="https://github.com/apache/jena/tree/main/jena-core/src-examples/jena/examples/rdf/Tutorial10.java">
tutorial 10</a>, which glues together the fragments above into a complete
example.</p>
<p>The Jena classes offer methods for manipulating containers including
adding new members, inserting new members into the middle of a container and
removing existing members. The Jena container classes currently ensure that
the list of ordinal properties used starts at rdf:_1 and is contiguous.
The RDFCore WG have relaxed this constraint, which allows partial
representation of containers. This therefore is an area of Jena may be
changed in the future.</p>
<h2 id="ch-More-about-Literals-and-Datatypes">More about Literals and Datatypes</h2>
<p>RDF literals are not just simple strings. Literals may have a language
tag to indicate the language of the literal. The literal "chat" with an
English language tag is considered different to the literal "chat" with a
French language tag. This rather strange behaviour is an artefact of the
original RDF/XML syntax.</p>
<p>Further there are really two sorts of Literals. In one, the string
component is just that, an ordinary string. In the other the string
component is expected to be a well balanced fragment of XML. When an RDF
Model is written as RDF/XML a special construction using a
parseType='Literal' attribute is used to represent it.</p>
<p>In Jena, these attributes of a literal may be set when the literal is
constructed, e.g. in <a href="https://github.com/apache/jena/tree/main/jena-core/src-examples/jena/examples/rdf/Tutorial11.java">tutorial 11</a>:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#080;font-style:italic">// create the resource
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>Resource r <span style="color:#666">=</span> model<span style="color:#666">.</span><span style="color:#b44">createResource</span><span style="color:#666">();</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic">// add the property
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>r<span style="color:#666">.</span><span style="color:#b44">addProperty</span><span style="color:#666">(</span>RDFS<span style="color:#666">.</span><span style="color:#b44">label</span><span style="color:#666">,</span> model<span style="color:#666">.</span><span style="color:#b44">createLiteral</span><span style="color:#666">(</span><span style="color:#b44">&#34;chat&#34;</span><span style="color:#666">,</span> <span style="color:#b44">&#34;en&#34;</span><span style="color:#666">))</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">.</span><span style="color:#b44">addProperty</span><span style="color:#666">(</span>RDFS<span style="color:#666">.</span><span style="color:#b44">label</span><span style="color:#666">,</span> model<span style="color:#666">.</span><span style="color:#b44">createLiteral</span><span style="color:#666">(</span><span style="color:#b44">&#34;chat&#34;</span><span style="color:#666">,</span> <span style="color:#b44">&#34;fr&#34;</span><span style="color:#666">))</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">.</span><span style="color:#b44">addProperty</span><span style="color:#666">(</span>RDFS<span style="color:#666">.</span><span style="color:#b44">label</span><span style="color:#666">,</span> model<span style="color:#666">.</span><span style="color:#b44">createLiteral</span><span style="color:#666">(</span><span style="color:#b44">&#34;&amp;lt;em&amp;gt;chat&amp;lt;/em&amp;gt;&#34;</span><span style="color:#666">,</span> <span style="color:#a2f;font-weight:bold">true</span><span style="color:#666">));</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic">// write out the Model
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>model<span style="color:#666">.</span><span style="color:#b44">write</span><span style="color:#666">(</span>system<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">);</span>
</span></span></code></pre></div><p>produces</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">&lt;rdf:RDF</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">xmlns:rdf=</span><span style="color:#b44">&#39;http://www.w3.org/1999/02/22-rdf-syntax-ns#&#39;</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">xmlns:rdfs=</span><span style="color:#b44">&#39;http://www.w3.org/2000/01/rdf-schema#&#39;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdf:Description</span> <span style="color:#b44">rdf:nodeID=</span><span style="color:#b44">&#34;A0&#34;</span><span style="color:#008000;font-weight:bold">&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdfs:label</span> <span style="color:#b44">xml:lang=</span><span style="color:#b44">&#39;en&#39;</span><span style="color:#008000;font-weight:bold">&gt;</span>chat<span style="color:#008000;font-weight:bold">&lt;/rdfs:label&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdfs:label</span> <span style="color:#b44">xml:lang=</span><span style="color:#b44">&#39;fr&#39;</span><span style="color:#008000;font-weight:bold">&gt;</span>chat<span style="color:#008000;font-weight:bold">&lt;/rdfs:label&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;rdfs:label</span> <span style="color:#b44">rdf:parseType=</span><span style="color:#b44">&#39;Literal&#39;</span><span style="color:#008000;font-weight:bold">&gt;&lt;em&gt;</span>chat<span style="color:#008000;font-weight:bold">&lt;/em&gt;&lt;/rdfs:label&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">&lt;/rdf:Description&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">&lt;/rdf:RDF&gt;</span>
</span></span></code></pre></div><p>For two literals to be considered equal, they must either both be XML
literals or both be simple literals. In addition, either both must have no
language tag, or if language tags are present they must be equal. For simple
literals the strings must be equal. XML literals have two notions of
equality. The simple notion is that the conditions previously mentioned are
true and the strings are also equal. The other notion is that they can be
equal if the canonicalization of their strings is equal.</p>
<p>Jena's interfaces also support typed literals. The old-fashioned way
(shown below) treats typed literals as shorthand for strings: typed
values are converted in the usual Java way to strings and these strings
are stored in the Model. For example, try (noting that for
simple literals, we can omit the <code>model.createLiteral</code>(...)
call):</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#080;font-style:italic">// create the resource
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>Resource r <span style="color:#666">=</span> model<span style="color:#666">.</span><span style="color:#b44">createResource</span><span style="color:#666">();</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic">// add the property
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>r<span style="color:#666">.</span><span style="color:#b44">addProperty</span><span style="color:#666">(</span>RDFS<span style="color:#666">.</span><span style="color:#b44">label</span><span style="color:#666">,</span> <span style="color:#b44">&#34;11&#34;</span><span style="color:#666">)</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">.</span><span style="color:#b44">addProperty</span><span style="color:#666">(</span>RDFS<span style="color:#666">.</span><span style="color:#b44">label</span><span style="color:#666">,</span> <span style="color:#666">11</span><span style="color:#666">);</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic">// write out the Model
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>model<span style="color:#666">.</span><span style="color:#b44">write</span><span style="color:#666">(</span>system<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">,</span> <span style="color:#b44">&#34;N-TRIPLE&#34;</span><span style="color:#666">);</span>
</span></span></code></pre></div><p>The output produced is:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-turtle" data-lang="turtle"><span style="display:flex;"><span><span style="">_</span><span style="color:#00f;font-weight:bold">:</span><span style="color:#008000;font-weight:bold">A</span>...<span style="color:#bbb"> </span><span style="color:#b8860b">&lt;http://www.w3.org/2000/01/rdf-schema#label&gt;</span><span style="color:#bbb"> </span><span style="color:#b44">&#34;11&#34;</span><span style="color:#bbb"> </span>.<span style="color:#bbb">
</span></span></span></code></pre></div><p>Since both literals are really just the string "11", then only one
statement is added.</p>
<p>The RDFCore WG has defined mechanisms for supporting datatypes in RDF.
Jena supports these using the <i>typed literal</i> mechanisms; they are
not discussed in this tutorial.</p>
<h2 id="ch-Glossary">Glossary</h2>
<dl>
<dt><a id="glos-blank node">Blank Node</a></dt>
<dd>Represents a resource, but does not indicate a URI for the
resource. Blank nodes act like existentially qualified variables in first
order logic.</dd>
<dt><a id="glos-dublin core">Dublin Core</a></dt>
<dd>A standard for metadata about web resources. Further
information can be found at the <a href="http://purl.oclc.org/dc/">Dublin Core
web site</a>.</dd>
<dt><a id="glos-Literal">Literal</a></dt>
<dd>A string of characters which can be the value of a property.</dd>
<dt><a id="glos-Object">Object</a></dt>
<dd>The part of a triple which is the value of the statement.</dd>
<dt><a id="glos-Predicate">Predicate</a></dt>
<dd>The property part of a triple.</dd>
<dt><a id="glos-Property">Property</a></dt>
<dd>A property is an attribute of a resource. For example
DC.title is a property, as is RDF.type.
<dt><a id="glos-Resource">Resource</a></dt>
<dd>Some entity. It could be a web resource such as web page, or
it could be a concrete physical thing such as a tree or a car. It could be an
abstract idea such as chess or football. Resources are named by URI's.</dd>
<dt><a id="glos-Statement">Statement</a></dt>
<dd>An arc in an RDF Model, normally interpreted as a fact.</dd>
<dt><a id="glos-Subject">Subject</a></dt>
<dd>The resource which is the source of an arc in an RDF Model</dd>
<dt><a id="glos-Triple">Triple</a></dt>
<dd>A structure containing a subject, a predicate and an object.
Another term for a statement.</dd>
</dl>
<h2 id="footnotes">Footnotes</h2>
<ol>
<li><a id="fn-01"></a>The identifier of an RDF resource can
include a fragment identifier, e.g.
http://hostname/rdf/tutorial/#ch-Introduction, so, strictly speaking, an
RDF resource is identified by a URI reference.</li>
<li><a id="fn-02"></a>As well as being a string of characters,
literals also have an optional language encoding to represent the
language of the string. For example the literal "two" might have a
language encoding of "en" for English and the literal "deux" might have a
language encoding of "fr" for France.</li>
</ol>
</article>
<aside class="text-muted align-self-start mb-3 mb-xl-5 p-0 d-none d-xl-flex flex-column sticky-top">
<h2 class="h6 sticky-top m-0 p-2 bg-body-tertiary">On this page</h2>
<nav id="TableOfContents">
<ul>
<li><a href="#preface">Preface</a></li>
<li><a href="#ch-Introduction">Introduction</a></li>
<li><a href="#ch-Statements">Statements</a></li>
<li><a href="#ch-Writing-RDF">Writing RDF</a></li>
<li><a href="#ch-Reading-RDF">Reading RDF</a></li>
<li><a href="#ch-Prefixes">Controlling Prefixes</a>
<ul>
<li><a href="#explicit-prefix-definitions">Explicit prefix definitions</a></li>
</ul>
</li>
<li><a href="#ch-Jena-RDF-Packages">Jena RDF Packages</a></li>
<li><a href="#ch-Navigating-a-Model">Navigating a Model</a></li>
<li><a href="#ch-Querying-a-Model">Querying a Model</a></li>
<li><a href="#ch-Operations-on-Models">Operations on Models</a></li>
<li><a href="#ch-Containers">Containers</a></li>
<li><a href="#ch-More-about-Literals-and-Datatypes">More about Literals and Datatypes</a></li>
<li><a href="#ch-Glossary">Glossary</a></li>
<li><a href="#footnotes">Footnotes</a></li>
</ul>
</nav>
</aside>
</main>
</div>
</div>
</div>
<footer class="bd-footer py-4 py-md-5 mt-4 mt-lg-5 bg-body-tertiary">
<div class="container" style="font-size:80%" >
<p>
Copyright &copy; 2011&ndash;2024 The Apache Software Foundation, Licensed under the
<a href="https://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.
</p>
<p>
Apache Jena, Jena, the Apache Jena project logo, Apache and the Apache feather logos are trademarks of
The Apache Software Foundation.
<br/>
<a href="https://privacy.apache.org/policies/privacy-policy-public.html"
>Apache Software Foundation Privacy Policy</a>.
</p>
</div>
</footer>
<script src="/js/popper.min.js.js" type="text/javascript"></script>
<script src="/js/bootstrap.min.js" type="text/javascript"></script>
<script src="/js/improve.js" type="text/javascript"></script>
<script type="text/javascript">
(function() {
'use strict'
const links = document.querySelectorAll(`a[href="${window.location.pathname}"]`)
if (links !== undefined && links !== null) {
for (const link of links) {
link.classList.add('active')
let parentElement = link.parentElement
let count = 0
const levelsLimit = 4
while (['UL', 'LI'].includes(parentElement.tagName) && count <= levelsLimit) {
if (parentElement.tagName === 'LI') {
parentElement.querySelector('a:first-child').classList.add('active')
}
parentElement = parentElement.parentElement
count++
}
}
}
})()
</script>
</body>
</html>