流口水总是真实的规则只触发一次

时间:2019-12-01 17:37:27

标签: java drools

我有这两个规则:

rule "challenge 1" salience 10
    when
        ClientTransaction(amount < 30)
    then
        challenge.setChallenge("challenge1");
end

rule "challenge 2" salience 0
    when
        eval( true )
    then
        challenge.setChallenge("challenge2");
end

和此Java代码:


public class ComputeChallengeDroolsRulesImplementation implements IComputeChallenge {
    private KieServices kieServices = KieServices.Factory.get();
    private KieSession kieSession = getKieSession();
    private static final String RULES_PATH = "rules/";

    public KieFileSystem getKieFileSystem() throws IOException {
        KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
        String[] rules = new File(getClass().getClassLoader().getResource(RULES_PATH).getFile()).list();
        for (String rule : rules) {
            kieFileSystem.write(ResourceFactory.newClassPathResource(RULES_PATH + rule));
        }
        return kieFileSystem;
    }

    public KieContainer getKieContainer() throws IOException {
        getKieRepository();

        KieBuilder kb = kieServices.newKieBuilder(getKieFileSystem());
        kb.buildAll();

        KieModule kieModule = kb.getKieModule();
        KieContainer kContainer = kieServices.newKieContainer(kieModule.getReleaseId());

        return kContainer;
    }

    public void getKieRepository() {
        final KieRepository kieRepository = kieServices.getRepository();
        kieRepository.addKieModule(
                new KieModule() {
                    public ReleaseId getReleaseId() {
                        return kieRepository.getDefaultReleaseId();
                    }
                });
    }

    @Bean
    public KieSession getKieSession() {
        try {
            return getKieContainer().newKieSession();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Challenge2 computeChallenge(ClientTransaction clientTransaction) {
        kieSession.insert(clientTransaction);
        Challenge2 challenge = new Challenge2();
        kieSession.setGlobal("challenge", challenge);
        kieSession.fireAllRules();
        return challenge;
    }

}

这是ClientTransation类:


public class ClientTransaction {
  private String id;
  private String merchant;
  private double amount;

  public ClientTransaction() {
  }

  public ClientTransaction(String id, String merchant, double amount) {
    this.id = id;
    this.merchant = merchant;
    this.amount = amount;
  }

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public String getMerchant() {
    return merchant;
  }

  public void setMerchant(String merchant) {
    this.merchant = merchant;
  }

  public double getAmount() {
    return amount;
  }

  public void setAmount(double amount) {
    this.amount = amount;
  }
}

这是Challenge2类:


public class Challenge2 {
  private String challenge;

  public Challenge2() {
  }

  public Challenge2(String challenge) {
    this.challenge = challenge;
  }

  public String getChallenge() {
    return challenge;
  }

  public void setChallenge(String challenge) {
    this.challenge = challenge;
  }
}


当我多次调用<30的ComputeChallengeDroolsRulesImplementation.computeChallenge时,它确实会按预期多次返回具有“ challenge1”值的挑战对象。

但是当我多次调用ComputeChallengeDroolsRulesImplementation.computeChallenge时,其次数大于等于30,则它第一次确实返回“ challenge2”值,但是随后每次它返回具有空值的Challenge2对象?

我做错什么了吗?

谢谢

2 个答案:

答案 0 :(得分:1)

我假设您仅在发生 ClientTransaction 事件时才触发挑战2 规则。您可以通过仅在drool规则的when部分调用默认构造函数来基于事件的发生在规则上添加无条件检查。尝试如下更改您的挑战2 规则:

rule "challenge 2" salience 0
    when
        ClientTransaction()
    then
        challenge.setChallenge("challenge2");
end

此外,不建议在流口水规则中过多使用eval。有关最佳做法的更多信息,请检查here

答案 1 :(得分:0)

如以下文档所述: https://docs.jboss.org/drools/release/5.2.0.Final/drools-expert-docs/html/ch05.html#d0e3313

  

无法通知引擎有关全局变量值的变化,并且不跟踪其变化。约束中错误使用全局变量   可能会产生令人惊讶的结果-以一种不好的方式令人惊讶。

     

[…]

     

全局变量不是为了在规则之间共享数据而设计的,它们应该   绝不用于该目的。规则始终会推理并做出反应   工作内存状态,因此,如果您要在规则之间传递数据,   将数据作为事实断言到工作内存中。

     

强烈建议您从规则内部设置或更改全局值。我们建议您始终从   使用工作内存界面的应用程序。