5DV248 · Trustworthy Human-AI Interaction · Interactive laboratory

Lab 16 — Asking and Telling with SWRL

Extend a small university ontology with rules. Tell the knowledge base facts and SWRL rules, then ask which additional class memberships and relationships can be inferred.

Intermediate · Semantic Web · Interactive

Learning objectives

Teaching scenario: a fictional university. The ontology and individuals are synthetic and are used only to illustrate the language.

1 · The formal language

SWRL (Semantic Web Rule Language) combines OWL ontology vocabulary with Horn-like rules. In this laboratory, the ontology provides classes, object properties and named individuals. Rules contain class atoms and object-property atoms only.

OWL vocabulary

\(\mathcal{O}=(\mathcal{C},\mathcal{P},\mathcal{I},\mathcal{A})\)

\(\mathcal{C}\): classes; \(\mathcal{P}\): object properties; \(\mathcal{I}\): named individuals; \(\mathcal{A}\): ontology axioms and assertions.

\(Student\sqsubseteq Person\)
\(Student(alice),\quad EnrolledIn(alice,ai)\)

SWRL rule

\(B_1\land\cdots\land B_n\rightarrow H\)

Each \(B_i\) and \(H\) is an atom. Variables are written with a leading question mark in the concrete syntax.

\(Student(?x)\land EnrolledIn(?x,?c)\rightarrow ActiveStudent(?x)\)

The rule means: every individual enrolled in at least one course and known to be a student is an active student.

Concrete SWRL atom forms used here

Atom typeAbstract formExample
Class atom\(C(x)\)Student(?x)
Object-property atom\(R(x,y)\)EnrolledIn(?x, ?c)
Rule conjunction\(B_1\land B_2\)Student(?x) ^ EnrolledIn(?x, ?c)
Rule implication\(B\rightarrow H\)... -> ActiveStudent(?x)

Mathematical semantics

A rule can be written in logical form as a universally quantified implication. For example:

\(r:\;\forall x\forall c\big(Student(x)\land EnrolledIn(x,c)\rightarrow ActiveStudent(x)\big)\)

An interpretation \(\mathcal{I}\) satisfies the rule when every assignment of its variables that makes all body atoms true also makes the head atom true:

\(\mathcal{I}\models r\iff\forall\theta\;[\,\mathcal{I},\theta\models Body(r)\Rightarrow\mathcal{I},\theta\models Head(r)\,]\)

Here, \(\theta\) maps variables to domain elements. A class atom is true when its assigned individual belongs to the class extension; an object-property atom is true when the assigned pair belongs to the property extension.

Scope of this implementation. General OWL + SWRL reasoning has important decidability and implementation complexities. This browser lab uses a finite, named-individual rule engine over a positive, function-free subset: class atoms, object-property atoms, conjunctions and one-atom rule heads. It does not implement a complete OWL 2 DL or SWRL reasoner, datatype atoms, built-ins, negation, equality, or arbitrary OWL entailment.

2 · The ontology and initial rules

The ontology contains a small class hierarchy and two object properties. Domain and range declarations are displayed as ontology information; this teaching engine does not perform general OWL domain/range reasoning.

OWL · Classes and subclass axioms
Student ⊑ Person
Lecturer ⊑ Person
ActiveStudent ⊑ Person
TeachingStaff ⊑ Person

Object properties

EnrolledIn : Person → Course
Teaches : Lecturer → Course
OWL · Asserted facts
Student(alice)
Student(bob)
Course(ai)
Course(robotics)
EnrolledIn(alice, ai)
EnrolledIn(bob, robotics)
Lecturer(mira)
Teaches(mira, ai)

Initial SWRL rules

Student(?x) ^ EnrolledIn(?x, ?c) -> ActiveStudent(?x)

A student enrolled in a course is inferred to be an active student.

Lecturer(?x) ^ Teaches(?x, ?c) -> TeachingStaff(?x)

A lecturer who teaches a course is inferred to be teaching staff.

For example, the first rule matches Student(alice) and EnrolledIn(alice, ai), so it derives ActiveStudent(alice).

3 · TELL — Add ontology assertions and SWRL rules

Add a class assertion, an object-property assertion, or a SWRL rule. Class names must be unary; object-property names must be binary. New predicates can be introduced by using them in a statement, but keep their arity consistent.

Choose a statement type and enter a statement.

Syntax reminders

  • Class assertion: Student(charlie)
  • Property assertion: EnrolledIn(charlie, ai)
  • Rule: Student(?x) ^ EnrolledIn(?x, ?c) -> ActiveStudent(?x)
  • Variables begin with ?.
  • Every variable in the rule head must occur in its body (range-restricted/safe rule).
  • Use ^ for conjunction and exactly one ->.

Current asserted ontology facts

Class assertions
Object-property assertions

Current SWRL rules

4 · APPLY RULES — Derive additional knowledge

The rule engine repeatedly matches rule-body atoms against the current set of known assertions. When a match is found, it instantiates the rule head and adds the resulting assertion if it is new. The process stops at a fixed point.

\(\mathcal{I}_{0}\subseteq\mathcal{I}_{1}\subseteq\cdots\subseteq\mathcal{I}_{n}=\mathcal{I}_{n+1}\)

For this finite positive rule fragment, the iteration terminates because no new individuals are generated and only finitely many ground atoms can be formed from the named individuals and predicates.

No rule application has been run yet.

Derived assertions and explanations

Apply the rules to see inferred assertions.

5 · ASK — Query the ontology

Ask whether a ground class/property assertion is supported, or submit a conjunctive query to find variable substitutions. Run the rules first (or use Ask, which computes the closure automatically).

For conjunctive queries, use ^ and variables such as ?x, e.g. ActiveStudent(?x) ^ EnrolledIn(?x, ai).

Submit a query to see the result.

Try these queries

ActiveStudent(alice)
TeachingStaff(mira)
ActiveStudent(mira)
ActiveStudent(?x) ^ EnrolledIn(?x, ai)
TeachingStaff(?x) ^ Teaches(?x, ?c)

Inference explanation

No query has been evaluated yet.

6 · Exercises

Exercise 1 — Add a new student

Tell Student(charlie) and EnrolledIn(charlie, ai). Apply the rules. Which new assertion is derived?

Exercise 2 — Write a rule

Write a rule that infers CourseParticipant(?x) when an individual is enrolled in a course. What must the rule body contain?

Exercise 3 — Asserted or inferred?

Was ActiveStudent(alice) explicitly added as an ontology assertion?

Exercise 4 — What does failure mean?

If ActiveStudent(mira) is not derived, can we conclude ¬ActiveStudent(mira)?

7 · What this laboratory does — and does not — implement

FeatureImplemented?Notes
Class assertionsYesUnary class atoms over named individuals.
Object-property assertionsYesBinary object-property atoms over named individuals.
Conjunctive rule bodiesYesPositive conjunction of class/property atoms.
Single-atom rule headsYesHead variables must occur in the body.
Repeated forward chainingYesComputes a finite closure over the current named individuals.
General OWL 2 DL reasoningNoOntology display is illustrative; no complete OWL reasoner is embedded.
SWRL built-ins / datatype atomsNoOnly class and object-property atoms are supported.
Negation, equality, arbitrary FOLNoOutside the supported rule fragment.

The implementation is a teaching engine for a finite, positive, function-free subset. It illustrates rule matching and derivation; it should not be treated as a standards-complete SWRL reasoner or as a general decision procedure for OWL/SWRL.