Comments (4)
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:
public class AutoEvaluationRuleBook extends CoRRuleBook<AutoEvaluationResult> {
private static final String DATE_PATTERN = "(\\d{4})-(\\d{2})-(\\d{2})";
private static final int YEAR_GROUP = 1;
@Override
public void defineRules() {
addRule(RuleBuilder.create().withFactType(CalculatedMatching.class).withResultType(AutoEvaluationResult.class)
.when(personTypeMismatch()).then(unevaluated()).stop().build());
// and so on...
}
}
I preferred using the bean approach rather than the map approach.
Thanks and best regards,
Alessandro.
from rulebook.
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.
from rulebook.
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());
});
}
}
from rulebook.
I updated the tests and just pushed 0.5.1 to Maven with this bug fix.
from rulebook.
Related Issues (20)
- Why withDefaultResult is mandatory? HOT 4
- How can i write dynamic rules to execute rather than writing in java program .like rules defined in JSON or Yaml? HOT 4
- Q: Rule as a singleton bean HOT 3
- Add the Supplier function to avoid the passage by reference for the default result HOT 2
- Should I fork? HOT 2
- using codegen to improving performance of rule execution HOT 1
- Rule name is dropping when build Rule withFactType. HOT 1
- How to write Unit Tests for a RuleBean? HOT 1
- Switch CI to GitHub Actions HOT 2
- Deprecated classes/methods and non-compiling examples in README.md HOT 1
- Auditor not set on rules when RuleBookBuilder.create(Class) is used HOT 2
- Futures/CompletionStage/Async HOT 2
- Fact checking before evaluating rules HOT 2
- Rulebook in Spring Boot - stop execution
- Project status HOT 2
- Lambdas stack error
- RuleBuilder doesn't create a Rule with action type RuleChainActionType.ERROR_ON_FAILURE HOT 2
- RuleBookRunner4PojoClasses does not run rules according to the order property
- Would need an update on how frequently this project is developed and actively supported HOT 1
- Looking for Updates on Rule Book Project
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rulebook.