Skip to content

Latest commit

 

History

History
274 lines (189 loc) · 6.75 KB

dmn.md

File metadata and controls

274 lines (189 loc) · 6.75 KB

Decision Modelling Notation (DMN)

Red Hat Decision Manager V7 implements DMN v1.1 (Compliance Level 3).

Red Hat Process Automation Manager V7 makes possible to leverage DMN in the context of a BPMN process.

Useful external source of information (Blogs and videos)

For a first introduction to the DMN here some resources:

Project dependencies

<!-- Required for the DMN runtime API -->
<dependency>
  <groupId>org.kie</groupId>
  <artifactId>kie-dmn-core</artifactId>
  <version>${drools.version}</version>
</dependency>

<!-- Required if not using classpath KIE container -->
<dependency>
  <groupId>org.kie</groupId>
  <artifactId>kie-ci</artifactId>
  <version>${drools.version}</version>
</dependency>

FEEL Language

Data Types

Basic data types: string, Boolean, Number, date, time and duration

You can express complex data types in DMN or importing XSD definitions.

E.g.

 <itemDefinition label="tBid" name="tBid">
    <itemComponent id="..." name="id">
      <typeRef>feel:string</typeRef>
    </itemComponent>
    <itemComponent id="..." name="quotation">
      <typeRef>feel:number</typeRef>
    </itemComponent>
    <itemComponent id="..." name="deliveryTime">
      <typeRef>feel:number</typeRef>
    </itemComponent>
  </itemDefinition>
  <itemDefinition isCollection="true" label="tBids" name="tBids">
    <typeRef>tBid</typeRef>
  </itemDefinition>
  <inputData id="..." name="Suppliers bids">
    <variable id="..." name="Suppliers bids" typeRef="tBids"/>
  </inputData>

In the previous example there are:

  1. tBid type definition:

    • id - string
    • quotation - number
    • deliveryTime - number
  2. tBids is a collection of tBid

  3. Suppliers bids is a variable defined as tBids

Feel annotation

To help the DMN engine to map the Java fields in the FEEL properties:

@FEELProperty("initial rating")
public double getInitialRating() {
    return initialRating;
}

Path Expressions

FEEL is able to query data in a sophisticated way with constructs that recall closely XPATH 2.0.

Dot to navigate the data structure

As many other languages the dot (".") is used to access to the nested data structure (JavaScript, Python).

E.g.

Let's consider the following variable Bid which type was described before:

<variable id="..." name="Bid" typeRef="tBid"/>

Bid.id retrieve the id

But if the type is a collection, the result is a collection with all the ids.

Example of Suppliers Bids:

id quotation deliveryTime
1 100 2
2 120 1
3 80 4

In this case the expression Suppliers Bids.id retrieve a list of values:

id
1
2
3
Suppliers bids[ quotation = lower quotation].id

List functions

This functions applies to lists.

There are functions that summarize the data: count, min, max, sum, mean, and, or, list contains.

E.g.

min(Suppliers bids.quotation)

Other list function manipulate the list.

Name(parameters) Parameter Domain Description Example
count(list) list return size of list count([1,2,3]) = 3
list contains(list, element) list, any element of the semantic domain including null does the list contain the element? list contains([1,2,3], 2) = true
min(list)
min(c1 ,..., cN ), N>1
max(list)
max(c1 ,..., cN ), N>1
(list of) comparable items return minimum(maximum) item min([1,2,3]) = 1
min(1,2,3) = 1
max([1,2,3]) = 3
max(1,2,3) = 3

Boxed Function implemented in Java

A Boxed Function Definition is the way to define function in DMN. In the Trisotech editor, it is "Function (Expression)".

There are 3 possible implementations (kinds): FEEL, PMML and Java.

This is the way to define a Java function:

dmn_boxed_function

Special character "?" for Decision Table expression

In a decision table cell, it's possible to write a complex FEEL expression and use the special character "?" (placeholder) to reference the column value.

E.g.

list contains(?, "A")

Time handling

FEEL extension to evaluate time:

  • now() : Returns the current local date and time.

  • today() : Returns the current local date.

Warning Using this functions in different point of a decision could give different results base on the real evaluation execution time.

Duration

Subtracting two dates gives a duration:

date1 - date2

Duration operations:

days, hours, minutes, seconds

Example: days between two dates

(date1 - date2).days

Testing

In order to initialize the context in a straightforward way, leverage the helper factories: prototype and entry.

  • Dependency

    <!-- DMN test-->
    <dependency>
      <groupId>org.kie</groupId>
      <artifactId>kie-dmn-core</artifactId>
      <type>test-jar</type>
      <scope>compile</scope>
    </dependency>
  • Code example:

    import static org.kie.dmn.core.util.DynamicTypeUtils.entry;
    import static org.kie.dmn.core.util.DynamicTypeUtils.prototype;
    
    Map<String, Object> transaction1 = prototype (
                                        entry("balance", 2200),
                                        entry("amount", -500),
                                        entry("value date", LocalDateTime.now()),
                                        entry("category", "shopping")
                                       );

Listeners

It's possible to add event listeners: DMNRuntimeEventListener

https://github.com/kiegroup/drools/blob/master/kie-dmn/kie-dmn-api/src/main/java/org/kie/dmn/api/core/event/DMNRuntimeEventListener.java#L21-L31

Programmatically, add it to DMNRuntime:

dmnRuntime.addListener( new DMNRuntimeEventListener() {
  @Override
  public void afterEvaluateBKM(AfterEvaluateBKMEvent event) {
    System.out.println(">>> " + event.getResult().getContext());
  }
});

Add listeners to the kie server using the property via kmodule.xml or system property: org.kie.dmn.runtime.listeners.$NAME.

Details described here: https://issues.redhat.com/browse/DROOLS-4488

Debugging

logs (tipo org.kie=DEBUG)