简体   繁体   English

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

[英]drools, rules directly in java

Is there any way to use drools by implementing rules condition directly and fully in java, like it's possible in 有没有办法通过直接在Java中完全实现规则条件来使用流口水,就像在

https://github.com/j-easy/easy-rules (look in section "declarative way" and section "programmatic way") https://github.com/j-easy/easy-rules (请参见“声明式”部分和“编程方式”部分)

something like that: 像这样的东西:

@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!");
}
}

or maybe 或者可能

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();

Recently a new feature was added that enables users to represent rules in a Java model [1]. 最近,增加了一项新功能,使用户可以用Java模型表示规则[1]。 You could use this feature to build rules in plain Java. 您可以使用此功能在纯Java中构建规则。 There is a test class that you could check to see some examples. 您可以检查一个测试类以查看一些示例。 See here [2]. 参见此处[2]。 You have the option to choose from PatternDSL or FlowDSL (what suits you better). 您可以选择PatternDSL或FlowDSL(更适合您)。

To get a KieBase with the Java rules, you could do this (as stated in the linked doc.): 要获得具有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 [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 [2] https://github.com/kiegroup/drools/blob/3826ee0c95fe139041880f52f3e00309b7907871/drools-model/drools-canonical-model/src/test/java/org/drools/model/FlowDSLTest.java#L21

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM