use of org.kie.kogito.explainability.api.BaseExplainabilityResult in project kogito-apps by kiegroup.
the class ExplanationServiceImplTest method testCounterfactualsExplainAsyncSuccess.
@SuppressWarnings("unchecked")
void testCounterfactualsExplainAsyncSuccess(ThrowingSupplier<BaseExplainabilityResult> invocation) {
when(instance.stream()).thenReturn(Stream.of(cfExplainerServiceHandlerMock));
when(cfExplainerMock.explainAsync(any(Prediction.class), eq(predictionProviderMock), any(Consumer.class))).thenReturn(CompletableFuture.completedFuture(COUNTERFACTUAL_RESULT));
BaseExplainabilityResult result = assertDoesNotThrow(invocation);
assertNotNull(result);
assertTrue(result instanceof CounterfactualExplainabilityResult);
CounterfactualExplainabilityResult counterfactualResult = (CounterfactualExplainabilityResult) result;
assertEquals(EXECUTION_ID, counterfactualResult.getExecutionId());
assertEquals(COUNTERFACTUAL_ID, counterfactualResult.getCounterfactualId());
assertSame(ExplainabilityStatus.SUCCEEDED, counterfactualResult.getStatus());
assertNull(counterfactualResult.getStatusDetails());
assertEquals(COUNTERFACTUAL_RESULT.getEntities().size(), counterfactualResult.getInputs().size());
assertEquals(COUNTERFACTUAL_RESULT.getOutput().size(), counterfactualResult.getOutputs().size());
assertTrue(counterfactualResult.getOutputs().stream().anyMatch(o -> o.getName().equals("output1")));
NamedTypedValue value = counterfactualResult.getOutputs().iterator().next();
assertTrue(value.getValue().isUnit());
assertEquals(Double.class.getSimpleName(), value.getValue().toUnit().getType());
assertEquals(555.0, value.getValue().toUnit().getValue().asDouble());
}
use of org.kie.kogito.explainability.api.BaseExplainabilityResult in project kogito-apps by kiegroup.
the class ExplanationServiceImplTest method testLIMEExplainAsyncFailed.
@Test
@SuppressWarnings("unchecked")
void testLIMEExplainAsyncFailed() {
String errorMessage = "Something bad happened";
RuntimeException exception = new RuntimeException(errorMessage);
when(instance.stream()).thenReturn(Stream.of(limeExplainerServiceHandlerMock));
when(limeExplainerMock.explainAsync(any(Prediction.class), eq(predictionProviderMock), any(Consumer.class))).thenThrow(exception);
BaseExplainabilityResult result = assertDoesNotThrow(() -> explanationService.explainAsync(LIME_REQUEST, callbackMock).toCompletableFuture().get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit()));
assertNotNull(result);
assertTrue(result instanceof LIMEExplainabilityResult);
LIMEExplainabilityResult exceptionResult = (LIMEExplainabilityResult) result;
assertEquals(EXECUTION_ID, exceptionResult.getExecutionId());
assertSame(ExplainabilityStatus.FAILED, exceptionResult.getStatus());
assertEquals(errorMessage, exceptionResult.getStatusDetails());
}
use of org.kie.kogito.explainability.api.BaseExplainabilityResult in project kogito-apps by kiegroup.
the class LocalExplainerServiceHandler method explainAsyncWithResults.
/**
* Requests calculation of an explanation decorated with both "success" and "failure" result handlers.
* See:
* - {@link LocalExplainer#explainAsync}
* - {@link LocalExplainerServiceHandler#createSucceededResult(BaseExplainabilityRequest, Object)}
* - {@link LocalExplainerServiceHandler#createFailedResult(BaseExplainabilityRequest, Throwable)}
*
* @param request The explanation request.
* @param intermediateResultsConsumer A consumer for intermediate results provided by the explainer.
* @return
*/
default CompletableFuture<BaseExplainabilityResult> explainAsyncWithResults(R request, Consumer<BaseExplainabilityResult> intermediateResultsConsumer) {
Prediction prediction = getPrediction(request);
PredictionProvider predictionProvider = getPredictionProvider(request);
return explainAsync(prediction, predictionProvider, s -> intermediateResultsConsumer.accept(createIntermediateResult(request, s))).thenApply(input -> createSucceededResult(request, input)).exceptionally(e -> createFailedResult(request, e));
}
use of org.kie.kogito.explainability.api.BaseExplainabilityResult in project kogito-apps by kiegroup.
the class BaseExplainabilityMessagingHandlerIT method explainabilityRequestIsProcessedAndAResultMessageIsSent.
@Test
void explainabilityRequestIsProcessedAndAResultMessageIsSent() throws Exception {
BaseExplainabilityRequest request = buildRequest();
BaseExplainabilityResult result = buildResult();
when(explanationService.explainAsync(any(BaseExplainabilityRequest.class), any())).thenReturn(CompletableFuture.completedFuture(result));
kafkaClient.produce(ExplainabilityCloudEventBuilder.buildCloudEventJsonString(request), TOPIC_REQUEST);
verify(explanationService, timeout(2000).times(1)).explainAsync(any(BaseExplainabilityRequest.class), any());
final CountDownLatch countDownLatch = new CountDownLatch(1);
kafkaClient.consume(TOPIC_RESULT, s -> {
LOGGER.info("Received from kafka: {}", s);
CloudEventUtils.decode(s).ifPresent((CloudEvent cloudEvent) -> {
try {
BaseExplainabilityResult event = objectMapper.readValue(cloudEvent.getData().toBytes(), BaseExplainabilityResult.class);
assertNotNull(event);
assertResult(event);
countDownLatch.countDown();
} catch (IOException e) {
LOGGER.error("Error parsing {}", s, e);
throw new RuntimeException(e);
}
});
});
assertTrue(countDownLatch.await(5, TimeUnit.SECONDS));
kafkaClient.shutdown();
}
use of org.kie.kogito.explainability.api.BaseExplainabilityResult in project kogito-apps by kiegroup.
the class ExplainabilityApiV1IT method testEndpointWithRequest.
@Test
void testEndpointWithRequest() throws JsonProcessingException {
ModelIdentifier modelIdentifier = new ModelIdentifier("dmn", "namespace:name");
String body = MAPPER.writeValueAsString(new LIMEExplainabilityRequest(executionId, serviceUrl, modelIdentifier, Collections.emptyList(), Collections.emptyList()));
BaseExplainabilityResult result = given().contentType(ContentType.JSON).body(body).when().post("/v1/explain").as(BaseExplainabilityResult.class);
assertEquals(executionId, result.getExecutionId());
}
Aggregations