21
1 An Introduction to RDF and the Jena RDF API

An Introduction to RDF and the Jena RDF API

Embed Size (px)

DESCRIPTION

An Introduction to RDF and the Jena RDF API. Outline. Introduction Statements Writing RDF Reading RDF Navigating a Graph Querying a Graph Operations on Graphs. http://a.b.com. creator. John. Introduction. An example of RDF :. 3-Tuple representation : - PowerPoint PPT Presentation

Citation preview

Page 1: An Introduction to RDF and the Jena RDF API

1

An Introduction to RDF and the Jena RDF API

Page 2: An Introduction to RDF and the Jena RDF API

2

Outline

Introduction Statements Writing RDF Reading RDF Navigating a Graph Querying a Graph Operations on Graphs

Page 3: An Introduction to RDF and the Jena RDF API

3

Introduction An example of RDF :

3-Tuple representation : {creator , [http://a.b.com] , [John]}

Graph representation :

http://a.b.com

creator

John

Page 4: An Introduction to RDF and the Jena RDF API

4

Introduction The Resource Description Framework (RDF)

is a standard for describing resources. A resource is anything we can identify. The resources in this tutorial will be about p

eople. They use an RDF representation of VCARDS.

Page 5: An Introduction to RDF and the Jena RDF API

5

Introduction (contd.) RDF is best thought of in the form of

node and arc diagrams. A simple vcard might look like this in RDF:

resource

property

value◆ Fig 1.

Page 6: An Introduction to RDF and the Jena RDF API

6

Introduction (contd.) Jena is a Java API which can be used to

create and manipulate RDF graphs. Jena has object classes to represent

graphs, resources and properties.

Page 7: An Introduction to RDF and the Jena RDF API

7

Introduction (contd.) The code to create the model above is : // some definitions static String personURI = "http://somewhere/JohnSmit

h"; static String fullName = "John Smith";

// create an empty graph Model model = new ModelMem();

// create the resource Resource johnSmith = model.createResource(personURI);

// add the property johnSmith.addProperty(VCARD.FN, fullName);

Page 8: An Introduction to RDF and the Jena RDF API

8

Introduction (contd.) The code above can be more compactly written in a c

ascading style: Resource johnSmith = model.createResource(personURI); johnSmith.addProperty(VCARD.FN, fullName);

Resource johnSmith = model.createResource(personURI) .addProperty(VCARD.FN, fullName);

Page 9: An Introduction to RDF and the Jena RDF API

9

Introduction (contd.) RDF properties can also take other

resources as their value.

property

Value (blank node)

◆ Fig 2.

Page 10: An Introduction to RDF and the Jena RDF API

10

Introduction (contd.) The Jena code to construct the example above is :// some definitionsString personURI = "http://somewhere/JohnSmith";String givenName = "John";String familyName = "Smith";String fullName = givenName + " " + familyName;

// create an empty graphModel model = new ModelMem();

// create the resource// and add the properties cascading styleResource johnSmith = model.createResource(personURI) .addProperty(VCARD.FN, fullName) .addProperty(VCARD.N, model.createResource() .addProperty(VCARD.Given, givenName) .addProperty(VCARD.Family, familyName));

Page 11: An Introduction to RDF and the Jena RDF API

11

Statements Each arc in an RDF graph is called a

statement. Each statement asserts a fact about a resource.

A statement has three parts: ˙subject

˙predicate ˙object A statement is sometimes called a triple, because of its three parts.

John Smith

http://.../JohnSmith

vcard:FN

Page 12: An Introduction to RDF and the Jena RDF API

12

Statements (contd.) We can run a program to get the statements in the Fig2. anon:150bd4d:1015b258a60:-7fff http://www.w3.org/2001/vcard-rdf/3.0#Family "Smit

h" . http://somewhere/JohnSmith http://www.w3.org/2001/vcard-rdf/3.0#FN "John Smith". anon:150bd4d:1015b258a60:-7fff http://www.w3.org/2001/vcard-rdf/3.0#Given "John" . http://somewhere/JohnSmith http://www.w3.org/2001/vcard-rdf/3.0#N anon:150bd4d:1015b258a60:-7fff .

1234

1

2

3

4

◆ Fig 2.

Page 13: An Introduction to RDF and the Jena RDF API

13

Writing RDF Jena has methods for reading and writing R

DF as XML. These can be used to save an RDF model to a file and later, read it back in again.

It can be easily done by creating a PrintWriter and call the model.write method.

// now write the model in XML form to a file model.write(new PrintWriter(System.out));

Page 14: An Introduction to RDF and the Jena RDF API

14

Reading RDF

Jena also has methods for reading the statements recorded in RDF XML form into a model.

The code is : // use the class loader to find the input file InputStream in = Tutorial05.class .getClassLoader() .getResourceAsStream(inputFileName); // read the RDF/XML file model.read(new InputStreamReader(in), "");

Page 15: An Introduction to RDF and the Jena RDF API

15

Navigating a Graph Given the URI of a resource, the resource object ca

n be retrieved from a model. // retrieve the John Smith vcard resource from the model Resource vcard = model.getResource(johnSmithURI); Also can accessing a property of the resource. // retrieve the value of the N property Resource name = vcard.getProperty(VCARD.N) .getResource(); // retrieve the given name property String fullName = vcard.getProperty(VCARD.FN) .getString();

Page 16: An Introduction to RDF and the Jena RDF API

16

Navigating a Graph (contd.)

The graph of the code above is :

http://...

John Smith

John Smith

vcard:FN

vcard:Familyvcard:Given

vcard:N

Resource vcard

http://...

Page 17: An Introduction to RDF and the Jena RDF API

17

Navigating a Graph (contd.)

RDF permits a resource to repeat a property :

// add two nick name properties to vcard vcard.addProperty(VCARD.NICKNAME, "Smithy") .addProperty(VCARD.NICKNAME, "Adman");

http://...

John Smith

John Smith

vcard:FN

vcard:Familyvcard:Given

vcard:NSmithy

Adman

vcard:NICKNAMEvcard:NICKNAME

Page 18: An Introduction to RDF and the Jena RDF API

18

Querying a Graph For example, we can select all the resources

with a VCARD.FN property in the model :

John Smith

Tom Smith

Tom Taylor

vcard:FN

vcard:FN

vcard:N

vcard:FN

http://...

Page 19: An Introduction to RDF and the Jena RDF API

19

Querying a Graph (contd.) We also can select all the resources with a

VCARD.FN property whose value ends with “Smith” :

John Smith

Tom Smith

Tom Taylor

vcard:FN

vcard:FN

vcard:N

vcard:FN

http://...

Page 20: An Introduction to RDF and the Jena RDF API

20

Operations on Graphs Jena provides three operations for

manipulating graphs as a whole : ˙ union, intersection and difference. Consider the following two graphs :

Page 21: An Introduction to RDF and the Jena RDF API

21

Operations on Graphs (contd.)

When the two models above are merged, the

graph will be :