-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RuleBook first run unexpected result #55
Comments
You are correct. It looks like that is a bug that was probably introduced in v0.5. I just put the fix in 0.5.1-SNAPSHOT. I used the same basic approach as you. The only difference was I checked for null in the first statement.Why? Because another contributor pointed out that it's not considered a good use of Optional just to check isPresent() on an existing variable - not really a big difference tho, IMO. Thanks for that! I'll update the tests to catch that condition and push it out as a release in the next couple of days. But for now, it's in the current SNAPSHOT release and in the develop branch. |
BTW, I can see that you create a RuleBook with the a result type of MyBean here. public class AutoEvaluationRuleBook extends CoRRuleBook<MyBean> {
@Override
public void defineRules() {
// here I define all my business rules
}
} But then you try to instantiate that RuleBook using a different result type of AutoEvaluationResult here. RuleBook<AutoEvaluationResult> autoEvaluationRuleBook = RuleBookBuilder.create(AutoEvaluationRuleBook.class)
.withResultType(AutoEvaluationResult.class)
.withDefaultResult(new AutoEvaluationResult(null, false, false, null))
.build(); I'm not 100% sure why. But here's an example of what I think you are trying to do. And once again, thanks for finding that bug! /**
* Custom RuleBook with a MyBean result type.
*/
public class AutoEvaluationRuleBook extends CoRRuleBook<MyBean> {
@Override
public void defineRules() {
addRule(RuleBuilder.create().withFactType(String.class)
.withResultType(MyBean.class) //should match the result type of the RuleBook, which is MyBean
.when(facts -> facts.containsKey("value1"))
.using("value1")
.then((facts, result) -> result.getValue().setStrValue1(facts.getOne()))
.build()
);
addRule(RuleBuilder.create().withFactType(String.class)
.withResultType(MyBean.class) //should match the result type of the RuleBook, which is MyBean
.when(facts -> facts.containsKey("value2"))
.using("value2")
.then((facts, result) -> result.getValue().setStrValue2(facts.getOne()))
.build()
);
}
} package com.deliveredtechnologies.rulebook.example;
public class MyBean {
private String strValue1;
private String strValue2;
public String getStrValue1() {
return strValue1;
}
public void setStrValue1(String strValue1) {
this.strValue1 = strValue1;
}
public String getStrValue2() {
return strValue2;
}
public void setStrValue2(String strValue2) {
this.strValue2 = strValue2;
}
} public class Application {
public static void main(String args[]) {
AutoEvaluationRuleBook ruleBook = new AutoEvaluationRuleBook();
ruleBook.setDefaultResult(new MyBean());
NameValueReferableMap factMap = new FactMap();
factMap.setValue("value1", "First Value");
factMap.setValue("value2", "Second Value");
ruleBook.run(factMap);
ruleBook.getResult().ifPresent(result ->
{
MyBean bean = result.getValue();
System.out.println(bean.getStrValue1());
System.out.println(bean.getStrValue2());
});
}
} |
Hi Clayton, sorry my sample is wrong, because I deleted the real bean name and put at last a generic MyBean. My real class is declared as follows:
I preferred using the bean approach rather than the map approach. Thanks and best regards, |
I updated the tests and just pushed 0.5.1 to Maven with this bug fix. |
Hi,
I'm trying to integrate RuleBook in an enterprise project, because it seems to be really interesting.
I'm using version 0.5 and found that first run returns a default value because headRule is not correctly valued in CoRRuleBook.run method. These are the steps I did:
In this way, the first run returns an unexpected result because I noticed in the run method inside the CoRRuleBook class the object headRule is not valued.
The original code:
In the original code if the headRule is not present, it calls the defineRules, defining the book rules, but after that the method isPresent is called on the Optional<Handler> headRule object which will return false. Is this wanted ?
I modified this code in this way:
In this way I have all the objects valued correctly (also the first one).
Let me know.
Thanks and best regards,
Alessandro Torrisi.
The text was updated successfully, but these errors were encountered: