package org.brms; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Map; import org.camunda.bpm.dmn.engine.DmnDecision; import org.camunda.bpm.dmn.engine.DmnDecisionTableResult; import org.camunda.bpm.dmn.engine.DmnEngine; import org.camunda.bpm.dmn.engine.DmnEngineConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.brms.ResourceHandler; import org.brms.Rule; import org.brms.RuleException; import org.brms.RuleNotFoundException; public class CustomFunctions { public static final Logger logger = LoggerFactory.getLogger(CustomFunctions.class); /* * Example method to demonstrate how minimums could be calculated in Camunda DMN */ public int min(int a, int b) { return Math.min(a, b); } /* * Evaluate a nested decision table and provide result as input for parent table */ public Object evaluate(String ruleName, Map variableContext) throws RuleException { logger.debug("evaluate chained rule '{}'", ruleName); Rule rule = ResourceHandler.getInstance().getRule(Constants.ruleBasePath + ruleName); DmnEngine dmnEngine = DmnEngineConfiguration.createDefaultDmnEngineConfiguration().buildEngine(); try (InputStream inputStream = rule.openStream()) { if (inputStream == null) { throw new RuleNotFoundException(ruleName); } DmnDecision decision = dmnEngine.parseDecision(new File(ruleName).getName(), inputStream); DmnDecisionTableResult result = dmnEngine.evaluateDecisionTable(decision, variableContext); logger.debug("sub-result: {}", result.collectEntries(Constants.result)); return result.getFirstResult().getFirstEntry(); //return result.collectEntries(Constants.result); } catch (IOException e) { throw new RuleException("unable to load rule " + ruleName, e); } } }