Compilation issue with block lambda expression in RHS of rule

52 views
Skip to first unread message

thomamt

unread,
Jun 2, 2022, 6:50:52 AM6/2/22
to Drools Usage
Drools version 7.70.0.Final

Hi,

I have a lambda expression in the RHS of a rule that fails to compile if code blocks are used. The example below is a simplified example that doesn't need to use them but is just to illustrate the problem.
 
 // Example 1, working
 $list.stream().forEach(evd ->  System.out.println("Here"));
 modify($someFact) {
    // ...
};

 
 // Example 2, failing to compile with: $someFact cannot be resolved to a variable
 $list.stream().forEach((i) -> {System.out.println("Here");});
 modify($someFact) {
    // ...
 };
                       
Looking at the generated code for Example 2, the modify block has not been translated in the generated java code, instead being transferred as is which is causing the compilation issue. 

Is there another way this is supposed to be written?

Thanks

Toshiya Kobayashi

unread,
Jun 21, 2022, 2:43:07 AM6/21/22
to Drools Usage
Hi,

I have filed a JIRA for this issue.

https://issues.redhat.com/browse/DROOLS-7029

At the moment, there are 2 workarounds.

A) Use executable-model. If you are not familiar with executable-model, please refer to

  https://docs.drools.org/latest/drools-docs/html_single/#executable-model-con_packaging-deploying

  https://blog.kie.org/2021/11/drools-basic-examples.html
    -> Section "Executable-model Kjar" and/or "Executable-model with KieFileSystem"

B) Use a java static method to encapsulate the lambda (= hide lambda from RHS).

  for example,

MyUtils.java
~~~
public class MyUtils {

    public static void processList(List $list) {

        $list.stream().forEach((i) -> {System.out.println("Here");});
    }
    ...
~~~

DRL
~~~
...
then
  MyUtils.processList($list);
  modify($someFact) {
     // ...
  };
~~~

I hope this helps
Toshiya

2022年6月2日木曜日 19:50:52 UTC+9 thomamt:

thomamt

unread,
Jun 21, 2022, 10:32:24 AM6/21/22
to Drools Usage
Thanks for the swift response Toshiya, much appreciated. And thanks for the workarounds, I'm still fairly new to Drools and not familiar with the executable-model but will take a look.

Andrew

Reply all
Reply to author
Forward
0 new messages