-
Notifications
You must be signed in to change notification settings - Fork 1.1k
air conditioning
This tutorial shows an example of how to use the InferenceRulesEngine
. We would like to implement a simple air conditioning system with the following requirement: Given the temperature as a fact, when it is hot, then the system should cool the air until a certain degree. We consider 25 degrees as the threshold for "hot".
First, let's create a condition to define when it is "hot":
public class HighTemperatureCondition implements Condition {
@Override
public boolean evaluate(Facts facts) {
Integer temperature = facts.get("temperature");
return temperature > 25;
}
static HighTemperatureCondition itIsHot() {
return new HighTemperatureCondition();
}
}
Then an action to decrease the temperature:
public class DecreaseTemperatureAction implements Action {
@Override
public void execute(Facts facts) throws Exception {
System.out.println("It is hot! cooling air..");
Integer temperature = facts.get("temperature");
facts.put("temperature", temperature - 1);
}
static DecreaseTemperatureAction decreaseTemperature() {
return new DecreaseTemperatureAction();
}
}
Now, let's create a rule using the previous condition and action:
Rule airConditioningRule = new RuleBuilder()
.name("air conditioning rule")
.when(itIsHot())
.then(decreaseTemperature())
.build();
Here we use the static methods itIsHot()
and decreaseTemperature()
defined respectively in HighTemperatureCondition
and DecreaseTemperatureAction
.
Finally, let's use a InferenceRulesEngine
to fire this rule:
public class Launcher {
public static void main(String[] args) {
// define facts
Facts facts = new Facts();
facts.put("temperature", 30);
// define rules
Rule airConditioningRule = new RuleBuilder()
.name("air conditioning rule")
.when(itIsHot())
.then(decreaseTemperature())
.build();
Rules rules = new Rules();
rules.register(airConditioningRule);
// fire rules on known facts
RulesEngine rulesEngine = new InferenceRulesEngine();
rulesEngine.fire(rules, facts);
}
}
To run this tutorial, you can follow these instructions:
$ git clone https://github.com/j-easy/easy-rules.git
$ cd easy-rules
$ mvn install
$ cd easy-rules-tutorials
$ mvn exec:java -P runAircoTutorial
You should see the following output:
INFO: Selecting candidate rules based on the following facts: [ { temperature : 30 } ]
INFO: Rule 'air conditioning rule' triggered
It is hot! cooling air..
INFO: Rule 'air conditioning rule' performed successfully
INFO: Selecting candidate rules based on the following facts: [ { temperature : 29 } ]
INFO: Rule 'air conditioning rule' triggered
It is hot! cooling air..
INFO: Rule 'air conditioning rule' performed successfully
INFO: Selecting candidate rules based on the following facts: [ { temperature : 28 } ]
INFO: Rule 'air conditioning rule' triggered
It is hot! cooling air..
INFO: Rule 'air conditioning rule' performed successfully
INFO: Selecting candidate rules based on the following facts: [ { temperature : 27 } ]
INFO: Rule 'air conditioning rule' triggered
It is hot! cooling air..
INFO: Rule 'air conditioning rule' performed successfully
INFO: Selecting candidate rules based on the following facts: [ { temperature : 26 } ]
INFO: Rule 'air conditioning rule' triggered
It is hot! cooling air..
INFO: Rule 'air conditioning rule' performed successfully
INFO: Selecting candidate rules based on the following facts: [ { temperature : 25 } ]
INFO: No candidate rules found for facts: [ { temperature : 25 } ]
As you can see, the InferenceRulesEngine
will continuously select and fire candidate rules until no more rules are applicable. If we used a DefaultRulesEngine
instead, only the first run would have been executed and the temperature would have remained at 29.
Easy Rules is created by Mahmoud Ben Hassine with the help of some awesome contributors
-
Introduction
-
User guide
-
Tutorials
-
Get involved