This tutorial shows how define priority in which rules must be fired.
In Easy Rules, every rule has a priority. Rules are fired by default according to their priorities (this can be changed as described in the [user guide]({{site.url}}/user-guide/defining-rules.html#rules-priorities)).
In this tutorial, we have an application that sells alcohol. The application must flag the customer as adult if his age is greater than 18,
and must deny children from buying alcohol. Customers are represented by the following `Person` class:
```java
public class Person {
private String name;
private int age;
private boolean adult;
//getters and setters omitted
}
```
Based on these requirements, we can define the following rules:
* Rule 1: This rule should operate an a `Person` instance, check that the peron age is greater than 18 and set the adult flag.
* Rule 2: This rule should operate an a `Person` instance, check that the peron is adult and deny children (ie, non adult) from buying alcohol.
Rule 1 should be fired **_before_** rule 2. We will set rule 1 priority to 1 and rule 2 priority to 2 so that Easy Rules engine fire them in this order.
First, let's create a class for rule 1:
```java
@Rule(name = "AgeRule",
description = "Check if person's age is > 18
and marks the person as adult")
public class AgeRule {
private Person person;
private int adultAge = 18;
public AgeRule(Person person) {
this.person = person;
}
@Condition
public boolean isAdult() {
return person.getAge() > adultAge;
}
@Action
public void markAsAdult(){
person.setAdult(true);
System.out.printf(
"Person %s has been marked as adult.\n",
person.getName());
}
@Priority
public int getPriority() {
return 1;
}
}
```
As required, this rule class operates on a person that is passed at construction time.
The `isAdult` method annotated with `@Condition` tells Easy Rules engine to call this method to check if the rule should be fired, in this case, if the person's age is greater than 18.
The `markAsAdult` method annotated with `@Action` will mark the person as adult by setting the `adult` flag.
Finally, the `getPriority` method annotated with `@Priority` tells Easy Rules engine to fire this rule in first order.