# Practicum 5: Ontologies and Description Logic

<div align="center">

**Practicum date: {{prac5_date}}**  

</div>

**Course:** RO47007 Knowledge Representation and Reasoning, TU Delft, CoR  
**Instructors:** Burak Şişman, email: B.Sisman@tudelft.nl

In this practicum you will gain hands-on experience with ontologies and Description Logic (DL). You will practice translating natural language statements into formal DL expressions, writing ABox assertions, constructing SPARQL queries, and reasoning about knowledge bases. All exercises use a consistent "kitchen robot" domain to reinforce concepts from the lecture.

We expect it will take you roughly 90 minutes for exercises 1-4. Exercise 5 is optional for those who finish early or want additional practice.

---

## Learning Objectives

By the end of this practicum, you will be able to:
- Translate natural language statements into Description Logic TBox axioms
- Write ABox assertions using proper DL notation
- Construct SPARQL queries to retrieve information from an ontology
- Apply reasoning to determine what can be inferred from a knowledge base
- Understand the difference between querying asserted vs. inferred facts

---

## Preparation

**Background Knowledge**: You should have attended the Week 5 lecture on Ontologies and Description Logic. Familiarity with the following concepts is assumed:
- Ontology components (classes, properties, individuals, axioms)
- ALC concept constructors (⊓, ⊔, ¬, ∃, ∀)
- TBox vs ABox distinction
- Open-World vs Closed-World Assumption

**Notation Reference**: Throughout this practicum, use the following DL notation:

| Symbol | Meaning | Example |
|--------|---------|---------|
| ⊑ | subsumption ("is a") | `Cup ⊑ Container` |
| ≡ | equivalence ("is defined as") | `MobileRobot ≡ Robot ⊓ ∃hasPart.Wheel` |
| ⊓ | conjunction ("and") | `Robot ⊓ Mobile` |
| ⊔ | disjunction ("or") | `Cup ⊔ Bowl` |
| ¬ | negation ("not") | `¬Robot` |
| ∃ | existential ("some") | `∃hasGripper.Gripper` |
| ∀ | universal ("all/only") | `∀contains.Liquid` |

> **Important**: Remember that `∀r.C` does NOT imply existence. It means "if there are any r-fillers, they must be C" — an empty set satisfies this vacuously. To require both existence and restriction, use `∃r.⊤ ⊓ ∀r.C`.

---

## Running Example: Kitchen Robot Domain

Throughout this practicum, you will work with a TIAGo robot operating in a kitchen environment. The robot must understand:
- **Objects**: cups, bowls, knives, cutting boards, apples
- **Surfaces**: tables, counters
- **Actions**: grasping, placing, cutting, containing

We will progressively build an ontology for this domain.

**Assumed Background TBox Axioms** (use these throughout):
```
Apple ⊑ PhysicalObject
Bowl ⊑ Container
Gripper ⊑ Tool
Liquid ⊑ PhysicalObject
```

---

## Exercise 1: Building the TBox (15 minutes)

Translate the following natural language statements into Description Logic TBox axioms.

| # | Natural Language | Your DL Translation |
|---|------------------|---------------------|
| a | Every cup is a container. | |
| b | A graspable object is a physical object that has small size. | |
| c | A knife is a tool that can cut. | |
| d | Containers can only contain physical objects. | |
| e | A mobile robot is a robot that has at least one wheel. | |

<details>
<summary>💡 Hints (click to expand)</summary>

- "Every X is Y" → `X ⊑ Y`
- "X is defined as Y that has Z" → `X ≡ Y ⊓ ∃r.Z`
- "can only" → universal restriction `∀r.C`
- "has at least one" → existential restriction `∃r.C`
- Decide whether each statement is a **definition** (≡) or just a **necessary condition** (⊑)

</details>

---

## Exercise 2: Populating the ABox (10 minutes)

Given our kitchen scenario, write ABox assertions for the following facts. 

**Use function-style notation for role assertions**: `r(a, b)` means "a is related to b via role r"

**Scenario**: The kitchen contains:
- TIAGo robot (`tiago01`) with a gripper (`gripper1`)
- A red mug (`redMug`) on the kitchen table (`kitchenTable`)
- A knife (`knife1`)
- A cutting board (`cuttingBoard`)
- A bowl of apples (`bowl1` containing `apple1`)

| # | Fact to Assert | Your ABox Assertion |
|---|----------------|---------------------|
| a | tiago01 is a MobileRobot | |
| b | tiago01 has gripper1 | |
| c | redMug is on kitchenTable | |
| d | bowl1 contains apple1 | |
| e | knife1 is both a Knife and a GraspableObject | |

<details>
<summary>💡 Hints (click to expand)</summary>

