en
Tutorials
Week 2: RDF & RDFS

Week 2: RDF & RDFS

Learning Objectives

This week covers the Resource Description Framework (RDF) and RDF Schema (RDFS), the foundational standards for semantic web technologies. You will learn to represent knowledge as SPO triples and query them using SPARQL.


1. Understanding RDF

The Atomic Unit of Knowledge: "Breaking Sentences into Data"

Imagine you want to teach a computer everything about a movie. You could give it a long paragraph of text, but computers represent specific facts better in structured forms.

RDF (Resource Description Framework) forces us to break every piece of knowledge down into its smallest, indivisible atomic unit: the Triple.

It's like breaking a complex LEGO castle back down into individual bricks so you can ship them to a friend. These "Knowledge Atoms" are universal.

RDF is the standard model for data interchange on the Web.

The Triple Model

RDF uses a simple but powerful data model: the triple.

Subject - Predicate - Object
ComponentDescriptionExample
SubjectThe resource being describedex:JohnSmith
PredicateThe property or relationshipex:worksFor
ObjectThe value or related resourceex:Acme_Corp

Example Triples

@prefix ex: <http://example.org/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
 
ex:JohnSmith a foaf:Person ;
    foaf:name "John Smith" ;
    ex:worksFor ex:Acme_Corp ;
    ex:age 35 .
 
ex:Acme_Corp a ex:Company ;
    foaf:name "Acme Corporation" ;
    ex:location "New York" .

2. RDF Serialization Formats

RDF can be serialized in multiple formats:

FormatExtensionUse Case
Turtle.ttlHuman-readable, compact
N-Triples.ntLine-based, streaming
RDF/XML.rdfXML-based, legacy systems
JSON-LD.jsonldWeb APIs, JavaScript

Turtle Format Example

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix ex: <http://example.org/> .
 
ex:Dog rdfs:subClassOf ex:Animal .
ex:Rex a ex:Dog ;
    ex:hasName "Rex" ;
    ex:hasAge 5 .

3. Working with rdflib in Python

rdflib is the standard Python library for working with RDF.

Installation and Basic Usage

from rdflib import Graph, Namespace, Literal, URIRef
from rdflib.namespace import RDF, RDFS, FOAF
 
# Create a new graph
g = Graph()
 
# Define namespaces
EX = Namespace("http://example.org/")
g.bind("ex", EX)
g.bind("foaf", FOAF)
 
# Add triples
g.add((EX.John, RDF.type, FOAF.Person))
g.add((EX.John, FOAF.name, Literal("John Smith")))
g.add((EX.John, EX.worksFor, EX.Acme))
g.add((EX.John, EX.age, Literal(35)))
 
# Serialize to Turtle
print(g.serialize(format="turtle"))

Loading and Parsing RDF

# Load from file
g = Graph()
g.parse("data.ttl", format="turtle")
 
# Load from URL
g.parse("http://dbpedia.org/resource/Python")
 
# Iterate over triples
for subj, pred, obj in g:
    print(f"{subj} - {pred} - {obj}")

4. RDFS: RDF Schema

RDFS extends RDF with a vocabulary for describing classes and properties.

RDFS Vocabulary

TermDescription
rdfs:ClassDeclares a class
rdfs:subClassOfClass hierarchy
rdfs:PropertyDeclares a property
rdfs:subPropertyOfProperty hierarchy
rdfs:domainProperty applies to this class
rdfs:rangeProperty values are of this class

RDFS Example

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix ex: <http://example.org/> .
 
# Class hierarchy
ex:Animal a rdfs:Class .
ex:Mammal a rdfs:Class ;
    rdfs:subClassOf ex:Animal .
ex:Dog a rdfs:Class ;
    rdfs:subClassOf ex:Mammal .
 
# Property definitions
ex:hasOwner a rdf:Property ;
    rdfs:domain ex:Animal ;
    rdfs:range ex:Person .

5. Introduction to SPARQL

SPARQL is the query language for RDF data, similar to SQL for relational databases.

Basic Query Structure

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX ex: <http://example.org/>
 
SELECT ?name ?company
WHERE {
    ?person a foaf:Person .
    ?person foaf:name ?name .
    ?person ex:worksFor ?company .
}

Common SPARQL Patterns

# Filter by condition
SELECT ?name ?age
WHERE {
    ?person foaf:name ?name ;
            ex:age ?age .
    FILTER (?age > 30)
}
 
