Method call without imput parameters in Rule

Hi,

Since the below block of code is kind of a method, which we can call in multiple places within the rule, what would be the syntax for a method without imput parameters?

var doTheMath = [ Integer i |
	var integer = null
	integer = i+i
	return integer;
]

when
	Test_Button changed
then
	logInfo("test.rules","the math result is: " + doTheMath.apply(2))
	//the above log print will be: "the math result is: 4"
end

I can’t get it right.

var NoARgs = [ |
    // code goes here
]

Note, do not fall into the trap of thinking that a global lambda is the same as a method or a regular function. Lambdas are not thread safe and there is only one of them. This means that if two rules call a lambda at the same time, they will both be inside the same function and will stomp over each other as each run.

As a general rule, in Rules DSL, the creation of global lambdas like this is a code smell. It’s not automatically something wrong, but it is very likely that there is a much better and/or safer approach.

If you really want to use functions, than you should switch to Jython, JavaScript, or Groovy in the NGRE.

1 Like

I think I was trying to do this without “|”, that’s why I couldn’t make it right.

I’ll keep in mind your remarks, thanks.