Git Product home page Git Product logo

Comments (13)

darrellme avatar darrellme commented on May 20, 2024 1

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.

darrellme avatar darrellme commented on May 20, 2024 1

Cool, thanks, I'll take at this hopefully sometime this week, many thanks for that

from rulebook.

Clayton7510 avatar Clayton7510 commented on May 20, 2024

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.

darrellme avatar darrellme commented on May 20, 2024

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.

Clayton7510 avatar Clayton7510 commented on May 20, 2024

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.

Clayton7510 avatar Clayton7510 commented on May 20, 2024

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.

Clayton7510 avatar Clayton7510 commented on May 20, 2024

closing due to inactivity

from rulebook.

darrellme avatar darrellme commented on May 20, 2024

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.

Clayton7510 avatar Clayton7510 commented on May 20, 2024

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.

Clayton7510 avatar Clayton7510 commented on May 20, 2024

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.

Clayton7510 avatar Clayton7510 commented on May 20, 2024

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.

Clayton7510 avatar Clayton7510 commented on May 20, 2024

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.

Clayton7510 avatar Clayton7510 commented on May 20, 2024

This enhancement was released in v0.8.

from rulebook.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.