Search in sources :

Example 1 with BaseExplainabilityRequest

use of org.kie.kogito.explainability.api.BaseExplainabilityRequest in project kogito-apps by kiegroup.

the class ExplainabilityMessagingHandler method handleCloudEvent.

@SuppressWarnings("unchecked")
private CompletionStage<Void> handleCloudEvent(CloudEvent cloudEvent) {
    BaseExplainabilityRequest request = null;
    try {
        if (cloudEvent.getData() != null) {
            request = objectMapper.readValue(cloudEvent.getData().toBytes(), BaseExplainabilityRequest.class);
        }
    } catch (IOException e) {
        LOGGER.error("Unable to deserialize CloudEvent data as ExplainabilityRequest", e);
        return CompletableFuture.completedFuture(null);
    }
    if (request == null) {
        LOGGER.error("Received CloudEvent with id {} from {} with empty data", cloudEvent.getId(), cloudEvent.getSource());
        return CompletableFuture.completedFuture(null);
    }
    LOGGER.info("Received CloudEvent with id {} from {}", cloudEvent.getId(), cloudEvent.getSource());
    return explanationService.explainAsync(request, this::sendEvent).thenApply(this::sendEvent);
}
Also used : BaseExplainabilityRequest(org.kie.kogito.explainability.api.BaseExplainabilityRequest) IOException(java.io.IOException)

Example 2 with BaseExplainabilityRequest

use of org.kie.kogito.explainability.api.BaseExplainabilityRequest in project kogito-apps by kiegroup.

the class BaseExplainabilityMessagingHandlerIT method explainabilityRequestIsProcessedAndAnIntermediateMessageIsSent.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
void explainabilityRequestIsProcessedAndAnIntermediateMessageIsSent() throws Exception {
    BaseExplainabilityRequest request = buildRequest();
    BaseExplainabilityResult result = buildResult();
    doAnswer(i -> {
        Object parameter = i.getArguments()[1];
        Consumer<BaseExplainabilityResult> consumer = (Consumer) parameter;
        mockExplainAsyncInvocationWithIntermediateResults(consumer);
        return CompletableFuture.completedFuture(result);
    }).when(explanationService).explainAsync(any(BaseExplainabilityRequest.class), any());
    kafkaClient.produce(ExplainabilityCloudEventBuilder.buildCloudEventJsonString(request), TOPIC_REQUEST);
    verify(explanationService, timeout(2000).times(1)).explainAsync(any(BaseExplainabilityRequest.class), any());
    final CountDownLatch countDownLatch = new CountDownLatch(getTotalExpectedEventCountWithIntermediateResults());
    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();
}
Also used : BaseExplainabilityResult(org.kie.kogito.explainability.api.BaseExplainabilityResult) Consumer(java.util.function.Consumer) BaseExplainabilityRequest(org.kie.kogito.explainability.api.BaseExplainabilityRequest) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) CloudEvent(io.cloudevents.CloudEvent) Test(org.junit.jupiter.api.Test)

Example 3 with BaseExplainabilityRequest

use of org.kie.kogito.explainability.api.BaseExplainabilityRequest in project kogito-apps by kiegroup.

the class TrustyServiceTest method doGivenStoredExecutionWhenCounterfactualRequestIsMadeThenExplainabilityEventIsEmittedTest.

