deliveredtechnologies / rulebook

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

Removing rules at runtime #123

Closed mxw589 closed 5 years ago

mxw589 commented 6 years ago

Hi, do you think it would be possible to add some system by which rules could be removed from a rulebook at runtime? I realise this would mean that you would need some way of uniquely identifying rules within a rulebook, so perhaps addRule could also take in a map of String -> Rule similarly to factmap.

The usecase is that I have a webservice wrapper around a rule book, to which I would like to be able to add and remove rules at runtime.

Unless I am missing something, I think my only option is to re-initialise the rulebook, and add all of the existing rules less the one to be removed back in?

Any advice appreciated

Clayton7510 commented 6 years ago

If I'm reading you correctly, you basically want be able to choose whether to invoke or ignore a rule at runtime? Adding/removing at runtime would have some consequences since multiple threads can share a RuleBook and there are some optimizations under planning that would explicitly parallelize the processing of a RuleBook. But coercing a RuleBook into ignoring some rules at runtime and running others is 100% do-able today.

What you could do is build RuleBook that contains a complete set of all the rules you want to run, along with those that you only want to conditionally run. On the rules that you might want to "remove" at runtime, set the rule's condition (i.e. "when" section) to include a particular fact as part of a logical AND - let's call this fact "InvokeFact." Then, when you feed in facts to a RuleBook, if InvokeFact is true, all of your rules fire as they normally would. But when InvokeFact is false, those rules that should be conditionally "removed" are omitted from execution like they never existed. If you had multiple scenarios where you wanted to turn off or on rules, you could have multiple "InvokeFacts" (e.g. InvokeFact1, InvokeFact2, InvokeFact3, etc).

Does that achieve what you were looking for?

Clayton7510 commented 5 years ago

Closed due to inactivity