These pages are deprecated ; please go to the pages of our new team Edelweiss

Semantic web tutorial: RDF, RDFS and SPARQL using CORESE

updated 17/04/2007


About Applications Downloads Mailing List Documentation Tutorial Contacts

For remarks or questions on this tutorial contact Fabien Gandon

This semantic web tutorial gives a quick tour of RDF, RDFS, SPARQL and Rules. It was designed as a hand-on-keyboard introduction to the basics of RDF model, RDFS semantics for lightweight ontologies, OWL Lite semantics for lightweight ontologies, SPARQL query language for RDF graph bases and production rules for knowledge factorisation in semantic web annotation bases.

TOC: RDFS intro, RDF/XML intro, SPARQL intro, Rules intro, OWL intro.

This tutorial uses four files:

A previous version of this tutorial (Corese V2.2.2) is available.

Rapid reminder of the basics

RDF

RDF is a triple model where every assertion is decomposed in three parts: (subject, predicate, object) for instance (tutorial.php, author, "Fabien"). The subject is URI identifying a resource. The predicate is a binary relation identified by a URI. The object is either a URI identifying a resource or a literal value. Each triple can be seen as a labelled arc and joining these arcs one obtains a graph that describes URI-identified resources and their relations.

The serialization of RDF in its XML syntax is not unique i.e. the same RDF graph may be represented in different XML forms. For instance the following examples are equivalent:

example 1:
<rdf:Description rdf:about="http://www-sop.inria.fr/acacia/soft/corese/tutorial.php">
  <author>
    <rdf:Description rdf:about="urn://inria.fr/~fgandon">
      <firstname>Fabien</firstname>
    </rdf:Description>
  </author>
  <subject>Web</subject>
</rdf:Description>
example 2:
<rdf:Description rdf:about="http://www-sop.inria.fr/acacia/soft/corese/tutorial.php">
  <author rdf:resource="urn://inria.fr/~fgandon"  />
  <subject>Web</subject>
</rdf:Description>

<rdf:Description rdf:about="urn://inria.fr/~fgandon">
  <firstname>Fabien</firstname>
</rdf:Description>
example 3:
<rdf:Description rdf:about="http://www-sop.inria.fr/acacia/soft/corese/tutorial.php" subject="Web">
  <author rdf:resource="urn://inria.fr/~fgandon"  />
</rdf:Description>

<rdf:Description rdf:about="urn://inria.fr/~fgandon" firstname="Fabien" />

In these descriptions some nodes may not have a URI, they are called blank nodes; in the following example the author is a blank node.

<rdf:Description rdf:about="http://www-sop.inria.fr/acacia/soft/corese/tutorial.php">
  <author>
    <rdf:Description >
      <firstname>Fabien</firstname>
    </rdf:Description>
  </author>
</rdf:Description>

XML Schema datatype may be applied to literal values, here is an example with dates:

<rdf:Description rdf:about="urn://inria.fr/~fgandon">
  <birthdate rdf:datatype="http://www.w3.org/2001/XMLSchema#date" >1975-07-31</birthdate>
</rdf:Description>

RDFS

RDFS is a set of primitives to describe lightweight ontologies in RDF (it uses the RDF model and syntax) and for RDF (the ontologies are used to type resources and relations). RDFS allows us:

  • to name and declare a vocabulary (name resource types and binary relation types called properties);
  • to specify the signature of properties (type of the domain i.e. type of the subject and type of the range i.e. type of the object);
  • specify the (multiple)-inheritance links between the types of classes (subClassOf);
  • specify the (multiple)-inheritance links between the types of properties (subPropertyOf);
  • to provide labels and comments in natural language to document and display these primitives.

Here is an example declaring a class #Man sub class of #Person and #Male, and a property #hasMother sub property of #hasParent, and that is used between instances of the class #Human and instances of the class #Female.

