1 / 21

An Introduction to RDF and the Jena RDF API

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 :

Download Presentation

An Introduction to RDF and the Jena RDF API

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 on Graphs

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

  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. 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. 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. 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. 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. Introduction (contd.) • RDF properties can also take other resources as their value. property Value (blank node) ◆ Fig 2.

  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. 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. http://.../JohnSmith vcard:FN John Smith

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

  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. 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. 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. http://... vcard:FN vcard:N John Smith vcard:Family vcard:Given John Smith Navigating a Graph (contd.) • The graph of the code above is : Resource vcard http://...

  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://... vcard:NICKNAME vcard:FN vcard:NICKNAME vcard:N Adman Smithy John Smith vcard:Family vcard:Given John Smith

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

  19. http://... vcard:FN vcard:N John Smith vcard:FN vcard:FN Tom Smith Tom Taylor Querying a Graph (contd.) • We also can select all the resources with a VCARD.FN property whose value ends with “Smith” :

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

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

More Related