Closed mill5james closed 6 years ago
For RuleBookRunner, you are correct. There is always some result. This allows the result to be injected and chained in POJO rules. However, if you were to create a RuleBook using the Java DSL or, say, CoRRuleBook then it would not always have a result. The implementation of a RuleBook and the RuleBook interface are separate, so getResult returns an Optional
@Clayton7510 I think I understand what you are saying. It just seems that if you have introduced an Optional<Result>
then it should be optional. Why does the RuleBookRunner require a result while other methods allow the optional behavior?
I have submitted a Pull Request that implements the behavior that if the result is not specified, the Optional<Result>.isPresent()
returns false
when no rule as set a result, and returns true
when a result is returned.
Also, there is a setDefaultResult(…)
method on RuleBookRunner so that if you wanted to always return well-know value and avoid the Optional<Result>.isPresent()
test, that is still possible.
I don't remember exactly what the reason was for RuleBookRunner doing that. I think it may have had something to do with some rules having a default result and others not having a default result and it not maintaining the reference from one to the next... honestly, I don't recall.
But I agree that it would be nice to not enforce a default result if none is specified. Thanks for the PR. I'll check it out shortly.
Merged.
I would expect that
public Optional<Result> getResult()
on theRuleBookRunner
class would not return a value if any result was not set during the run of the rules.Given a very simple rule
But if I run the rules expecting no result if my rule does not match
I get an exception:
I believe that this is a bug in the AbstractRuleBookRunner class
https://github.com/rulebook-rules/rulebook/blob/400495a53a6129c15481871400a1ea0e6c49395c/rulebook-core/src/main/java/com/deliveredtechnologies/rulebook/model/runner/AbstractRuleBookRunner.java#L56
I don't think you should create a
new Object()
if the_result.getValue() == null
. This has the effect of setting the default result value to returntrue
for a call toOptional<T>.isPresent()
.Attached is a sample project simple-result-pojo.zip