Documentation, backgrounders and tutorial material related to information design, engineering, semantics, ontologies, and vocabularies
→ Standards and namespaces
→ Controlled vocabularies
→ Semantic Web Tools
→ Learning resources
A tutorial to introduce RDF and OWL concepts using the TopBraid Composer (Free) Editor.
In this tutorial, we will learn to:
pizza:Pizza
pizza:Pizza
(direct and indirect)pizza:Pizza
by its labelTomatoTopping
GarlicTopping
Attendees will:
Objective: Creating a simple Pizza ontology in RDF and OWL using Topbraid Composer
In this part of the tutorial, we will be creating a simple Pizza ontology. Before we start using the Topbraid Composer tool, we’ll need an information model to guide us.
The figure below shows a suggested information model for a simple Pizza ontology. We have 2 main classes, called Pizza
and PizzaTopping
and we’ll be creating 2 pizza types - the classic, MargheritaPizza
(which, according to legend, in 1889 was created and named after her…) and the AussiePizza
(a local favourite).
Fire up Topbraid Composer. We’ll be creating:
In your new RDF ‘mypizza’ file, create a new class.
Click on the existing class owl:Thing
and click on the “Create subclass” button. We subclass owl:Thing
because in OWL, every user-defined class is a subclass of owl:Thing
.
Create a sibling class called PizzaTopping
and the remaining topping subclasses,
e.g. CheeseTopping, HamTopping, TomatoTopping, EggTopping
It should look like this:
Select owl:ObjectProperty
in the in the Create property
panel.
Add the name of the property in the text field, i.e. hasTopping.
This creates an OWL ObjectProperty
which can be used to relate instances of two classes. In OWL, there are 2 different property types ObjectProperty
and DatatypeProperty
. The latter can be used to relate an instance of a class and RDF literals and XML Schema datatypes, or Datatypes.
MargheritaPizza
class and add an annotationCreate a new subclass of the Pizza
class as you did earlier with the other classes, called MargheritaPizza
.
Often it is useful to add an annotation to the class. You will notice, there is a field called rdf:label
in the “Annotations” panel. Also, there is a list of annotation DatatypeProperty
items in the “Properties” panel (yellow icon).
Add an new annotation property, called rdfs:comment
. Select rdfs:comment
from the “Properties” panel, drag-and-drop it from the “Properties” panel to the “Annotations” panel. Edit the rdfs:comment
field and enter in some text about the MargheritaPizza
like so:
According to legend, in 1889, the Margherita Pizza was created and named after Queen Margherita.
MargheritaPizza
classWe now want to add more semantics to the MargheritaPizza
class to express that it has a relationship with the TomatoTopping
and the CheeseTopping
. To do so, we need to introduce the idea of a Class Restriction.
Background to Class Restrictions
To understand Class Restrictions, it’s useful to think about it in terms of a Venn diagram. See below:
In OWL, we use Description Logic to capture class semantics. We use class restrictions to narrow down the possible logical statements about that class and their set of instances. Using the MargheritaPizza example, we know that it is a pizza that has cheese and tomato toppings. To express this, we create a restriction on the subClassOf property for MargheritaPizza with the following:
hasTopping
property is CheeseTopping
hasTopping
property is TomatoTopping
The other example in the diagram show that, the BiancaPizza
is a pizza that has CheeseTopping but no TomatoTopping, and we can express that using subClassOf restrictions.
These restrictions can then be used in software reasoners to infer a list of pizzas which has no TomatoToppings, which would include the BiancaPizza. Similarly, we can use software reasoners to infer a list of pizzas which has CheeseToppings, which would include the BiancaPizza, AussiePizza and the MargheritaPizza.
Create the Pizza class restrictions
To create a subClassOf restriction, click on the dropdown button on the rdfs:subClassOf
field, and select “Create restriction”.
Select the “hasTopping” property, and the “someValuesFrom” Restriction Type.
We now need to specify the PizzaTopping classes for MargheritaPizza.
Click on the “+” button in the “Filler” field and select “CheeseTopping”.
Repeat for “TomatoTopping”.
Create a new class called AussiePizza
and create subClassOf restrictions of hasTopping
with CheeseTopping
, TomatoTopping
, EggTopping
and HamTopping
Often you won’t be creating an ontology from scratch, but rather importing this into your workspace. In this part of the tutorial, we will import the Pizza ontology created by the University of Manchester which was developed for learning OWL.
In the current project, create a new RDF file, called “pizza”
In your new RDF ‘pizza’ file, import the Pizza ontology from this URL: https://protege.stanford.edu/ontologies/pizza/pizza.owl
To import the Pizza ontology, navigate to the Imports
tab and click on the “Import from URL
” button.
Enter in the Pizza ontology URL (see above) into the text input box like so:
The imported Pizza ontology will appear in the Imports tab like so:
Create a new class called AussiePizza
by extending the framework in the imported pizza ontology
Objective: Learn how to write simple SPARQL queries to understand RDF data using Topbraid Composer
The part of the tutorial aims to provide a very quick overview of SPARQL and write some simple queries. We will focus on SPARQL SELECT queries.
SPARQL = SPARQL Protocol and RDF Query Language (pronounced “spar-kle”)
SPARQL is a structured query language that is used to query RDF data, much like SQL is used to query relational databases. There are 4 query forms:
For the purposes of this tutorial, we’ll explore using SPARQL SELECT query
The SPARQL query takes the form
SELECT [ list of variables delimited by a space ]
WHERE
{
[
list triple statements separated by '.'
]
}
Example:
SELECT ?subject1 ?subject2
WHERE
{
?subject1 ?predicate1 ?object1 .
?subject2 ?predicate2 ?object2 .
}
A list of rows will be returned based on the list of variables in the SELECT line.
In the example above, rows with columns of ?subject1
?subject2
will be returned. e.g.
| ?subject1 | ?subject2 |
-------------------------
| "foo" | 'bar" |
-------------------------
Using the imported Pizza ontology from Part 2, select the SPARQL
tab like so:
Use the queries below to get hands-on with SPARQL SELECT queries with the Pizza ontology.
pizza:Pizza
SELECT ?x
WHERE {
?x rdfs:subClassOf pizza:Pizza
}
Discuss: What’s happening here?
pizza:Pizza
(direct and indirect)SELECT ?x
WHERE {
?x rdfs:subClassOf+ pizza:Pizza
}
Discuss: What’s happening here?
pizza:Pizza
by its labelSELECT ?x
WHERE {
?x rdfs:subClassOf+ pizza:Pizza .
?x rdfs:label ?label
FILTER (regex(?label, "margherita", "i")) .
}
Discuss: What’s happening here?
TomatoTopping
SELECT ?x
WHERE {
?x rdfs:subClassOf+ pizza:Pizza .
?x rdfs:subClassOf [
a owl:Restriction ;
owl:onProperty pizza:hasTopping;
owl:someValuesFrom pizza:TomatoTopping
]
}
Discuss: What’s happening here?
GarlicTopping
Find all Pizza classes that have TomatoTopping
and all types of classes of FishTopping
SELECT ?x
WHERE {
?x rdfs:subClassOf+ pizza:Pizza .
?x rdfs:subClassOf [
a owl:Restriction ;
owl:onProperty pizza:hasTopping;
owl:someValuesFrom pizza:TomatoTopping
] .
?x rdfs:subClassOf [
a owl:Restriction ;
owl:onProperty pizza:hasTopping;
owl:someValuesFrom ?fishClasses
] .
?fishClasses rdfs:subClassOf+ pizza:FishTopping
}
Find all Pizza classes that have a topping with “seafood” in its label
SELECT ?x
WHERE {
?x rdfs:subClassOf+ pizza:Pizza .
?x rdfs:subClassOf [
a owl:Restriction ;
owl:onProperty pizza:hasTopping;
owl:someValuesFrom ?topping
] .
?topping rdfs:subClassOf+ pizza:PizzaTopping .
?topping rdfs:label ?label .
FILTER (regex(?label, "seafood", "i")) .
}
Discuss: What’s happening here?
If you would like to explore more about SPARQL, we’d recommend the following tutorial: SPARQL Tutorial by Apache Jena
Check out the Learning resources section for more material.
Other related pages: