--- layout: docs title: Defining rules header: Defining rules prev_section: user-guide/introduction next_section: user-guide/rules-engine doc: true --- The key API in Easy Rules is the `Rule` interface: ```java public interface Rule { /** * This method encapsulates the rule's conditions. * @return true if the rule should be applied, false else */ boolean evaluateConditions(); /** * This method encapsulates the rule's actions. * @throws Exception thrown if an exception occurs * during actions performing */ void performActions() throws Exception; //Getters and setters for rule name, //description and priority omitted. } ``` The `evaluateConditions` method encapsulates conditions that must evaluate to _TRUE_ to trigger the rule. The `performActions` method encapsulates actions that should be performed when rule's conditions are satisfied. ## Defining rules by extending _BasicRule_ Easy Rules provides a simple implementation of the `Rule` interface named `BasicRule`. This class implements most of methods defined in the `Rule` interface. You can extends this class and override `evaluateConditions` and `performActions` methods to provide your conditions and actions logic. Here is an example: ```java public class MyRule extends BasicRule { private BusinessData myBusinessData; //data to operate on @Override public boolean evaluateConditions() { //my rule conditions return true; } @Override public void performActions() throws Exception { //my actions } } ``` ## Defining rules using annotations Easy Rules provides the `@Rule` annotation that can turn a POJO into a rule. Here is an example: ```java @Rule(name = "my rule", description = "my rule description") public class MyRule { private BusinessData myBusinessData; //data to operate on @Condition public boolean when() { //my rule conditions return true; } @Action(order = 1) public void then() throws Exception { //my actions } @Action(order = 2) public void finally() throws Exception { //my final actions } } ``` You can use `@Condition` and `@Action` annotations to mark methods to execute to check rule conditions and perform rule actions respectively.
You can annotate multiple methods with the Action annotation. You can also define the execution order of actions with the order attribute: @Action(order = 1).