Last active
December 23, 2018 14:31
-
-
Save dschulten/fe3dadbd8eb5e79aa94db682b1e552c8 to your computer and use it in GitHub Desktop.
Activate Feel Scala Plugin for Camunda Dmn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
public class DmnConfig { | |
/** | |
* Creates DMN engine with feel-scala engine as default language. | |
* | |
* @return engine | |
* @see <a href="https://github.com/camunda/camunda-engine-dmn">Simple Engine Setup</a> | |
* @see <a href="https://github.com/camunda/feel-scala/tree/master/feel-engine#how-to-use-it">Using full FEEL language in Camunda DMN</a> | |
*/ | |
@Bean | |
public DmnEngine dmnEngine() { | |
DefaultDmnEngineConfiguration dmnEngineConfig = (DefaultDmnEngineConfiguration) | |
DmnEngineConfiguration.createDefaultDmnEngineConfiguration(); | |
// make feel-scala the default expression language | |
CamundaFeelEngineFactory feelEngineFactory = new CamundaFeelEngineFactory(); | |
dmnEngineConfig.setFeelEngineFactory(feelEngineFactory); | |
// Multiple language definitions for decisions of type decision table: | |
// language of input column header cell = input expressions in DMN terminology | |
dmnEngineConfig.setDefaultInputExpressionExpressionLanguage("feel-scala"); | |
// language of decision input cells = input entries in DMN terminology | |
dmnEngineConfig.setDefaultInputEntryExpressionLanguage("feel-scala-unary-tests"); | |
// language of output cells = output entries | |
dmnEngineConfig.setDefaultOutputEntryExpressionLanguage("feel-scala"); | |
// Single language definition for decisions of type literal expression: | |
dmnEngineConfig.setDefaultLiteralExpressionLanguage("feel-scala"); | |
// tell camunda that there may be an input type list (DMN 1.2) | |
DmnTransformer transformer = dmnEngineConfig.getTransformer(); | |
DmnDataTypeTransformerRegistry dataTypeTransformerRegistry = transformer.getDataTypeTransformerRegistry(); | |
dataTypeTransformerRegistry.addTransformer("list", listDataTypeTransformer()); | |
// listen for decision events | |
dmnEngineConfig.setCustomPostDecisionTableEvaluationListeners(Arrays.asList(auditListener)); | |
return dmnEngineConfig.buildEngine(); | |
} | |
@Bean | |
public ListDataTypeTransformer listDataTypeTransformer() { | |
return new ListDataTypeTransformer(); | |
} | |
public static class ListDataTypeTransformer implements DmnDataTypeTransformer { | |
/** | |
* Transform the given value. | |
* | |
* @param value of any type | |
* @return value of the specific type | |
* @throws IllegalArgumentException if the value can not be transformed | |
*/ | |
@Override | |
public TypedValue transform(Object value) throws IllegalArgumentException { | |
return Variables.objectValue(value) | |
.create(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.escalon.dmn; | |
import com.google.common.collect.ImmutableMap; | |
import org.camunda.feel.interpreter.ValDate; | |
import org.camunda.feel.spi.JavaFunction; | |
import org.camunda.feel.spi.JavaFunctionProvider; | |
import java.time.LocalDate; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.Map; | |
/** | |
* Register provider class in file META-INF/services/org.camunda.feel.spi.CustomFunctionProvider | |
*/ | |
public class FunctionProvider extends JavaFunctionProvider { | |
private static Map<String, List<JavaFunction>> functionMap = ImmutableMap.of("now", | |
Arrays.asList(new JavaFunction(Collections.emptyList(), vals -> new ValDate(LocalDate.now())))); | |
@Override | |
public List<JavaFunction> resolveFunctions(String functionName) { | |
if (functionMap.containsKey(functionName)) { | |
return functionMap.get(functionName); | |
} else { | |
return Collections.emptyList(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
de.escalon.dmn.FunctionProvider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class RegisterDmnDataTypeTransformerProcessEnginePlugin implements ProcessEnginePlugin { | |
@Override | |
public void preInit(ProcessEngineConfigurationImpl processEngineConfiguration) { | |
} | |
@Override | |
public void postInit(ProcessEngineConfigurationImpl processEngineConfiguration) { | |
DefaultDmnEngineConfiguration dmnEngineConfiguration = processEngineConfiguration.getDmnEngineConfiguration(); | |
DmnTransformer transformer = dmnEngineConfiguration.getTransformer(); | |
DmnDataTypeTransformerRegistry dataTypeTransformerRegistry = transformer.getDataTypeTransformerRegistry(); | |
dataTypeTransformerRegistry.addTransformer("list", new ListDataTypeTransformer()); | |
} | |
@Override | |
public void postProcessEngineBuild(ProcessEngine processEngine) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment