Is it possible to compile groovy script task statically

Is it possible to get script tasks of a process to be statically compiled?

The Groovy scripting engine is used to evaluate the script. If there are options to use statically compile scripts in the Groovy scripting engine then we could have a look at how to support it. Right now, it’s evaluated at execution time.

Best regards,

Tijs

I performed some tests and results are encouraging. Some AST transformations must be added so probably best way to implement it is to create new class which extends org.codehaus.groovy.jsr223.GroovyScriptEngineImpl similarly to JuelScriptEngine.
Let me know if you want me to implement it or you prefer to do it by yourself, I can provide some more hints as well as code.

Best regards,
Filip

Hi Filip,

Great, if you could help with a contribution here that would be preferable (mainly because there’s not much Groovy knowledge in the core Flowable team). Let us know if we can help.

Best regards,

Tijs

Hi Tijs.
I’m glad I can contribute to a such great project . Should I create issue or pull request is enough?

Best regards
Filip

Hi Filip,

It’s great that have you as a contributor to the Flowable project.
A pull request should be enough.

Best regards,

Tijs

Nice one, Filip - many thanks for your contribution!

Cheers
Paul.

Could you please point out the actual modification? (PR is just fine)
Thanks!

Hi.
I made the first version of the static compilation. It works but in my opinion this solution has too many limitations to enable it in the project. Firstly, from now groovy-all must be on the path because some classes from groovy package are used in scripting engine. In addition, the process variables are not visible inscript (type is unknown at compile time). Values from script is not assigned to the process variables (for the same reason). The result can be picked up only as flowable: resultVariable.
The code can be found here (branch StaticallyGroovyScriptsCompilation):
https://github.com/fgroch/flowable-engine/tree/StaticallyGroovyScriptsCompilation
I added testStaticallyScriptCompilation to GroovyScriptTest class where you can check it out.
The advantage of my work on this is that I know flowable better for now and I could efficiently contribute :slight_smile:.
I am waiting for your opinions.

Hi,

Thanks, I think the changes you made are good.
The statically compile version of the Groovy script engine should be separated in a new Flowable module to prevent the engine from having the groovy-all dependency as requirement. Or we should use reflection to load the statically compile Groovy engine instead. We do something similar with the Drools integration for business rule tasks in the Flowable Engine.

Do you have an idea how you can make the process variables available in the script?

Best regards,

Tijs

Hi.
I t take some time but the job is done :slight_smile:. Here is PR:
https://github.com/flowable/flowable-engine/pull/421

Hi Filip,

Thanks for the PR, merged it.

Best regards,

Tijs

This sound great, thanks!

Two questions:

  1. any chance to backport it to 5.X?
  2. how to enable the new feature?

Hi.
Ad 1. I will check it if will be helpful.
Ad 2. There is no need to enable it. Library is a part of flowable web apps. Script will be treated as static if script format is set to groovy-static.
Example:

<scriptTask id="theScriptTask" name="Execute script" scriptFormat="groovy-static" flowable:resultVariable="a">
  <script>
    int sum = 0
    for ( i in inputArray ) {
      sum += i
    }
    execution.setVariable("sum", sum)
    String a = "abc".toUpperCase()
  </script>
</scriptTask>

Best regards
Filip

Nice work, Filip. Really appreciate the effort for the contribution.

Cheers
Paul.

  1. Cool, I’ll keep watching this thread for news
  2. Agree, so the key to enable is using groovy-static rather than groovy

Thanks!

Hi.
Which branch should I use to implement static compilation for version 5.X, flowable5?

Best regards
Filip

Hi Filip, the flowable5 branch is what you’re looking for: https://github.com/flowable/flowable-engine/tree/flowable5