Difference between revisions of "Python Labs"

From Info216
(Undo revision 15 by Sinoa (talk))
Line 242: Line 242:
  
 
Here is an example of a results string you can use, if you have trouble connecting to GeoNames (note that you have to escape all the quotation marks inside the Java string):
 
Here is an example of a results string you can use, if you have trouble connecting to GeoNames (note that you have to escape all the quotation marks inside the Java string):
  {\"postalcodes\":[
+
{\"postalcodes\":[{\"adminCode2\":\"V\",\"adminCode1\":\"VC\",\"adminName2\":\"Valencia\",\"lng\":-0.377386808395386,\"countryCode\":\"ES\",\"postalcode\":\"46020\",\"adminName1\":\"Comunidad Valenciana\",\"placeName\":\"Valencia\",\"lat\":39.4697524227712}]}"
      { \"adminCode2\":\"V\",
 
        \"adminCode1\":\"VC\",
 
        \"adminName2\":\"Valencia\",
 
        \"lng\":-0.377386808395386,
 
        \"countryCode\":\"ES\",
 
        \"postalcode\":\"46020\",
 
        \"adminName1\":\"Comunidad Valenciana\",
 
        \"placeName\":\"Valencia\",
 
        \"lat\":39.4697524227712
 
      }
 
  ]}"
 
  
 
So far we have only used plain JSON. Now we want to move to JSON-LD. Make a new HashMap (and therefore also a JSON object) called context. Add at least one pair of strings to it. For example the pair "lat" and "http://www.w3.org/2003/01/geo/wgs84_pos#lat". You can also add the pair "lng" and "http://www.w3.org/2003/01/geo/wgs84_pos#long".
 
So far we have only used plain JSON. Now we want to move to JSON-LD. Make a new HashMap (and therefore also a JSON object) called context. Add at least one pair of strings to it. For example the pair "lat" and "http://www.w3.org/2003/01/geo/wgs84_pos#lat". You can also add the pair "lng" and "http://www.w3.org/2003/01/geo/wgs84_pos#long".

Revision as of 17:55, 10 January 2017

Lab 1 outline: Getting started with Eclipse, Java and Jena

Topics

Installation of and introduction to Eclipse and Jena.

Relevant Jena classes/interfaces (and methods)

  • ModelFactory (createDefaultModel),
  • Model (createResource, createProperty, write)
  • Resource (addLiteral)
  • Property