- Concept assertion: `individual : Concept`
- Role assertion: `roleName(subject, object)`
- Multiple concept membership: write two separate assertions or use `individual : C₁ ⊓ C₂`

</details>

---

## Exercise 3: Writing SPARQL Queries (15 minutes)

Write SPARQL queries to answer the following questions about our kitchen domain.

**Use these prefixes in all queries:**
```sparql
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX kitchen: <http://example.org/kitchen#>
```

### 3-A: Find all containers in the kitchen.

> **Think about this**: Will this query find Cups if we only asserted `Cup ⊑ Container` in the TBox?

```sparql
# Your query here

```

### 3-B: Find all objects that are on the kitchen table.

```sparql
# Your query here

```

### 3-C: Find all tools that can cut (i.e., have the canCut property).

```sparql
# Your query here

```

### 3-D: Find robots and their grippers (if any). Use OPTIONAL to include robots without grippers.

```sparql
# Your query here

```

<details>
<summary>⚠️ Important Note on SPARQL vs Reasoning</summary>

**SPARQL queries the asserted graph, not inferred facts.**

If you only asserted `redMug : Cup` and have `Cup ⊑ Container` in the TBox, a SPARQL query for `?x rdf:type kitchen:Container` will **NOT** return `redMug` unless:
1. A reasoner has materialized the inferred type, OR
2. The SPARQL endpoint has OWL/RDFS entailment enabled

This is a fundamental distinction: **SPARQL queries the graph; reasoning computes the entailments.**

</details>

---

## Exercise 4: Reasoning and Inference (15 minutes)

Given the TBox and ABox you created, answer the following reasoning questions.

### 4-A: Subsumption Inference

If we assert `redMug : Cup`, can we infer `redMug : Container`? 

**Your answer**: (Yes/No)

**Explain the reasoning chain using TBox axioms**:

```
# Your reasoning steps here

```

### 4-B: Satisfiability Check

Is the concept `Cup ⊓ ¬Container` satisfiable given our TBox (which includes `Cup ⊑ Container`)?

**Your answer**: (Yes/No)

**Explanation**:

```
# Your explanation here

```

### 4-C: Open-World vs Closed-World

We have **NOT** asserted that `knife1` is a `Robot`.

| Assumption | Is knife1 a Robot? | Explanation |
|------------|-------------------|-------------|
| Under OWA | | |
| Under CWA | | |

### 4-D: Universal Restriction Gotcha

Given the axiom `Container ⊑ ∀contains.PhysicalObject`, does this mean every container **must contain** something?

**Your answer**: (Yes/No)

**Explanation**:

```
# Your explanation here

```

---

## Exercise 5: Robotics Application (Optional, 10 minutes)

Design ontology components for a robot task scenario.

**Scenario**: The robot needs to "cut the apple." Design TBox axioms and write a SPARQL query that would help the robot find an appropriate tool.

### 5-A: Write TBox axioms that define what makes something a cutting tool.

```
# Your TBox axioms here

```

### 5-B: Write a SPARQL query to find all tools the robot could use to cut the apple.

```sparql
# Your query here

```

### 5-C: Discussion Question

What additional information would the robot need (beyond the ontology) to actually **execute** the cutting task?

Think about:
- What does PDDL provide that ontologies don't?
- What's the difference between knowing "what can cut" vs "how to cut"?

```
# Your answer here

```

<details>
<summary>💡 Key Insight</summary>

**Ontologies provide semantic filtering** — they tell the robot what's possible, what categories exist, and what objects have which affordances.

**Action dynamics require planners** — preconditions, effects, temporal ordering, and state changes are the domain of PDDL (which you'll explore in Week 6).

The ontology answers: "What tools CAN cut?"  
The planner answers: "HOW do I use a knife to cut an apple?"

</details>

---

## Key Takeaways

1. **DL Notation Matters**: Use the correct symbols (⊑ vs ≡, ∃ vs ∀) — they have precise meanings
2. **∀ Does Not Imply Existence**: `∀r.C` is satisfied by things with no r-relationships at all
3. **SPARQL ≠ Reasoning**: SPARQL queries the asserted graph; inference requires a reasoner
4. **OWA for Robotics**: Robots operate with incomplete information — unknown ≠ false
5. **Ontologies + Planners**: Ontologies provide semantic knowledge; PDDL provides action dynamics

---

## Additional Resources

- **Protégé**: [protege.stanford.edu](https://protege.stanford.edu/) — Free ontology editor
- **OWL 2 Primer**: [w3.org/TR/owl2-primer](https://www.w3.org/TR/owl2-primer/)
- **SPARQL Reference**: [w3.org/TR/sparql11-query](https://www.w3.org/TR/sparql11-query/)
- **Description Logic Handbook**: Comprehensive reference for DL theory


