Programmatically create decisions tables and run cases against it

Hi, I need to create decision tables in back-end, deploy them and test cases against the decision table. I am stuck on which services to use. Can you please help me in how to go about it?

which services to use

Which API are you planning to use? The REST API?

What have you tried so far?

I was thinking of creating the decision table using flowable-ui-modeler. Then deploying it using DMN rest API. But I am new to flowable and JAVA . So I am not sure if am going in the right path. I am open to different implementations. Please help.

Hi.
It really depends on the use case.
Do you want / need to create te decision tables programatically? Or can they be created separately in using the Modeler?
With ‘test cases’ do you mean using unit tests?
Is there requirement to deploy via REST or can I it also be deploying using the Java services? (F.e. during bootup)

Regards,

Yvo

Hi thank you for the quick reply, yes I want to create decision tables programatically. No they cannot be created separately using the Modeler.
By test cases I mean, I have to execute decision table against set of input. No I don’t mean unit tests.
I can deploy using either, REST or Java services. What do you recommend?

You can create the decision (/ decision table) programatically by constructing a org.flowable.dmn.model.DmnDefinition.
That can be used when creating a org.flowable.dmn.api.DmnDeploymentBuilder (addDmnModel) and deploy the decision.
The deployed decision can be executed using one of the execute methods of org.flowable.dmn.api.DmnRuleService.

This way you can construct the Decision Table … deploy it … and control the input variables in code.

Tip: Take a look at the unit tests to see some examples how to execute decision tables and handle the results;

Hope this helps.

Regards,

Yvo

HI yvo, thanks for the quick reply and help. I have created the DmnDefinition from the json using flowable.editor.dmn.converter.DmnJsonConverter. I would however require some help in deploying it using org.flowable.dmn.api.DmnDeploymentBuilder (addDmnModel). I am not understanding how to use it. Please help in this case.
Thanks

Hi.

Did you take a look at the unit tests I mentioned in my previous reply? There is code there that does similar stuff.

But; you could something like this;

dmnRepositoryService.createDeployment().name("exampleDeployment")
        .addDmnModel("exampleDecision", dmnDefinition)
        .deploy();

I hope this helps.

Regards,

Yvo

HI yvo,
Thanks for your help. I followed the steps you suggested and the deployment was made. But there was no entry created in ACT_DMN_DECISION_TABLE and hence I am not able to execute the decision because it needs executes with the help of key. An entry is made in the models ACTACT_DMN_DEPLOYMENT and ACT_DMN_DEPLOYMENT_RESOURCE but not in ACT_DMN_DEPLOYMENT_RESOURCE. Do you have any idea about that ?

Thanks again.

Screenshot%20from%202019-01-09%2013-23-27

This is my DB entry for ACT_DMN_DEPLOYMENT_RESOURCE which has a deployed decision with id : decisionnew2.

But when I try to execute it using dmnRuleService, it is unable to find the decision.
Code I have written:
DmnRuleService dmnRuleService = dmnEngine.getDmnRuleService();
Map<String, Object> inputVariables = new HashMap<>();
inputVariables.put(“date1”, new Date());
inputVariables.put(“date2”, new Date());
Map<String, Object> result = dmnRuleService.createExecuteDecisionBuilder()
.decisionKey(“decisionnew2”).tenantId(“abcd”)
.variables(inputVariables)
.executeWithSingleResult();

It is giving the following exception
org.flowable.common.engine.api.FlowableObjectNotFoundException: no decisions deployed with key ‘decisionnew2’ for tenant identifier ‘abcd’

Does the dmnRuleService look in some other table to execute the result ?
Please have a look and help.

Did you open a seperate topic for this?
Please keep it contained in one thread.

https://forum.flowable.org/t/unable-to-execute-decisions-using-dmnruleservice/3201

How did you deploy the table? And is the tenant correct?

Regards,

Yvo

Yes I opened a separate topic. Yes I will close that one.

I deployed it by writing the following code :

DmnDeploymentdmnDeployment=dmnRepositoryService.createDeployment().name(“newdeployment2”).tenantId(“abcd”).addDmnModel(“dmn_example2”, dmnDefinition).deploy();

Yes the tenant is correct. Also I had tried deploying without tenant also and then executing also without teant. It gave me the same error.

