Writing and executing our first rule
Now that we have our project structure ready, we can write our first rule. For that, we will create a new empty text file. This will be a static resource, therefore, we need to place it in the src/main/resources
directory. This text file needs to have the .drl
extension so that it can be picked up as a rule file. In the .drl
files, we will write as many rules as we want. Now, we will start easy with just one rule.
Let's write our first rule to classify the items based on what they cost us. Our rules.drl
text file will look similar to the following:
package myfirstproject.rules import org.drools.devguide.eshop.model.Item; import org.drools.devguide.eshop.model.Item.Category; rule "Classify Item - Low Range" when $i: Item(cost < 200) then $i.setCategory(Category.LOW_RANGE); end
This rule checks for each item that costs less than 200
USD and automatically tags it with a category, in this case, LOW_RANGE
. For our shop...