Search in sources :

Example 1 with DMNResultWithExplanation

use of org.kie.kogito.jitexecutor.dmn.responses.DMNResultWithExplanation in project kogito-apps by kiegroup.

the class JITDMNServiceImpl method evaluateModelAndExplain.

public DMNResultWithExplanation evaluateModelAndExplain(DMNEvaluator dmnEvaluator, Map<String, Object> context) {
    LocalDMNPredictionProvider localDMNPredictionProvider = new LocalDMNPredictionProvider(dmnEvaluator);
    DMNResult dmnResult = dmnEvaluator.evaluate(context);
    Prediction prediction = new SimplePrediction(LocalDMNPredictionProvider.toPredictionInput(context), LocalDMNPredictionProvider.toPredictionOutput(dmnResult));
    LimeConfig limeConfig = new LimeConfig().withSamples(explainabilityLimeSampleSize).withPerturbationContext(new PerturbationContext(new Random(), explainabilityLimeNoOfPerturbation));
    LimeExplainer limeExplainer = new LimeExplainer(limeConfig);
    Map<String, Saliency> saliencyMap;
    try {
        saliencyMap = limeExplainer.explainAsync(prediction, localDMNPredictionProvider).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
    } catch (TimeoutException | InterruptedException | ExecutionException e) {
        if (e instanceof InterruptedException) {
            LOGGER.error("Critical InterruptedException occurred", e);
            Thread.currentThread().interrupt();
        }
        return new DMNResultWithExplanation(new JITDMNResult(dmnEvaluator.getNamespace(), dmnEvaluator.getName(), dmnResult), new SalienciesResponse(EXPLAINABILITY_FAILED, EXPLAINABILITY_FAILED_MESSAGE, null));
    }
    List<SaliencyResponse> saliencyModelResponse = buildSalienciesResponse(dmnEvaluator.getDmnModel(), saliencyMap);
    return new DMNResultWithExplanation(new JITDMNResult(dmnEvaluator.getNamespace(), dmnEvaluator.getName(), dmnResult), new SalienciesResponse(EXPLAINABILITY_SUCCEEDED, null, saliencyModelResponse));
}
Also used : SimplePrediction(org.kie.kogito.explainability.model.SimplePrediction) SalienciesResponse(org.kie.kogito.trusty.service.common.responses.SalienciesResponse) DMNResult(org.kie.dmn.api.core.DMNResult) JITDMNResult(org.kie.kogito.jitexecutor.dmn.responses.JITDMNResult) PerturbationContext(org.kie.kogito.explainability.model.PerturbationContext) SaliencyResponse(org.kie.kogito.trusty.service.common.responses.SaliencyResponse) LimeExplainer(org.kie.kogito.explainability.local.lime.LimeExplainer) Prediction(org.kie.kogito.explainability.model.Prediction) SimplePrediction(org.kie.kogito.explainability.model.SimplePrediction) DMNResultWithExplanation(org.kie.kogito.jitexecutor.dmn.responses.DMNResultWithExplanation) JITDMNResult(org.kie.kogito.jitexecutor.dmn.responses.JITDMNResult) Saliency(org.kie.kogito.explainability.model.Saliency) LimeConfig(org.kie.kogito.explainability.local.lime.LimeConfig) Random(java.util.Random) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with DMNResultWithExplanation

use of org.kie.kogito.jitexecutor.dmn.responses.DMNResultWithExplanation in project kogito-apps by kiegroup.

the class JITDMNServiceImplTest method testExplainability.

@Test
public void testExplainability() throws IOException {
    String allTypesModel = new String(IoUtils.readBytesFromInputStream(JITDMNResourceTest.class.getResourceAsStream("/allTypes.dmn")));
    Map<String, Object> context = new HashMap<>();
    context.put("stringInput", "test");
    context.put("listOfStringInput", Collections.singletonList("test"));
    context.put("numberInput", 1);
    context.put("listOfNumbersInput", Collections.singletonList(1));
    context.put("booleanInput", true);
    context.put("listOfBooleansInput", Collections.singletonList(true));
    context.put("timeInput", "h09:00");
    context.put("dateInput", "2020-04-02");
    context.put("dateAndTimeInput", "2020-04-02T09:00:00");
    context.put("daysAndTimeDurationInput", "P1DT1H");
    context.put("yearsAndMonthDurationInput", "P1Y1M");
    Map<String, Object> complexInput = new HashMap<>();
    complexInput.put("aNestedListOfNumbers", Collections.singletonList(1));
    complexInput.put("aNestedString", "test");
    complexInput.put("aNestedComplexInput", Collections.singletonMap("doubleNestedNumber", 1));
    context.put("complexInput", complexInput);
    context.put("listOfComplexInput", Collections.singletonList(complexInput));
    DMNResultWithExplanation response = jitdmnService.evaluateModelAndExplain(allTypesModel, context);
    Assertions.assertNotNull(response.dmnResult);
    Assertions.assertEquals(1, response.dmnResult.getDecisionResults().size());
    Assertions.assertNotNull(response.salienciesResponse);
    Assertions.assertEquals(1, response.salienciesResponse.getSaliencies().size());
    Assertions.assertEquals(17, response.salienciesResponse.getSaliencies().get(0).getFeatureImportance().size());
}
Also used : HashMap(java.util.HashMap) DMNResultWithExplanation(org.kie.kogito.jitexecutor.dmn.responses.DMNResultWithExplanation) Test(org.junit.jupiter.api.Test) JITDMNResourceTest(org.kie.kogito.jitexecutor.dmn.api.JITDMNResourceTest)

Aggregations

DMNResultWithExplanation (org.kie.kogito.jitexecutor.dmn.responses.DMNResultWithExplanation)2 HashMap (java.util.HashMap)1 Random (java.util.Random)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Test (org.junit.jupiter.api.Test)1 DMNResult (org.kie.dmn.api.core.DMNResult)1 LimeConfig (org.kie.kogito.explainability.local.lime.LimeConfig)1 LimeExplainer (org.kie.kogito.explainability.local.lime.LimeExplainer)1 PerturbationContext (org.kie.kogito.explainability.model.PerturbationContext)1 Prediction (org.kie.kogito.explainability.model.Prediction)1 Saliency (org.kie.kogito.explainability.model.Saliency)1 SimplePrediction (org.kie.kogito.explainability.model.SimplePrediction)1 JITDMNResourceTest (org.kie.kogito.jitexecutor.dmn.api.JITDMNResourceTest)1 JITDMNResult (org.kie.kogito.jitexecutor.dmn.responses.JITDMNResult)1 SalienciesResponse (org.kie.kogito.trusty.service.common.responses.SalienciesResponse)1 SaliencyResponse (org.kie.kogito.trusty.service.common.responses.SaliencyResponse)1