Also I tried dmnRepositoryService.createDecisionTableQuery().list() . It gave an empty list.
I don’t understand the entries are there in ACT_DMN_DEPLOYMENT and ACT_DMN_DEPLOYMENT_RESOURCE but not in ACT_DMN_DECISION_TABLE. I thought entries are created in all these models after deployment.

Could you share the complete Java code so I can check it for errors / completeness (and not pseudo code)?

So; the way you read the JSON; create the DmnDefinition; deploy and execute.

Sure, following is the code:

ObjectMapper objectMapper = new ObjectMapper();
try {
JsonNode decisionTableNode = objectMapper.readTree("{\n" +
" “id”: “8dd36898-4c56-11e7-9d73-a26608126000”,\n" +
" “modelVersion”: “2”,\n" +
" “name”: “Dates134”,\n" +
" “key”: “decisionnew2”,\n" +
" “hitIndicator”: “FIRST”,\n" +
" “inputExpressions”: [\n" +
" {\n" +
" “id”: “1”,\n" +
" “variableId”: “date1”,\n" +
" “variableType”: null,\n" +
" “type”: “date”,\n" +
" “label”: “Date One”,\n" +
" “entries”: [],\n" +
" “newVariable”: false\n" +
" },\n" +
" {\n" +
" “id”: “3”,\n" +
" “variableId”: “date2”,\n" +
" “variableType”: null,\n" +
" “type”: “date”,\n" +
" “label”: “Date Two”,\n" +
" “entries”: [],\n" +
" “newVariable”: false\n" +
" }\n" +
" ],\n" +
" “outputExpressions”: [\n" +
" {\n" +
" “id”: “2”,\n" +
" “variableId”: “output1”,\n" +
" “variableType”: null,\n" +
" “type”: “date”,\n" +
" “label”: “Output One”,\n" +
" “entries”: [],\n" +
" “newVariable”: false\n" +
" }\n" +
" ],\n" +
" “rules”: [\n" +
" {\n" +
" “2”: “is 14/06/2017”,\n" +
" “1_operator”: “==”,\n" +
" “1_expression”: “14-06-2017”\n" +
" },\n" +
" {\n" +
" “1_operator”: “!=”,\n" +
" “1_expression”: “16-06-2017”,\n" +
" “2”: “not 16/06/2017”\n" +
" }\n" +
" ]\n" +
“}”);
DmnDefinition dmnDefinition = new DmnJsonConverter().convertToDmn(decisionTableNode,“abcd”, 1, new Date());
DmnEngine dmnEngine = DmnEngines.getDefaultDmnEngine();
DmnRepositoryService dmnRepositoryService = dmnEngine.getDmnRepositoryService();
DmnDeployment dmnDeployment = dmnRepositoryService.createDeployment().name(“newdeployment2”).tenantId(“abcd”)
.addDmnModel(“dmn_example2”, dmnDefinition)
.deploy();
DmnRuleService dmnRuleService = dmnEngine.getDmnRuleService();
Map<String, Object> inputVariables = new HashMap<>();
inputVariables.put(“date1”, new Date());
inputVariables.put(“date2”, new Date());
Map<String, Object> result = dmnRuleService.createExecuteDecisionBuilder()
.decisionKey(“decisionnew2”).tenantId(“abcd”)
.variables(inputVariables)
.executeWithSingleResult();

Hi tvo, Any update ?

Yes :wink:

I had some issue running your code because of encoding issues in your paste.

The resource name has to end with “dmn”, “dmn.xml”, “dmn11” or “.dmn11.xml”.
So f.e.

.addDmnModel(“dmn_example2.dmn”, dmnDefinition)

A naming convention for deployed resources is needed in order to differentiate them.

Let me know if this helps.

Yvo

Thank You Yvo. It worked :slight_smile: Thank you so much.

NP. Thanks for the feedback.

Hi yvo, just a quick question, How do we run flowable’s rest API’s on our local system/server.

Hi,

There are several ways to deploy and run the Flowable REST APIs.
OOTB you can run the Flowable REST web app (deploy the WAR or run the Docker container)
Or when you’re running the Flowable Task web app. The REST API’s are exposed there.

Or you can include them yourself in your custom app. F.e. using the Spring Boot starters.

Regards,

Yvo