<rdf:RDF xml:base="http://www.inria.fr/2007/04/17/humans.rdfs"
            xmlns:rdf ="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
            xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
            xmlns="http://www.w3.org/2000/01/rdf-schema#">
            
  <Class rdf:ID="Man">
    <subClassOf rdf:resource="#Person"/>
    <subClassOf rdf:resource="#Male"/>
    <label xml:lang="en">man</label>
    <comment xml:lang="en">an adult male person</comment>
  </Class>

  <rdf:Property rdf:ID="hasMother">
    <subPropertyOf rdf:resource="#hasParent"/>
    <range rdf:resource="#Female"/>
    <domain rdf:resource="#Human"/>
    <label xml:lang="en">has for mother</label>
    <comment xml:lang="en">to have for parent a female.</comment>
  </rdf:Property>

</rdf:RDF>

Then using this ontology (with the namespace http://www.inria.fr/2007/04/17/humans.rdfs) one could declare that #Lucas is a #Man and that his mother is #Laura. Here are three examples of possible serialization:

<rdf:RDF xmlns:rdf ="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
  xmlns="http://www.inria.fr/2007/04/17/humans.rdfs#"
  xml:base="http://www.inria.fr/2007/04/17/humans.rdfs-instances" >

<rdf:Description rdf:ID="Lucas">
 <rdf:type rdf:resource="http://www.inria.fr/2007/04/17/humans.rdfs#Man"/>
 <hasMother rdf:resource="#Laura"/> 
</rdf:Description>
or
<rdf:Description rdf:ID="Lucas">
 <hasMother rdf:resource="#Laura"/> 
</rdf:Description>

<Man rdf:about="#Lucas" />
or
<Man rdf:ID="Lucas">
   <hasMother rdf:resource="#Laura"/>
</Man>

The semantics of RDFS would then allows us to infer that #Lucas is a #Person and a #Male, that #Laura is a #Female and a #Human and also that (#Lucas, #hasParent, #Laura).

SPARQL

SPARQL is a query language for RDF i.e. a language to query triple stores. In its most usual form it uses the clause SELECT WHERE. The WHERE clause uses a triple syntax with question marks to prefix variables e.g. to retrieve instances of persons one could use the pattern ?x rdf:type humans:Person

The SPARQL output is a graph serialized in RDF/XML or a binding serialized in XML. Here is an example of SPARQL query. It retrieves the students of a university which web site is http//www.mit.edu or http//www.stanford.edu. It requests their name and optionally their firstname if available. These students must be older than 21. The result will be sorted by their names and only the first twenty results are requested.

PREFIX tutor: <http://inria.fr/2007/tutorial.rdfs>
SELECT ?student ?name ?firstname
WHERE {
 ?student tutor:isStudentOf ?univ .
 {
   {
    ?univ tutor:siteweb <http//www.mit.edu> .
   }
   UNION
   {
    ?univ tutor:siteweb <http//www.stanford.edu> .
   }
 }
 ?student tutor:name ?name .
 OPTIONAL { ?student tutor:firstname ?firstname . }
 ?student tutor:age ?age .
 FILTER ( ?age > 21 )
} 
ORDER BY ?name
LIMIT 20

When used in the SELECT clause, the keyword DISTINCT prevents redundancy in the results.

In the Filter we can use comparators (<, >, =, <=, >=, !=), tests (isURI(), isBLANK(), isLITERAL(), BOUND()), regular expressions, characteristics of values (LANG(), DATATYPE(), STR()), casting operators, etc.

OWL

OWL extends RDFS with additional predicates to characterise properties and classes. For instance one may declare that a property is symmetric e.g.:

<owl:SymmetricProperty rdf:ID="sibling">
  <rdfs:domain rdf:resource="#Animal" />
  <rdfs:range rdf:resource="#Animal" />
</owl:SymmetricProperty>
or may restrict the use of a property for certain classes e.g.:
<owl:ObjectProperty rdf:ID="hasParent">
  <rdfs:domain rdf:resource="#Animal" />
  <rdfs:range rdf:resource="#Animal" />
</owl:ObjectProperty>     

<owl:Class rdf:ID="Human">
  <rdfs:subClassOf rdf:resource="#Animal" />
  ...
  <rdfs:subClassOf>
    <owl:Restriction>
      <owl:onProperty rdf:resource="#hasParent" />
      <owl:allValuesFrom rdf:resource="#Human" />
    </owl:Restriction>
  </rdfs:subClassOf>
  ...
</owl:Class>
For an exhaustive introduction see the OWL Overview.

Ontologies in RDFS/XML

Use your favourite text editor to open the file human_2007_04_17.rdfs containing a small ontology coded in RDFS and using the XML syntax of RDF

Question 1: looking at the top of the file, can you tell what is the namespace associated with this ontology? What mechanism is used to declare the namespace?

answer

Question 2: have a look at the XML structure of the file and find the different uses of the XML tagging system (open and close tags, standalone tags)

answer

Question 3: study the use of the following tags Class, Property, label, comment, range, domain, subClassOf, subPropertyOf and attributes ID, resource. In which namespaces are they defined?

answer

Question 4: according to the signatures (range and domain) of the properties age and hasBrother what are the types of the resources that can be linked by them?

answer

Question 5: look at the beginning of the file and draw the subgraph of the hierarchy containing the classes Animal, Man and Woman

answer

Annotations in RDF/XML

Use you favourite text editor to open the file human_2007_04_17.rdf containing a small set of annotations describing people and using the previous ontology.

Question 1: what is the namespace associated with the instances created by the annotations of this file?

answer

Question 2: what is the namespace of the ontology used by these annotations and how is-it associated with the tags of this XML file?

answer

Question 3: looking at the file, what do we know about John?

answer

Question 4: propose as many ways as possible to declare that a person (e.g. "Stephen") exists.

answer

Querying with SPARQL

In this art we use the standalone version of the search engine Corese distributed as one executable ".jar" file. To run this file you need java 1.5 or above on your machine. Depending on the configuration of your operating system, double-clicking on the file might be enough to start the simplified interface. Otherwise open a shell window move to the directory where the .jar file is and use the command java -jar -Dfile.encoding=UTF8 <NAME OF THE FILE.jar> to start the application ; this command also has the advantage to ensure the JVM is using UTF8 encoding.

You should obtain one window with two tabs:



This is a simplified interface used for testing, debugging and teaching. The "Loading and messages" tab allows you to load Ontologies in RDFS/XML, annotations in RDF/XML, rules in a XML/SPARQL-like format, and check errors while loading these files. The "Queries and bindings" tab allows you to write SPARQL queries and visualize the result.

Using the first tab, load the ontology humans.rdfs and then load the annotations human_2007_04_17.rdf. Normally you shouldn't see any error message and you can move to the "Queries and bindings" tab and start writing queries.

Question 1: what does the following query retrieve? Look at the first answers.

SELECT ?x ?t
WHERE
{
 ?x rdf:type ?t
}

answer

Question 2: adapt the previous query to retrieve all the classes.

answer

Question 3: write a new query to extract all the subsumption links between classes (subClassOf).

answer

Question 4: what does the following query retrieve? Translate this query in plain English.

PREFIX humans: <http://www.inria.fr/2007/04/17/humans.rdfs#>
SELECT *
WHERE
{
 ?x humans:hasSpouse ?y
}

answer

Question 5: constrain the previous query to build two queries to retrieve only males and their spouses.

answer

Question 6: what does the following query retrieve? Translate this query in plain English.

PREFIX humans: <http://www.inria.fr/2007/04/17/humans.rdfs#>
SELECT ?x ?y count(?x) as ?count group ?y
WHERE
{
 ?x humans:hasFriend ?y
}

answer

Question 7: retrieve resources of which we know at least one parent.

answer

Question 8: retreive persons with their age if it is known.

answer

Question 9: identify adults in the base (hint: in france you are an adult when you are over 18).

answer

Question 10: modifying the query in question 9 ask if Mark is an adult. Mark's URI is

http://www.inria.fr/2007/04/17/humans.rdfs-instances#Mark

answer

Question 11: look for all the Lecturers and request their types. How come they have several types?

answer

Question 12: retrieve all the instances that are both Male and Person. Explain the results such as Jack and Pierre. Why were they retreived?

answer

Question 13: retrieve all the instances of Lecturers or Researchers.

answer

Question 14: query the base to get all the researchers and then all the non researchers.

answer

Question 15: ask for instances of the relation hasAncestor. How comes you have results while this property is not used in the annotations (check the content of the annotations in human_2007_04_17.rdf).

answer

Question 16: find the different meanings of the word "size" (hint: it is a label).

answer

Question 17: find synonyms of the word "person".

answer

Question 18: ask for the translation of "shoe size" in French.

answer

Question 19: ask everything about Laura and use the ontology to get the english labels of the properties.

answer

Question 20: use the describe clause to obtain a describtion of Laura.

answer

Question 21: construct all the triples asserting Man instances using known men and known male persons.

answer

Question 22: retrieve all the persons whose name contains "ar", using the function regex(STRING, PATTERN).

answer

Running rules

Use your favourite editor to open the file human_2007_04_17.rul. The principle of these rules is very simple:
  • for each answer found for the query in the <cos:if> element
  • the conclusion in the <cos:then> element is derived i.e. asserted in the base.
<cos:rule>

    <cos:if>
      ... a condition ...
    </cos:if>
      
    <cos:then>
      ... a conclusion ...
    </cos:then>

</cos:rule> 

Question 1: what does the unique rule contained in human_2007_04_17.rul do?

answer

Question 2: before running the rules, write and run a query to retrieve the instances of Man and check the result.

answer

Question 3: adapting the previous rule, add a new rule to define the class Woman. Check the result before and after loading the rules.

answer

Question 4: propose a rule defining the symmetry of the property hasSpouse and check the result with a query before and after loading the rule.

answer

Question 5: can you think of a way to declare that hasChild and hasParent are inverse properties? Check the result.

answer

Question 6: propose a rule defining the transitivity of the property hasAncestor and check the result with a query before and after loading the rule.

answer

Question 7: propose a rule defining the property hasFather from the property hasParent and check the result with a query before and after loading the rule.

answer

Question 8: declare a new class Adult in the ontology and define it with a rule (hint: in france you are an adult when you are over 18). Check the result with a query.

answer

Using OWL

For this part of the tutorial you will no longer use the rule file i.e. reset Corese and load only rdfs and rdf files ; don't load the rules during this section.

Question 1: At the top of the ontology add the namespace for OWL using the prefix "owl".

answer

Question 2: Using property characteristics of OWL Lite declare that hasSpouse and hasFriend are symmetric and test the result with a query before and after you made the change.

answer

Question 3: Using property characteristics of OWL Lite declare that hasAncestor is symmetric and test the result with a query before and after you made the change.

answer

Question 4: Using property characteristics of OWL Lite declare that hasChild is inverse of hasParent and test the result with a query before and after you made the change.

answer

Question 5: Using the predicate "disjoint with" declare that the classes Male and Female are disjoints. Test this by creating an instance that is both a Man and a Woman.

answer

Question 6: Using the predicate "intersection of" declare that the class Professor is the intersection of the class Researcher and the class Lecturer.

answer

Question 7: Using the predicate "union of" declare that the class Academic is the union of the class Researcher and the class Lecturer.

answer

Question 8: Using the restriction allValuesFrom declare that all the spouses of a man must be women and that all the spouses of a woman must be men. Then test on the base.

answer

Question 9: Using the restriction someValuesFrom declare that the parent of a person must include a woman. Load the modified schema and see the resulting warnings when running the rule engine.

answer

Corese API

The new Corese API is described in the Corese User Manual