流口水,直接在Java中的规则

时间:2019-01-04 02:07:12

标签: java drools rule-engine

是否可以通过直接在Java中完全实现规则条件来使用流口水,就像在

中一样

https://github.com/j-easy/easy-rules(请参见“声明方式”和“编程方式”)

类似的东西:

@Rule(name = "weather rule", description = "if it rains then take an umbrella" )
public class WeatherRule {

@Condition
public boolean itRains(@Fact("rain") boolean rain) {
    return rain;
}

@Action
public void takeAnUmbrella() {
    System.out.println("It rains, take an umbrella!");
}
}

或者也许

Rule weatherRule = new RuleBuilder()
    .name("weather rule")
    .description("if it rains then take an umbrella")
    .when(facts -> facts.get("rain").equals(true))
    .then(facts -> System.out.println("It rains, take an umbrella!"))
    .build();

1 个答案:

答案 0 :(得分:1)

最近添加了一个新功能,该功能使用户可以用Java模型表示规则[1]。您可以使用此功能在纯Java中构建规则。您可以检查一个测试类以查看一些示例。参见此处[2]。您可以选择PatternDSL或FlowDSL(更适合您)。

要获取具有Java规则的KieBase,您可以执行此操作(如链接文档中所述):

Model model = new ModelImpl().addRule( rule );
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel( model );

[1] https://docs.jboss.org/drools/release/7.15.0.Final/drools-docs/html_single/index.html#executable-model-con_kie-apis
[2] https://github.com/kiegroup/drools/blob/3826ee0c95fe139041880f52f3e00309b7907871/drools-model/drools-canonical-model/src/test/java/org/drools/model/FlowDSLTest.java#L21