Search in sources :

Example 1 with CounterfactualExplainabilityResult

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

the class CounterfactualExplainabilityResultsManagerDuplicatesTest method testPurgeWhenResultSetSizeIsGreaterThanMinimum_WithDuplicateInputs.

@Test
public void testPurgeWhenResultSetSizeIsGreaterThanMinimum_WithDuplicateInputs() {
    CounterfactualExplainabilityResult result0 = makeResult(0, Collections.emptyList(), Collections.emptyList());
    CounterfactualExplainabilityResult result1 = makeResult(1, Collections.emptyList(), Collections.emptyList());
    CounterfactualExplainabilityResult result2 = makeResult(2, List.of(makeCounterfactualInput("a")), Collections.emptyList());
    CounterfactualExplainabilityResult result3 = makeResult(3, List.of(makeCounterfactualInput("a")), Collections.emptyList());
    when(query.execute()).thenReturn(List.of(result0, result1, result2, result3));
    manager.purge(COUNTERFACTUAL_ID, storage);
    verify(storage).remove(anyString());
    verify(storage).remove(result3.getSolutionId());
}
Also used : CounterfactualExplainabilityResult(org.kie.kogito.explainability.api.CounterfactualExplainabilityResult) Test(org.junit.jupiter.api.Test)

Example 2 with CounterfactualExplainabilityResult

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

the class CounterfactualExplainabilityResultsManagerDuplicatesTest method testPurgeWhenResultSetSizeIsGreaterThanMinimum_WithDuplicateInputs_FinalPenultimate.

@Test
public void testPurgeWhenResultSetSizeIsGreaterThanMinimum_WithDuplicateInputs_FinalPenultimate() {
    CounterfactualExplainabilityResult result0 = makeResult(0, Collections.emptyList(), Collections.emptyList());
    CounterfactualExplainabilityResult result1 = makeResult(1, Collections.emptyList(), Collections.emptyList());
    CounterfactualExplainabilityResult result2 = makeResult(2, CounterfactualExplainabilityResult.Stage.FINAL, List.of(makeCounterfactualInput("a")), Collections.emptyList());
    CounterfactualExplainabilityResult result3 = makeResult(3, List.of(makeCounterfactualInput("a")), Collections.emptyList());
    when(query.execute()).thenReturn(List.of(result0, result1, result2, result3));
    manager.purge(COUNTERFACTUAL_ID, storage);
    verify(storage).remove(anyString());
    verify(storage).remove(result3.getSolutionId());
}
Also used : CounterfactualExplainabilityResult(org.kie.kogito.explainability.api.CounterfactualExplainabilityResult) Test(org.junit.jupiter.api.Test)

Example 3 with CounterfactualExplainabilityResult

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

the class ExplainerServiceHandlerRegistryTest method testCounterfactual_getExplainabilityResultByIdWithFinalResult.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testCounterfactual_getExplainabilityResultByIdWithFinalResult() {
    Query query = mock(Query.class);
    CounterfactualExplainabilityResult result = mock(CounterfactualExplainabilityResult.class);
    when(result.getStage()).thenReturn(CounterfactualExplainabilityResult.Stage.FINAL);
    when(storageCounterfactual.containsKey(eq(EXECUTION_ID))).thenReturn(true);
    when(storageCounterfactual.query()).thenReturn(query);
    when(query.filter(any())).thenReturn(query);
    when(query.execute()).thenReturn(List.of(result));
    CounterfactualExplainabilityResult actual = registry.getExplainabilityResultById(EXECUTION_ID, CounterfactualExplainabilityResult.class);
    verify(counterfactualExplainerServiceHandler).getExplainabilityResultById(eq(EXECUTION_ID));
    assertEquals(result, actual);
}
Also used : Query(org.kie.kogito.persistence.api.query.Query) CounterfactualExplainabilityResult(org.kie.kogito.explainability.api.CounterfactualExplainabilityResult) Test(org.junit.jupiter.api.Test)

Example 4 with CounterfactualExplainabilityResult

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

the class ExplainerServiceHandlerRegistryTest method testCounterfactual_storeExplainabilityResult.

@Test
public void testCounterfactual_storeExplainabilityResult() {
    when(storageCounterfactual.containsKey(eq(EXECUTION_ID))).thenReturn(false);
    CounterfactualExplainabilityResult result = mock(CounterfactualExplainabilityResult.class);
    registry.storeExplainabilityResult(EXECUTION_ID, result);
    verify(counterfactualExplainerServiceHandler).storeExplainabilityResult(eq(EXECUTION_ID), eq(result));
}
Also used : CounterfactualExplainabilityResult(org.kie.kogito.explainability.api.CounterfactualExplainabilityResult) Test(org.junit.jupiter.api.Test)

Example 5 with CounterfactualExplainabilityResult

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

the class ExplanationServiceImplTest method testCounterfactualsxplainAsyncFailed.

@Test
@SuppressWarnings("unchecked")
void testCounterfactualsxplainAsyncFailed() {
    String errorMessage = "Something bad happened";
    RuntimeException exception = new RuntimeException(errorMessage);
    when(instance.stream()).thenReturn(Stream.of(cfExplainerServiceHandlerMock));
    when(cfExplainerMock.explainAsync(any(Prediction.class), eq(predictionProviderMock), any(Consumer.class))).thenThrow(exception);
    BaseExplainabilityResult result = assertDoesNotThrow(() -> explanationService.explainAsync(COUNTERFACTUAL_REQUEST, callbackMock).toCompletableFuture().get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit()));
    assertNotNull(result);
    assertTrue(result instanceof CounterfactualExplainabilityResult);
    CounterfactualExplainabilityResult exceptionResult = (CounterfactualExplainabilityResult) result;
    assertEquals(EXECUTION_ID, exceptionResult.getExecutionId());
    assertSame(ExplainabilityStatus.FAILED, exceptionResult.getStatus());
    assertEquals(errorMessage, exceptionResult.getStatusDetails());
}
Also used : Consumer(java.util.function.Consumer) BaseExplainabilityResult(org.kie.kogito.explainability.api.BaseExplainabilityResult) Prediction(org.kie.kogito.explainability.model.Prediction) CounterfactualExplainabilityResult(org.kie.kogito.explainability.api.CounterfactualExplainabilityResult) Test(org.junit.jupiter.api.Test)

Aggregations

CounterfactualExplainabilityResult (org.kie.kogito.explainability.api.CounterfactualExplainabilityResult)29 Test (org.junit.jupiter.api.Test)26 NamedTypedValue (org.kie.kogito.explainability.api.NamedTypedValue)8 BaseExplainabilityResult (org.kie.kogito.explainability.api.BaseExplainabilityResult)6 UnitValue (org.kie.kogito.tracing.typedvalue.UnitValue)6 IntNode (com.fasterxml.jackson.databind.node.IntNode)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Consumer (java.util.function.Consumer)4 CounterfactualExplainabilityRequest (org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest)4 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)3 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)3 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 CounterfactualSearchDomain (org.kie.kogito.explainability.api.CounterfactualSearchDomain)3 ExplainabilityStatus (org.kie.kogito.explainability.api.ExplainabilityStatus)3 ModelIdentifier (org.kie.kogito.explainability.api.ModelIdentifier)3 Prediction (org.kie.kogito.explainability.model.Prediction)3 Query (org.kie.kogito.persistence.api.query.Query)3 BooleanNode (com.fasterxml.jackson.databind.node.BooleanNode)2