rulebook icon indicating copy to clipboard operation
rulebook copied to clipboard

Is it possible to have an "else" for a rule?

Open LvelezH opened this issue 7 years ago • 10 comments

Hi, is there some way to have like an "else" for a rule? I have to run a set of rules, but I need to know which one "passed" and which ones not, so I was thinking about having a Result consisting on a list of "states" for each rule ran. The problem is, I have no access to the "result" in the "when" rule part, and the "then" is never ran if the rule result is false. Is there any way to achieve this with Rulebook? Thanks in advance!.

LvelezH avatar Jul 19 '17 14:07 LvelezH

Your concern is actually addressed in Issue #38.

But to answer your question... No, 'else' is not available for a rule. The decision not to allow for rules firing on 'else' was intentional. The idea being that each rule is an independent action that executes given some specific state. There was also a concern that building out if/then/else logic could lead to nesting rules and basically creating the exact kind of conditional branching chain complexity that rules are intended to address.

Still, there are some approaches you 'could' use.

  1. Change your way of thinking [slightly]. Make every rule simply an action that executes based on some required or expected state. I think you'll find thinking this way about rules greatly simplifies the organization and maintenance of rules.

  2. Use a Fact to store stuff that doesn't make it to the 'then' logic. It's not elegant, but you can do this, even in the 'when' statement.

  3. Have a rule that fires on negative logic. Again, this is not elegant. But it is one way to achieve what you are trying to do.

Auditing is coming that should address your need - allow you to determine which rules fired and which ones didn't. But there are some other things on deck before it - I've got to put a bow on thread safety for POJO rules and my time commitments at work have increased a bit, which takes some time away from RuleBook.

Clayton7510 avatar Jul 19 '17 17:07 Clayton7510

Hi, and thanks for the fast response :) I had already started implementing it storing stuff in the "facts", and it's working correctly. Auditing would be great, if it comes I will probably change the way I run my rules.

Thanks again, and kudos for the great lib :)

LvelezH avatar Jul 20 '17 09:07 LvelezH

@LvelezH auditing is currently available in 0.9-SNAPSHOT. Auditing will allow you to determine which rules were run, and which rules were not run. Here's a link that explains how you can use the snapshot repository in your project: https://github.com/rulebook-rules/rulebook/wiki/Download-and-Install.

Clayton7510 avatar Sep 16 '17 03:09 Clayton7510

The plan is to have 0.9 released to Maven central within the next week or two. For an explanation on how to enable and use auditing, check out the README in this branch: https://github.com/rulebook-rules/rulebook/tree/Issue38_RulesAudit

Clayton7510 avatar Sep 16 '17 03:09 Clayton7510

The current snapshot 0.9-SNAPSHOT is released to the sonatype snapshot repo. Assuming no issues, this snapshot release will become the 0.9 release in Maven Central shortly. It will contain rules auditing.

Clayton7510 avatar Sep 24 '17 14:09 Clayton7510

After further consideration, I think adding "orElse" to the DSL might be a good idea. The "orElse" could create a rule action based on the negative condition. This would still make each rule an independent assertion.

As far as POJO Rules are concerned, I don't think an orElse is either needed, nor would it be a good idea - it would just make things more complicated and messy, IMO.

Clayton7510 avatar Sep 30 '17 00:09 Clayton7510

@Clayton7510 Any example for OrElse in DSL? I am planning to create child of GoldenRule Class and have custom ELSE method which will be called when the condition returns true. I am not sure if that will be better approach. As you are pretty familiar with the code, can you suggest? Thanks

ruchichhabra009 avatar Apr 17 '18 18:04 ruchichhabra009

I think first, have it work in the Rules - add it to the interface and put the functionality in GoldenRule so that when the "when" condition was not true, an OrElse action would fire. Then, the DSL could be modified to streamline building the OrElse action. Finally, an @OrElse annotation should be added for POJO rules.

I also think that OrElse should only support a single action, whereas Then supports multiple actions that are chained together.

Clayton7510 avatar May 04 '18 17:05 Clayton7510

Else is coming to Rules. v0.11 is targeted for Else rules at the moment.

Clayton7510 avatar May 09 '18 03:05 Clayton7510

Actually, it's looking more like v0.12

Clayton7510 avatar Nov 30 '18 22:11 Clayton7510