|
|
@@ -1,12 +1,12 @@
|
|
|
package com.citu.module.menduner.reward;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.citu.module.menduner.reward.core.EasyRulesEngine;
|
|
|
+import com.citu.module.menduner.reward.core.RuleBase;
|
|
|
import com.citu.module.menduner.reward.rule.DynamicRule;
|
|
|
+import com.citu.module.menduner.reward.rule.DynamicRuleAction;
|
|
|
import org.jeasy.rules.api.Facts;
|
|
|
-import org.jeasy.rules.api.Rule;
|
|
|
-import org.jeasy.rules.api.Rules;
|
|
|
-import org.jeasy.rules.api.RulesEngine;
|
|
|
-import org.jeasy.rules.core.DefaultRulesEngine;
|
|
|
-import org.jeasy.rules.mvel.MVELRule;
|
|
|
|
|
|
/**
|
|
|
* @author rayson
|
|
|
@@ -16,37 +16,172 @@ import org.jeasy.rules.mvel.MVELRule;
|
|
|
public class PointRuleTest {
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
- // 创建一个规则配置
|
|
|
+// // 创建一个规则配置
|
|
|
+// PointRuleConfigTest ruleConfig = new PointRuleConfigTest(1L,
|
|
|
+// "/menduner/test",
|
|
|
+// "(age>=18 && age<=30) || (name = 张三 || name =李四)",
|
|
|
+// "num <=1",
|
|
|
+// 100L);
|
|
|
+//
|
|
|
+// // 创建一个动态规则
|
|
|
+// DynamicRule dynamicRule = new DynamicRule(ruleConfig);
|
|
|
+//
|
|
|
+//
|
|
|
+// DynamicRuleAction<PointRuleConfigTest> action = (t) -> {
|
|
|
+// System.out.println("加分:" + t.getPoint());
|
|
|
+// };
|
|
|
+//
|
|
|
+// // 规则的上下文
|
|
|
+// Facts facts = new Facts();
|
|
|
+// facts.put("rule", dynamicRule);
|
|
|
+// facts.put("action", action);
|
|
|
+// facts.put("config", ruleConfig);
|
|
|
+//
|
|
|
+//
|
|
|
+// Rule lessRule = new MVELRule()
|
|
|
+// .name("aaaa")
|
|
|
+// .description("xxxx")
|
|
|
+// .when("rule.when()")
|
|
|
+// .then("action.invoke(config)");
|
|
|
+//
|
|
|
+//
|
|
|
+// Rules rules = new Rules();
|
|
|
+// rules.register(lessRule);
|
|
|
+// DefaultRulesEngine engine = new DefaultRulesEngine();
|
|
|
+// engine.fire(rules, facts);
|
|
|
+// System.out.println(facts);
|
|
|
+
|
|
|
+
|
|
|
+// Rules rules2 = new Rules();
|
|
|
+// rules2.register(new DynamicRule(ruleConfig));
|
|
|
+//// Facts facts2 = new Facts();
|
|
|
+// RulesEngine actualRules = new DefaultRulesEngine();
|
|
|
+// actualRules.fire(rules2, facts);
|
|
|
+
|
|
|
+ // 1. 新增规则
|
|
|
+ RuleBase baseRule = new RuleBase();
|
|
|
+ baseRule.setRuleId("1");
|
|
|
+ baseRule.setRuleName("rule1");
|
|
|
+ baseRule.setDescription("测试规则");
|
|
|
+ // 2. 设置规则条件
|
|
|
+ String ruleJson = generateRuleModel();
|
|
|
+ JSONObject conditionJson = JSON.parseObject(ruleJson);
|
|
|
+ // 3. 设置fact
|
|
|
+ String whenExpression = EasyRulesEngine.buildWhenExpression(conditionJson.getJSONObject("condition"));
|
|
|
+ baseRule.setWhenExpression(whenExpression);
|
|
|
+ // 4. 设置结果表达式
|
|
|
+ baseRule.setThenExpression(EasyRulesEngine.buildThenExpression());
|
|
|
+
|
|
|
+ // 5. 设置匹配条件
|
|
|
+ Facts facts = new Facts();
|
|
|
+
|
|
|
PointRuleConfigTest ruleConfig = new PointRuleConfigTest(1L,
|
|
|
"/menduner/test",
|
|
|
- "(age>=18 && age<=30) || (name = 张三 || name =李四)",
|
|
|
+ whenExpression,
|
|
|
"num <=1",
|
|
|
100L);
|
|
|
|
|
|
- // 创建一个动态规则
|
|
|
- DynamicRule dynamicRule = new DynamicRule(ruleConfig);
|
|
|
-
|
|
|
- Facts facts = new Facts();
|
|
|
facts.put("rule", new DynamicRule(ruleConfig));
|
|
|
+ facts.put("config", ruleConfig);
|
|
|
|
|
|
- Rule lessRule = new MVELRule()
|
|
|
- .name("aaaa")
|
|
|
- .description("xxxx")
|
|
|
- .when("rule.when()")
|
|
|
- .then("rule.action()");
|
|
|
+ DynamicRuleAction<PointRuleConfigTest> action = (t) -> {
|
|
|
+ System.out.println("加分:" + t.getPoint());
|
|
|
+ };
|
|
|
+ facts.put("action", action);
|
|
|
|
|
|
+ baseRule.setWhenExpression("rule.when()");
|
|
|
+ baseRule.setThenExpression("action.invoke(config)");
|
|
|
|
|
|
-// Rules rules = new Rules();
|
|
|
-// rules.register(lessRule);
|
|
|
-// DefaultRulesEngine engine = new DefaultRulesEngine();
|
|
|
-// engine.fire(rules, facts);
|
|
|
-// System.out.println(facts);
|
|
|
+
|
|
|
+ // 6. 调用规则匹配
|
|
|
+ EasyRulesEngine.RuleResult result = EasyRulesEngine.match(facts, baseRule);
|
|
|
+ System.out.println(result);
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
|
|
|
- Rules rules2 = new Rules();
|
|
|
- rules2.register(new DynamicRule(ruleConfig));
|
|
|
- Facts facts2 = new Facts();
|
|
|
- RulesEngine actualRules = new DefaultRulesEngine();
|
|
|
- actualRules.fire(rules2, facts2);
|
|
|
+ public static String generateRuleModel() {
|
|
|
+ return "{\n" +
|
|
|
+ " \"condition\": {\n" +
|
|
|
+ " \"type\": \"RELATION\",\n" +
|
|
|
+ " \"operator\": \"OR\",\n" +
|
|
|
+ " \"children\": [\n" +
|
|
|
+ " {\n" +
|
|
|
+ " \"type\": \"EXPRESSION\",\n" +
|
|
|
+ " \"operator\": \"LE\",\n" +
|
|
|
+ " \"key\": \"age\",\n" +
|
|
|
+ " \"value\": 10,\n" +
|
|
|
+ " \"children\": []\n" +
|
|
|
+ " },\n" +
|
|
|
+ " {\n" +
|
|
|
+ " \"type\": \"EXPRESSION\",\n" +
|
|
|
+ " \"operator\": \"EQ\",\n" +
|
|
|
+ " \"key\": \"name\",\n" +
|
|
|
+ " \"value\": \"老六\",\n" +
|
|
|
+ " \"children\": []\n" +
|
|
|
+ " },\n" +
|
|
|
+ " {\n" +
|
|
|
+ " \"type\": \"RELATION\",\n" +
|
|
|
+ " \"children\": [\n" +
|
|
|
+ " {\n" +
|
|
|
+ " \"type\": \"EXPRESSION\",\n" +
|
|
|
+ " \"key\": \"age\",\n" +
|
|
|
+ " \"operator\": \"GE\",\n" +
|
|
|
+ " \"value\": 35,\n" +
|
|
|
+ " \"children\": []\n" +
|
|
|
+ " },\n" +
|
|
|
+ " {\n" +
|
|
|
+ " \"type\": \"EXPRESSION\",\n" +
|
|
|
+ " \"key\": \"name\",\n" +
|
|
|
+ " \"operator\": \"EQ\",\n" +
|
|
|
+ " \"value\": \"李四\",\n" +
|
|
|
+ " \"children\": []\n" +
|
|
|
+ " },\n" +
|
|
|
+ " {\n" +
|
|
|
+ " \"type\": \"RELATION\",\n" +
|
|
|
+ " \"children\": [\n" +
|
|
|
+ " {\n" +
|
|
|
+ " \"type\": \"EXPRESSION\",\n" +
|
|
|
+ " \"key\": \"age\",\n" +
|
|
|
+ " \"operator\": \"EQ\",\n" +
|
|
|
+ " \"value\": 108,\n" +
|
|
|
+ " \"children\": []\n" +
|
|
|
+ " },\n" +
|
|
|
+ " {\n" +
|
|
|
+ " \"type\": \"EXPRESSION\",\n" +
|
|
|
+ " \"key\": \"name\",\n" +
|
|
|
+ " \"operator\": \"EQ\",\n" +
|
|
|
+ " \"value\": \"王五\",\n" +
|
|
|
+ " \"children \": []\n" +
|
|
|
+ " }\n" +
|
|
|
+ " ],\n" +
|
|
|
+ " \"operator\": \"OR\"\n" +
|
|
|
+ " }\n" +
|
|
|
+ " ],\n" +
|
|
|
+ " \"operator\": \"OR\"\n" +
|
|
|
+ " },\n" +
|
|
|
+ " {\n" +
|
|
|
+ " \"type\": \"RELATION\",\n" +
|
|
|
+ " \"children\": [\n" +
|
|
|
+ " {\n" +
|
|
|
+ " \"type\": \"EXPRESSION\",\n" +
|
|
|
+ " \"key\": \"age\",\n" +
|
|
|
+ " \"operator\": \"GT\",\n" +
|
|
|
+ " \"value\": 24,\n" +
|
|
|
+ " \"children\": []\n" +
|
|
|
+ " },\n" +
|
|
|
+ " {\n" +
|
|
|
+ " \"type\": \"EXPRESSION\",\n" +
|
|
|
+ " \"key\": \"name\",\n" +
|
|
|
+ " \"operator\": \"EQ\",\n" +
|
|
|
+ " \"value\": \"张三\",\n" +
|
|
|
+ " \"children\": []\n" +
|
|
|
+ " }\n" +
|
|
|
+ " ],\n" +
|
|
|
+ " \"operator\": \"AND\"\n" +
|
|
|
+ " }\n" +
|
|
|
+ " ]\n" +
|
|
|
+ " }\n" +
|
|
|
+ "}";
|
|
|
}
|
|
|
}
|