How using bonita user-defined function

1
+1
-1

I have create a test function but is not present in Groovy Script user-defined List :

package com.company.script

public class MesScripts {

public static String test() {
return "test"
}

}

Can you help me ?

Thanks

2 answers

1
+3
-1
This one is the BEST answer!

[French version below]

Sounds like you have already a class in a groovy file. Once checked that this class is visible in your project tree, I have two solutions for you. You can keep your current code and manually import your class in the expression Groovy script editor -> see 2). Or you can, as expected in the question name of this topic, make it appears in the "user_defined function" panel -> see 1).

These solutions work for Bonita Studio 7.8 under Windows 7.

Two possible ways : choose one of them.

1) First solution : put your class on the default package and pick your function in the "user-defined" section in script expression editor

Right click on "Groovy scripts" in the work tree : create your class (ie : MesScripts) but leave the package name empty. Once created, your class appeared in the "(default package)". In this package, static methods of your class normally appear in the "User defined" panel.

Click on the crayon icon somewhere in the studio to edit an expression (in a operation for instance, or when step selected : General > Portal > displayDescription). Check the "User defined" on right side of th pop-up window.

If your function is not visible, you may have to restart Bonita Studio (I didn't find another way to update the list of available scripts).

You should see your function in the user-defined scripts.

So if you simply write

return MesScripts.test()

and click evaluate, you should have a pop-up windows where your message "test" appears.

Done.

2) Second solution : keep using your custom package, and manually import your class in script expression editor

Your function can still be accessed from script expression edition. You simply have to manually import the package.

For this write

import com.company.script.MesScripts

return MesScripts.test()

And then the script should work during your process.

Beware : with this method, the button "Evaluate" under the script edition won't work (a multiple compilation exception is thrown). But don't worry, it works during the process when you are using the Bonita Portal.

Done.

I hope it would help. It is up to you to use the solution which fits your needs. Personally I prefer the second solution. Indeed, it allows you to set a personnalized package name, which is for me a good practice.

[Version française]

Il semble que vous ayez déjà une classe dans un fichier Groovy. Après avoir vérifié que votre classse apparaissaît bien dans l'arborescence de votre projet, j'ai deux solutions différentes pour vous. Vous pouvez garder votre script actuel et l'importer manuellement dans le panneau d'édition d'expression Groovy -> voir 2). Ou alors, comme attendu d'après la formulation de la question, faire apparaître votre fonction dans le panneau "User-defined" -> voir 1).

Ces deux solutions fonctionnent pour Bonita Studio 7.8 avec Windows 7.

1) Première solution : créer sa classe dans le package par défaut

Cliquez droit sur l'item "Groovy Scripts" dans l'arborescence de votre projet : créez votre classe normalement (nom : MesScripts) mais laissez le nom de package vide. Une fois créé, votre classe apparaît dans le package par défaut (default-package). Une fois dans ce package, les méthodes statiques de vos classes doivent apparaître dans le panneau "user-defined".

Si votre fonction n'apparaît pas dans la liste "user defined", vous devez sans doute redémarrer Bonita Studio (Je n'ai pas trouvé de façon plus rapide de recharger les scripts disponibles).

Vous devez maintenant voir apparaître votre fonction dans la liste des scripts.

Donc si vous écrivez simplement :

return MesScripts.test()

et cliquez sur le bouton "Evaluate", vous devez voir apparaître une pop-up avec "test" marqué à l'intérieur, comme attendu.

2) Seconde solution : utilisez votre script Groovy actuel en l'important manuellement dans vos expressions

Cette fois-ci il faut spécifier le package avant d'utiliser la méthode :

import com.company.script.MesScripts
return MesScripts.test()

Le script doit fonctionner pendant l'exécution d'un processus.

Attention : cette fois-ci, le bouton "Evaluate" ne fonctionne pas (exception levée par Groovy).

J'espère vous avoir apporté une aide suffisante, il ne vous reste qu'à utiliser la solution qui vous convient le plus. Personnellement, je préfère la seconde : je pense que pouvoir spécifier le nom d'un package est une bonne pratique.

Comments

Submitted by Jerome.DELANGUE... on Tue, 02/26/2019 - 08:55

Thanks, the return MesScripts.test() is working in my groovy script.
But my function test() is always not present in my user defined list. I try to restart my studio but the problem is always present.

Submitted by jovian.hersemeu... on Tue, 02/26/2019 - 09:40

Which solution have you implemented ? If you have chosen the second solution, by manually importing your function using

import com.company.script.MesScripts

return MesScripts.test()

your function doesn't appear in the user defined list. That's the way Bonita works. If you prefer to have your own function become visible in the "user defined function" list, please make your groovy class again and leave the package name field empty at the creation (the first solution in my answer).

Submitted by antoine.mottier on Tue, 02/26/2019 - 15:02

If you choose the first solution here is an example of the content of the Groovy file. Note that the file should not include class declaration:

/**
 * My Groovy function
 * @return a constant value
 */
static def String test() {
    return 'A string'
}

If you follow such syntax the Groovy function should be listed under " User defined" category after a Studio restart (it seems that this list is only refresh at Studio startup):

Expression editor - Groovy - User defined scripts

Submitted by Jerome.DELANGUE... on Wed, 02/27/2019 - 10:06

In fact, without class declaration the function is present in User Defined Category
Thanks a lot

1
+1
-1

This solution is ok when calling the script from a process.

But now, we've got an error when calling this script in a subprocess :

2019-03-06 11:21:35.965 +0100 GRAVE: org.bonitasoft.engine.execution.work.InSessionBonitaWork THREAD_ID=128 | HOSTNAME=not11873 | TENANT_ID=1 | The work [ExecuteFlowNodeWork: flowNodeInstanceId: 100297] failed. The failure will be handled.
2019-03-06 11:21:35.966 +0100 GRAVE: org.bonitasoft.engine.execution.work.InSessionBonitaWork THREAD_ID=128 | HOSTNAME=not11873 | TENANT_ID=1 | org.bonitasoft.engine.core.process.instance.api.exceptions.SActivityStateExecutionException : "PROCESS_DEFINITION_ID=5017854892370112667 | PROCESS_NAME=Préparation Consultation | PROCESS_VERSION=1.0 | PROCESS_INSTANCE_ID=5029 | ROOT_PROCESS_INSTANCE_ID=5027 | FLOW_NODE_DEFINITION_ID=6285408241647357089 | FLOW_NODE_INSTANCE_ID=100297 | FLOW_NODE_NAME=DA26 - Envoi mail SCP | org.bonitasoft.engine.core.process.instance.api.exceptions.SProcessInstanceCreationException: org.bonitasoft.engine.expression.exception.SExpressionEvaluationException: Expression agentDOPSCP() with content =  depends on MesScripts is neither defined in the script nor in dependencies."
org.bonitasoft.engine.core.process.instance.api.exceptions.SActivityStateExecutionException: PROCESS_DEFINITION_ID=5017854892370112667 | PROCESS_NAME=Préparation Consultation | PROCESS_VERSION=1.0 | PROCESS_INSTANCE_ID=5029 | ROOT_PROCESS_INSTANCE_ID=5027 | FLOW_NODE_DEFINITION_ID=6285408241647357089 | FLOW_NODE_INSTANCE_ID=100297 | FLOW_NODE_NAME=DA26 - Envoi mail SCP | org.bonitasoft.engine.core.process.instance.api.exceptions.SProcessInstanceCreationException: org.bonitasoft.engine.expression.exception.SExpressionEvaluationException: Expression agentDOPSCP() with content =  depends on MesScripts is neither defined in the script nor in dependencies.
    at org.bonitasoft.engine.execution.StateBehaviors.handleCallActivity(StateBehaviors.java:418)
    at org.bonitasoft.engine.execution.state.InitializingActivityWithBoundaryEventsStateImpl.afterConnectors(InitializingActivityWithBoundaryEventsStateImpl.java:98)
    at org.bonitasoft.engine.execution.state.OnEnterOrOnFinishConnectorState.execute(OnEnterOrOnFinishConnectorState.java:65)
    at org.bonitasoft.engine.execution.state.OnEnterOrOnFinishConnectorState.execute(OnEnterOrOnFinishConnectorState.java:1)
    at org.bonitasoft.engine.execution.FlowNodeExecutorImpl.executeState(FlowNodeExecutorImpl.java:133)
    at org.bonitasoft.engine.execution.FlowNodeExecutorImpl.updateState(FlowNodeExecutorImpl.java:184)
    at org.bonitasoft.engine.execution.FlowNodeExecutorImpl.stepForward(FlowNodeExecutorImpl.java:168)
    at org.bonitasoft.engine.execution.FlowNodeExecutorImpl.executeFlowNode(FlowNodeExecutorImpl.java:348)
    at org.bonitasoft.engine.execution.work.ExecuteFlowNodeWork.work(ExecuteFlowNodeWork.java:86)
    at org.bonitasoft.engine.execution.work.TxBonitaWork.lambda$0(TxBonitaWork.java:42)
    at org.bonitasoft.engine.transaction.JTATransactionServiceImpl.executeInTransaction(JTATransactionServiceImpl.java:274)
    at org.bonitasoft.engine.execution.work.TxBonitaWork.work(TxBonitaWork.java:42)
    at org.bonitasoft.engine.execution.work.LockProcessInstanceWork.work(LockProcessInstanceWork.java:73)
    at org.bonitasoft.engine.execution.work.failurewrapping.TxInHandleFailureWrappingWork.work(TxInHandleFailureWrappingWork.java:41)
    at org.bonitasoft.engine.execution.work.failurewrapping.TxInHandleFailureWrappingWork.work(TxInHandleFailureWrappingWork.java:41)
    at org.bonitasoft.engine.execution.work.failurewrapping.TxInHandleFailureWrappingWork.work(TxInHandleFailureWrappingWork.java:41)
    at org.bonitasoft.engine.execution.work.InSessionBonitaWork.work(InSessionBonitaWork.java:59)
    at org.bonitasoft.engine.work.BonitaThreadPoolExecutor.lambda$submit$1(BonitaThreadPoolExecutor.java:98)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.bonitasoft.engine.core.process.instance.api.exceptions.SProcessInstanceCreationException: org.bonitasoft.engine.expression.exception.SExpressionEvaluationException: Expression agentDOPSCP() with content =  depends on MesScripts is neither defined in the script nor in dependencies.
    at org.bonitasoft.engine.execution.ProcessExecutorImpl.start(ProcessExecutorImpl.java:838)
    at com.bonitasoft.engine.execution.ProcessExecutorExt.startSuper(ProcessExecutorExt.java:192)
    at com.bonitasoft.engine.execution.ProcessExecutorExt.start(ProcessExecutorExt.java:162)
    at org.bonitasoft.engine.execution.ProcessExecutorImpl.start(ProcessExecutorImpl.java:793)
    at org.bonitasoft.engine.execution.StateBehaviors.instantiateProcess(StateBehaviors.java:446)
    at org.bonitasoft.engine.execution.StateBehaviors.handleCallActivity(StateBehaviors.java:407)
    ... 20 more
Caused by: org.bonitasoft.engine.expression.exception.SExpressionEvaluationException: Expression agentDOPSCP() with content =  depends on MesScripts is neither defined in the script nor in dependencies.
    at org.bonitasoft.engine.expression.impl.GroovyScriptExpressionExecutorCacheStrategy.evaluate(GroovyScriptExpressionExecutorCacheStrategy.java:144)
    at org.bonitasoft.engine.expression.impl.ExpressionServiceImpl.evaluate(ExpressionServiceImpl.java:91)
    at org.bonitasoft.engine.core.expression.control.api.impl.ExpressionResolverServiceImpl.evaluateExpressionWithResolvedDependencies(ExpressionResolverServiceImpl.java:213)
    at org.bonitasoft.engine.core.expression.control.api.impl.ExpressionResolverServiceImpl.evaluateExpressionsFlatten(ExpressionResolverServiceImpl.java:120)
    at org.bonitasoft.engine.core.expression.control.api.impl.ExpressionResolverServiceImpl.evaluate(ExpressionResolverServiceImpl.java:83)
    at org.bonitasoft.engine.bpm.model.impl.BPMInstancesCreator.evaluateExpression(BPMInstancesCreator.java:600)
    at org.bonitasoft.engine.bpm.model.impl.BPMInstancesCreator.createDataInstance(BPMInstancesCreator.java:594)
    at org.bonitasoft.engine.bpm.model.impl.BPMInstancesCreator.createDataInstances(BPMInstancesCreator.java:568)
    at org.bonitasoft.engine.execution.ProcessExecutorImpl.initialize(ProcessExecutorImpl.java:384)
    at org.bonitasoft.engine.execution.ProcessExecutorImpl.start(ProcessExecutorImpl.java:823)
    ... 25 more
Caused by: groovy.lang.MissingPropertyException: No such property: MesScripts for class: BScript704
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
    at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:52)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:307)
    at BScript704.run(BScript704.groovy:1)
    at org.bonitasoft.engine.expression.impl.GroovyScriptExpressionExecutorCacheStrategy.evaluate(GroovyScriptExpressionExecutorCacheStrategy.java:141)
    ... 34 more

Have you got an explanation ?

Thanks.

Comments

Submitted by fabien.delannoy on Thu, 03/07/2019 - 14:51

Support response :

Ok, the problem was in dependencies :

-a- open diagram
-b- click on pool
-c- click on params (configuration)
-d- click on java dependencies

=> checked the library scripts

Notifications