add air conditioning tutorial
parent
d8211625b4
commit
5cee7c6afd
@ -0,0 +1,24 @@
|
|||||||
|
package org.jeasy.rules.tutorials.airco;
|
||||||
|
|
||||||
|
import org.jeasy.rules.annotation.Action;
|
||||||
|
import org.jeasy.rules.annotation.Condition;
|
||||||
|
import org.jeasy.rules.annotation.Fact;
|
||||||
|
import org.jeasy.rules.annotation.Rule;
|
||||||
|
import org.jeasy.rules.api.Facts;
|
||||||
|
|
||||||
|
@Rule(name = "air conditioning rule", description = "if it is hot, decrease temperature" )
|
||||||
|
public class AirConditioningRule {
|
||||||
|
|
||||||
|
@Condition
|
||||||
|
public boolean isItHot(@Fact("temperature") int temperature) {
|
||||||
|
return temperature > 25;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Action
|
||||||
|
public void coolAir(Facts facts) {
|
||||||
|
System.out.println("It is hot! cooling air..");
|
||||||
|
Integer temperature = (Integer) facts.get("temperature");
|
||||||
|
facts.put("temperature", temperature - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package org.jeasy.rules.tutorials.airco;
|
||||||
|
|
||||||
|
import org.jeasy.rules.api.Facts;
|
||||||
|
import org.jeasy.rules.api.Rules;
|
||||||
|
import org.jeasy.rules.api.RulesEngine;
|
||||||
|
import org.jeasy.rules.core.InferenceRulesEngine;
|
||||||
|
|
||||||
|
public class Launcher {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// define facts
|
||||||
|
Facts facts = new Facts();
|
||||||
|
facts.put("temperature", 30);
|
||||||
|
|
||||||
|
// define rules
|
||||||
|
AirConditioningRule airConditioningRule = new AirConditioningRule();
|
||||||
|
Rules rules = new Rules();
|
||||||
|
rules.register(airConditioningRule);
|
||||||
|
|
||||||
|
// fire rules on known facts
|
||||||
|
RulesEngine rulesEngine = new InferenceRulesEngine();
|
||||||
|
rulesEngine.fire(rules, facts);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue