Comments (13)
That would be great. I don't think it would be unreasonable to simply have a setting to determine whether a rule chain should break on failure of a rule, that would suit my purposes entirely (well, certainly for the foreseeable future). What I'm trying to avoid is having to have a negative rule for each positive rule with the negative rule simply breaking the chain...it seems a little excessive :-).
from rulebook.
Cool, thanks, I'll take at this hopefully sometime this week, many thanks for that
from rulebook.
My apologies for the delayed response. I was on vacation for a bit.
I think what you are looking for was answered in this issue: #74
from rulebook.
Hi
Unfortunately I'm not using the POJO rules and the @then annotation. I need to dynamically create the various rules so I'm using the RuleBuilder.create(..) method, which doesn't look like I can return a RuleState from the then method. Is there another way of doing it ?
Thanks
D
from rulebook.
You can still do it! Pojo rules just use an adapter under the hood to convert to non-pojo rules. ;)
I'll post an example of how it looks with non-pojo rules sometime tomorrow - it would be today, but I am otherwise committed.
from rulebook.
Here is an example
RuleBook ruleBook = RuleBookBuilder.create()
.addRule(rule -> rule.withNoSpecifiedFactType()
.when(f->true)
.then(f -> System.out.println("breaking the rule chain here"))
.stop())
.addRule(rule -> rule.withNoSpecifiedFactType()
.when(f->true)
.then(f -> System.out.println("next rule"))
.stop()).build();
The first rule will execute and then it will break the rule chain after the rule is invoked. This shows how to break the rule chain when the rule evaluates to true. In order for a rule to perform any action, including breaking the rule chain, its 'when' condition must be true and its 'then' condition must be invoked. Of course, you could always have a rule that breaks the rule chain based on the inverse being evaluated in a 'when' statement and then having the stop() method set on that rule. A trivial example would be as follows.
RuleBook ruleBook = RuleBookBuilder.create()
.addRule(rule -> rule.withNoSpecifiedFactType()
.when(f-> !isTrue)
.then(f -> {})
.stop()) //stops the rule chain if isTrue == false
.addRule(rule -> rule.withNoSpecifiedFactType()
.when(f->true)
.then(f -> System.out.println("next rule"))
.stop()).build();
from rulebook.
closing due to inactivity
from rulebook.
Sorry for the late reply. Many thanks for the example, although its not quite what I was hoping for as the result of the when needs to evaluate to true to execute the then, which is where the rule is used to break the rule chain. However, I'd like to be able to break the rule chain if the when fails and only perform the then if the when is successful. Kind of like the the .orElse terminology of java 8. Would this be possible ?
from rulebook.
A related question was asked in Issue #79. In the interest of DRY, I will paraphrase the response :)
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.
With that being said, adding in a feature that would allow for breaking the rule chain on a 'false' rule... that could happen. But it would be specific to the CoR implementation of RuleBook (currently, the only implementation). Future RuleBook implementations (future sate, there will be more than one packaged implementation), however, may have different execution sequences, where that would not be possible.
from rulebook.
This update is on deck for the v0.8 release. v0.7 is planned for release to Maven Central on August 7. v0.8 is due to be released by the end of August and the SNAPSHOT release will be pushed on August 7. The v0.8 code is currently in the Issue67_EnhanceDSL branch which includes DSL related feature updates for v0.8.
from rulebook.
Here's what is planned:
Upon release, rules can break the rule chain for compatible RuleBooks as follows.
RuleBook ruleBook = RuleBookBuilder.create()
.addRule(
RuleBuilder.create(GoldenRule.class, true) //second param indicates that on failure this rule can stop the rule chain
.withFactType(String.class)
.when(facts -> false)
.then(someConsumer)
.stop() //signals that this rule stops the rule chain
.build())
.addRule(
RuleBuilder.create() //this rule will never be invoked since the above rule stopped the rule chain when its condition was false
.withFactType(String.class)
.when(facts -> true)
.then(someOtherConsumer)
.build())
.build();
Since this behavior is specific to sequential rules execution, it can only work with RuleBook implementations that use sequential rules execution (i.e. CoRRuleBook). So, the default behavior is still when a rule fails or its condition is not met, the rule chain continues. However, if a developer would like to make so that on a failure or failed rule condition, the rule breaks the rule chain, they may do so by specifying that in the creation of the rule (only available via RuleBuilder or the rule's constructor) using the optional boolean parameter. In that case, those rules that do not error and their condition is true will NOT break the rule chain; it will only break if the rule's condition evaluates false or the rule fails.
from rulebook.
This is updated in the 0.8-SNAPSHOT. One change was made, however. The above code would now look like this:
RuleBook ruleBook = RuleBookBuilder.create()
.addRule(
RuleBuilder.create(GoldenRule.class, STOP_ON_FAILURE) //second param indicates that on failure this rule can stop the rule chain
.withFactType(String.class)
.when(facts -> false)
.then(someConsumer)
.stop() //signals that this rule stops the rule chain
.build())
.addRule(
RuleBuilder.create() //this rule will never be invoked since the above rule stopped the rule chain when its condition was false
.withFactType(String.class)
.when(facts -> true)
.then(someOtherConsumer)
.build())
.build();
from rulebook.
This enhancement was released in v0.8.
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.