Search in sources :

Example 16 with CounterfactualExplainabilityRequest

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

the class TrustyServiceTest method givenMultipleStoredCounterfactualRequestsWhenGetCounterfactualRequestThenIllegalArgumentExceptionIsThrown.

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
void givenMultipleStoredCounterfactualRequestsWhenGetCounterfactualRequestThenIllegalArgumentExceptionIsThrown() {
    Storage<String, CounterfactualExplainabilityRequest> counterfactualStorage = mock(Storage.class);
    CounterfactualExplainabilityRequest request1 = mock(CounterfactualExplainabilityRequest.class);
    CounterfactualExplainabilityRequest request2 = mock(CounterfactualExplainabilityRequest.class);
    Query queryMock = mock(Query.class);
    when(queryMock.filter(any(List.class))).thenReturn(queryMock);
    when(queryMock.execute()).thenReturn(List.of(request1, request2));
    when(counterfactualStorage.query()).thenReturn(queryMock);
    when(trustyStorageServiceMock.getCounterfactualRequestStorage()).thenReturn(counterfactualStorage);
    assertThrows(IllegalArgumentException.class, () -> trustyService.getCounterfactualRequest(TEST_EXECUTION_ID, TEST_COUNTERFACTUAL_ID));
}
Also used : CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) Query(org.kie.kogito.persistence.api.query.Query) List(java.util.List) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 17 with CounterfactualExplainabilityRequest

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

the class AbstractTrustyServiceIT method testCounterfactuals_StoreSingleAndRetrieveSingleWithEmptyDefinition.

@Test
public void testCounterfactuals_StoreSingleAndRetrieveSingleWithEmptyDefinition() {
    String executionId = "myCFExecution1";
    storeExecution(executionId, 0L);
    // The Goals structures must be comparable to the original decisions outcomes.
    // The Search Domain structures must be identical those of the original decision inputs.
    CounterfactualSearchDomain searchDomain = buildSearchDomainUnit("test", "number", new CounterfactualDomainRange(new IntNode(1), new IntNode(2)));
    CounterfactualExplainabilityRequest request = trustyService.requestCounterfactuals(executionId, Collections.emptyList(), Collections.singletonList(searchDomain));
    assertNotNull(request);
    assertEquals(request.getExecutionId(), executionId);
    assertNotNull(request.getCounterfactualId());
    CounterfactualExplainabilityRequest result = trustyService.getCounterfactualRequest(executionId, request.getCounterfactualId());
    assertNotNull(result);
    assertEquals(request.getExecutionId(), result.getExecutionId());
    assertEquals(request.getCounterfactualId(), result.getCounterfactualId());
}
Also used : CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) IntNode(com.fasterxml.jackson.databind.node.IntNode) CounterfactualDomainRange(org.kie.kogito.explainability.api.CounterfactualDomainRange) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain) Test(org.junit.jupiter.api.Test)

Example 18 with CounterfactualExplainabilityRequest

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

the class AbstractTrustyServiceIT method testCounterfactuals_StoreSingleAndRetrieveSingleWithSearchDomainRange.

@Test
public void testCounterfactuals_StoreSingleAndRetrieveSingleWithSearchDomainRange() {
    String executionId = "myCFExecution1";
    storeExecution(executionId, 0L);
    // The Goals structures must be comparable to the original decisions outcomes.
    // The Search Domain structures must be identical those of the original decision inputs.
    CounterfactualSearchDomain searchDomain = buildSearchDomainUnit("test", "number", new CounterfactualDomainRange(new IntNode(1), new IntNode(2)));
    CounterfactualExplainabilityRequest request = trustyService.requestCounterfactuals(executionId, Collections.emptyList(), Collections.singletonList(searchDomain));
    assertNotNull(request);
    assertEquals(request.getExecutionId(), executionId);
    assertNotNull(request.getCounterfactualId());
    assertEquals(1, request.getSearchDomains().size());
    List<CounterfactualSearchDomain> requestSearchDomains = new ArrayList<>(request.getSearchDomains());
    assertCounterfactualSearchDomainRange(searchDomain, requestSearchDomains.get(0));
    CounterfactualExplainabilityRequest result = trustyService.getCounterfactualRequest(executionId, request.getCounterfactualId());
    assertNotNull(result);
    assertEquals(request.getExecutionId(), result.getExecutionId());
    assertEquals(request.getCounterfactualId(), result.getCounterfactualId());
    assertEquals(1, result.getSearchDomains().size());
    List<CounterfactualSearchDomain> resultSearchDomains = new ArrayList<>(result.getSearchDomains());
    assertCounterfactualSearchDomainRange(searchDomain, resultSearchDomains.get(0));
}
Also used : CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) IntNode(com.fasterxml.jackson.databind.node.IntNode) CounterfactualDomainRange(org.kie.kogito.explainability.api.CounterfactualDomainRange) ArrayList(java.util.ArrayList) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain) Test(org.junit.jupiter.api.Test)

