SPARQLing
Constraints for RDF
Michael Schmidt
EDBT, 2008 March 28
joint work with Prof. Georg Lausen, Michael Meier
1
SPARQLing Constraints for RDF
RDF Data Format
Extension of RDF by constraints
• Machine-readable information
• Established in the Semantic Web
With fixed semantics
Integration into the Framework
Constraints
bases on
SPARQL Query Language
• Declarative Language
• W3C Recommendation since Jan.
• Primary and foreign keys
• Cardinality constraints, …
The role of SPARQL in this context
Extracting constraints
Checking constraints
Optimization of SPARQL
queries under constraints
2
Why Constraints?
Restricting the state space of the database
Maintenance of data consistency (e.g. when
data is updated)
Semantic Query Optimization
Better understanding of the data
Here: Translation of Relational Schemata to
RDF without loss of information
3
The RDF Data Format
rdf:type
Teachers
„Joe“
„CS“
name
t1
knows
faculty
name
t2
age
„Fred“
„43“
„Triples of Knowledge“
(t1, name, „Joe“) , (t1, faculty, „CS“) , (t1, knows, t2)
4
The RDF Data Format
rdf:type
Teachers
„Joe“
„CS“
name
t1
knows
faculty
name
t2
age
„Fred“
„43“
Three elementary types
URIs (describe physical/logical entities & properties)
Literals (string values)
Blank Nodes (not conisdered)
5
A Relational Data Scheme
Teachers
name
faculty
Students
matric
name
Joe
CS
11111
John
Fred
CS
22222
Ed
Courses
taught_by
name
Participants
c_id
s_id
Joe
DB
Fred
11111
Fred
Web
Fred
22222
+ NOT NULL constraints on each column
6
Problem:
Constraints only
implicitly given!
A Translation into RDF
Teachers
Students
t2
t1
faculty
name
Joe
taught_by
faculty
name
name
matric
“CS“ Fred
“CS“
name
matric
name
taught_by
Courses
s_id
c_id
s_id
c_id
c2
c1
“Ed“
“John“ 22222
11111
“Web“
“DB“
name
rdf:type
s2
s1
p2
p1
Participants
7
Constraints for RDF
Encoding in the schema layer
New namespace „rdfc“ provides constraint
vocabulary with fixed semantics
rdfc:Key for primary keys
rdfc:FKey for foreign keys
rdfc:ref links foreign keys to primary keys
Use built-in RDF container class rdf:Seq
8
Encoding Constraints
Teachers
rdfc:Key rdf:Seq
rdfc:Key
t2
t1
name
faculty
name
Joe
“CS“
faculty
Fred
T_Key
rdf:_1
rdfc:ref
“CS“
rdfc:FKey
rdf:Seq
“Web“
“DB“
name
taught_by
name
name
taught_by
C_FKey
c2
c1
rdf:_1
taught_by
rdfc:FKey
Courses
9
Types of Constraints
Let C, C1, C2 be classes and Qi, Ri properties
Primary keys, foreign keys
Key(C,[Q1,…Qn]), FKey(C1,[Q1,…Qn],C2,[R1,…Rn])
Cardinality constraints
Min(C,n,R), Max(C,n,R) for n N
Functionality constraints, totality constraints
Func(C,Q), Total(C,Q)
and many more in the full paper: singleton, subclass,
subproperty, property domain, property range
10
Satisfiability
Given an RDF vocabulary and a set of constraints. Is there a
non-empty RDF graph that satisfies the constraints?
in general undecidable
Shown by reduction from the key implication
problem in Relational Databases
In the paper, we indicate
satisfiable constraint subclasses
decidable constraint subclasses
11
The SPARQL Query Language
Declarative language
Bases upon graph patterns that are matched
against the input graph
Different operators to combine these patterns
AND („.“)
OPTIONAL
UNION
FILTER
SELECT ?name ?faculty ?title
WHERE {
?teacher rdf:type Teachers.
?teacher name
?name.
?teacher faculty ?faculty.
OPTIONAL {
?teacher title ?title.
}
}
12
SPARQL Query Evaluation
?teacher
Variables are
matched against
the input graph
„Professor“
?name
Teachers
title
?faculty
?title: unbound
t2
t1
name faculty
faculty
name
SELECT ?name ?faculty ?title
WHERE {
?teacher rdf:type Teachers.
?teacher name
?name.
?teacher faculty ?faculty.
OPTIONAL {
?teacher title ?title.
}
}
Joe
?name
“CS“
Fred
?faculty
Joe
“CS“
Fred
“CS“
“CS“
?title
“Professor“
13
Extracting Key Constraints
rdfc:Key rdf:Seq
Teachers
…
?keyname
T_Key
rdfc:Key
T_Key
…
?class
Teachers
?keyatt
name
rdf:_1
name
SELECT ?keyname ?class ?keyatt
WHERE {
?class
rdfc:Key ?keyname.
?keyname rdf:type rdfc:Key.
?keyname ?seq
?keyatt.
FILTER (?seq!=rdf:type)
}
Extraction of foreign keys very similar
14
Checking Constraints with SPARQL
Use SPARQL „ASK“ query form
(returns „yes“ exactly if query contains a result,
„no“ otherwise)
A SPARQL query checks a constraint C if it returns yes for
each graph that violates C, no otherwise.
Constraint checks possible for many types
constraints
15
Checking Constraints with SPARQL
Checking primary key constraints
Key(C,[p1,. . . ,pn])
ASK {
?x rdf:type C.
?y rdf:type C.
?x p1 ?p1; [...]; pn ?pn.
?y p1 ?p1; [...]; pn ?pn.
FILTER (?x!=?y)
}
Returns „yes“ exactly if constraint is violated.
Checking of foreign keys is a little more
complicated, but also possible
16
Semantic Query Optimization
Idea: use constraint knowledge to find a more
efficient query execution plan
Has been studied in the context of relational
and datalog databases…
… and now is applicable in the context of
RDF and SPARQL
17
Semantic Query Optimization
SELECT ?teachername ?coursename ?studentname
WHERE {
?course rdf:type Courses;
taught_by ?teachername;
name
?coursename.
?participant rdf:type Participants;
c_id
?teachername;
s_id
?studentmatric.
?teacher rdf:type
Teachers;
name
?teachername.
OPTIONAL {
?student rdf:type Students;
matric
?studentmatric;
name
?studentname.
}
}
18
A Solution Candidate Subgraph
Teachers
Students
faculty
name
Joe
taught_by
s1
t2
t1
faculty
name
s2
name
matric
“CS“ Fred
“CS“
name
matric
“Web“
“DB“
name
name
taught_by
Courses
s_id
c_id
s_id
c_id
c2
c1
“Ed“
“John“ 22222
11111
p2
p1
Participants
19
Semantic Query Optimization
SELECT ?teachername ?coursename ?studentname
WHERE {
?course rdf:type Courses;
taught_by ?teachername; FKey(Participants, [s_id],
Students, [matric])
name
?coursename.
?participant rdf:type Participants;
c_id
?teachername;
s_id
?studentmatric.
?teacher rdf:type
Teachers;
name
?teachername.
OPTIONAL {
Key(Students,[matric])
?student rdf:type Students;
matric
?studentmatric;
name
?studentname.
Total(Students,[name])
}
}
20
Semantic Query Optimization
SELECT ?teachername ?coursename ?studentname
WHERE {
?course rdf:type Courses;
taught_by ?teachername; FKey(Courses, taught_by,
Teacher, [name])
name
?coursename.
?participant rdf:type Participants;
c_id
?teachername;
s_id
?studentmatric.
?teacher rdf:type
Teachers;
Key(Teacher, [name])
name
?teachername.
?student rdf:type Students;
matric
?studentmatric;
name
?studentname.
}
21
Semantic Query Optimization
SELECT ?teachername ?coursename ?studentname
WHERE {
?course rdf:type Courses;
taught_by ?teachername;
name
?coursename.
?participant rdf:type Participants;
c_id
?teachername;
s_id
?studentmatric.
?student rdf:type Students;
matric
?studentmatric;
name
?studentname.
}
Many more optimizations possible
Rewriting of filter expressions
Elimination of redundant rdf:type specifications
22
Future Work
Study of other types of constraints and the
interaction between constraints
Development of a schematic approach to
Semantic Query Optimization
Mapping to SQL/Datalog?
SPARQL-specific semantic optimizations?
Efficient constraint checking algorithms
23
Thank you for your attention!
• C. Bizer.D2R MAP-A Database to RDF Mapping
Language. In WWW (Posters), 2003.
• C.Bizer, R.Cyganiak, J. Garbers, and O.
Maresch. D2RQ: Treading Non-RDF Relational
Databases as Virtual RDF Graphs. User Manual
and Language Specification.
• J. J. King. QUIST: A System for Semantic Query
Optimization in Relational Databases. Distributed
systems, Vol. II, pages 287-294, 1986.
• G. Lausen. Relational Databases in RDF. In
Joint ODBIS & SWDB Workshop on Semantic
Web, Ontologies, Databases, 2007.
B. Motik, I. Horrocks, and U. Sattler. Bridging the
Gap Between OWL and Relational Databases, In
WWW, pages 807-816, 2007.
• J. Pérez, M. Arenas, and C. Gutierrez.
Semantics and Complexity of SPARQL. In CoRR
Technical Report cs.DB/0605124, 2006.
• Recourse Description Framework (RDF):
Concepts and Abstract Syntax.
http://www.w3.org/TR/rdf-schema/. W3C
Recommendation, February 10, 2004.
• RDF Vocabulary Description Language 1.0: RDF
Schema.
http://www.w3.org/TR/rdf-schema/. W3C
Recommendation, Febuary 10, 2004.
• RDF Semantics.
http://www.w3.org/TR/rdf-mt/. W3C
Recommendation, February 10, 2004.
• S.T. Shenoy and Z.M. Ozsoyoglu. A System for
Semantic Query Optimization. In SIGMOD, pages
181-195, 1987.
• SPAQL Query Language for RDF.
http://www.w3.org/TR/rdf-sparql-query/. W3C
Proposed Recommendation, November 12, 2007.
• G.E. Weddell. A Theory of Functional
Dependencies for Object-Oriented Data Models. In
DOOD, pages 165-184, 1989.
24
Additional Resources
25
Checking Constraints with SPARQL
Checking foreign key constraints
FKey(C,[p1,. . . ,pn],D,[q1,... qn])
Bind objects of type C, with properties bound to ?p1, …, ?pn
Only keep results
for which no
referenced object
exists
ASK {
?x rdf:type C; p1 ?p1; [...]; pn ?pn.
OPTIONAL {
?y rdf:type D; q1 ?p1; [...]; qn ?pn.
}
FILTER (!bound(?y))
}
Bind the (referenced) object to variable ?y, if any
26
RDFS Constraints
Let Ci denote classes, Qi denote properties
Subclass Constraint
SubC(C1,C2)
Subproperty Constraint
SubP(Q1,Q2)
Property Domain/Range
PropD(Q,C), PropR(Q,C)
Restrict the state
space of the
database
No „axioms“ that
are used for
inferencing
27
Satisfiability
Given an RDF vocabulary and a set of constraints. Is there a
non-empty RDF graph that satisfies the constraints?
in general undecidable
Primary keys + Foreign Keys
Singleton
Max-Cardinality
always satisfiable
Subclass + Subproperty
Property Domain + Property Range
28
Satisfiability
Given an RDF vocabulary and a set of constraints. Is there a
non-empty RDF graph that satisfies the constraints?
in general undecidable
Primary keys + Foreign Keys
Singleton
Max-Cardinality
undecidable
Subclass + Subproperty
Property Domain + Property Range
Min-Cardinality
29
Satisfiability
Given an RDF vocabulary and a set of constraints. Is there a
non-empty RDF graph that satisfies the constraints?
in general undecidable
Unary primary keys
Unary foreign keys
Min-Cardinality + Max-Cardinality
decidable in ExpTime
Subclass + Subproperty
Property Domain + Property Range
30
The SPARQL Query Language
Operator AND („.“)
SELECT ?name ?faculty
WHERE {
?teacher rdf:type Teachers.
?teacher name
?name.
?teacher faculty ?faculty.
}
Teachers
t2
t1
faculty
name
faculty
name
?name
Joe
“CS“
Fred
“CS“
?faculty
Joe
“CS“
Fred
“CS“
31
The SPARQL Query Language
Operator UNION
Teachers
t2
t1
faculty
name
faculty
name
“CS“
Joe
?name
Fred
?faculty
Joe
“CS“
Fred
“CS“
“CS“
SELECT ?name ?faculty
WHERE {
{
?teacher rdf:type Teachers.
?teacher name
?name.
?teacher faculty ?faculty.
FILTER (?name=„Joe“).
} UNION {
?teacher rdf:type Teachers.
?teacher name
?name.
?teacher faculty ?faculty.
FILTER (?name=„Fred“).
}
}
32
The SPARQL Query Language
Operator FILTER
SELECT ?name ?faculty
WHERE {
?teacher rdf:type Teachers.
?teacher name
?name.
?teacher faculty ?faculty.
FILTER (?name=„Joe“)
}
Teachers
t2
t1
faculty
name
Joe
faculty
name
“CS“
Fred
“CS“
?name
Joe
?faculty
“CS“
33
The SPARQL Query Language
Operator OPTIONAL
„Professor“
Teachers
title
t2
t1
faculty
name
Joe
faculty
SELECT ?name ?faculty ?title
WHERE {
?teacher rdf:type Teachers.
?teacher name
?name.
?teacher faculty ?faculty.
OPTIONAL {
?teacher title ?title.
}
}
name
“CS“
Fred
“CS“
?name
?faculty
Joe
“CS“
Fred
“CS“
?title
“Professor“
34
© Copyright 2026 Paperzz