@SuppressWarnings("unchecked")
void doGivenStoredExecutionWhenCounterfactualRequestIsMadeThenExplainabilityEventIsEmittedTest(CounterfactualDomain domain) {
    Storage<String, Decision> decisionStorage = mock(Storage.class);
    Storage<String, CounterfactualExplainabilityRequest> counterfactualStorage = mock(Storage.class);
    ArgumentCaptor<BaseExplainabilityRequest> explainabilityEventArgumentCaptor = ArgumentCaptor.forClass(BaseExplainabilityRequest.class);
    when(decisionStorage.containsKey(eq(TEST_EXECUTION_ID))).thenReturn(true);
    when(trustyStorageServiceMock.getDecisionsStorage()).thenReturn(decisionStorage);
    when(trustyStorageServiceMock.getCounterfactualRequestStorage()).thenReturn(counterfactualStorage);
    when(decisionStorage.get(eq(TEST_EXECUTION_ID))).thenReturn(TrustyServiceTestUtils.buildCorrectDecision(TEST_EXECUTION_ID));
    // The Goals structures must be comparable to the original decisions outcomes.
    // The Search Domain structures must be identical those of the original decision inputs.
    trustyService.requestCounterfactuals(TEST_EXECUTION_ID, List.of(new NamedTypedValue("Fine", new StructureValue("tFine", Map.of("Amount", new UnitValue("number", "number", new IntNode(0)), "Points", new UnitValue("number", "number", new IntNode(0))))), new NamedTypedValue("Should the driver be suspended?", new UnitValue("string", "string", new TextNode("No")))), List.of(new CounterfactualSearchDomain("Violation", new CounterfactualSearchDomainStructureValue("tViolation", Map.of("Type", new CounterfactualSearchDomainUnitValue("string", "string", true, domain), "Actual Speed", new CounterfactualSearchDomainUnitValue("number", "number", true, domain), "Speed Limit", new CounterfactualSearchDomainUnitValue("number", "number", true, domain)))), new CounterfactualSearchDomain("Driver", new CounterfactualSearchDomainStructureValue("tDriver", Map.of("Age", new CounterfactualSearchDomainUnitValue("number", "number", true, domain), "Points", new CounterfactualSearchDomainUnitValue("number", "number", true, domain))))));
    verify(explainabilityRequestProducerMock).sendEvent(explainabilityEventArgumentCaptor.capture());
    BaseExplainabilityRequest event = explainabilityEventArgumentCaptor.getValue();
    assertNotNull(event);
    assertTrue(event instanceof CounterfactualExplainabilityRequest);
    CounterfactualExplainabilityRequest request = (CounterfactualExplainabilityRequest) event;
    assertEquals(TEST_EXECUTION_ID, request.getExecutionId());
}
Also used : CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) TextNode(com.fasterxml.jackson.databind.node.TextNode) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Decision(org.kie.kogito.trusty.storage.api.model.decision.Decision) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) IntNode(com.fasterxml.jackson.databind.node.IntNode) BaseExplainabilityRequest(org.kie.kogito.explainability.api.BaseExplainabilityRequest) StructureValue(org.kie.kogito.tracing.typedvalue.StructureValue) CounterfactualSearchDomainStructureValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainStructureValue) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain) CounterfactualSearchDomainStructureValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainStructureValue)

Example 4 with BaseExplainabilityRequest

use of org.kie.kogito.explainability.api.BaseExplainabilityRequest 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));
}
Also used : Consumer(java.util.function.Consumer) BaseExplainabilityRequest(org.kie.kogito.explainability.api.BaseExplainabilityRequest) LocalExplainer(org.kie.kogito.explainability.local.LocalExplainer) Prediction(org.kie.kogito.explainability.model.Prediction) CompletableFuture(java.util.concurrent.CompletableFuture) BaseExplainabilityResult(org.kie.kogito.explainability.api.BaseExplainabilityResult) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) Prediction(org.kie.kogito.explainability.model.Prediction) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider)

Example 5 with BaseExplainabilityRequest

use of org.kie.kogito.explainability.api.BaseExplainabilityRequest 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();
}
Also used : BaseExplainabilityResult(org.kie.kogito.explainability.api.BaseExplainabilityResult) BaseExplainabilityRequest(org.kie.kogito.explainability.api.BaseExplainabilityRequest) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) CloudEvent(io.cloudevents.CloudEvent) Test(org.junit.jupiter.api.Test)

Aggregations

BaseExplainabilityRequest (org.kie.kogito.explainability.api.BaseExplainabilityRequest)6 IOException (java.io.IOException)3 Test (org.junit.jupiter.api.Test)3 BaseExplainabilityResult (org.kie.kogito.explainability.api.BaseExplainabilityResult)3 IntNode (com.fasterxml.jackson.databind.node.IntNode)2 TextNode (com.fasterxml.jackson.databind.node.TextNode)2 CloudEvent (io.cloudevents.CloudEvent)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Consumer (java.util.function.Consumer)2 CounterfactualExplainabilityRequest (org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest)2 CounterfactualSearchDomain (org.kie.kogito.explainability.api.CounterfactualSearchDomain)2 CounterfactualSearchDomainStructureValue (org.kie.kogito.explainability.api.CounterfactualSearchDomainStructureValue)2 CounterfactualSearchDomainUnitValue (org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue)2 NamedTypedValue (org.kie.kogito.explainability.api.NamedTypedValue)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 OffsetDateTime (java.time.OffsetDateTime)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1