Java规则引擎——EasyRules的使⽤实例⼀、easy-rule使⽤的⼏种⽅式:
1、直接在代码中写规则
package org.songdan.easy.rules.anno;
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.api.RulesEngine;
import org.DefaultRulesEngine;
import org.RulesEngineParameters;
/**
* @author coco_xu
* @date 2020-04-16
*/
public class FizzBuzzMainWithRule {
public static void main(String[] args) {
// create a rules engine
RulesEngineParameters parameters = new RulesEngineParameters().skipOnFirstAppliedRule(true);
RulesEngine fizzBuzzEngine = new DefaultRulesEngine(parameters);
// create rules
Rules rules = new Rules();
// fire rules
Facts facts = new Facts();
for (int i = 1; i <= 100; i++) {
facts.put("number", i);
fizzBuzzEngine.fire(rules, facts);
System.out.println();
}
}
}
2、
package org.songdan.easy.rules.fluent;
import org.jeasy.rules.api.*;
import org.DefaultRulesEngine;
import org.InferenceRulesEngine;
import org.RuleBuilder;
import org.RulesEngineParameters;
import org.jeasy.rules.mvel.MVELRule;
import org.jeasy.rules.support.UnitRuleGroup;
import org.songdan.del.NumObj;
/**
* @author: coco_xu
* @create: 2020-04-16
**/
public class FizzBuzzMainWIthFluent {
public static void main(String[] args) {
Rule fizzRule = new RuleBuilder().name("fizzRule").description("fizz num").when(new Condition() {
public boolean evaluate(Facts facts) {
Integer num = ("num");
return num % 5 == 0;
}
}).then(new Action() {
public void execute(Facts facts) throws Exception {
System.out.("num")+" is a fizz num");
}
}).priority(1).build();
Rule buzzRule = new RuleBuilder().name("fizzRule").description("fizz num").when(new Condition() {
public boolean evaluate(Facts facts) {
Integer num = ("num");
return num % 7 == 0;
}
}).then(new Action() {
public void execute(Facts facts) throws Exception {
System.out.("num")+" is a buzz num");
}
}).priority(2).build();
UnitRuleGroup unitRuleGroup = new UnitRuleGroup("mixRule","fizz and buzz num ",3);
unitRuleGroup.addRule(fizzRule);
unitRuleGroup.addRule(buzzRule);
MVELRule mvelRule = new MVELRule()
.name("weather rule")
.description("if it rains then take an umbrella")
.when("ate() == 10")
.then("System.out.println(\"It rains, take an umbrella!\");");
RulesEngineParameters parameters = new RulesEngineParameters().skipOnFirstAppliedRule(fal);
RulesEngine fizzBuzzEngine = new DefaultRulesEngine(parameters);
Facts facts = new Facts();
Rules rules = new Rules();
for (int i = 0; i < 100; i++) {
facts.put("num",i);
fizzBuzzEngine.fire(rules,facts);
System.out.println();
}
facts.put("obj", new NumObj(10));
fizzBuzzEngine.fire(rules,facts);
}
}
3、在yml⽂件中配置规则,然后进⾏读取
package org.songdan.l;
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rules;
骚气十足的网名
import org.jeasy.rules.api.RulesEngine;
import org.DefaultRulesEngine;
import org.RulesEngineParameters;
import org.jeasy.rules.mvel.MVELRuleFactory;
import org.songdan.easy.rules.anno.DiaModel;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
/**
陈翠萍
* @author: coco_xu
* @create: 2020-04-16
**/
public class FizzBuzzMainWithYmlRule {
private static String result ;
描写冬天的景象public static void main(String[] args) throws FileNotFoundException {
// create a rules engine
RulesEngineParameters parameters = new RulesEngineParameters().skipOnFirstAppliedRule(true);
RulesEngine fizzBuzzEngine = new DefaultRulesEngine(parameters);
InputStreamReader inputStreamReader = new InputStreamReader(ClassLoader().getResourceAsStream("l"));
// create rules
Rules rules = ateRulesFrom(inputStreamReader);
// fire rules
宝马广告词Facts facts = new Facts();
DiaModel diaModel = new DiaModel("ssss化疗");
String input ="ssss化疗";
facts.put("diaModel", diaModel);
fizzBuzzEngine.fire(rules, facts);
System.out.Result());
}
}
⼆、使⽤easy-rules返回数据对象
小学一年级语文easy-rules的返回结果中只能使⽤void⽅法,不能直接返回Object。否则报错:
Exception in thread "main" java.lang.IllegalArgumentException: Action method 'public java.lang.String
org.easy.rules.anno.RuleClass$FizzRule.printFizz()' defined in rule 'org.songdan.easy.rules.anno.RuleClass$FizzRule' must be public, must return
void type and may have parameters annotated with @Fact (and/or exactly one parameter of type or
extending Facts).此时可以将输⼊定义为⼀个实体类,在action中,直接修改需要修改的内容即可,如下:
---
name: "rule_1_对症治疗"
description: "print fizz if the number is multiple of 5"
priority: 1
condition: "Input().contains(\"对症治疗\")"
actions:
- "sult = \"对症治疗\")"
---
name: "rule_2_⼦宫平滑肌瘤"
description: "print buzz if the number is multiple of 7"
priority: 2
condition: "Input().contains(\"⼦宫平滑肌瘤\")"
actions:
- "sult = \"⼦宫平滑肌瘤\")"
颜色的词
---摽有梅
name: "rule_3_化学治疗"
description: "Input().contains(\"化疗\") "
priority: 3
condition: "Input().contains(\"化疗\") || ains(\"化学治疗\")"
actions:
- "sult = \"化学治疗\""个性签名女生可爱