(The JavaDoc is here: https://jena.apache.org/documentation/javadoc/jena/ . For example, click "All classes" in the upper-left frame. Then you can focus the lower-left frame with a browser click, and use browser search (often Ctrl-F) to find the class or interface you want.)

Tasks

Download and install a Java Runtime Environment (JRE) if you do not have it, for example Java 8 update 66: https://www.java.com/en/download/

Download and unpack Java SE Development Kit (JDK) 8 (e.g. update 66): http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html#javasejdk. (It is smart to download and unpack all the files into subfolders of the same folder, so you know where you have them. On my Windows computer, I put everything under a folder called "C:\Programs". On Linux, I use the folder "/opt".

Download and unpack Eclipse Mars.1 IDE for Java Developers: http://www.eclipse.org/downloads/packages/eclipse-ide-java-developers/marsr

Start Eclipse by going to the directory where you installed it and doubleclick the "eclipse" program. If you want to, you can make a shortcut to the "eclipse" program from your desktop or program bar.

Tell Eclipse that you are going to use the JDK 8 you just downloaded. Help -> Preferences -> Java -> Installed JREs -> Add...

Download and unpack Jena 3.0.1, both the binary and the source distribution: https://jena.apache.org/download/

(While you are at it, you can just download and unpack Apache Jena Fuseki too. Take both the 2.3.1 and 1.3.1 distributions for now.)

Start Eclipse again and create an Eclipse project for Java (File -> New -> Java Project). Copy or link the Jena JAR-files into the project: .../apache-jena-3.0.1/lib/*.jar .

Write a Java program that creates a single triple and writes it out. Use the Classes/interfaces and methods listed above. The simplest way to run the program is using the green "play" buttom in the top toolbar.

Go to the File Storage here in the portal and download the file log4j.properties from the Misc folder. You can get rid of the "log4j warnings" by copying the log4j.properties file into exactly the same directory as your Java-files.

Honour tasks

(For some of the later labs we will also suggest more advanced tasks for those of you who are more experienced.)

Lab 2: RDF programming in Jena

Topics

Draw RDF graphs on paper. Basic RDF graph programming in Jena. Simple reading/writing from/to file.

Note

Because lab 1 went quite well, there is quite a lot to do this time, maybe a bit too much. We will of course try to adjust the upcoming labs to fit the available time.

Classes/interfaces

  • ModelFactory (createDefaultModel),
  • Model (createResource, createProperty, createLiteral, createStatement, add, createList, write, read, listStatements)
  • StmtIterator (hasNext, nextStatement)
  • Statement (getSubject, getPredicate, getObject)
  • Resource (addProperty, addLiteral, removeProperties, removeAll)
  • Property
  • Literal
  • RDFType

Tasks

Draw an RDF graph on paper for (as much as you can of) the following situation: "Cade Tracy lives in 1516 Henry Street, Berkely, California 94709, USA. He has a B.Sc. in biology from the University of California, Berkeley from 2011. His interests include birds, ecology, the environment, photography and travelling. He has visited Canada and France. Ines Dominguez lives in Carrer de la Guardia Civil 20, 46020 Valencia, Spain. She has a M.Sc. in chemistry from the University of Valencia from 2015. Her areas of expertise include waste management, toxic waste, air pollution. Her interests include bike riding, music and travelling. She has visited Portugal, Italy, France, Germany, Denmark and Sweden. Cade knows Ines. They met in Paris in August 2014." (Make up your own terms when you need to, or even better: use terms you know from vocabularies such as FOAF and RDFS. But we are not so careful with full IRIs and prefix definitions in this exercise.)

Create a Model object and create the graph in Jena using the RDF API. Try to use as many different methods as possible to create the triples.

Try to use createList to create the lists of interests and expertise.

Write out your model to the console in the following formats: TURTLE, N-TRIPLE, N3, JSON-LD, RDF/XML. How do they differ? What is the default? Check how the lists are serialised in TURTLE and JSON-LD.

Write your model to a file. (Remember to catch exceptions.)

Edit the file so that Case has the middle name Creighton and Ines' middle name is María.

Create a new program that reads your model in again from the file and writes it to the console. Check that the new middle names are there!

Continuing with either your first or second program, write a loop that goes through all the statements in the graph and prints them to the console.

Change the loop so that (a) it only loops through statements about Ines (b) it only loops through statements involving the names of people.

Remove all addresses from the graph.

Close the graph (not important now, but will be later when we use the TDB triple store.)

If you have more time ("honour tasks")

Below are four lines of comma-separated values (csv - five lines with the headers) that could have been saved from a spreadsheet. Copy them into a file and write a program with a loop that reads each line from that file (except the initial header line) and adds it to your graph as triples:

 "Name","Gender","Country","Town","Experise","Interests"
 "Regina Catherine Hall","F","Great Britain","Manchester","Ecology, zoology","Football, music travelling"
 "Achille Blaise","M","France","Nancy","","Chess, computer games"
 "Nyarai Awotwi Ihejirika","F","Kenya","Nairobi","Computers, semantic networks","Hiking, botany"
 "Xun He Zhang","M","China","Chengdu","Internet, mathematics, logistics","Dancing, music, trombone"

In the resulting graph, delete all information about Achille.

Have you used all the classes/intefaces listed at the beginning, and all the variants of all the methods? If not, try to change your code to try them all!

Lab 3: RDFS Programming in Jena

Topics

Draw RDFS graphs on paper. Basic RDFS graph programming in Jena. Entailments and axioms.

Classes/interfaces

Model (createRDFSModel) InfModel (getRawModel, remove + the same methods as Model) RDFS (label, comment, subClassOf, subPropertyOf, domain, range)

Tasks

"Draw an RDFS graph on paper for (as much as you can of) the following extensions to the task from lab 2: University of California, Berkeley and University of Valencia are both Universities. All universities are higher education instituttions (HEIs). Having a B.Sc. from a HEI and having a M.Sc. from a HEI are special cases of gradutating from that HEI. That a person has a degree in a subject means that the person has expertise in that subject. Only persons can have expertise, and what they have expertise about is always a subject."

Create and output the RDFS graph in Jena (as an InfModel that wraps a default Model) - if you can, try to build on your example from lab 2!

Check that simple inference works - make sure that your graph contains triples like these, even if you have not asserted them explicitly:

  • that UCB and UV are HEIs
  • that Case and Ines have both graduated from some HEI
  • that Case and Ines both has expertises
  • that Case and Ines are both persons
  • that biology and chemistry are both subjects

Rewrite some of your existing code to use rdfs:label in a triple and add an rdfs:comment to the same resource.

Create a new RDFS graph that wraps an empty base (or raw) model. This graph contains only RDFS axioms. Write it out in Turtle and read through them.

Honour tasks

Get the base (or raw) model from the RDFS graph (InfModel) and write it out (to check that it is empty).

Add some triple to the RDFS graph (InfModel). Get the base (or raw model) again and write it out to see where the new triple went.

Take the first RDFS graph you made and remove from it all the triples that are also in the graph of RDFS axioms (the one with an empty base/raw model). Write out the result. What do you see?

Download the FOAF vocabulary from http://xmlns.com/foaf/spec/index.rdf and save it to a file called, e.g., FOAF.rdf . Use the schemagen tool (it is inside your Jena folders) to generate a Java class for the FOAF vocabulary.

Include the FOAF.java file into your project and try to use FOAF classes and properties where they fit. Try to relate the other classes and properties you have used to the classes and properties in FOAF.

Lab 4: Storing graphs and datasets with Jena and TDB

Because both labs 2 and 3 were rather large, and because you spent some time last week working on the project ideas, lab 4 is shorter. Use the extra time to continue your work on labs 2 and 3. The labs do not depend much on one another, so you do not have to do them in strict sequence.

Classes/interfaces

TDBFactory (createDataset) Dataset (getDefaultModel, close, addNamedModel, getNamedModel) RDFDataMgr (write) RDFFormat

Topics

Storing graphs in files and in the TDB triple store. Working with multiple graphs (in a dataset).

Tasks

Take one of the models you have created before and turn it into the default model in a TDB-backed dataset. (Tip: make a copy of your existing program first, in case you mess up the code. You can duplicate Java classes in Eclipse using cut-and-paste.) Remember to close the dataset at the end of the program!

Write a new program that opens the same TDB-backed dataset, gets the default model and writes it to System.out, without creating any resources and triples first. Check that all the triples from the previous task are still there nevertheless.

Honour tasks

Create another (default) model using the ModelFactory. Just add a single simple triple to it. Add the default model to your TDB-backed dataset as a named model.

Write out the old default and new named model in TRIG and TRIX formats. Are the results as you expected? Does the Dataset class in Jena offer a straightforward way to write out datasets?

Use RDFDataMgr to write your dataset (with the two models) first in TRIG and then TRIX formats. (Hint: you cannot give "TRIX" and "TRIG" as string parameters to this method. Instead, use RDFFormat values!) Compare the results.

Lab 5: Accessing and lifting Web APIs (RESTful web services)

Topics

Programming semantic Web APIs (RESTful web services) with Jena and JSON-LD

Classes/interfaces

  • HashMap (add, get), List
  • Model (read)
  • JsonUtils (toPrettyString)
  • JsonLdOptions (setExpandContext)
  • JsonLdProcessor (compact, expand, flatten, normalize)
  • IOUtils (toInputStream)

HashMap and List are parts of the basic Java API (JavaDoc here: https://docs.oracle.com/javase/8/docs/api). The other classes are all available through Jena, but Jena does not include JavaDoc for all of them. The file jsonld-java-0.7.0-javadoc.zip in the protal contains JavaDoc for the Java implementation of JSON-LD (unpack it and open the file index.html in a browser).

Because JSON-LD is quite new, there are not yet many good tutorials available. This lab outline is therefore a little more detailed than the others!

Tasks

Write a small program that accesses the GeoNames web API (http://www.geonames.org/export/ws-overview.html) and download the result. For example, you can use this URL to access more information about Ines' neighbourhood in Valencia: http://api.geonames.org/postalCodeLookupJSON?postalcode=46020&country=ES&username=demo . You can use the getJsonBody method (attached to the end of this message) to write this program. (If you call getJsonBody from the static main method in your program, you must define getJsonBody as static too).

The getJsonBody method returns a JSON object, which is either a Java List or a HashMap. Use the toPrettyString method in the JsonUtils class to format and then print your JSON object.

You do not have to use the GeoNames web API. There are lots and lots of other web APIs out there. But we want something simple that does not require registration (HTTPS can also make things more complex when the certificates are outdated). Here are some examples to get you started if you want to try out other APIs: http://opendata.app.uib.no/ , http://data.ssb.no/api , http://ws.audioscrobbler.com/2.0/ , http://www.last.fm/api /intro , http://wiki.musicbrainz.org/Development/JSON_Web_Service .

Be nice! While you are testing things, write a new method getJsonBodyProxy. This method takes a URL parameter just like the original getJsonBody. But it never connects to that URL. Instead, it returns a jsonObject created locally from a results string you have copied into your program. By letting the rest of your program call the new getJsonBodyProxy instead of getJsonBody while you are debugging your code, you do not need to call the GeoNames API over and over.

Here is an example of a results string you can use, if you have trouble connecting to GeoNames (note that you have to escape all the quotation marks inside the Java string): {\"postalcodes\":[{\"adminCode2\":\"V\",\"adminCode1\":\"VC\",\"adminName2\":\"Valencia\",\"lng\":-0.377386808395386,\"countryCode\":\"ES\",\"postalcode\":\"46020\",\"adminName1\":\"Comunidad Valenciana\",\"placeName\":\"Valencia\",\"lat\":39.4697524227712}]}"

So far we have only used plain JSON. Now we want to move to JSON-LD. Make a new HashMap (and therefore also a JSON object) called context. Add at least one pair of strings to it. For example the pair "lat" and "http://www.w3.org/2003/01/geo/wgs84_pos#lat". You can also add the pair "lng" and "http://www.w3.org/2003/01/geo/wgs84_pos#long". Create a JsonLdOptions object and set its expand context to be the context object with the pair of strings in.

Use the JsonLdProcessor to expand your jsonObject and pretty print the result. Has anything happened? Why/why not?!

Add this pair too to the context object: "postalcodes" and "http://dbpedia.org/ontology/postalCode". Rerun. Has anything happened now? Why/why not?!

Add more string pairs, using existing or inventing new terms as you go along, to the context object and rerun expand. The expanded JSON object lifts the data from the web API. It can be used to provide a semantic version of the original web API.

In addition to expand, try the compact and flatten operations on the JSON object. What do they do?

Go back to the RDF/RDFS programs your wrote in labs 2 and 3. Extend the program so that it adds further information about the post codes of every person in your graph.

We will now make a Jena model from the JSON-LD object. To do this, first create a new default Jena model. Then convert the JSON-LD object to a string (use JsonUtils.toPrettyString). Then turn the string into an input stream (use IOUtils.toInputStream, with "UTF-8" as character set). Then read the input stream into your Jena model (use model.read). (There may be other ways to move from JSON object to Jena models, but this is a simple and straightforward way to start.)

Congratulations - you have now gone through the steps of accessing a web API over the net, lifting the results using JSON-LD, manipulating the in JSON-LD and reading them into a Jena RDF model. Of course, it is easy to convert the Jena model back into JSON-LD using model.write(..., "JSON-LD") ...

 /**    Vi brukeraksesserer vev-API-ene på enklest mulig måte, siden det er Jena og JSON-LD
        som er fokus i INFO216, ikke selve vevprogrammeringen.
 */
   static Object getJsonBody(URL serverAddress) {
       Object jsonObject = null;
       HttpURLConnection connection = null;
       try {
           // send GET request
           connection = null;
           connection = (HttpURLConnection)serverAddress.openConnection();
           connection.setRequestMethod("GET");
           connection.setDoOutput(true);
           connection.setReadTimeout(10000);
           connection.connect();
           // parse JSON reponse
           jsonObject = JsonUtils.fromInputStream(connection.getInputStream());
       } catch (MalformedURLException e) {
           e.printStackTrace();
       } catch (ProtocolException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }
       finally
       {
           // close the connection
           connection.disconnect();
           connection = null;
       }
       return jsonObject;
   }

Lab 6: SPARQL queries with Fuseki

Topics

The Fuseki tool. SPARQL queries and updates. Programming SPARQL queries and updates in Jena.

Preparations

Read up on Fuseki1 here: https://jena.apache.org/documentation/serving_data/ . We are aware that Fuseki2 is out now, but we prefer to run Fuseki1 this semester because it seems to offer simpler support for SPARQL Update.

Classes/interfaces

SPARQL queries and updates are part of ARQ, whose JavaDoc is separate from the rest of Jena: https://jena.apache.org/documentation/javadoc/arq/ .

  • QueryFactory (create)
  • Query
  • QueryExecutionFactory (create)
  • QueryExecution (execSelect, execConstruct, execAsk, execDescribe)
  • ResultSet (hasNext, nextSolution) - this contains the table that results from SPARQL SELECT
  • QuerySolution (get, getResource, getLiteral) - this contains a row in that table
  • GraphStore
  • GraphStoreFactory (create)
  • UpdateFactory (create)
  • UpdateRequest
  • UpdateAction (exectute)

Tasks: Go to the folder to which you downloaded Jena. Go to the jena-fuseki1-1.3.1 or similar subfolder. Start the Fuseki server as follows:

 ./fuseki-server --mem --update --localhost /mydataset

NOTES: The most recent versions of Fuseki needs Java 8, so make sure you have that installed (or you get an "Unsupported major.minor version" error). Check this resource if you have other trouble: https://jena.apache.org/documentation/serving_data/#getting-started-with-fuseki .

Open a web browser and goto http://localhost:3030 . You are now in contact with the Fuseki server you just started. Go to the control panel and choose the dataset /mydataset . You can now run SPARQL queries and updates and load RDF graphs from file into your SPARQL engine.

Use SPARQL Update's INSERT DATA operation to add triples to the default graph of /mydataset. You can use triples from the graphs you made in labs 2 and 3. IMPORTANT TIP: You dataset is only stored in memory, and will disappear when Fuseki is stopped or crashes. So save all your INSERT DATA and other queries and updates in a file as you go along. (Also, you will include them in Java programs later :-))

Write a SPARQL SELECT or CONSTRUCT query to view all the triples in your graph.

Use SPARQL Update's DELETE DATA to delete a triple. Run your SPARQL query again to check that the graph has changed.

Use INSERT DATA to add information about Sergio Pastor, who lives in 4 Carrer del Serpis, 46021 Valencia, Spain. he has a M.Sc. in computer from the University of Valencia from 2008. His areas of expertise include big data, semantic technologies and machine learning.

Write a SPARQL DELETE/INSERT update to change the name of "University of Valencia" to "Universidad de Valencia" whereever it occurs.

Write a SPARQL DESCRIBE query to get basic information about the University of Valencia.

Redo all the above steps, this time writing a Java/Jena program.

Honour task: What do the various parameters used to start fuseki-server mean? (Tip: https://jena.apache.org/documentation/serving_data/#running-a-fuseki-server)

Run Fuseki against your TDB database from lab 4. Update the database. Terminate the server. Start it again, reload http://localhost:3030, and check that the changes were persistent. Alternatively, check by running your program to read the TDB database.

Lab 7: Sgvizler

Topics

Using Sgvizler to visualise the results of SPARQL queries on web pages

BECAUSE YOU NEED TO RUN A WEB HOST AND A SPARQL ENDPOINT IN THIS LAB, YOU SHOULD BRING YOUR OWN LAPTOP TO THIS LAB, OR WORK WITH SOMEONE WHO HAS BROUGHT A LAPTOP.

Readings

Skjæveland 2012: Sgvizler. Paper in the file storage in the portal. Sgvizler 0.6 web page (http://mgskjaeveland.github.io/sgvizler/) Google Charts web page (https://google-developers.appspot.com/chart/interactive/docs/)

Tasks

You must have Jena Fuseki1 running on your machine, so you can access it from a web browser at http://localhost:3030 (or http://127.0.0.1:3030). So please make sure you have at least finished the first parts of Lab 6 (SPARQL queries with Fuseki) before you start this one!

You need a web server running on your machine. For example, you can follow this guide: https://www.maketecheasier.com/setup-local-web-server-all-platforms/ . For Windows, this guide recommends XAMPP, which includes the Apache web server. You only need Apache, although XAMPP wants to install a few other useful packages too. Up to you what you want!

TIP: In the final step on Windows, when you click the Start button in the XAMPP Control Panel, things may not work unless you are running the Control Panel as Administrator. (To run the Control Panel as Administrator, locate it in the Start meny under XAMPP, right-click it and select "Run as Administrator". Do not install XAMPP under C:\Program files on Windows 10, because you will have trouble with write permissions later. Put it in root (C:\) or under a directory you make yourselves such as C:\Programs .

(TIP: On some computers, another server may already be listening to port 80. If so, you may need to terminate that server or move the XAMPP server to another port. There are lots of questions on the web about this.))

Open a web browser and go to http://localhost:80 , or just http://localhost . You are now in contact with the web server you just installed and started.

TIP: For security purposes, make sure you turn off your web server when you no longer use it. Because you will only use it as localhost, it is possible to use very strict security settings if you want, but we will not go into that here.

Locate the index.htm file you just read. On Windows, it is located by default in C:\xampp\htdocs , or in the htdocs-folder of the directory wherre you installed XAMPP. On Linux, it may be located in /var/www/html . From now on, we will call this folder your HTML-folder. Make a change to this file, and then reload it in the web browser to see the change. Writing to the htdocs-folder udner Windows can be a challenge. You may need to change the file and folder permissions.

Go to the first of the "Minimal working examples" on http://mgskjaeveland.github.io/sgvizler/#examples . Save this example to the file quick-start.html in your HTML-folder. Go to http://localhost/quick-start.html in your web browser to see that it works.

Download these two files from the File storage/Misc-folder in the portal: lab7-simple-data.ttl and european-populations.ttl .

Start Fuseki as in Lab 6 (you need the --update). Go to http://localhost:3030, select your dataset and uplodad the file lab7-simple-data.ttl . Write and execute a SELECT query to see that the default graph now contains 20-25 simple rdf:type triples.

Edit the file quick-start.html in your HTML-folder so the data-sgvizler-endpoint is set to "http://localhost/mydataset/query". ("/mydataset" must be the name you used when you started the fuseki-server"!) Go to http://localhost/quick-start.html in your web browser to see that it has changed.

Congratulations! You have now used Sgvizler to visualise the results of a query to your own SPARQL endpoint on a web page hosted on your own web server. Although we have run all this locally (on http://localhost), the principles and steps involved are the same as on the open (semantic) web.

This is where the fun begins! Sgvizler's (and Google's) Table and BarChart charts allow the same data tables as PieChart. Change your page to display each of them. Sgvizler has an attribute for setting chart options: data-sgvizler-chart-options . Use it to set a title for your charts. (You may need to go into the Google documentation to find the available options, or you can guess.)

You can draw the data as a Gauge too, but you need to reverse the order of the coloumns in your table (so the counts come before the class names). Set the chart options so that the gauges go up to max 10 instead of max 100.

Upload the file european-populations.ttl and run a simple SPARQL query to see that the default graph now contains the populations of European countries.

Show the European population data as a GeoChart. Look at the Google documentation to explore different ways of showing the data in the map. For example, show populations as differently sized circles, set the background colour to black, and colour the regions with no data grey.

Lab 8: RDFS Plus

Topics

Draw RDF Plus graphs on paper. Basic OWL ontology programming in Jena.

Tutorial

https://jena.apache.org/documentation/ontology/

Classes/interfaces (some in package org.apache.jena.ontology)

  • ModelFactory (createOntologyModel),
  • Model (createList, write),
  • OWL (sameAs, equivalentClass, equivalentProperty, differentFrom, disjointWith, inverseOf)
  • OntModel (createClass, createIndividual, createObjectProperty, CreateDatatypeProperty, createAllDifferent, createSymmetricProperty, createTransitiveProperty, createInverseFunctionalProperty),
  • OntClass,
  • Individual
  • DatatypeProperty,
  • ObjectProperty

createRDFList

Note that OntModel extends Model.

Tasks

In labs 2 and 3 you modelled and programmed the following situations:

In RDF: "Cade Tracy lives in 1516 Henry Street, Berkeley, California 94709, USA. He has a B.Sc. in biology from the University of California, Berkeley from 2011. His interests include birds, ecology, the environment, photography and travelling. He has visited Canada and France. Ines Dominguez lives in Carrer de la Guardia Civil 20, 46020 Valencia, Spain. She has a M.Sc. in chemistry from the University of Valencia from 2015. Her areas of expertise include waste management, toxic waste, air pollution. Her interests include bike riding, music and travelling. She has visited Portugal, Italy, France, Germany, Denmark and Sweden. Cade knows Ines. They met in Paris in August 2014."

In RDFS: "University of California, Berkeley and University of Valencia are both Universities. All universities are higher education instituttions (HEIs). Having a B.Sc. from a HEI and having a M.Sc. from a HEI are special cases of gradutating from that HEI. That a person has a degree in a subject means that the person has expertise in that subject. Only persons can have expertise, and what they have expertise about is always a subject."

Extend the RDF and RDFS graphs you drew on paper before to account for (as much as you can of) the following situation: Cade and Ines are two different persons. All the countries mentioned above are different. The country USA above is the same as the DBpedia resource http://dbpedia.org/resource/United_States (dbr:United_States) and the GeoNames resource http://sws.geonames.org/6252001/ (gn:6252001). The person class (the RDF type the Cade and Ines resources) in your graph is the same as FOAF's, schema.org's and AKT's person classes (they are http://xmlns.com/foaf/0.1/Person, http://schema.org/Person, and http://www.aktors.org/ontology/portal#Person, respectively, but on paper you can use prefixes). Nothing can be any two of a person, a university, a city, and a person at the same time. The property you have used in your RDF/RDFS graph to represent that 94709 is the US zip code of Berkeley, California in US is a subproperty of VCard's postal code-property (http://www.w3.org/2006/vcard/ns#postal-code). No two US cities can have the same postal code. The property you have used for Ines living in Valencia is the same property as FOAF's based near-property (http://xmlns.com/foaf/0.1/based_near), and it is the inverse of DBpedia's hometown property (http://dbpedia.org/ontology/hometown, dbo:hometown). (This is not completely precise: but "hometown" is perhaps the inverse of a subproperty of "based near".)

Look through your graph and try to identify at least one of each: a reflexive and an irreflexive, a symmetric and an asymmetric, as well as a transitive property.

Create and output the OWL ontology in Jena (as an OntModel - it does not have to explicitly wrap a Model or InfModel) - if you can, try to build on your example from labs 2 and 3!

Write out the OntModel as you go. You do not have to do any reasoning. It is a bit more complicated in OWL, and we will talk about it later!

Lab 9: OWL

Topics

Draw OWL graphs on paper. Basic OWL ontology programming in Jena.

Tutorial

https://jena.apache.org/documentation/ontology/

Classes and methods

In Lab 8 you have already used these classes and methods:

  • ModelFactory (createOntologyModel),
  • Model (createList, write),
  • OWL (sameAs, equivalentClass, equivalentProperty, differentFrom, disjointWith, inverseOf)
  • OntModel (createClass, createIndividual, createObjectProperty, CreateDatatypeProperty, createAllDifferent, createSymmetricProperty, createTransitiveProperty, createInverseFunctionalProperty),
  • OntClass, Individual, DatatypeProperty, and ObjectProperty.

In this lab you may also use the following ones:

  • OntModel (createAllValuesFromRestriction, create(Min/Max)CardinalityQRestriction, create(Min/Max)CardinalityRestriction, createEnumeratedClass, createHasValueRestriction, createInverseFunctionalProperty, createSomeValuesFromRestriction, createUnionClass)

Tasks

In labs 2, 3 and 8 you have modelled and programmed a scenario. You will find the descriptions at the end of this text.

Extend your graph into an ontology that expresses the following using concepts from OWL (and some from RDF/RDFS):

  • anyone who is a graduate has at least one degree
  • anyone who is a university graduate has at least one degree from a university
  • a grade is either an A, B, C, D, E or F
  • a straight A student is a student that has only A grades
  • a graduate has no F grades
  • a student has a unique student number
  • each student has exactly one average grade
  • a course is either a bachelor, a master or a Ph.D course
  • a bachelor student takes only bachelor courses
  • a masters student takes only master courses and at most one bachelor course
  • a Ph.D student takes only Ph.D and at most two masters courses
  • a Ph.D. student cannot take a bachelor course

Write each of the above staments in Turtle.

Program your ontology in Jena.

Scenarios from previous labs

In RDF: "Cade Tracy lives in 1516 Henry Street, Berkeley, California 94709, USA. He has a B.Sc. in biology from the University of California, Berkeley from 2011. His interests include birds, ecology, the environment, photography and travelling. He has visited Canada and France. Ines Dominguez lives in Carrer de la Guardia Civil 20, 46020 Valencia, Spain. She has a M.Sc. in chemistry from the University of Valencia from 2015. Her areas of expertise include waste management, toxic waste, air pollution. Her interests include bike riding, music and travelling. She has visited Portugal, Italy, France, Germany, Denmark and Sweden. Cade knows Ines. They met in Paris in August 2014."

In RDFS: "University of California, Berkeley and University of Valencia are both Universities. All universities are higher education instituttions (HEIs). Having a B.Sc. from a HEI and having a M.Sc. from a HEI are special cases of gradutating from that HEI. That a person has a degree in a subject means that the person has expertise in that subject. Only persons can have expertise, and what they have expertise about is always a subject."

In RDFS Plus: "Cade and Ines are two different persons. All the countries mentioned above are different. The country USA above is the same as the DBpedia resource http://dbpedia.org/resource/United_States (dbr:United_States) and the GeoNames resource http://sws.geonames.org/6252001/ (gn:6252001). The person class (the RDF type the Cade and Ines resources) in your graph is the same as FOAF's, schema.org's and AKT's person classes (they are http://xmlns.com/foaf/0.1/Person, http://schema.org/Person, and http://www.aktors.org/ontology/portal#Person, respectively, but on paper you can use prefixes). Nothing can be any two of a person, a university, a city, and a person at the same time. The property you have used in your RDF/RDFS graph to represent that 94709 is the US zip code of Berkeley, California in US is a subproperty of VCard's postal code-property (http://www.w3.org/2006/vcard/ns#postal-code). No two US cities can have the same postal code. The property you have used for Ines living in Valencia is the same property as FOAF's based near-property (http://xmlns.com/foaf/0.1/based_near), and it is the inverse of DBpedia's hometown property (http://dbpedia.org/ontology/hometown, dbo:hometown). (This is not completely precise: but "hometown" is perhaps the inverse of a subproperty of "based near".)