Example 19 with CounterfactualExplainabilityRequest

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

the class AbstractTrustyServiceIT method testCounterfactuals_StoreMultipleAndRetrieveAllWithEmptyDefinition.

@Test
public void testCounterfactuals_StoreMultipleAndRetrieveAllWithEmptyDefinition() {
    String executionId = "myCFExecution2";
    storeExecution(executionId, 0L);
    // The Goals structures must be comparable to the original decisions outcomes.
    // The Search Domain structures must be identical those of the original decision inputs.
    CounterfactualSearchDomain searchDomain = buildSearchDomainUnit("test", "number", new CounterfactualDomainRange(new IntNode(1), new IntNode(2)));
    CounterfactualExplainabilityRequest request1 = trustyService.requestCounterfactuals(executionId, Collections.emptyList(), Collections.singletonList(searchDomain));
    CounterfactualExplainabilityRequest request2 = trustyService.requestCounterfactuals(executionId, Collections.emptyList(), Collections.singletonList(searchDomain));
    List<CounterfactualExplainabilityRequest> result = trustyService.getCounterfactualRequests(executionId);
    assertNotNull(result);
    assertEquals(2, result.size());
    assertTrue(result.stream().anyMatch(c -> c.getCounterfactualId().equals(request1.getCounterfactualId())));
    assertTrue(result.stream().anyMatch(c -> c.getCounterfactualId().equals(request2.getCounterfactualId())));
}
Also used : CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) BeforeEach(org.junit.jupiter.api.BeforeEach) LIMEExplainabilityResult(org.kie.kogito.explainability.api.LIMEExplainabilityResult) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) FeatureImportanceModel(org.kie.kogito.explainability.api.FeatureImportanceModel) Decision(org.kie.kogito.trusty.storage.api.model.decision.Decision) DecisionInput(org.kie.kogito.trusty.storage.api.model.decision.DecisionInput) IntNode(com.fasterxml.jackson.databind.node.IntNode) CounterfactualDomainRange(org.kie.kogito.explainability.api.CounterfactualDomainRange) ArrayList(java.util.ArrayList) MatchedExecutionHeaders(org.kie.kogito.trusty.service.common.models.MatchedExecutionHeaders) Inject(javax.inject.Inject) JsonNode(com.fasterxml.jackson.databind.JsonNode) TypedValueTestUtils.buildSearchDomainUnit(org.kie.kogito.trusty.service.common.TypedValueTestUtils.buildSearchDomainUnit) ZoneOffset(java.time.ZoneOffset) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) CounterfactualDomainCategorical(org.kie.kogito.explainability.api.CounterfactualDomainCategorical) TypedValueTestUtils.buildGoalUnit(org.kie.kogito.trusty.service.common.TypedValueTestUtils.buildGoalUnit) CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) TrustyStorageService(org.kie.kogito.trusty.storage.common.TrustyStorageService) Collection(java.util.Collection) ExplainabilityStatus(org.kie.kogito.explainability.api.ExplainabilityStatus) Instant(java.time.Instant) TextNode(com.fasterxml.jackson.databind.node.TextNode) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) Test(org.junit.jupiter.api.Test) DMNModelMetadata(org.kie.kogito.trusty.storage.api.model.decision.DMNModelMetadata) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) CounterfactualDomain(org.kie.kogito.explainability.api.CounterfactualDomain) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions(org.junit.jupiter.api.Assertions) CounterfactualExplainabilityResult(org.kie.kogito.explainability.api.CounterfactualExplainabilityResult) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain) SaliencyModel(org.kie.kogito.explainability.api.SaliencyModel) Collections(java.util.Collections) DMNModelWithMetadata(org.kie.kogito.trusty.storage.api.model.decision.DMNModelWithMetadata) DecisionOutcome(org.kie.kogito.trusty.storage.api.model.decision.DecisionOutcome) IntNode(com.fasterxml.jackson.databind.node.IntNode) CounterfactualDomainRange(org.kie.kogito.explainability.api.CounterfactualDomainRange) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain) Test(org.junit.jupiter.api.Test)

