gratiartis/scattercode-drools-scenarios — pull request #1 by gratiartis (2013-06-05 10:23:38+00:00)
Title: Created a demo of setting vars in LHS

I just couldn't find any examples of the DRL syntax for 'creating' a value like that in the LHS. A quick test with a contrived little example shows me that the following works:

rule "Warn when my meal is too large"
when
$meal: Meal($calories: calories > 1000)
$unit: String() from "calories"
then
insert(new MealTooBigWarning("That meal is bigger than your 1000 " + $unit + " limit."));
end

So, based on that I'm able to write a DSL like this:

[when][]I am given a meal of more than "{sizeLimit}" "{unit}" = $meal: Meal($size: {unit} > {sizeLimit}); $sizeLimit: Integer() from {sizeLimit}; $unit: String() from "{unit}"
[then][]Warn that the meal is too big = insertLogical(new MealTooBigWarning("That meal is bigger than your " + $sizeLimit + " " + $unit + " meal limit."));

Which means that I can write DSLR like this:

rule "Warn when my meal has too many calories"
when
I am given a meal of more than "1000" "calories"
then
Warn that the meal is too big
end

… which gives me:

That meal is bigger than your 1000 calories meal limit.

Or this:

rule "Warn when my meal has too many diet club points"
when
I am given a meal of more than "11" "dietClubPoints"
then
Warn that the meal is too big
end

… which gives me:

That meal is bigger than your 11 dietClubPoints meal limit.

So I can define an enumeration:

'MealSize.units': ['calories=calories', 'dietClubPoints=diet club points']

So that in Guvnor, I have a single sentence with a drop-down menu containing the options "calories" and "diet club points". That keeps things nice and clean for the users.