210 likes | 431 Views
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 :
E N D
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 : {creator,[http://a.b.com],[John]} Graph representation :
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.
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.
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.
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);
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);
Introduction (contd.) • RDF properties can also take other resources as their value. property Value (blank node) ◆ Fig 2.
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));
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
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.
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));
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), "");
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();
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://...
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
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
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” :
Operations on Graphs • Jena provides three operations for manipulating graphs as a whole : ˙ union, intersection and difference. • Consider the following two graphs :
Operations on Graphs (contd.) • When the two models above are merged, the graph will be :