Search code examples
droolsdrools-guvnordrools-fusiondrools-flow

Multiple end for a rule in Drools


I am completely new to Drools and just exploring. So far I had been using a single rule and withing which we use if conditions just like java.

Now I had to use complex rules where I need to use multiple if and else chain but to end it when one in the series of conditions satisfies.

I call the drools from the Apache Camel. The rules should reply back to Camel.

Now How do I do to break the chain of rules and then send response back to the caller which is Camel code when one of if and else condition fails.

1.Can I use multiple end statements to respond back?

2.I can use functions and I know is to write functions in java and import them.

3.Is there any possibility to create functions in Drools and use just like in java?

I am not using the Drools in the way it should be used but so far the rules had not been so complex as the one we are using them now . Any help is is useful for me.

Here is an example which I would like to use please suggest if the below would work or some other alternative like the below.

rule "my rule"
when
#some condition

then

 if(){

 end

 }else if(){

 #do something

 }

 if(){

  #do some other logic

}
 end

Sample after My second comment

When

object:SomeObject(); // This helps for my camel code to trigger this rule and this rule only

then

if(){

}
else if()
{

return;
}else if() {
}

if(){

}else if(){
return;
}
if(){

}
end

Solution

  • I don't know what you mean by "breaking a chain of rules". Evaluating rules ends all by iteself, when there aren't any more rule activations to be executed,

    Answers to 1. - 3.:

    1. No. - There is no such thing I can associate with the term "respond back", but there aren't "multiple end statements [for one rule]".

    2. Yes, you can use static Java function: import the class, call it, as in Java.

    3. Yes. this is explained in the Drools manual, in a section with the title "Function". The example is from the manual:

      function String hello(String name) { return "Hello "+name+"!"; }

    Comment on the rule added later to the question

    You cannot use end between then and the actual end of the rule. If you want to terminate the consequence part of a rule, use Java's return statement (without an expression, of course).

    Reconsider using complex conditional statements in the consequence. Logic decisions should be expressed in the condition parts of rules.

    Much later Workaround due to possible Guvnor bug - does not accept return;

    boolean skip = false;
    if(){
    } else 
    if() {
        skip = true; // return;
    } else 
    if() {
    }
    if( ! skip ){
        if(){
        } else
        if(){
            skip = true; // return;
        }
     }
    if( ! skip ){
        if (){
        }
    }
    

    end