我有很多规则要根据数据库中的数据进行评估,我打算使用easyrules api。 我不喜欢在我的代码中编程或硬编码规则。 一旦构建代码,应该很容易更改规则标准。 请帮助我如何使其动态和可读。 Easyrules api坚持为每个规则设置一个单独的类,如下所示:我希望根据易于修改的规则输入动态构建以下代码: 我在想下面但不确定哪一个是最好的: 1.数据库表 - 包含规则条件的规则表。 (我可以使用更新查询来更改规则) 2. JSON或XML。
我对其他工具也非常灵活,并不局限于easyrules。 https://github.com/j-easy/easy-rules/wiki/hello-world
/**
* Hello World rule class.
*
* @author Mahmoud Ben Hassine (mahmoud@benhassine.fr)
*/
@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;
}
答案 0 :(得分:2)
我可能会建议你使用java + groovySrcipt引擎。 您可以在GroovyScripts中提供规则,并可以使用GroovyEngine动态执行它们。这只是一个想法。
答案 1 :(得分:0)
我知道我的答案很晚,但可能会让其他人感兴趣。
我以不同的方式使用easyrules引擎。
我只使用一种规则,我可以用easyRule做很复杂的事情。
我的规则是硬编码的,我想可能可以用存储在数据库中的规则做类似的事情。
在我的项目中,我需要根据一系列复杂的条件逐步进行。并且每个步骤的条件变化集。 我的规则引擎将根据条件从当前情况构建新情况。
我的独特规则是,juste拥有一个测试对象和一个下一个对象。
我有一个currentSituation对象,它保存当前情况的所有参数。 它有
然后我有一个ruleEngine工厂根据当前步骤提供一套规则。
每个RuleEngine都很容易使用几乎自然的语言。
前
Situation currentSituation = getSituation();
int priority=0;
Rule rule1 = new Rule(
"MONDAY_RULE",
priority++,
currentSituation.test().isMonday().isRaining(),
currentSituation.next().docallMyBoss("won't work today").doGoBackToBed());
....
Rule rule7 = newRule(
"FRIDAY_RULE",
priority++,
currentSituation.test().isFriday().isSunny(),
currentSituation.next().doBuy("bathing Suit").doPlanWeekendBreak());
register(rule1);
register(rule2);
当“是”方法和“do”方法返回currentSituation对象时,我可以轻松地链接测试条件和构建选项。
在规则的evaluate()方法中,我需要测试testObject的RuleEvalue标志以查看它是否为真,如果为true,我提供使用next()方法构建的对象
效果很好,易于维护和阅读。我只需要创建新的“是”和“做”方法来获得更多权力。
我非常感谢Mahmoud Ben Hassine提供这样一个简单易用的规则引擎!
答案 2 :(得分:0)
这是一个古老的问题,但是到2017年底,“简单规则”可以通过外部文件包含动态规则。
来自他们的website:
name: "alcohol rule"
description: "children are not allowed to buy alcohol"
priority: 2 condition: "person.isAdult() == false"
actions: - "System.out.println(\"Shop: Sorry, you are not allowed to buy alcohol\");"
答案 3 :(得分:-1)
我认为你可以参考这个页面,Easy Rules Example
是@override的代码
public class DefenseFourthDownRule extends BasicFootballRule {
// return `true` if the rule should be applied, `false` else
@Override public boolean evaluate() {
if (gameState == null) return false;
if (gameState.down != 4) return false;
// we know it's 4th down ...
return true;
}
@Override public void execute() {
System.out.println("4th Down rule fired");
}
}