Blog Posts BPMN DMN

DMN Types from Java Classes

Blog: Drools & jBPM Blog

Drawing a real case DMN asset may become a time-consuming activity. In some domains, the possible types involved in DMN logic can explode into dozens or even hundreds of possible involved objects.
Although a well-designed UI can support users to define your domain object type in a simpler and faster way possible, other alternative strategies are a practical choice for the designer, in cases where a large domain is in place.

This is one of the reasons, we designed and implemented a new feature in the DMN Editor: Importing Java Beans and translating them as DMN Type.

Suppose you are a regular reader of the KIE Blog. In that case, you should already know our tooling editors offer developer-centric features, which place the developer in the center of the feature experience. In a few words, the scope of this goal relies on task automation, testing management, and reuse.

The Import of Java Classes feature is one of these kinds of functionalities, and in detail:

In this post, we will guide you in using this new feature!

Tutorial

Here are the required elements for this guide:

However, I strongly suggest using the latest versions of the above elements.

Project Setup

Let’s start laying the foundation of our project. Let’s consider a Kogito-based project present in our kogito-examples repository. Our pick-up is the dmn-quarkus-example, which already contains DMN assets. So, clone that module in your local. The next step is to add some Java Bean classes, which describe your own domain. In our case, we will use Book and Author classes.

Our dmn-quarkus-example project

An important point to highlight is your Java Classes must be Java Beans and, in detail, they must have public getters of their internal fields. This means if a field of your Java Class is private or without a public getter method, our functionality will be not able to find that field. An alternative is to set that field with the public identifier, but we strongly recommend referring to the Java Beans specification to use this feature.

The second point to analyze is the required Activator Class (no matter the class name). This is a class that contains a @KieActivatorcode>@KieActivator</code annotation, and it’s essential for the correct behavior of the functionality. This annotation is available in the latest version of Kogito, that’s the reason we chose (and recommend) to use a kogito-based project. In case you need to directly import the annotation, just import in your maven project the kie-api dependency, with at least version 8.22.0.Beta.

The Activator class

The Activator class must be composed exactly in that way, an empty Java Class with @KieActivator annotation. Any additional code can affect negatively the feature behavior.
Why are these requisites mandatory? Let’s go deeper to see how it works under the hood.

The Java Completion Extension

In the last months, we designed and implemented an extension of Language Support for Java(TM) by Red Hat VSCode plugin. As you can imagine, the main aim of that plugin is to provide language support for Java class files in VSCode. It provides, for instance, the Code completion feature when a user starts to type a Class in the editor.
Our extension leverages that plugin and that Code Completion feature to retrieve Java Classes names and field names and eventually translated them to DMN types.

Java Completion Extention Architecture

In particular, the extension is composed by:

Template used to simulate a Code Completion request

Considering our example, that template is filled with the parameter shown in the below screenshot:

The template filled with our example parameters

As you noticed, the Code Completion correctly suggests the Book class, and this represents the output of the API call too.
Please note that this logic doesn’t actually modify the user-defined Activator class file, but it simply sends a request using LSP protocol using the Eclipse JDT.LS library, which is implemented by the Language Support for Java(TM) by Red Hat.

Importing your Java Classes

At this point, we are ready to Import our Java classes as DMN Type, let’s start!
Open your DMN model, in our case our reference is the TrafficViolation.dmn file already available in the example project. Go to the "Data Types" tab. Here, you should notice a new button, Import Java Classes.
Be aware that this button is enabled only if the previously described requirements are satisfied.

Import Java classes button in Data Types section

After pressing the button, a pop will appear. Here, you can start to type the name of the class you need to import as DMN type. Let’s type "Book".

Selecting Book class

As a result of the query (and of the Code Completion behind the scenes), you should see the com.Book class as the query result. Select it and press the "Next" button.

Managing the fields of the Book class

In this step, you should expect to see all the fields of the com.Book class, with their own DMN type (eg. Book is a structure, name a string, numberOfCopies a number) Let’s pay attention to the first field, author. That field type is another user-defined Java class, com.Author. If you want to import that class too, just press the "Fetch "Author" class".

Importing both Author and Book classes

As a result, both com.Author and com.Book classes with their own fields are present in the list. Note that author field’s type is now Author.
Time to land on the last step, the review one. Here, we should simply review if the selected classes are consistent with our expectations. We are definitely ready to import these DMN Type, so press the "Import" straightaway!

Java classes imported as DMN Types

And voila! com.Author and com.Book Java classes have been translated to Author and Book DMN types, ready to be used in your DMN logic.

Conclusion

In this article, we showed how to use our new "Import Java Classes" feature to import your domain Java classes as DMN Types, a useful alternative to importing your domain types.

Thank you for reading!

The post DMN Types from Java Classes appeared first on KIE Community.

Leave a Comment

Get the BPI Web Feed

Using the HTML code below, you can display this Business Process Incubator page content with the current filter and sorting inside your web site for FREE.

Copy/Paste this code in your website html code:

<iframe src="https://www.businessprocessincubator.com/content/dmn-types-from-java-classes/?feed=html" frameborder="0" scrolling="auto" width="100%" height="700">

Customizing your BPI Web Feed

You can click on the Get the BPI Web Feed link on any of our page to create the best possible feed for your site. Here are a few tips to customize your BPI Web Feed.

Customizing the Content Filter
On any page, you can add filter criteria using the MORE FILTERS interface:

Customizing the Content Filter

Customizing the Content Sorting
Clicking on the sorting options will also change the way your BPI Web Feed will be ordered on your site:

Get the BPI Web Feed

Some integration examples

BPMN.org

XPDL.org

×