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
Problem with executing all the rules in the easy-rules-2.0.0 #13
Comments
Hi, Rules must have a unique name with in a rules engine registry. Try to give them different names and it should work fine. Regards |
A Rule has failed when the condition method returns false right ? Consider this case: @Rule(description="ruleA",name="ruleA")
public class RuleA {
@Condition
public boolean when(){
System.out.println("A is called");
return false;
}
@Action
public void then(){
System.out.println("A is executed");
}
} @Rule(description="ruleB",name="ruleB")
public class RuleB {
@Condition
public boolean when(){
System.out.println("B is called");
return true;
}
@Action
public void then(){
System.out.println("B is executed");
}
} The Main method public static void main(String[] args) {
RuleA a = new RuleA();
RuleB b = new RuleB();
RulesEngine rulesEngine = RulesEngineBuilder.aNewRulesEngine().withSkipOnFirstFailedRule(true).build();
rulesEngine.registerRule(a);
rulesEngine.registerRule(b);
rulesEngine.fireRules();
} The console output May 21, 2015 12:27:34 PM org.easyrules.core.DefaultRulesEngine logEngineParameters Question RuleA returned false. So we should be skipping the remaining rules right ? But RuleB seems to have been executed. |
No, a rule fails when it executes with error (throw an exception) Try to throw an exception in RuleA action method (to make it fail) and RuleB will not be executed. |
Thank you very much and thanks for updating the framework in a short period of time. :) |
You are welcome! Do not hesitate to ask me if you need further support. |
Hi @benas , But if the condition is false, then the action is never executed even if we have an Exception thrown, right? |
Hi @salvitas Yes, since (the code of) the action is not executed, it can not throw an exception. Regards |
So...I am trying to use this library to build a simple engine but i am not sure what the proper approach would be, cause i have set up the parameter skipOnFirstFailedRule = true, but because the condition is false, the action never gets executed and therefore the engine does not stop , because there is no failure! |
I think there is a little confusion about what a failure means here, just like the previous comment. A rule is said to be triggered when its condition evaluates to true. When a rule is not triggered (its condition evaluates to false), the engine will simply move to the next registered rule. This is called forward chaining in production systems and Easy Rules is of this type.
No, this is not how rule based systems are intended to work.
Yes I see. It's a bit tricky, but in production systems, there is no "ELSE" part. All rules have only two parts: "IF" (LHS) and "THEN" (RHS). Hope this helps |
Except for this approach dumps exceptions all over the place even though the behaviour is normal. |
Don't use exceptions for flow control. If you can't design your requirement as a set of ordered rules (IF/THEN (with no else) statements), don't use a production system. |
Thanks for your reply but it puzzles me a little. Maybe as you mention I am looking for a different type of rules engine... but the requirement is simple. If Rule1 fails dont move on to Rule2. From my understanding the only way to accomplish this is throwing an exception in the @action. I am not looking for an ELSE. I simply want the rule chain to break as soon as a @condition is not met. |
Sorry, no offense in my last comment. But I see the confusion here:
What does "fails" means to you? In Easy Rules, a rule is said to be failed when it is triggered and (one of) its action(s) throws an exception (I tried to summarize definitions in my previous comment) . So in Easy Rules, implementing "If Rule1 fails dont move on to Rule2" is done by setting the
That's fine, in this case, probably Easy Rules needs to add a parameter like |
For me a Rule failing means that the Condition was not met. For you it means that an exception was thrown in the @action portion. Ultimately we just need a simple way to break out of the Rule chain on the first occurence of a non met condition. "skipOnFirstNonTriggeredRule" is a possibility. |
I see, but this is a common confusion indeed (same here with drools). When the condition of the rule is met, the rule is triggered (or fired) but it can execute successfully or with a failure. Anyway, I see your point. Please open an issue with your feature request and an example. I'll plan it for next release. Many thanks upfront. Kind regards |
Hi Mahmoud, I am also keen to know when the skipOnFirstNonTriggeredRule will be released. Can you please let me know as I also have a need for this feature? Thanks |
Hello @benas / @vinoct6 / @salvitas / @ssozonoff / @shefworx , |
I tried out the new version 2.0.0. All the registered rules does not seem to be triggered.
I'm pasting the code I have written, and I'm expecting all the rules to be executed, but it's not working this way. Let me know if I'm missing something.
The Main method for calling these rules:
Output printed in the console-------------------
May 21, 2015 10:18:26 AM org.easyrules.core.DefaultRulesEngine logEngineParameters
INFO: Rule priority threshold: 2,147,483,647
May 21, 2015 10:18:26 AM org.easyrules.core.DefaultRulesEngine logEngineParameters
INFO: Skip on first applied rule: false
May 21, 2015 10:18:26 AM org.easyrules.core.DefaultRulesEngine logEngineParameters
INFO: Skip on first failed rule: false
May 21, 2015 10:18:26 AM org.easyrules.core.DefaultRulesEngine applyRules
INFO: Rule 'rule' triggered.
A is called
A is executed
May 21, 2015 10:18:26 AM org.easyrules.core.DefaultRulesEngine applyRules
INFO: Rule 'rule' performed successfully.
Question:
Why aren't RuleB and RuleC executed in this case ?
The text was updated successfully, but these errors were encountered: