How to get failed and passed rules in Camunda DMN

Hi
I have DMN decision table with multiple attributes. When I evaluate decision table, I want to know which rules are passed and which rules are failed. This is required to give the recommendation to user to update their selection. Can you please help me how to achieve this in Camunda DMN?

You need to register the implementation of DmnDecisionTableEvaluationListener to the DmnEngineConfiguration like below:

DmnEngineConfiguration configuration = DmnEngineConfiguration
    .createDefaultDmnEngineConfiguration();

// instantiate the listener
DmnDecisionTableEvaluationListener myListener = new DecisionTablePostEvaluationListener();

// notify after default listeners
configuration.getCustomPostDecisionTableEvaluationListeners()
  .add(myListener);

DmnDecisionTableEvaluationListener implementation example:

public class DecisionTablePostEvaluationListener implements DmnDecisionTableEvaluationListener {

  @Override
  public void notify(DmnDecisionTableEvaluationEvent decisionTableEvaluationEvent) {
    /* TODO: Decision table post evaluation listener can be executed based on configuration */
    List<DecisionTableEvaluation> decisionInputKeyValues = new ArrayList<>();
    List<DecisionTableEvaluation> decisionOutputKeyValues = new ArrayList<>();
    Map<String, List<DecisionTableEvaluation>> decisionEvaluationKeyValueMap = new HashMap<>();
    log.info("DecisionLogic:{}", decisionTableEvaluationEvent.getDecision().getDecisionLogic());
    Optional<List<DmnEvaluatedInput>> optionalDmnEvaluatedInput = Optional
        .ofNullable(decisionTableEvaluationEvent.getInputs());
    if (optionalDmnEvaluatedInput.isPresent() && CollectionUtils.isNotEmpty(optionalDmnEvaluatedInput.get())) {
      optionalDmnEvaluatedInput.get()
          .forEach(dmnEvaluatedInput -> decisionInputKeyValues
              .add(new DecisionTableEvaluation(dmnEvaluatedInput.getInputVariable(),
                  dmnEvaluatedInput.getValue().getType().getName(), dmnEvaluatedInput.getValue().getValue())));
      Optional<List<DmnEvaluatedDecisionRule>> optionalDmnEvaluatedDecisionRule = Optional
          .ofNullable(decisionTableEvaluationEvent.getMatchingRules());
      if (optionalDmnEvaluatedDecisionRule.isPresent()
          && CollectionUtils.isNotEmpty(optionalDmnEvaluatedDecisionRule.get())) {
        optionalDmnEvaluatedDecisionRule.get()
            .forEach(dmnEvaluatedDecisionRule -> dmnEvaluatedDecisionRule.getOutputEntries().entrySet()
                .forEach(dmnEvaluatedOutput -> decisionOutputKeyValues.add(new DecisionTableEvaluation(
                    dmnEvaluatedOutput.getKey(), dmnEvaluatedOutput.getValue().getValue().getType().getName(),
                    dmnEvaluatedOutput.getValue().getValue().getValue()))));
      }
    }
    decisionEvaluationKeyValueMap.put("decisioninputs", decisionInputKeyValues);
    decisionEvaluationKeyValueMap.put("decisionoutputs", decisionOutputKeyValues);
    log.info("DecisionEvaluation: {}",
        new Gson().toJson(decisionEvaluationKeyValueMap, new TypeToken<Map<String, List<DecisionTableEvaluation>>>() {
        }.getType()));
  }

}

Note: You can implement it according to your requirement in similar way.

Thank you for your quick response. I will try this logic. But this logic goes in listener class while I need this result in main class where I call the evaluateDecisionTable. If there is a way to add listener for each evaluation, then it can be solved. But this listener is common to all evaluation.

Hi, I would like to use this DmnDecisionTableEvaluationListener to get fired rules. Is there a way that i could register this for all DMNs being invoked in Camunda instance. Lets say i have 4 DMN’s spread across different BPMN’s and i would like to get fired rules for all those DMN’s whenever they are invoked. Is there a way to register this evaluationListener across the camunda instance?

@Varun_k yes. below configuration configures at the Dmn Engine itself. It will be applicable for all the dmns being executed

DmnEngineConfiguration configuration = DmnEngineConfiguration
    .createDefaultDmnEngineConfiguration();

// instantiate the listener
DmnDecisionTableEvaluationListener myListener = new DecisionTablePostEvaluationListener();

// notify after default listeners
configuration.getCustomPostDecisionTableEvaluationListeners()
  .add(myListener);

Thank you for reply @aravindhrs . I would like to check and see if there is a way to retrieve these failed and passed rules from database. Will camunda store these details in ACT tables somewhere.

And going back to java impl of getCustomPostDecisionTableEvaluationListeners, how do i add this custom listener. Do i need to create class and just add it to project and this listener will be registered when any dmn evaluation happens? I am confused on how camunda knows about this custom listener

Refer this docs: Embedding the DMN Engine | docs.camunda.org