Example 20 with CounterfactualExplainabilityRequest

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

the class TrustyServiceImpl method makeCounterfactualRequest.

protected CounterfactualExplainabilityRequest makeCounterfactualRequest(String executionId, List<NamedTypedValue> goals, List<CounterfactualSearchDomain> searchDomains, Long maxRunningTimeSeconds) {
    Decision decision = getDecisionById(executionId);
    // This is returned as null under Redis, so play safe
    Collection<DecisionInput> decisionInputs = Objects.nonNull(decision.getInputs()) ? decision.getInputs() : Collections.emptyList();
    if (!isStructureIdentical(decisionInputs, searchDomains)) {
        String error = buildCounterfactualErrorMessage(String.format("The structure of the Search Domains do not match the structure of the original Inputs for decision with ID %s.", executionId), "Decision inputs:-", decisionInputs, "Search domains:-", searchDomains);
        LOG.error(error);
        throw new IllegalArgumentException(error);
    }
    // This is returned as null under Redis, so play safe
    Collection<DecisionOutcome> decisionOutcomes = Objects.nonNull(decision.getOutcomes()) ? decision.getOutcomes() : Collections.emptyList();
    if (!isStructureSubset(decisionOutcomes, goals)) {
        String error = buildCounterfactualErrorMessage(String.format("The structure of the Goals is not comparable to the structure of the original Outcomes for decision with ID %s.", executionId), "Decision outcomes:-", decisionOutcomes, "Goals:-", goals);
        LOG.error(error);
        throw new IllegalArgumentException(error);
    }
    List<NamedTypedValue> cfInputs = decision.getInputs() != null ? decision.getInputs().stream().map(input -> new NamedTypedValue(input.getName(), input.getValue())).collect(Collectors.toList()) : Collections.emptyList();
    List<NamedTypedValue> cfGoals = goals != null ? goals : Collections.emptyList();
    List<CounterfactualSearchDomain> cfSearchDomains = searchDomains != null ? searchDomains : Collections.emptyList();
    return new CounterfactualExplainabilityRequest(executionId, decision.getServiceUrl(), createDecisionModelIdentifier(decision), UUID.randomUUID().toString(), cfInputs, cfGoals, cfSearchDomains, maxRunningTimeSeconds);
}
Also used : DecisionInput(org.kie.kogito.trusty.storage.api.model.decision.DecisionInput) CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) DecisionOutcome(org.kie.kogito.trusty.storage.api.model.decision.DecisionOutcome) Decision(org.kie.kogito.trusty.storage.api.model.decision.Decision) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain)

Aggregations

CounterfactualExplainabilityRequest (org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest)41 Test (org.junit.jupiter.api.Test)37 CounterfactualSearchDomain (org.kie.kogito.explainability.api.CounterfactualSearchDomain)23 IntNode (com.fasterxml.jackson.databind.node.IntNode)22 NamedTypedValue (org.kie.kogito.explainability.api.NamedTypedValue)21 CounterfactualSearchDomainUnitValue (org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue)18 CounterfactualDomainRange (org.kie.kogito.explainability.api.CounterfactualDomainRange)17 UnitValue (org.kie.kogito.tracing.typedvalue.UnitValue)17 List (java.util.List)11 CounterfactualSearchDomainStructureValue (org.kie.kogito.explainability.api.CounterfactualSearchDomainStructureValue)11 StructureValue (org.kie.kogito.tracing.typedvalue.StructureValue)10 ArrayList (java.util.ArrayList)9 ModelIdentifier (org.kie.kogito.explainability.api.ModelIdentifier)9 TextNode (com.fasterxml.jackson.databind.node.TextNode)8 CounterfactualExplainabilityResult (org.kie.kogito.explainability.api.CounterfactualExplainabilityResult)8 CounterfactualSearchDomainCollectionValue (org.kie.kogito.explainability.api.CounterfactualSearchDomainCollectionValue)8 CounterfactualPrediction (org.kie.kogito.explainability.model.CounterfactualPrediction)8 Prediction (org.kie.kogito.explainability.model.Prediction)8 CollectionValue (org.kie.kogito.tracing.typedvalue.CollectionValue)8 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7