Skip to content

hello world

Mahmoud Ben Hassine edited this page May 17, 2017 · 8 revisions

This tutorial shows how to use Easy Rules in a very simple application. The goal is to ask the user if he is a friend of duke and say 'Hello duke's friend!' only if he replies 'yes'.

Based on this requirement, the rule is pretty straightforward :

  • The condition is that the user input must be equal to 'yes'
  • The action is to say 'Hello duke's friend!' to the user

First, let's create a rule class:

@Rule(name = "Hello World rule",
    description = "Say Hello to duke's friends only")
public class HelloWorldRule {

    /**
     * The user input which represents the data
     * that the rule will operate on.
     */
    private String input;

    @Condition
    public boolean checkInput() {
        //The rule should be applied only if
        //the user's response is yes (duke friend)
        return input.equalsIgnoreCase("yes");
    }

    @Action
    public void sayHelloToDukeFriend() throws Exception {
        //When rule conditions are satisfied,
        //prints 'Hello duke's friend!' to the console
        System.out.println("Hello duke's friend!");
    }

    public void setInput(String input) {
        this.input = input;
    }

}

Then, we have to register an instance of this rule in a Easy Rules engine and launch the tutorial with the following class:

public class Launcher {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.println("Are you a friend of duke?[yes/no]:");
        String input = scanner.nextLine();

        /**
         * Declare the rule
         */
        HelloWorldRule helloWorldRule = new HelloWorldRule();

        /**
         * Set business data to operate on
         */
        helloWorldRule.setInput(input.trim());

        /**
         * Create a rules engine and register the business rule
         */
        RulesEngine rulesEngine = aNewRulesEngine().build();
        
        rulesEngine.registerRule(helloWorldRule);

        /**
         * Fire rules
         */
        rulesEngine.fireRules();

    }
}

To run this tutorial, you can follow these instructions:

{% highlight bash %} $ git clone https://github.com/EasyRules/easyrules-tutorials.git $ cd easyrules-tutorials $ mvn install exec:java -P runHelloWorldTutorial {% endhighlight %}

You should get the following output:

Are you a friend of duke? [yes/no]:
yes
INFO: Engine name: engine
INFO: Rule priority threshold: 2,147,483,647
INFO: Skip on first applied rule: false
INFO: Skip on first failed rule: false
INFO: Registered rules:
INFO: Rule { name = 'Hello World rule', description = 'Say Hello to duke's friends only', priority = '2147483646'}
INFO: Rules evaluation started
INFO: Rule 'Hello World rule' triggered
Hello duke's friend!
INFO: Rule 'Hello World rule' performed successfully
Clone this wiki locally