Jena 1162020 1 RDF Stores l Jena jena

  • Slides: 44
Download presentation
Jena 11/6/2020 1

Jena 11/6/2020 1

RDF Stores l Jena (jena. semanticweb. org) l l l l Sesame (sesame. semanticweb.

RDF Stores l Jena (jena. semanticweb. org) l l l l Sesame (sesame. semanticweb. org) l l l l Popular RDF store RDF and RDFS querying Limited OWL reasoning Forward chaining and backward chaining rule engines Open source implementation in Java Command-line and Java API access Scalable RDF store Open source implementation in Java RDF and RDFS querying Limited OWL reasoning Forward chaining rules engine Java API and HTTP access RDFStore l l 11/6/2020 C-based RDF store RDQL support 2

RDF and RDF API in Jena 2 l l Framework developed by HP Labs

RDF and RDF API in Jena 2 l l Framework developed by HP Labs for manipulating with metadata in Java applications Two versions: l Jena 1 l l l Jena 2 l l 11/6/2020 Expressive support for RDF Limited reasoning facilities (RDQL) Ontology API included Support for OWL included 3

Jena l Includes components: l l l 11/6/2020 API for RDF (ARP: Another RDF

Jena l Includes components: l l l 11/6/2020 API for RDF (ARP: Another RDF parser) API for ontologies with support for OWL, DAML and RDF schema Reasoners Persistent storage support SPARQL: query language for RDF 4

Jena Installation l l Step 1: Download Jena version 2. 5. 5 from course

Jena Installation l l Step 1: Download Jena version 2. 5. 5 from course website to your home directory (say C: Jena) Step 2: Unzip the file on the same folder, it will create the directory structure: l l C: Jena-2. 5. 5 Step 3: Set the classpath, put the lib of Jena to classpath: 11/6/2020 5

Set up many classpaths l l l off store setcp. bat @echo set CLASSPATH=

Set up many classpaths l l l off store setcp. bat @echo set CLASSPATH= call : setall. %* setcp. bat in C: Jena goto end C: Jena>setcp. b : setall if. %1. ==. . goto end dir=%1 at C: Jena- set dir=%dir: "=% if not "%CLASSPATH%"=="" set CLASSPATH=%CLASSPATH%; %dir% 2. 5. 5lib if "%CLASSPATH%"=="" set CLASSPATH=%dir% for %%i in ("%dir%*. jar") do call : setone "%%i" %%i in ("%dir%*. zip") do call : setone "%%i" C: Jena- for shift goto setall 2. 5. 5>echo : setone %classpath% set file=%1 set file=%file: "=% set CLASSPATH=%CLASSPATH%; %file% : end 11/6/2020 6

Jena classpaths 11/6/2020 7

Jena classpaths 11/6/2020 7

Jena Installation l Step 4: test Jena by running the regression tests: l l

Jena Installation l Step 4: test Jena by running the regression tests: l l l test. bat (on windows) C: Jena-2. 5. 5test Regression testing is any type of software testing which seeks to uncover regression bugs. l 11/6/2020 Regression bugs occur when software previously worked as desired, stops working or no longer works in the same way that was previously planned. 8

Jena RDF API http: //jena. sourceforge. net/tutorial /RDF_API/ 11/6/2020 9

Jena RDF API http: //jena. sourceforge. net/tutorial /RDF_API/ 11/6/2020 9

Setting up l l l Properly install Jena at Jena_home Go to Jena_homedoctutorialRDF_APIindex. html

Setting up l l l Properly install Jena at Jena_home Go to Jena_homedoctutorialRDF_APIindex. html All tutorial java files are located at: l l l Jena_homesrc-examplesjenaexamplesrdf Or C: JenaTutorialjenaexamplesrdf Download Tutorial. zip from course website (under software subtitle), unzip it as: l 11/6/2020 C: JenaTutorial 10

Jena Tutorials l l Jena is a Java API which can be used to

Jena Tutorials l l Jena is a Java API which can be used to create and manipulate RDF graphs. 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. 11/6/2020 11

Tutorial 01 – Representing a RDF graph // some definitions static String person. URI

Tutorial 01 – Representing a RDF graph // some definitions static String person. URI = "http: //somewhere/John. Smith"; static String full. Name = "John Smith"; // create an empty Model model = Model. Factory. create. Default. Model(); // create the resource Resource john. Smith = model. create. Resource(person. URI); // add the property john. Smith. add. Property(VCARD. FN, full. Name); Tutorial 01. java 11/6/2020 12

Representing a RDF graph l l It begins with some constant definitions and then

Representing a RDF graph l l It begins with some constant definitions and then creates an empty Model or model, using the Model. Factory method create. Default. Model() 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 Model. Factory. 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 DAML. 11/6/2020 13

package jena. examples. rdf ; import com. hpl. jena. rdf. model. *; import com.

package jena. examples. rdf ; import com. hpl. jena. rdf. model. *; import com. hpl. jena. vocabulary. *; public class Tutorial 01 extends Object { // some definitions static String person. URI = "http: //somewhere/John. Smith"; static String full. Name = "John Smith"; public static void main (String args[]) { // create an empty model Model model = Model. Factory. create. Default. Model(); // create the resource Resource john. Smith = model. create. Resource(person. URI); // add the property john. Smith. add. Property(VCARD. FN, full. Name); //write the model in XML form to a file model. write(System. out); } } 11/6/2020 Tutorial 01. java 14

Compile and Run Tutorial 01 l l Store Tutorial 01. java in C: JenaTutorialjenaexamplesrdf

Compile and Run Tutorial 01 l l Store Tutorial 01. java in C: JenaTutorialjenaexamplesrdf (because of the package stated in Tutorial 01. java) Compile and Run 11/6/2020 15

Tutorial 02 RDF Graph with blank node // some definitions String person. URI String

Tutorial 02 RDF Graph with blank node // some definitions String person. URI String given. Name String family. Name String full. Name = = "http: //somewhere/John. Smith"; "John"; "Smith"; given. Name + " " + family. Name; // create an empty Model model = Model. Factory. create. Default. Model(); // create the resource // and add the properties cascading style Resource john. Smith = model. create. Resource(person. URI). add. Property(VCARD. FN, full. Name). add. Property(VCARD. N, model. create. Resource(). add. Property(VCARD. Given, given. Name). add. Property(VCARD. Family, 11/6/2020 family. Name)); 16

Compile and Run Tutorial 02 l l Store Tutorial 02. java in C: JenaTutorialjenaexamplesrdf

Compile and Run Tutorial 02 l l Store Tutorial 02. java in C: JenaTutorialjenaexamplesrdf Compile and Run 11/6/2020 17

Tutorial 03 - Statement // list the statements in the Model Stmt. Iterator iter

Tutorial 03 - Statement // list the statements in the Model Stmt. Iterator iter = model. list. Statements(); // print out the predicate, subject and object of each statement while (iter. has. Next()) { Statement stmt = iter. next. Statement(); // get next statement Resource subject = stmt. get. Subject(); // get the subject Property predicate = stmt. get. Predicate(); // get the predicate RDFNode object = stmt. get. Object(); // get the object System. out. print(subject. to. String()); System. out. print(" " + predicate. to. String() + " "); if (object instanceof Resource) { System. out. print(object. to. String()); } else { // object is a literal System. out. print(" "" + object. to. String() + """); } System. out. println(". "); } 11/6/2020 18

Compile and Run Tutorial 03 l l Store Tutorial 03. java in C: JenaTutorialjenaexamplesrdf

Compile and Run Tutorial 03 l l Store Tutorial 03. java in C: JenaTutorialjenaexamplesrdf Compile and Run 11/6/2020 19

Tutorial 04 – output in RDF/XML l Write the result of Tutorial 03 in

Tutorial 04 – output in RDF/XML l Write the result of Tutorial 03 in RDF/XML: l l l model. write(System. out); Store Tutorial 04. java in C: JenaTutorialjenaexamplesrdf Compile and Run 11/6/2020 20

Tutorial 04 – output in other formats l Write the result of Tutorial 03

Tutorial 04 – output in other formats l Write the result of Tutorial 03 in: l Other formats: "RDF/XML", "RDF/XML-ABBREV", "N-TRIPLE", "N 3" l l 11/6/2020 XML: model. write(System. out, "RDF/XML-ABBREV"); N-Triple: model. write(System. out, "N-TRIPLE"); 21

Tutorial 05 – Reading RDF l Read a RDF from a file and write

Tutorial 05 – Reading RDF l Read a RDF from a file and write it out // create an empty model Model model = Model. Factory. create. Default. Model(); // use the File. Manager to find the input file Input. Stream in = File. Manager. get(). open( input. File. Name ); if (in == null) { throw new Illegal. Argument. Exception( "File: " + input. File. Name + " not found"); } // read the RDF/XML file model. read(in, ""); // write it to standard out model. write(System. out); 11/6/2020 22

Tutorial 05 – Reading RDF l Source rdf (vc-db-1. rdf) to be read: <rdf:

Tutorial 05 – Reading RDF l Source rdf (vc-db-1. rdf) to be read: <rdf: RDF xmlns: rdf='http: //www. w 3. org/1999/02/22 -rdf-syntax-ns#' xmlns: v. Card='http: //www. w 3. org/2001/vcard-rdf/3. 0#' > <rdf: Description rdf: about="http: //somewhere/John. Smith/"> <v. Card: FN>John Smith</v. Card: FN> <v. Card: N rdf: parse. Type="Resource"> rdf: parse. Type=“Resource” is <v. Card: Family>Smith</v. Card: Family> used to represent blank nodes. <v. Card: Given>John</v. Card: Given> </v. Card: N> </rdf: Description> <rdf: Description rdf: about="http: //somewhere/Rebecca. Smith/"> <v. Card: FN>Becky Smith</v. Card: FN> <v. Card: N rdf: parse. Type="Resource"> <v. Card: Family>Smith</v. Card: Family> <v. Card: Given>Rebecca</v. Card: Given> </v. Card: N> </rdf: Description>. 11/6/2020. . </rdf: RDF> 23

Tutorial 05 – Reading RDF l l l Store Tutorial 05. java in C:

Tutorial 05 – Reading RDF l l l Store Tutorial 05. java in C: JenaTutorialjenaexamplesrdf Store vc-db-1. rdf in C: JenaTutorial (it has to be in this folder) Compile and Run 11/6/2020 24

Tutorial 06 – Navigating a Model l l Deal with accessing information held in

Tutorial 06 – Navigating a Model l l Deal with accessing information held in a Model Given the URI of a resource, the resource object can be retrieved from a model using Model. get. Resource(String uri) 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: l 11/6/2020 // retrieve the John Smith vcard resource from the model Resource vcard = model. get. Resource(john. Smith. URI); 25

Tutorial 06 – Navigating a Model l Accessing properties of a resource l l

Tutorial 06 – Navigating a Model l Accessing properties of a resource l l Resource. get. Property(Property p) This method returns the whole statement. Then using get. Object() to get the value of the property. Example l // retrieve the value of the N property Resource name = (Resource) vcard. get. Property(VCARD. N). get. Object(); 11/6/2020 26

Tutorial 06 – Navigating a Model The object of a statement could be a

Tutorial 06 – Navigating a Model The object of a statement could be a resource or a literal l Knowing the value to be a resource l l // retrieve the value of the FN property Resource name = vcard. get. Property(VCARD. N). get. Resource(); Knowing the value to be literal l l // retrieve the given name property String full. Name = vcard. get. Property(VCARD. FN). get. String(); 11/6/2020 27

Tutorial 06 – Navigating a Model l RDF permits a resource to repeat a

Tutorial 06 – Navigating a Model l RDF permits a resource to repeat a property, l e. g. // add two nickname properties to vcard. add. Property(VCARD. NICKNAME, "Smithy") . add. Property(VCARD. NICKNAME, "Adman"); l l The result of calling vcard. get. Property(VCARD. NICKNAME) is indeterminate. Jena will return one of the values. It is possible to list all the properties by using Resource. list. Properties(Property p) l Stmt. Iterator iter = vcard. list. Properties(VCARD. NICKNAME); while (iter. has. Next()) { System. out. println(" " + iter. next. Statement(). get. Object(). to. String()); } 11/6/2020 28

Tutorial 06 – Navigating a Model l Store Tutorial 06. java in C: JenaTutorialjenaexamplesrdf

Tutorial 06 – Navigating a Model l Store Tutorial 06. java in C: JenaTutorialjenaexamplesrdf Store vc-db-1. rdf in C: JenaTutorial (it has to be in this folder) Compile and Run 11/6/2020 29

Tutorial 07 -Query a Model I l Here introduces some limited query primitive l

Tutorial 07 -Query a Model I l Here introduces some limited query primitive l model. list. Statements(): lists all the statements in a model, not recommended on large models l l l model. list. Statements(Selector s): returns an iterator over all the statements in the model selected by s. model. list. Subjects(): returns an iterator over all resources which are subjects of the statements model. list. Subjects. With. Property(Property p, RDFNode o): returns an iterator over all the resources which have property p with value o. // list vcards Res. Iterator iter = model. list. Subjects. With. Property(VCARD. FN); while (iter. has. Next()) { Resource r = iter. next. Resource(); . . . } 11/6/2020 30

Tutorial 07 -Query a Model I l Select subject, predicate and object l l

Tutorial 07 -Query a Model I l Select subject, predicate and object l l 11/6/2020 Selector selector = new Simple. Selector(subject, predicate, object): It will select all the statements with a subject that matches subject, a predicate that matches predicate and an object that matches object. If a null is supplied in any of the positions, it matches anything. l Selector selector = new Simple. Selector(null, null) Example: l Selector selector = new Simple. Selector(null, VCARD. FN, null): will select all the statements with VCARD. FN as their predicate, whatever the subject or object. 31

Tutorial 07 -Query a Model I l l l Store Tutorial 07. java in

Tutorial 07 -Query a Model I l l l Store Tutorial 07. java in C: JenaTutorialjenaexamplesrdf Store vc-db-1. rdf in C: JenaTutorial (it has to be in this folder) Compile and Run 11/6/2020 32

Tutorial 08 -Query a Model II l Lets see some finer control over the

Tutorial 08 -Query a Model II l Lets see some finer control over the selected statements // select all the resources with a VCARD. FN property // whose value ends with "Smith" Stmt. Iterator iter = model. list. Statements( new Simple. Selector(null, VCARD. FN, (RDFNode) null) { public boolean selects(Statement s) {return s. get. String(). ends. With("Smith"); } }); 11/6/2020 33

Tutorial 08 -Query a Model II l l l Store Tutorial 08. java in

Tutorial 08 -Query a Model II l l l Store Tutorial 08. java in C: JenaTutorialjenaexamplesrdf Store vc-db-1. rdf in C: JenaTutorial (it has to be in this folder) Compile and Run 11/6/2020 34

Tutorial 09 -Operations on Models l Jena provides three operations for manipulating models: l

Tutorial 09 -Operations on Models l Jena provides three operations for manipulating models: l l l 11/6/2020 Union -. union(Model): creates a new model containing all the statements in this model together with all of those in another given model. l It can merge data from different data sources Intersection -. intersection(Model): create a new model containing all the statements which are in both this model and another Difference -. difference(Model): create a new model containing all the statements in this model which are not in another. 35

Tutorial 09 -Operations on Models // read the RDF/XML files model 1. read(new Input.

Tutorial 09 -Operations on Models // read the RDF/XML files model 1. read(new Input. Stream. Reader(in 1), ""); model 2. read(new Input. Stream. Reader(in 2), ""); // merge the Models Model model = model 1. union(model 2); // print the Model as RDF/XML model. write(system. out, "RDF/XML-ABBREV"); 11/6/2020 36

Tutorial 09 -Operations on Models l l l Store Tutorial 09. java in C:

Tutorial 09 -Operations on Models l l l Store Tutorial 09. java in C: JenaTutorialjenaexamplesrdf Store vc-db-3. rdf and vc-db-4. rdf in C: JenaTutorial Compile and Run 11/6/2020 37

Tutorial 10 - Containers l RDF provides containers to represent collections of things. There

Tutorial 10 - Containers l RDF provides containers to represent collections of things. There are three kinds of containers: l l l A BAG is an unordered collection An ALT is an unordered collection for which only one selection can be made. A SEQ is an ordered collection A container is represented by a resource, which has an rdf: type property whose value can be: rdf: Bag, rdf: Alt or rdf: Seq. The first number of the container is the value of the container’s rdf: _1 property, rdf: _2, …rdf: _nn 11/6/2020 38

Tutorial 10 - Containers 11/6/2020 39

Tutorial 10 - Containers 11/6/2020 39

Tutorial 10 - Containers l Lets create a bag container // create a bag

Tutorial 10 - Containers l Lets create a bag container // create a bag Bag smiths = model. create. Bag(); // select all the resources with a VCARD. FN property // whose value ends with "Smith" Stmt. Iterator iter = model. list. Statements( new Simple. Selector(null, VCARD. FN, (RDFNode) null) { public boolean selects(Statement s) { return s. get. String(). ends. With("Smith"); } }); // add the Smith's to the bag while (iter. has. Next()) { smiths. add(iter. next. Statement(). get. Subject()); } 11/6/2020 40

Tutorial 10 - Containers l l l Store Tutorial 10. java in C: JenaTutorialjenaexamplesrdf

Tutorial 10 - Containers l l l Store Tutorial 10. java in C: JenaTutorialjenaexamplesrdf Store vc-db-3. rdf and vc-db-4. rdf in C: JenaTutorial Compile and Run 11/6/2020 41

Tutorial 11 -Literals and Datatypes l model. create. Literal(): create literal // create the

Tutorial 11 -Literals and Datatypes l model. create. Literal(): create literal // create the resource Resource r = model. create. Resource(); // add the property r. add. Property(RDFS. label, model. create. Literal("chat", "en")). add. Property(RDFS. label, model. create. Literal("chat", "fr")). add. Property(RDFS. label, model. create. Literal("<em>chat</em>", true)); // write out the Model model. write(system. out); 11/6/2020 42

Tutorial 11 -Literals and Datatypes l l Store Tutorial 11. java in C: JenaTutorialjenaexamplesrdf

Tutorial 11 -Literals and Datatypes l l Store Tutorial 11. java in C: JenaTutorialjenaexamplesrdf Compile and Run 11/6/2020 43

Summary l l Practicing and mastering all the tutorials on your own. Be able

Summary l l Practicing and mastering all the tutorials on your own. Be able to create similar tutorials using your own examples. 11/6/2020 44