1

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
Naveen Raj
  • 801
  • 11
  • 26

1 Answers1

2

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

laune
  • 31,114
  • 3
  • 29
  • 42
  • Thanks for the answer. I saw the function one. But i would like to code like a sample which I have added in my question now. Can you confirm with that? – Naveen Raj Jul 12 '14 at 15:34
  • @NaveenRaj Added a comment on the rule in my answer. – laune Jul 12 '14 at 17:47
  • it says that void methods cannot return value. Can you help me ? – Naveen Raj Jul 23 '14 at 06:20
  • See my answer: "return statement ... **without an expression**" – laune Jul 23 '14 at 06:23
  • I have given just "return;" I did not get without expression .. You mean in the when condition ? Please clarify me.. – Naveen Raj Jul 23 '14 at 06:24
  • A statement `return;` is OK (not `return someStr;` or similar). But it could be something else - I can't tell without the rule code. – laune Jul 23 '14 at 06:28
  • I have a similar code for this. Can you tell something on this? or do you need more info – Naveen Raj Jul 23 '14 at 06:34
  • This: `else if{ return;}else if {}` is not valid Java code. – laune Jul 23 '14 at 06:37
  • This kind if if else conditions work in the then part and with my little experience it works but the issue i face is the return statement . which gives me error when validating with validate button in the guvnor. Any Ideas. – Naveen Raj Jul 23 '14 at 06:38
  • But the sequence of tokens in that line is **not valid Java code**. I won't answer questions based on it. - However, if you do have problems in Guvnor with correct return statements, it is a Guvnor issue, to be reported on Drools JIRA. - You can work around this issue by setting a flag and testing it subsequently. – laune Jul 23 '14 at 06:58
  • Sorry the earlier one was a typo and I misunderstood that the series of if else could not be used. I have made relevant changes in my question . Please check if they are releveant – Naveen Raj Jul 23 '14 at 07:03
  • Thanks @laune I used functions to make a work around and am calling them in the drools file. I am testing them and let you know about it once completed – Naveen Raj Jul 23 '14 at 09:37