The Camunda DMN engine can be used as a library in a custom application. To achieve this,add the camunda-engine-dmn artifact to the classpath of the application and thenconfigure and build a decision engine instance. This section provides therequired maven coordinates to add the DMN engine as a dependency to yourproject. It then shows how to configure and build a new DMN engine instance.

Maven Coordinates

The Camunda DMN engine is released to Maven Central.

Start by importing the camunda-engine-dmn BOMto ensure correct dependency management.

Next, include the camunda-engine-dmnartifact in the dependencies section.

Building a DMN Engine

To build a new DMN engine, create a DMN engine configuration.Configure it as needed and then build a new DMN engine from it.

  1. // create default DMN engine configuration
  2. DmnEngineConfiguration configuration = DmnEngineConfiguration
  3. .createDefaultDmnEngineConfiguration();
  4. // configure as needed
  5. // ...
  6. // build a new DMN engine
  7. DmnEngine dmnEngine = configuration.buildEngine();

Configuration of the DMN Engine

Decision Table Evaluation Listeners

The DMN engine configuration allows you add a custom decision table evaluation listener. A decision table evaluation listener isnotified after a decision table has been evaluated. It receives an evaluation eventwhich contains the result of the evaluation. You can decide if thelistener should be notified before or after the default listeners.

  1. // create default DMN engine configuration
  2. DmnEngineConfiguration configuration = DmnEngineConfiguration
  3. .createDefaultDmnEngineConfiguration();
  4. // instantiate the listener
  5. DmnDecisionTableEvaluationListener myListener = ...;
  6. // notify before default listeners
  7. configuration.getCustomPreDecisionTableEvaluationListeners()
  8. .add(myListener);
  9. // notify after default listeners
  10. configuration.getCustomPostDecisionTableEvaluationListeners()
  11. .add(myListener);

A specialized evaluation listener is the metric collector, which records the number of executed decisionelements. This metric can be used to monitor the workload of a decision engine.

  1. // create default DMN engine configuration
  2. DmnEngineConfiguration configuration = DmnEngineConfiguration
  3. .createDefaultDmnEngineConfiguration();
  4. // create your metric collector
  5. DmnEngineMetricCollector metricCollector = ...;
  6. // set the metric collector
  7. configuration.setEngineMetricCollector(metricCollector);

Decision Evaluation Listeners

The DMN engine configuration allows you add a custom decision evaluation listener. A decision evaluation listener isnotified after a decision with all the required decisions has been evaluated. It receives an evaluation eventwhich contains the result of the evaluation. You can decide if thelistener should be notified before or after the default listeners.

  1. // create default DMN engine configuration
  2. DmnEngineConfiguration configuration = DmnEngineConfiguration
  3. .createDefaultDmnEngineConfiguration();
  4. // instantiate the listener
  5. DmnDecisionEvaluationListener myListener = ...;
  6. // notify before default listeners
  7. configuration.getCustomPreDecisionEvaluationListeners()
  8. .add(myListener);
  9. // notify after default listeners
  10. configuration.getCustomPostDecisionEvaluationListeners()
  11. .add(myListener);

Customizing and Extending the DMN Engine

Use of Internal API

Please be aware that these APIs are not part of the public API and may change in later releases.

The default DMN engine configuration has further customization andextension points.

Customize DMN Transformation

It is possible to customize the transformation of DMN by providing a DMN transformer or configuring the default one.

Register DMN Transform Listeners

The simplest customization is to provide a transform listener. The Listener is notified after a DMN element istransformed. The listener can modify the transformed object.

  1. // with a default DMN engine configuration
  2. DefaultDmnEngineConfiguration configuration = (DefaultDmnEngineConfiguration) DmnEngineConfiguration
  3. .createDefaultDmnEngineConfiguration();
  4. // instantiate transform listener
  5. DmnTransformListener myTransformListener = ... ;
  6. // add the transform listener
  7. configuration.getTransformer()
  8. .getTransformListeners()
  9. .add(myTransformListener);

Register DMN Element Transform Handler

While the transform listener allows modifying of the transformed objects, it does not support instantiating custom subclasses.This can be achieved using a custom transform handler.

A transform handler is registered for a given DMN model API type like a DecisionTable.

First, implement a transform handler which can transform a decision table.

  1. public class MyDecisionTableHandler extends DmnElementTransformHandler<DecisionTable, MyDecisionTableImpl> {
  2. public MyDecisionTableImpl handleElement(DmnElementTransformContext context, DecisionTable element) {
  3. // implement
  4. }
  5. }

Then, register an instance of the handler in the default DMN transformer element handler registry.

  1. // with a default DMN engine configuration
  2. DefaultDmnEngineConfiguration configuration = (DefaultDmnEngineConfiguration) DmnEngineConfiguration
  3. .createDefaultDmnEngineConfiguration();
  4. // add the handler
  5. configuration.getTransformer()
  6. .getElementTransformHandlerRegistry()
  7. .addHandler(DecisionTable.class, new MyDecisionTableHandler());

Register DMN Data Type Transformers

The DMN engine supports a set of built-in data types. It is possible to override existing types with new types.

Assume you want to support a local date format type.To achieve this, override the existing date transformer by implementing a custom transformer:

  1. public class GermanDateDataTypeTransformer extends DateDataTypeTransformer {
  2. protected SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
  3. protected Date transformString(String value) {
  4. try {
  5. return format.parse(value);
  6. }
  7. catch (ParseException e) {
  8. throw new IllegalArgumentException(e);
  9. }
  10. }
  11. }

Then, register an instance of the handler in the default DMN transformer element handler registry:

  1. // with a default DMN engine configuration
  2. DefaultDmnEngineConfiguration configuration = (DefaultDmnEngineConfiguration) DmnEngineConfiguration
  3. .createDefaultDmnEngineConfiguration();
  4. // add the data type transformer,
  5. // overriding the existing type "date":
  6. configuration
  7. .getTransformer()
  8. .getDataTypeTransformerRegistry()
  9. .addTransformer("date", new GermanDateDataTypeTransformer());

It is also possible to add a new data type by implementing a new transformer and registering it for a non-existing type name:

  1. // with a default DMN engine configuration
  2. DefaultDmnEngineConfiguration configuration = (DefaultDmnEngineConfiguration) DmnEngineConfiguration
  3. .createDefaultDmnEngineConfiguration();
  4. // add the data type transformer for custom type "currency"
  5. configuration
  6. .getTransformer()
  7. .getDataTypeTransformerRegistry()
  8. .addTransformer("currency", new currencyTypeTransformer());

Register Hit Policy Handlers

The DMN engine supports a subset of the DMN 1.1 hit policies. It is possible to implement new hit policies oroverride an existing hit policy implementation.

  1. // with a default DMN engine configuration
  2. DefaultDmnEngineConfiguration configuration = (DefaultDmnEngineConfiguration) DmnEngineConfiguration
  3. .createDefaultDmnEngineConfiguration();
  4. // get the DmnHitPolicyHandlerRegistry
  5. DmnHitPolicyHandlerRegistry hitPolicyHandlerRegistry = configuration
  6. .getTransformer()
  7. .getHitPolicyHandlerRegistry();
  8. // register handler you own priority hit policy handler
  9. hitPolicyHandlerRegistry
  10. .addHandler(HitPolicy.PRIORITY, null, new MyPriorityHitPolicyHandler());

Change default expression languages

A DMN decision table has multiple expressions which are evaluated when the table is executed.The default expression language for every expression type can be configured.The following expression types exist:

  • Input Expression: Used to specify the input of a column in a decisiontable. The default language for input expressions in the DMN engine isJUEL.
  • Input Entry: Used to specify the condition of a rule in a decisiontable. The default language for input entries in the DMN engine isFEEL.
  • Output Entry: Used to specify the output of a rule in a decisiontable. The default language for output entries in the DMN engine isJUEL.
    The default expression language of a DMN decision literal expression can also be configured, the default in the DMN engine is JUEL.

Read more about the default expressions in the corresponding section.

It is possible to change the default expression language on the DMN engine configuration:

  1. DefaultDmnEngineConfiguration configuration = (DefaultDmnEngineConfiguration) DmnEngineConfiguration
  2. .createDefaultDmnEngineConfiguration();
  3. configuration
  4. .setDefaultInputExpressionExpressionLanguage("javascript");

Please note that the chosen language must be available in the classpath. Bydefault JUEL and FEEL are available. The default FEEL implementationis only supported for input entries.

If the JDK includes a JavaScriptimplementation like Rhino or Nashorn, then javascript is available as well.

It is also possible to use other script languages like groovy, python or ruby.Just make sure that the corresponding libraries are available on the classpath at runtime.

Customize Expression and Script Resolving

The default DMN engine resolves the supported expression and script languagesusing different providers.

To evaluate JUEL expressions, the DMN engine uses the ElProvider configured in theDMN engine configuration. To use another implementation of the Unified Expression Language, replace this implementation.

  1. // with a default DMN engine configuration
  2. DefaultDmnEngineConfiguration configuration = (DefaultDmnEngineConfiguration) DmnEngineConfiguration
  3. .createDefaultDmnEngineConfiguration();
  4. // set a custom el provider
  5. configuration.setElProvider(new MyElProvider());

To configure the FEEL engine used you can provide a custom FeelEngineFactory.

  1. // with a default DMN engine configuration
  2. DefaultDmnEngineConfiguration configuration = (DefaultDmnEngineConfiguration) DmnEngineConfiguration
  3. .createDefaultDmnEngineConfiguration();
  4. // set a custom feel engine factory
  5. configuration.setFeelEngineFactory(new MyFeelEngineFactory());

Script languages are resolved by the DmnScriptEngineResolver. To customize the script engine resolving, provide an own implementation.

  1. // with a default DMN engine configuration
  2. DefaultDmnEngineConfiguration configuration = (DefaultDmnEngineConfiguration) DmnEngineConfiguration
  3. .createDefaultDmnEngineConfiguration();
  4. // set custom script engine resolver
  5. configuration.setScriptEngineResolver(new MyScriptEngineResolver());

Logging

The DMN engine uses SLF4J as logging API. The camunda-dmn-engine artifactdoes not have a dependency to any of the existing SLF4J backends. This means thatyou can choose which backend you want to use. One example would be LOGBack, orif you want to use Java util logging, you could use the slf4j-jdk14 artifact.For more information on how to configure and use SLF4J, please refer to theuser manual.

原文: https://docs.camunda.org/manual/7.9/user-guide/dmn-engine/embed/