Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 An Introduction to RDF and the Jena RDF API. 2 Outline Introduction Statements Writing RDF Reading RDF Navigating a Graph Querying a Graph Operations.

Similar presentations


Presentation on theme: "1 An Introduction to RDF and the Jena RDF API. 2 Outline Introduction Statements Writing RDF Reading RDF Navigating a Graph Querying a Graph Operations."— Presentation transcript:

1 1 An Introduction to RDF and the Jena RDF API

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

3 3 Introduction An example of RDF : 3-Tuple representation : {creator , [http://a.b.com] , [John]} Graph representation : http://a.b.com creator John

4 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 people. They use an RDF representation of VCARDS.

5 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.

6 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.

7 7 Introduction (contd.) The code to create the model above is : // some definitions static String personURI = "http://somewhere/JohnSmith"; 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);

8 8 Introduction (contd.) The code above can be more compactly written in a cascading style: Resource johnSmith = model.createResource(personURI); johnSmith.addProperty(VCARD.FN, fullName); Resource johnSmith = model.createResource(personURI).addProperty(VCARD.FN, fullName);

9 9 Introduction (contd.) RDF properties can also take other resources as their value. property Value (blank node) ◆ Fig 2.

10 10 Introduction (contd.) The Jena code to construct the example above is : // some definitions String personURI = "http://somewhere/JohnSmith"; String givenName = "John"; String familyName = "Smith"; String fullName = givenName + " " + familyName; // create an empty graph Model model = new ModelMem(); // create the resource // and add the properties cascading style Resource johnSmith = model.createResource(personURI).addProperty(VCARD.FN, fullName).addProperty(VCARD.N, model.createResource().addProperty(VCARD.Given, givenName).addProperty(VCARD.Family, familyName));

11 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

12 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 "Smith". 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. 1 2 3 4 1 2 3 4 ◆ Fig 2.

13 13 Writing RDF 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. 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));

14 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), "");

15 15 Navigating a Graph Given the URI of a resource, the resource object can 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();

16 16 Navigating a Graph (contd.) The graph of the code above is : http://... John Smith JohnSmith vcard:FN vcard:Family vcard:Given vcard:N Resource vcard http://...

17 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 JohnSmith vcard:FN vcard:Family vcard:Given vcard:N Smithy Adman vcard:NICKNAME

18 18 Querying a Graph For example, we can select all the resources with a VCARD.FN property in the model : John Smith Tom SmithTom Taylor vcard:FN vcard:N vcard:FN http://...

19 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 SmithTom Taylor vcard:FN vcard:N vcard:FN http://...

20 20 Operations on Graphs Jena provides three operations for manipulating graphs as a whole : ˙ union, intersection and difference. Consider the following two graphs :

21 21 Operations on Graphs (contd.) When the two models above are merged, the graph will be :


Download ppt "1 An Introduction to RDF and the Jena RDF API. 2 Outline Introduction Statements Writing RDF Reading RDF Navigating a Graph Querying a Graph Operations."

Similar presentations


Ads by Google