Extending JUL with Java functions for global use


#1

Hi,
how can we extend JUL with Java functions so that they can be used from different DMNs in a shared camunda server. I have tried the example (AzimoLabs/extend-camunda-dmn-jul-with-java-functions) but the extended functions are not accessable from DMNs not deployed in this war. Our goal is to provide a set of functions to provide complex business functions in an easy way to our users.
Is this possible?
Any help will be appreciated.

Regards
Edmund


#2

Hi,
I have managed to have it working. I have adapted the example from:
https://medium.com/azimolabs/extending-juel-with-custom-functions-in-camunda-dmn-engine-616f38d200f3. I decided to use the FunctionMapper approach.

The business function, just an example:

public class DateHelper {

    public static Date addDays(Date date, Integer days) {
        if (date == null) {
            return new Date();
        }
        LocalDateTime dt = new LocalDateTime(date.getTime(), DateTimeZone.getDefault());
        dt = dt.plusDays(days);
        return dt.toDate();
    }
}

The FunctionMapper:

public class ExFunctionsMapper extends FunctionMapper {

private final static String PREFIX = "ex";
private static Map<String, Method> functionCache = null;

public Method resolveFunction(String prefix, String localName) {
    ensureFunctionMapInitialized();
    return functionCache.get(getFullFunctionName(prefix, localName));
}

private String getFullFunctionName(String prefix, String localName) {
    return prefix + ":" + localName;
}

private void ensureFunctionMapInitialized() {
    if (functionCache == null) {
        synchronized (MoraFunctionsMapper.class) {
            if (functionCache == null) {
                functionCache = new HashMap<>();
                createMethodBindings();
            }
        }
    }
}

private void createMethodBindings() {
    try {
        functionCache.put(getFullFunctionName(PREFIX, "addDays"),
                DateHelper.class.getMethod("addDays", java.util.Date.class, Integer.class));
    } catch (Exception ex) {
        throw new FunctionRegistrationFailedException(ex);
    }
}

and the Plugin:

public class ExProcessEnginePlugin extends AbstractProcessEnginePlugin {
    @Override
     public void postInit(ProcessEngineConfigurationImpl processEngineConfiguration) {
         processEngineConfiguration.getExpressionManager().addFunctionMapper(new ExFunctionsMapper());
     }
}

The plugin is registered using the bpm-platfom.xml:

  <plugin>
    <class>dxx.plugin.ExProcessEnginePlugin</class>
  </plugin>

I registered this FunctionMapper inside the ProcessEngine as a plugin as described in the DmnDecisionChainingPlugin.java example.
I can now access the custom function from inside the Dmn using ex:addDays(date, days) and it works fine but now I am struggling with testing. How can I register this plugin in the embedded DmnEngine for executing JUnit-Tests. I can only find examples with Spring Beans.
Any help will be appreciated?

Regards
Edmund


#3

Has anyone an idea how to register this plugin in the embedded DmnEngine for execution of JUnit-Tests?
It doesn’t matter what I try nothing works, the custom functions are not recognized during the JUnit-Test.

Regards
Edmund