deliveredtechnologies / rulebook

100% Java, Lambda Enabled, Lightweight Rules Engine with a Simple and Intuitive DSL
http://www.deliveredtechnologies.com
Apache License 2.0
716 stars 124 forks source link

Can't add new facts in POJO rule #172

Open holken1 opened 4 years ago

holken1 commented 4 years ago

Hi. When using POJO rules, how can I add facts in one rule that can be used by a subsequent rule in the same ruleset?

I saw in the FAQ that "If a fact is changed or added in a Rule then that change or addition is present for other Rules down the rule chain." But if I specify a @Given for something that is not yet defined, it throws an error*. And it's not clear how I could create a new NameValueReferable that is then picked up and stored in the fact map.

*Error on undefined @Given:

[main] ERROR com.deliveredtechnologies.rulebook.model.runner.RuleAdapter - Unable to access test.rules.myrules.CatRule when converting then to Consumer java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.deliveredtechnologies.rulebook.model.runner.RuleAdapter.lambda$getThenMethodAsConsumer$14(RuleAdapter.java:292) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.deliveredtechnologies.rulebook.model.GoldenRule.lambda$invoke$8(GoldenRule.java:174) at java.util.Optional.ifPresent(Optional.java:159) at com.deliveredtechnologies.rulebook.model.GoldenRule.invoke(GoldenRule.java:171) at com.deliveredtechnologies.rulebook.model.runner.RuleAdapter.invoke(RuleAdapter.java:180) at com.deliveredtechnologies.rulebook.model.AuditableRule.invoke(AuditableRule.java:94) at com.deliveredtechnologies.rulebook.model.rulechain.cor.RuleHandler.handleRequest(RuleHandler.java:30) at com.deliveredtechnologies.rulebook.model.rulechain.cor.CoRRuleBook.lambda$run$2(CoRRuleBook.java:46) at java.util.Optional.ifPresent(Optional.java:159) at com.deliveredtechnologies.rulebook.model.rulechain.cor.CoRRuleBook.run(CoRRuleBook.java:44) at com.deliveredtechnologies.rulebook.model.runner.AbstractRuleBookRunner.run(AbstractRuleBookRunner.java:69) at test.rules.Test.main(Test.java:19) Caused by: java.lang.NullPointerException at test.rules.myrules.CatRule.then(CatRule.java:22) ... 20 more

holken1 commented 4 years ago

Could this be a bug? I noticed that declaring a fact works but as soon as I try to get or set it, it fails. I'm attaching a very simple example that shows this behavior, and fails even though I think it should run. non-working-example.tar.gz

Clayton7510 commented 4 years ago

I think you are correct. I don’t think a given for a fact not defined is propagated down the rule chain - seems like an oversight.

RicardoRdzG commented 3 years ago

even when it should be fixed you can add facts for other rules down the evaluation chain by having the facts map as a given

@Given NameValueReferableMap facts;

It should be shown in one of the examples since it is a useful feature and not everybody is using lambda style