0
votes

I am pretty new to drools and am given the below task.

I am inserting a few POJO into my KieSession object and am retreiving them into variables in my decision table as follows.

CONDITION            CONDITION            CONDITION          ACTION
abc: classABC       xyz: classXYZ        lmn : classLMN
var1 == $param       var2 == $param                                                                                

1                   2                     3

As far as I understand, the above table would yield the following rule

 when 
    abc:classABC(var1==1)
    xyz:classXYZ(var2==2)
    lmn:classLMN(var3==3)
 then
    some action

What I want is to get the following.

  when 
    abc:classABC(var1==1)
    xyz:classXYZ(var2==2)
    lmn:classLMN(var3==3)
    fgh:classFGH($var:var4)           // I think this step is creating a new variable to hold value of var4
 then
    some action

How do I get this on a decision table ?

I tried just adding a condition column with the variable declaration as fgh :classFGH, but since there is no data to be provided in the data row, this column would be ignored. If I do, give some data, there is an error at compile time "no code sinppet at xyz column". All I need is to declare a variable that can hold the value of the object that I have passed in my main method and use that object later in a different column of my decision table.

3
If you are assigning a variable to the POJO fact, by convention it is prefixed with $, viz when $abc:classABC(var1==1) - StuartLC
but thats just the naming convention right ? I mean it wouldnt really impact the functionality. - Adarsh
I think you need a global? But how can you "pass an object in [the] main method"? - laune
I am inserting multiple objects into the KieSession. I am working on the generated HelloWorld program that eclipse generates when creating a new drools project. - Adarsh
If you are a beginner, I suggest you start by writing DRL rules by hand unless your logic is really and truly structured to fit the narrow design pattern of a decision table. As soon as you leave the run-of-the-mill pattern "snippet here, value below" things can get very nasty indeed. - laune

3 Answers

0
votes

I'm not sure I get the requirement around the decision table, but you can 'use' the firing of a rule to create new facts and insert them, with parameters from the original events. These can then be used to trigger further rules, like so (assuming var4 is boolean):

declare AllMoonsInAlignmentEvent
    @role (event)
    extraCheese : boolean
end

rule "Some Rule"
  when
    $abc:classABC(var1==1)
    $xyz:classXYZ(var2==2)
    $lmn:classLMN(var3==3)
    $fgh:classFGH($var:var4)
 then
    ... some action using `$var`, `$abc` etc
    AllMoonsInAlignmentEvent myEvent= new AllMoonsInAlignmentEvent();
    myEvent.extraCheese = $var;
    insert(myEvent); 

rule "With Extra Cheese"
 when
    $moonsAligned:AllMoonsInAlignmentEvent(extraCheese == true)
 then
    ...

rule "Without Extra Cheese"
 when
    $moonsAligned:AllMoonsInAlignmentEvent(extraCheese == false)
 then
    ...
0
votes

You can get X($y:y) into a spreadsheet in two ways. First, in column 4

X($y:y /*$param*/)

and fill the column with any character you like. The other way might be in column 3 (!)

fgh:classFGH($var:var4) lmn:classLMN
var3==$param

These tricks are always a bit iffy. Rules requiring the simple "grab" of a fact aren't typical for spreadsheets and could be the first indication that you aren't pursuing the best approach.

-1
votes
  • CONDITION
  • fgh:classFGH
  • $param:var4
  • Comment cell
  • $var