72 
 
Chapter 10 SWRL and SQWRL 
 
The Semantic Web Rule Language (SWRL) was created because there are certain kinds of inferences that 
can’t be done by Description Logic (DL) axioms. Also, in my experience there are also times where an 
inference can be done using DL, but it can be more intuitive to define that inference as a rule.  
There are actually two UI’s for SWRL in Protégé. There is the SWRL tab and there is also a Rules view 
that can be added to the UI as we added a view in section 8.2. The SWRL tab is the one that is being more 
actively developed and I recommend you always use that. This chapter will focus on the SWRL tab. 
Everything in this chapter applies to the SWRL tab and will be slightly different in the Rules view. For an 
overview of the Rules view see the SWRL Process Modeling tutorial listed at the end of this chapter.  
Like all rule systems, SWRL consists of a left-hand side (called the antecedent) and a right-hand side 
(called the consequent). The two are separated by an arrow created with a dash and a greater than 
character like this: ->. Each expression in a SWRL rule is separated by a ^ sign. The consequent of the 
rule fires if and only if every expression in the antecedent is satisfied. Since the antecedent can be 
satisfied multiple times, this means that SWRL rules can do iteration. They will fire for every 
combination of values that can satisfy the antecedent. All parameters (variables that are wildcards and get 
bound dynamically as the rule fires) are preceded by a ?.  
SWRL expressions consist of 3 types: 
1. Class expressions. This is the name of a class followed by parentheses with a parameter inside. 
For example Customer(?c) will bind ?c to an instance of the class Customer and (assuming the 
rest of the antecedent is satisfied) will iterate over each instance of the Customer class.  
2. Property expressions. This is the name of a property followed by parentheses and two parameters: 
the first for the individual that is being tested and the second to bind to the value of that property 
for that individual. Note that since individuals can have more than one value for a property this 
can also create iteration, where the rules will iterate over every property value for each individual. 
E.g., purchasedPizza(?c, ?p) will bind ?p to each Pizza purchased by each customer 
?c.  
3. Built-in functions. SWRL has a number of built-in functions for doing mathematical tests, string 
tests, etc. The SWRL built-ins are documented here: https://www.w3.org/Submission/SWRL/  
All SWRL built-ins are prefaced by the swrlb prefix. E.g., the math built-in 
swrlb:greaterThan(?np, 1) succeeds if the value of ?np is greater than 1.  
We are going to add two simple SWRL rules to our Pizza ontology to compute discounts for some 
Customers. We are assuming that our Pizza restaurant hasn’t been in business long, so they want to 
give a discount to anyone who has purchased more than one Pizza. Also, their manager overestimated 
the love of spicy ingredients of their customers, and they have a lot of Jalapeno peppers that they want to 
use before they go bad, so they are offering a larger discount to customers who prefer Hot pizzas rather 
than those who prefer Medium or Mild.  
To begin with let’s write the first rule to give a 20% discount to all customers who have purchased more 
than 2 Pizzas and prefer Hot Pizzas.