# Optional patterns
SELECT ?name ?email
WHERE {
    ?person foaf:name ?name .
    OPTIONAL { ?person foaf:mbox ?email }
}
 
# Aggregation
SELECT ?company (COUNT(?person) as ?employeeCount)
WHERE {
    ?person ex:worksFor ?company .
}
GROUP BY ?company
ORDER BY DESC(?employeeCount)

SPARQL in Python

from rdflib import Graph
 
g = Graph()
g.parse("data.ttl", format="turtle")
 
query = """
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX ex: <http://example.org/>
 
SELECT ?name ?age
WHERE {
    ?person a foaf:Person ;
            foaf:name ?name ;
            ex:age ?age .
}
"""
 
results = g.query(query)
for row in results:
    print(f"Name: {row.name}, Age: {row.age}")

6. Linked Data Principles

Tim Berners-Lee's four principles for Linked Data:

  • Use URIs as names for things
  • Use HTTP URIs so people can look up those names
  • Provide useful information using standards (RDF, SPARQL)
  • Include links to other URIs for discovery

Example: Linking to DBpedia

@prefix ex: <http://example.org/> .
@prefix dbr: <http://dbpedia.org/resource/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
 
ex:Python owl:sameAs dbr:Python_(programming_language) .

Project: Movie Recommendation Knowledge Graph

Progress

WeekTopicProject Milestone
1Ontology IntroductionMovie domain design completed
2RDF & RDFSConvert movie data to triples
3OWL & ReasoningDiscover hidden relationships with inference rules
4Knowledge ExtractionCollect movie information from Wikipedia
5Neo4jStore in graph DB and query
6GraphRAGNatural language queries
7Ontology AgentAutomatic updates for new movies
8Domain ExtensionMedical/Legal/Finance cases
9Service DeploymentAPI + Dashboard

Week 2 Milestone: Representing Movie Data as RDF

This week, you will convert the ontology designed in Week 1 into RDF triples.

Data to Convert:

@prefix movie: <http://example.org/movie/> .
@prefix schema: <http://schema.org/> .
 
movie:Inception a schema:Movie ;
    schema:name "Inception" ;
    schema:datePublished "2010-07-16" ;
    schema:director movie:ChristopherNolan ;
    schema:actor movie:LeonardoDiCaprio ;
    schema:genre movie:SciFi .
 
movie:ChristopherNolan a schema:Person ;
    schema:name "Christopher Nolan" ;
    schema:birthDate "1970-07-30" .

Example SPARQL Query:

# Find movies directed by Christopher Nolan
SELECT ?movie ?title WHERE {
    ?movie schema:director movie:ChristopherNolan ;
           schema:name ?title .
}

In the project notebook, you'll convert movie data to web-standard formats.

In the project notebook, you will implement:

  • Convert 10 movies (Inception, The Dark Knight, etc.) to Turtle format
  • Model director/actor relationships using Schema.org vocabulary
  • Write "List Nolan's movies" SPARQL query
  • Export to JSON-LD (web compatible)

What you'll build by Week 9: An AI agent that answers "Recommend sci-fi movies like Nolan's style" by reasoning over director-genre-rating relationships in the knowledge graph


Practice Notebook

For deeper exploration of the theory:

The practice notebook covers additional topics:

  • Comparing Turtle, N-Triples, JSON-LD formats
  • Advanced SPARQL queries (FILTER, OPTIONAL, UNION)
  • Modeling person networks with FOAF
  • Linking to external ontologies (DBpedia)

Interview Questions

What are the advantages of using RDF over traditional relational databases?

Key Points:

  • Flexibility: Schema-less, can add new relationships without restructuring
  • Integration: Standard model enables easy data merging from multiple sources
  • Semantics: Formal meaning enables automated reasoning
  • Linked Data: Natural support for connecting to external knowledge bases
  • Graph Structure: Better for highly connected, heterogeneous data

Premium Content

Want complete solutions with detailed explanations and production-ready code?

Check out the Ontology & Knowledge Graph Cookbook Premium (opens in a new tab) for:

  • Complete notebook solutions with step-by-step explanations
  • Real-world case studies and best practices
  • Interview preparation materials
  • Production deployment guides

Next Steps

In Week 3: OWL & Reasoning, you will learn about the Web Ontology Language and how to perform automated reasoning.