Search in sources :

Example 1 with JITDMNResult

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

the class JITDMNServiceImpl method evaluateModel.

@Override
public JITDMNResult evaluateModel(String modelXML, Map<String, Object> context) {
    DMNEvaluator dmnEvaluator = DMNEvaluator.fromXML(modelXML);
    DMNResult dmnResult = dmnEvaluator.evaluate(context);
    return new JITDMNResult(dmnEvaluator.getNamespace(), dmnEvaluator.getName(), dmnResult);
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) JITDMNResult(org.kie.kogito.jitexecutor.dmn.responses.JITDMNResult) JITDMNResult(org.kie.kogito.jitexecutor.dmn.responses.JITDMNResult)

Example 2 with JITDMNResult

use of org.kie.kogito.jitexecutor.dmn.responses.JITDMNResult 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 3 with JITDMNResult

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

the class JITDMNServiceImpl method evaluateModel.

@Override
public JITDMNResult evaluateModel(MultipleResourcesPayload payload, Map<String, Object> context) {
    DMNEvaluator dmnEvaluator = DMNEvaluator.fromMultiple(payload);
    DMNResult dmnResult = dmnEvaluator.evaluate(context);
    return new JITDMNResult(dmnEvaluator.getNamespace(), dmnEvaluator.getName(), dmnResult);
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) JITDMNResult(org.kie.kogito.jitexecutor.dmn.responses.JITDMNResult) JITDMNResult(org.kie.kogito.jitexecutor.dmn.responses.JITDMNResult)

Example 4 with JITDMNResult

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

the class JITDMNResource method jitdmn.

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response jitdmn(JITDMNPayload payload) {
    JITDMNResult evaluateAll = payload.getModel() != null ? jitdmnService.evaluateModel(payload.getModel(), payload.getContext()) : jitdmnService.evaluateModel(payload, payload.getContext());
    Map<String, Object> restResulk = new HashMap<>();
    for (Entry<String, Object> kv : evaluateAll.getContext().getAll().entrySet()) {
        restResulk.put(kv.getKey(), MarshallingStubUtils.stubDMNResult(kv.getValue(), String::valueOf));
    }
    return Response.ok(restResulk).build();
}
Also used : HashMap(java.util.HashMap) JITDMNResult(org.kie.kogito.jitexecutor.dmn.responses.JITDMNResult) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 5 with JITDMNResult

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

the class JITDMNServiceImplTest method testModelEvaluation.

@Test
public void testModelEvaluation() {
    Map<String, Object> context = new HashMap<>();
    context.put("FICO Score", 800);
    context.put("DTI Ratio", .1);
    context.put("PITI Ratio", .1);
    JITDMNResult dmnResult = jitdmnService.evaluateModel(model, context);
    Assertions.assertEquals("xls2dmn", dmnResult.getModelName());
    Assertions.assertEquals("xls2dmn_741b355c-685c-4827-b13a-833da8321da4", dmnResult.getNamespace());
    Assertions.assertTrue(dmnResult.getMessages().isEmpty());
    Assertions.assertEquals("Approved", dmnResult.getDecisionResultByName("Loan Approval").getResult());
}
Also used : HashMap(java.util.HashMap) JITDMNResult(org.kie.kogito.jitexecutor.dmn.responses.JITDMNResult) Test(org.junit.jupiter.api.Test) JITDMNResourceTest(org.kie.kogito.jitexecutor.dmn.api.JITDMNResourceTest)

Aggregations

JITDMNResult (org.kie.kogito.jitexecutor.dmn.responses.JITDMNResult)6 HashMap (java.util.HashMap)3 DMNResult (org.kie.dmn.api.core.DMNResult)3 Test (org.junit.jupiter.api.Test)2 QuarkusTest (io.quarkus.test.junit.QuarkusTest)1 Random (java.util.Random)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Produces (javax.ws.rs.Produces)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)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 JITDMNPayload (org.kie.kogito.jitexecutor.dmn.requests.JITDMNPayload)1