5
votes

I want to declare a global variable in a drools rule file (mvel). This is because this global is used in all the rules as a parameter to another function. I could easily pass this string explicitly in every call to the function, but this makes it hard if the string changes.

I thought I could do a:

global String someStr = "some string";

But on compile, I get:

[11,31]: [ERR 107] Line 11:31 mismatched input '=' expecting one of the following tokens: '[package, import, global, declare, function, rule, query]'.

So obviously, I can't assign it this way. Nor do I seem to be able to declare a class and a string in that class to reference through the class.

So I found I could so something that seems silly:

global String someStr;
rule "Initialize"
when
then
   someStr = "some string";
end

This seems to work, but, this will log every single time this rule matches (always) to just assign a global.

Is there a better way that I'm missing???

2
How about reading Section 7.5.2, Global, in the Drools manual? There may be other details you might need in your context, so I think it would be rewarding to read the documentation.laune
Do you think I would have posted without reading the manual first? Come on. The silly way to do it, a rule to execute as true, is straight from the manual!RallyRabbit
Which manual, which section? You must be confusing things. - The statement "this will log every single time" is not correct.laune
You are asking "How to declare and assign a global". But your actual problem seems to be passing a string value to a function. For the first: see the manual. For the second: show the function and describe when and how it should be possible to change this parameter - at compile time, at runtime: once at start-up time, or many times depending on...? --- The quality of an answer depends on the quality of the question.laune
Again, no offense, nowhere have I stated that passing a string value to a function is an issue. I simply asked how you declare and assign global value in a drools file. Declaring a global is not a problem, assigning a global variable is a problem. You cannot declare and assign at the same time, which means you have do assign it another way.RallyRabbit

2 Answers

5
votes

Using globals is a two step process. Declaration and assignment.

Declaration takes place in the rule file.

Assignment takes place at session level in the java code.

In the java code:

    List<String> list= new ArrayList<String>();
    list.add("a");
    list.add("b");
    list.add("c");
    KieSession ks = kcontainer.newKieSession("test");
    ks.setGlobal("glist", list);

In the drl file:

global java.util.List glist;

Now this glist global variable which has values "a", "b", "c" can be used across all the rules in the drl file.

0
votes

you can also define a function in the DRL

function String getParam(String name){
   return name.equals("x") ? "xValue" : "yValue";
}


.....
/// call it in you rule as below
.....

rule "my rule"
when 
then
  System.out.println(getParam("x"));
end