Interoperability of Embedded Systems Introduction to Semantic Web technologies
Postgraduate Degree in Information Engineering
Adding and querying semantics: RDF/RDFS and SPARQL
Outline Introduction Data model Syntax
Statements, properties attributes and containers
RDFS: RDF Schema Short SPARQL tutorial
What is RDF?
RDF stands for Resource Description Framework
RDF is a framework for describing resources on the web
RDF is designed to be read and understood by computers RDF is not designed for being displayed to people
RDF is a part of the W3C's Semantic Web Activity
RDF is a W3C Recommendation since 2004
RDF - Examples of Use
RDF provides a common framework for expressing information so it can be exchanged between applications without loss of meaning. Examples of use:
Describing properties for shopping items, such as price and availability Describing time schedules for web events Describing information about web pages (content, author, created and modified date)
Describing content and rating for web pictures
Describing content for search engines Describing electronic libraries ...
RDF is Written in XML?
RDF is a data model
Resources are identified using URIs
Information is a set of statements (subject - predicate object)
Subject and predicate are always resources (i.e. identified by URIs)
Objects may be URIs or literal
Every RDF document corresponds to an oriented and labeled graph
RDF can be Written in XML
Many serialization formats exist for the RDF data model:
Notation 3 (N3)
Turtle RDF/XML
an XML-based syntax
By using XML, RDF information can easily be exchanged between different types of computers using different types of operating systems and application languages During this course we will use the RDF/XML serialization format
RDF and "The Semantic Web"
The RDF language is a part of the W3C's Semantic Web Activity W3C's "Semantic Web Vision" is a future where:
Web information has exact meaning Web information can be understood and processed by computers Computers can integrate information from the web
RDF is a W3C Recommendation
RDF became a W3C Recommendation on February 2004
RDF documents consists of set of triples defining a knowledge base
Each triple have a subject a predicate and an object
In RDF data sets each triple may belong to a named graph or to the default un-named graph.
RDF Resource, Property, and Property Value
RDF identifies things using Web identifiers (URIs), and describes resources with properties and property values
This enables to represent RDF statements about resources as a graph of nodes and arcs representing the resources, and their properties and values
A Resource is anything identified by a URI
such as "[Link]
A Property is a Resource that in triples is used to connect the subject with the object
such as "[Link] or "[Link]
A Property value is the value of a Property
A literal such as Mario Rossi Or a resurce lice [Link]
RDF URIs
Unlike conventional hypertext, RDF URIs can refer to any identifiable thing
including things that may not be directly retrievable on the Web
RDF in addition to describing such things as Web pages can also describe cars, businesses, people, news events, etc.
In addition, RDF properties themselves have URIs, to precisely identify the relationships that exist between the linked items
In RDF is also possible to define resources with unknown URI, this resources are called blank nodes
RDF Resource, Property, and Property Value
The following RDF document could describe the resource The example is simplified, rdf namespace is omitted
[Link]
<?xml version="1.0"?> <rdf:RDF xmlns=[Link]
RDF document and default namespace A set of statements begin
<rdf:Description
Specify subject
Subject Predicates Literal object Resource object
rdf:about="[Link]
<author>Mario Rossi</author> <homepage>[Link] </rdf:Description> </rdf:RDF>
[Link]
[Link]
Mario Rossi
RDF Graphs
The underlying structure of any expression in RDF is a collection of triples
each consisting of a subject, a predicate and an object
A set of such triples form an RDF graph
This can be illustrated by a node and directed-arc diagram in which each triple is represented as a node-arc-node link
RDF Graphs
The direction of the arc is significant: it always points toward the object The nodes of an RDF graph are its subjects and objects The assertion of an RDF triple says that some relationship, indicated by the predicate, holds between the things denoted by subject and object
The meaning of an RDF graph is the conjunction (logical AND) of the statements corresponding to all the triples it contains
RDF Global Graph
As Linked Data URIs are globally unique and can be dereferenced into sets of RDF triples
It is possible to imagine all Linked Data as one giant global graph Linked Data applications operate on top of this giant global graph and retrieve parts of it by dereferencing URIs as required
Merging is a very powerful feature of RDF
Metadata may be defined by several (independent) parties and combined by an application
Blank nodes
<rdf:RDF xmlns=[Link] <rdf:Description rdf:about=[Link] <know> <rdf:Description> <homePage> <rdf:Description rdf:about=[Link] </rdf:Description> </homePage> [Link] <fullName>John Doe</ex:fullName> </rdf:Description> </know> <fullName>Mario Rossi<full name> </rdf:Description> </rdf:RDF> Mario Rossi [Link]
[Link]
[Link]
[Link]
John Doe
[Link]
Reification
Syntactic sugar used:
rdf:Statement rdf:subject rdf:predicate rdf:object Given the triple < URI_sub, URI_pred, URI_obj> the reification is operated by creating a statement instance and relating it to the subject, the predicate and the object of the statement to be reified. This allows to have a more abstract view of the knowledge base e.g. giving the possibility to assert that a certain statement is trustable or not. Ntriple syntax for reification _:statement_bn rdf:type rdf:Statement . _: statement_bn rdf:subject < URI_sub > . _: statement_bn rdf:predicate < URI_pred > . _: statement_bn rdf:object < URI_obj > . rdf:subject rdf:predicate URI_pred
URI_sub URI_obj
rdf:type
Rdf:Statement
rdf:object
RDF Example
Suppose we have two records from a CD-list:
RDF Example
The first line of the RDF document is the XML declaration The XML declaration is followed by the root element of RDF documents: <rdf:RDF>
The xmlns:rdf namespace, specifies that elements with the rdf prefix are from the namespace "[Link]
The xmlns:cd namespace, specifies that elements with the cd prefix are from the namespace "[Link]
The <rdf:Description> element contains the description of the resource identified by the rdf:about attribute
The elements: <cd:artist>, <cd:country>, <cd:company>, etc. are properties of the resource.
RDF Example
The graphical view of the RDF document:
The <rdf:RDF> Element
<rdf:RDF> is the root element of an RDF document
It defines the XML document to be an RDF document
It also contains a reference to the RDF namespace:
The <rdf:Description> Element
The <rdf:Description> element may identify the subject resource with the about attribute
It contains statements that describe the subject
The <rdf:Description> Element
The elements, artist, country, company, price, and year, are defined in the [Link] namespace
This namespace is outside RDF (and not a part of RDF) The elements, artist, country, company, price, and year, must be defined by someone else (company, organization, person, etc. )
Properties as Attributes
The property elements can also be defined as predicates
Properties as Resources
The statements objects can also be existing resources
In the example, the property cd:artist identifies the object through the rdf:resource attribute
The RDF Container Element
RDF Container classes:
rdf:Bag
rdf:Seq rdf:Alt
Note - We will talk about RDFS (RDF Schema) Vocabulary Description Language later Now let's focus on container elements
The <rdf:Bag> Element
The <rdf:Bag> element is a subclass of rdfs:Container It is used to describe a list of values that do not have to be in a specific order It may contain duplicate values
The <rdf:Seq> Element
The <rdf:Seq> element is a subclass of rdfs:Container
It is used to describe an ordered list of values
(for example in alphabetical order)
It may contain duplicate values
The <rdf:Alt> Element
It is used to describe a list of alternative values
the user is supposed to select only one of the values
The first member of the container the value of the rdf:_1 property, is the default choice.
RDF Terms
In the previous examples we have talked about "list of values" when describing the container elements In RDF these "values" are called members So, we have the following:
A container is a resource that contains things The contained things are called members
The rdf:parseType="Collection" Attribute
A container says that the containing resources are members
it does not say that other members are not allowed
RDF collections are used to describe groups that can ONLY contain the specified members
A collection is described by the attribute rdf:parseType="Collection"
The rdf:parseType="Collection" Attribute
RDF List
Ordered opened and closed (using rdf:nil ) lists are represented in RDF through
rdf:List rdf:first rdf:rest rdf:nil
The example below represents the ordered and closed list 3, 2, 1.
3 rdf:first rdf:rest _b1 _b2 rdf:rest _b3 rdf:rest rdf:nil 2 1
rdf:type rdf:List
RDF Schema (RDFS)
RDF Schema (RDFS) is an extension to RDF
W3C references: Primer: [Link] Concepts: [Link] Syntax: [Link] Semantics: [Link] Vocabulary: [Link] Test Cases: [Link]
RDF Schema
RDF describes resources with properties, and values
RDF is domain-independent
RDF user communities need the ability to define application-specific classes and properties
RDF Schema provides a mechanism for describing specific domains
RDF Schema
RDF Schema
variously abbreviated as RDFS, RDF(S), RDF-S, or RDF/S
is a set of classes and properties provides basic elements for the description of ontologies, otherwise called RDF vocabularies, intended to structure RDF resources RDF Schema provides a type system for RDF
RDF Schema
The resources in the RDF Schema vocabulary have URI references with the namespace
[Link] conventionally associated with the prefix rdfs:
Vocabulary descriptions (schemas) written in the RDF Schema language are legal RDF graphs
RDF software that is not written to also process the additional RDF Schema vocabulary can still interpret a schema as a legal RDF graph consisting of various resources and properties But it will not "understand" the additional built-in meanings of the RDF Schema terms
RDFS schema vocabulary: classes
Classes describe abstract concepts or categories or set to which individual may belong. An individual is bound to the classes it belongs to through the rdf:type property
rdfs:Resource: rdfs:Class: rdfs:Literal: rdfs:Datatype: rdfs:Container: rdfs:ContainerMembershipProperty:
the class of everything the class of all classes the class of literal values the class of data types the class of rdf containers i.e. rdf:Bag, rdf:Seq, rdf:Alt the class of rdf selectors i.e. the properties rdf:_1, rdf:_2, ...
RDFS schema vocabulary: properties
Properties are relationships defined in the domain terminology and used to specify domain assertions in the form of triples. Properties are represented as labeled arcs when drawing of an RDF documents and the are always oriented towards the statement object
rdfs:domain: rdfs:range: rdfs:subClassOf: rdfs:subPropetyOf: rdfs:member: rdfs:label: rdfs:comment: rdfs:seeAlso: rdfs:isDefinedBy:
defines the domain of a property defines the range of a property associates a class with its super-class associates a property with its super-property superproperty of rdf:_1, rdf:_2, etc. associates a resource to a human readable label ssociatres a resource to a human readable description associates a resource to another one which provides additional details associates a resource to another one which provides the definition.
Classes
The rdfs:subClassOf property may be used to state that one class is a subclass of another Example
<rdfs:class rdf:about="person"/>
In order to define a student class as a subclass of the class person, you have to specify the statement: <rdfs:subClassOf rdf:resource="#person"/>
Classes
The class rdfs:Literal is the class of literal values such as strings and integers
rdfs:Literal is an instance of rdfs:Class
rdfs:Literal is a subclass of rdfs:Resource
rdfs:Datatype is the class of datatypes
All instances of rdfs:Datatype correspond to the RDF model of a datatype rdfs:Datatype is both an instance of and a subclass of rdfs:Class Each instance of rdfs:Datatype is a subclass of rdfs:Literal
Properties
Properties are instances of the class rdf:Property and describe a relation between subject resources and object resources rdfs:range is an instance of rdf:Property that defines the super-class of the values of a property rdfs:domain is an instance of rdf:Property that is used to define the super-class of the possible subjects for a given property
Properties
Example
<rdfs:property rdf:ID="#attends">
<rdfs:domain rdf:domain="#student"> <rdfs:range rdf:range="#course">
</rdfs:property>
The property #attends can be used in statements whose subject belongs to the class #student and whose object belong to he class #course
Containers
RDF containers are resources that are used to represent collections
The rdfs:Container class is a super-class of the RDF Container classes (discussed earlier)
rdf:Bag
rdf:Seq rdf:Alt
Collections
RDF containers are open
The RDF Collection vocabulary of classes and properties can describe a closed collection
A collection is represented as a list of items
rdf:List - used to build descriptions of lists and other list-like structures (it is an instance of rdf:Class) rdf:first (it is an instance of rdf:Property) rdf:rest (it is an instance of rdf:Property)
rdf:nil (it is an instance of rdf:Property)
Short SPARQL tutorial
Plenty of resources and tutorial can be found on the web like on W3C site and in [Link] SPARQL queries data represented as an oriented graph: very different from relational databases data model. Usage is simple but the approach of accessing a new data model can be something hard the first times. SPARQL 1.1 is in development, it will add many features to the original specification. Some of its parts are currently supported by major semantic web frameworks like Jena The main resource to learn SPARL grammar is: [Link]
Short SPARQL tutorial
A SPARQL Query is pure text.
Queries can be classified in
SELECT
ASK
CONSTRUCT DESCRIBE
The SPARQL results set can be represented in XML by using the XML schema provided by W3C to wrap the results of SELECT and ASK queries XML is not mandatory but often useful when representing results
The WHERE Clause
The WHERE clause is the most important to be understood.
It contains an abstract description of a graph called GroupGraphPattern in the official grammar description
Besides the operators defined on graphs is important to understand that the basic expressive unit is that of the triples.
Triples are specified with subject, predicate and object separated by spaces
The WHERE Clause
Triples are separated by .
There are shortcuts to express triples with recurring subjects or object by using ; and ,
; after the subject tell that couples of predicates and objects will follw, sharing the same subject
, after an object tell that another objects will follow sharing the same subject and predicate
The WHERE Clause
IRI are eclosed between < and > Qualified names does't need enclosing characters Strings are double quoted other data types may be not, in general the literals are represented in this way: quoted_literal^^IRI details
Details in the suggested internet address
SPARQL Examples
Initial knowledge base:
Subject
Predicate
Object
[Link]
[Link]
[Link]
[Link]
Alfredo
[Link]
[Link]
[Link] [Link] [Link] [Link]
[Link]
[Link] [Link] [Link] [Link]
Admin
31 John User 44
SPARQL Examples
SELECT ?a ?b ?c WHERE { ?a ?b ?c } This query defines the variable a, b and c, which in the query must be preceded by ? or $ and then specifies in the WHERE clause the graph pattern against which all the triples will be matched, each triple matching the graph pattern is included in the results. In this case all the triples match so the result will be the entire data set, if we suppose N stated triples:
a [Link] [Link] [Link] b [Link] [Link] [Link] c Alfredo [Link] Admin
[Link]
[Link] [Link] [Link]
[Link]
[Link] [Link] [Link]
31
John User 44
SPARQL Examples
SELECT ?a WHERE { ?a ?b ?c } variables in the SELECT clause are the ones included in the result set
a [Link] [Link] [Link] [Link] [Link] [Link] [Link]
SPARQL Examples
The triples in the WHERE clause are considered in a conjunctive way by default: SELECT ?a ?b ?c WHERE { ?a ?b ?c . ?a <[Link] ?c}
a [Link] [Link]
b [Link] [Link]
c Alfredo John
SPARQL Examples
Disjunction is obtained through the UNION pattern SELECT ?a ?c WHERE { { ?a <[Link] ?c } UNION { ?a <[Link] ?c } }
a c
[Link]
[Link] [Link] [Link]
Alfredo
31 John 44
SPARQL Examples
Filtering is used to restrict the admitted values of literals
SELECT ?a ?c WHERE { { ?a <[Link] ?c } UNION { ?a <[Link] ?c . FILTER regex(?c, John ) } } Many filtering options available, read documentation for detail
a [Link] [Link] [Link] [Link] c Alfredo 31 John 44
SPARQL Examples
Optional patterns can be specified with the OPTIONAL keyword SELECT ?a ?c ?d WHERE { { ?a <[Link] ?c } UNION { ?a <[Link] ?c} . OPTIONAL { ?a <[Link] ?d } }
a [Link] [Link] [Link] c Alfredo 31 John d [Link] [Link]
[Link]
44
SPARQL Examples
ASK queries are used to receive the information if at least one solution exists for the specified patterns ASK WHERE { { ?a <[Link] ?c } UNION { ?a <[Link] ?b } }
<sparql xmlns="[Link] <head> </head> <boolean>true</boolean> </sparql>
SPARQL Examples
CONSTRUCT queries specifies at least two graph patterns: one is a template in which are present variables, like in a WHERE clause, but the data set is not queried against this graph pattern. The second is a normal WHERE clause. What happens is that the data are queried against the WHERE clause and the value of the variables, for each solution, are substituted into the template and the resulting graph (template with substituted values) is returned as a query answer.
Construct { ?a <[Link] ?c } WHERE { { ?c <[Link] ?a }
<rdf:RDF xmlns:rdf="[Link] xmlns:j.0="[Link] <rdf:Description rdf:about="[Link] <j.0:home_belong_to rdf:resource="[Link] /> </rdf:Description> </rdf:RDF>
SPARQL Examples
DESCRIBE queries return an RDF graph representing a description of the specified resource.
The description is built using the rules decided by the SPARQL end point manager, there is no standard defining it
DESCRIBE <[Link]
This is a possible response (by ARQ libraries)
<rdf:RDF xmlns:rdf="[Link] xmlns:j.0="[Link] <rdf:Description rdf:about="[Link] <j.0:home>[Link] <j.0:role>Admin</j.0:role> <j.0:age>31</j.0:age> <j.0:name>Alfredo</j.0:name> </rdf:Description> </rdf:RDF>
SPARQL Examples
DESCRIBE ?x WHERE { ?x <[Link] ?y }
This is a possible response (by ARQ libraries)
<rdf:RDF xmlns:rdf="[Link] xmlns:j.0="[Link] <rdf:Description rdf:about="[Link] <j.0:role>User</j.0:role> <j.0:age>44</j.0:age> <j.0:name>John</j.0:name> </rdf:Description> <rdf:Description rdf:about="[Link] <j.0:home>[Link] <j.0:role>Admin</j.0:role> <j.0:age>31</j.0:age> <j.0:name>Alfredo</j.0:name> </rdf:Description> </rdf:RDF>
SPARQL
Other samples
I want URI and age only for that URI with an associated home
Subquery: a SPARQL 1.1 query feature (not standardized yet)
Select ?a ?b where { ?a <[Link] ?b . { Select ?a Where { ?a <[Link] ?c } } }
a [Link]
b 31
SPARQL
Aggregates and Groups: a SPARQL 1.1 query feature (not standardized yet) We add to the knowledge base:
Subject Predicate Object
[Link]
[Link]
[Link]
[Link]
Mark
User
Select (SUM(STRLEN(?b)) as ?RolesLenght) where { ?a <[Link] ?b } Having ( SUM(STRLEN(?b)) >0 )
RolesLenght 13 The response is a single group of results on which the functions STRLEN and SUM are Applied.
Select (SUM(STRLEN(?b)) as ?RolesLenght) where { ?a <[Link] ?b } Group by ?b Having ( SUM(STRLEN(?b)) >0 )
RolesLenght 8 5 The response is made up of as many groups as the number of different roles. The STRLEN and SUM functions are applied separately on each group
SPARQL
Negation: Many possible approaches such as I want URI and name only for that URI without an associated home. This solution is valid in standard SPARQL
OPTIONAL + !BOUND : Select ?a ?c Where { ?a <[Link] ?c . Optional { ?a <[Link] ?b } . Filter (!Bound(?b)) } a [Link] [Link] c John Mark
Same objective and result, but with a feature of SPARQL 1.1 Query language Not exists: Select ?a ?c Where { ?a <[Link] ?c . Filter not exists { ?a <[Link] ?b } } a [Link] [Link] c John Mark
SPARQL
In this short tutorial we will not investigate the support for other features of SPARQL among them the named graphs for which clear documentation can be found in the main documentation page. This because SIB versions currently doesn't support natively them, so it is difficult to verify in concrete applications during the course [Link]
RDF Summary
RDF Schema (RDFS) provides basic vocabulary for RDF
RDF also allows for the re-use, extendibility and refinement of established resource description standards since these will be available in machinereadable form
Using RDFS it is for example possible to create hierarchies of classes and properties RDF knowledge bases can be queried using the SPARQL language.
Acknowledgments
This tutorial is based on the material developed within RECOCAPE, an FP7 international cooperation project of the capacities programme.