74 
 
part of the antecedent so we write -> to signal the beginning of the consequent. At this point your rule 
should look like: Customer(?c) ^ numberOfPizzasPurchased(?c, ?np) ^ hasSpicinessPreference(?c, Hot) ^ 
swrlb:greaterThan(?np, 1) -> 
8. Finally, we write the consequent of the rule, the part after the arrow that signifies what to do each time 
the rule succeeds. We want to give these customers a 20% discount so we write: hasDiscount(?c, 0.2). 
Whereas the expressions on the left hand side are tests to see if the rule should fire, the expression on the 
right is an assertion of a new value to be added to the ontology. For those with a logic background the 
simple way to think of this is that the antecedent is implicitly universally quantified whereas the 
consequent is implicitly existentially quantified.  
9. Thus the whole rule should look like: Customer(?c) ^ numberOfPizzasPurchased(?c, ?np) ^ 
hasSpicinessPreference(?c, Hot) ^ swrlb:greaterThan(?np, 1) -> hasDiscount(?c, 0.2). Take note that the 
OK button at the bottom is only possible to select when the rule has a valid syntax.  It should be 
selectable now so select it. You should see the new rule show up at the top of the top most view.  
_____________________________________________________________________________________ 
Note that there is a minor bug in SWRL where sometimes the prefix for the current ontology will be 
added to all the expressions without a prefix. So at some point you may see that your expressions end up 
looking like this: pizza:Customer(?c). If this happens don’t worry it won’t affect the way the rule works at 
all. If at some point this happens and you want to remove the prefixes there is a way to do this described 
in my blog: https://www.michaeldebellis.com/post/removing-ontology-prefixes-from-swrl-rules  
Next, we want to write a second SWRL rule for other customers who have ordered more than one Pizza 
but don’t prefer Hot Pizzas.  
Exercise 35: Write Another SWRL Rule  
_____________________________________________________________________________________ 
1. Make sure you are still in the SWRLTab. Click on the HotDiscountRule and select Clone  
2. This should bring up the same window you used to create your first rule with the code for that rule in 
the window. Change the name of this rule from S1 to LessSpicyDiscountRule.      
3. Next edit the test for the Customer’s spiciness preference. Rather than just testing if it is Hot we want 
to test this time if it isMilderThan Hot.  This is an example of using the order relation we defined in 
chapter 6. Change hasSpicinessPreference(?c, Hot) to hasSpicinessPreference(?c, ?spr). Rather than just 
test if it is equal to Hot we need to bind the preference value to the parameter ?spr. Then after this add 
the usual and character and the new test, so you should add:  ^ isMilderThan(?spr, Hot) 
4. Finally, we want to change the discount for these Customers to be 10% rather than 20%. So change the 
consequent to be:  hasDiscount(?c, 0.1). 
5. Thus the whole rule should look like: Customer(?c) ^ numberOfPizzasPurchased(?c, ?np) ^ 
hasSpicinessPreference(?c, ?spr) ^ isMilderThan(?spr, Hot) ^ swrlb:greaterThan(?np, 1) -> 
hasDiscount(?c, 0.1).  
_____________________________________________________________________________________ 
Now we want to run our rules. Remember there is no need to use those DROOLS buttons. Just 
synchronize the reasoner and your rules should fire just as other DL axioms that assert values based on