How to reset result
jlerbsc opened this issue · 5 comments
Could you tell us how to reset result between every rule execution. We don't want to accumulate results for each rule execution. Below an exemple of the rulebook creation we use.
RuleBook<RuleBookExecutionContext> ruleBook = (RuleBook) RuleBookBuilder.create()
.addRule(new VerboseRuleAdapter(RuleBuilder.create().withFactType(StringLiteralExpr.class)
.withResultType(RuleBookExecutionContext.class)
.when(StringLiteralsShouldNotBeDuplicatedConfig
.getWhen(StringLiteralsShouldNotBeDuplicatedConfig.defaultMinCharacters))
.then(StringLiteralsShouldNotBeDuplicatedConfig
.getThen(StringLiteralsShouldNotBeDuplicatedConfig.defaultMinRepetitions))
.build(), StringLiteralsShouldNotBeDuplicatedConfig.getViolations()))
.build();
;
ruleBook.setDefaultResult(new RuleBookExecutionContext());
return ruleBook;
Thanks
No response so i setup in this exemple a new RuleBookExecutionContext before each rule call to the invoke method. Thanks to the adapter design pattern.
Sorry about the delayed/lack of response. You can specify when results are updated and a result can hold any kind of object. You can also add/update facts as you choose in the rule chain.
Thanks for your response but could you elaborate how to specify when results are updated ?
Results are optional in the 'then.' You can also can have conditional logic logic that determines how and if they are set.
For example:
RuleBook ruleBook = RuleBookBuilder.create()
.addRule(rule -> rule.withNoSpecifiedFactType().then(f -> System.out.print("Hello ")))
.addRule(rule -> rule.withNoSpecifiedFactType().then(f -> System.out.println("World")))
.build();
The above rule has no result.
RuleBuilder.create().withFactType(ApplicantBean.class).withResultType(Double.class)
.when(facts -> facts.getOne().getCreditScore() < 600)
.then((facts, result) -> result.setValue(result.getValue() * 4)) //the result won't be updated if the 'when' condition is not satisfied
.stop()
.build()
The rule directly above only updates the result if the 'when' condition is satisfied.
Thanks for the explanation.