Search in sources :

Example 6 with DecisionOutcome

use of org.kie.kogito.trusty.storage.api.model.decision.DecisionOutcome in project kogito-apps by kiegroup.

the class DecisionsApiV1IT method assertGetOutcomeByIdCorrectResponse.

private void assertGetOutcomeByIdCorrectResponse(ListStatus inputsStatus) throws Exception {
    mockServiceWithDecision(inputsStatus, ListStatus.FULL);
    DecisionOutcome response = get("/outcomes/" + TEST_OUTCOME_ID).as(DecisionOutcome.class);
    assertDecisionOutcomeResponse(buildDecisionOutcomeResponse(), response);
}
Also used : DecisionOutcome(org.kie.kogito.trusty.storage.api.model.decision.DecisionOutcome)

Example 7 with DecisionOutcome

use of org.kie.kogito.trusty.storage.api.model.decision.DecisionOutcome 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)

Example 8 with DecisionOutcome

use of org.kie.kogito.trusty.storage.api.model.decision.DecisionOutcome in project kogito-apps by kiegroup.

the class LIMESaliencyConverterTest method testFromResult_DecisionExists_WhenOutcomeNameNotFound.

@Test
public void testFromResult_DecisionExists_WhenOutcomeNameNotFound() {
    LIMEExplainabilityResult result = LIMEExplainabilityResult.buildSucceeded(EXECUTION_ID, List.of(new SaliencyModel("outcomeName1", List.of(new FeatureImportanceModel("feature1", 1.0))), new SaliencyModel("outcomeName2", List.of(new FeatureImportanceModel("feature2", 2.0)))));
    Decision decision = new Decision(EXECUTION_ID, "sourceUrl", "serviceUrl", 0L, true, "executorName", "executorModelName", "executorModelNamespace", new ArrayList<>(), new ArrayList<>());
    decision.getOutcomes().add(new DecisionOutcome("outcomeId1", "outcomeName1", ExplainabilityStatus.SUCCEEDED.name(), new UnitValue("type", new IntNode(1)), Collections.emptyList(), Collections.emptyList()));
    decision.getOutcomes().add(new DecisionOutcome("outcomeId2", "outcomeNameX", ExplainabilityStatus.SUCCEEDED.name(), new UnitValue("type2", new IntNode(2)), Collections.emptyList(), Collections.emptyList()));
    when(trustyService.getDecisionById(eq(EXECUTION_ID))).thenReturn(decision);
    SalienciesResponse response = converter.fromResult(EXECUTION_ID, result);
    assertNotNull(response);
    assertEquals(ExplainabilityStatus.SUCCEEDED.name(), response.getStatus());
    assertEquals(1, response.getSaliencies().size());
    List<SaliencyResponse> saliencyResponses = new ArrayList<>(response.getSaliencies());
    SaliencyResponse saliencyResponse1 = saliencyResponses.get(0);
    assertEquals("outcomeId1", saliencyResponse1.getOutcomeId());
    assertEquals("outcomeName1", saliencyResponse1.getOutcomeName());
    assertEquals(1, saliencyResponse1.getFeatureImportance().size());
    Optional<FeatureImportanceModel> oFeatureImportance1Model1 = saliencyResponse1.getFeatureImportance().stream().filter(fim -> fim.getFeatureName().equals("feature1")).findFirst();
    assertTrue(oFeatureImportance1Model1.isPresent());
    assertEquals(1.0, oFeatureImportance1Model1.get().getFeatureScore());
}
Also used : 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) IntNode(com.fasterxml.jackson.databind.node.IntNode) Mock(org.mockito.Mock) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) SaliencyResponse(org.kie.kogito.trusty.service.common.responses.SaliencyResponse) ArrayList(java.util.ArrayList) SalienciesResponse(org.kie.kogito.trusty.service.common.responses.SalienciesResponse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ExplainabilityStatus(org.kie.kogito.explainability.api.ExplainabilityStatus) Mockito.when(org.mockito.Mockito.when) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) Test(org.junit.jupiter.api.Test) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) SaliencyModel(org.kie.kogito.explainability.api.SaliencyModel) Collections(java.util.Collections) TrustyService(org.kie.kogito.trusty.service.common.TrustyService) DecisionOutcome(org.kie.kogito.trusty.storage.api.model.decision.DecisionOutcome) SalienciesResponse(org.kie.kogito.trusty.service.common.responses.SalienciesResponse) LIMEExplainabilityResult(org.kie.kogito.explainability.api.LIMEExplainabilityResult) SaliencyResponse(org.kie.kogito.trusty.service.common.responses.SaliencyResponse) SaliencyModel(org.kie.kogito.explainability.api.SaliencyModel) DecisionOutcome(org.kie.kogito.trusty.storage.api.model.decision.DecisionOutcome) ArrayList(java.util.ArrayList) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) Decision(org.kie.kogito.trusty.storage.api.model.decision.Decision) IntNode(com.fasterxml.jackson.databind.node.IntNode) FeatureImportanceModel(org.kie.kogito.explainability.api.FeatureImportanceModel) Test(org.junit.jupiter.api.Test)

Example 9 with DecisionOutcome

use of org.kie.kogito.trusty.storage.api.model.decision.DecisionOutcome in project kogito-apps by kiegroup.

the class TrustyServiceTest method givenStoredExecutionWhenCounterfactualRequestIsMadeThenExplainabilityEventHasCorrectPayload.

@Test
@SuppressWarnings("unchecked")
void givenStoredExecutionWhenCounterfactualRequestIsMadeThenExplainabilityEventHasCorrectPayload() {
    Storage<String, Decision> decisionStorage = mock(Storage.class);
    Storage<String, CounterfactualExplainabilityRequest> counterfactualStorage = mock(Storage.class);
    ArgumentCaptor<BaseExplainabilityRequest> explainabilityEventArgumentCaptor = ArgumentCaptor.forClass(BaseExplainabilityRequest.class);
    Decision decision = new Decision(TEST_EXECUTION_ID, TEST_SOURCE_URL, TEST_SERVICE_URL, 0L, true, null, "model", "modelNamespace", List.of(new DecisionInput("IN1", "yearsOfService", new UnitValue("integer", "integer", new IntNode(10)))), List.of(new DecisionOutcome("OUT1", "salary", "SUCCEEDED", new UnitValue("integer", "integer", new IntNode(1000)), Collections.emptyList(), Collections.emptyList())));
    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(decision);
    trustyService.requestCounterfactuals(TEST_EXECUTION_ID, List.of(new NamedTypedValue("salary", new UnitValue("integer", "integer", new IntNode(2000)))), List.of(new CounterfactualSearchDomain("yearsOfService", new CounterfactualSearchDomainUnitValue("integer", "integer", false, new CounterfactualDomainRange(new IntNode(10), new IntNode(30))))));
    verify(explainabilityRequestProducerMock).sendEvent(explainabilityEventArgumentCaptor.capture());
    BaseExplainabilityRequest event = explainabilityEventArgumentCaptor.getValue();
    CounterfactualExplainabilityRequest request = (CounterfactualExplainabilityRequest) event;
    assertEquals(TEST_EXECUTION_ID, request.getExecutionId());
    assertEquals(TEST_SERVICE_URL, request.getServiceUrl());
    // Check original input value has been copied into CF request
    assertEquals(1, request.getOriginalInputs().size());
    assertTrue(request.getOriginalInputs().stream().anyMatch(i -> i.getName().equals("yearsOfService")));
    // It is safe to use the iterator unchecked as the collection only contains one item
    assertEquals(decision.getInputs().iterator().next().getValue().toUnit().getValue().asInt(), request.getOriginalInputs().iterator().next().getValue().toUnit().getValue().asInt());
    // Check CF goals have been copied into CF request
    assertEquals(1, request.getGoals().size());
    assertTrue(request.getGoals().stream().anyMatch(g -> g.getName().equals("salary")));
    // It is safe to use the iterator unchecked as the collection only contains one item
    assertEquals(2000, request.getGoals().iterator().next().getValue().toUnit().getValue().asInt());
    // Check CF search domains have been copied into CF request
    assertEquals(1, request.getSearchDomains().size());
    assertTrue(request.getSearchDomains().stream().anyMatch(sd -> sd.getName().equals("yearsOfService")));
    // It is safe to use the iterator unchecked as the collection only contains one item
    CounterfactualSearchDomainValue searchDomain = request.getSearchDomains().iterator().next().getValue();
    assertTrue(searchDomain instanceof CounterfactualSearchDomainUnitValue);
    CounterfactualSearchDomainUnitValue unit = (CounterfactualSearchDomainUnitValue) searchDomain;
    assertFalse(unit.isFixed());
    assertNotNull(unit.getDomain());
    assertTrue(unit.getDomain() instanceof CounterfactualDomainRange);
    CounterfactualDomainRange range = (CounterfactualDomainRange) unit.getDomain();
    assertEquals(10, range.getLowerBound().asInt());
    assertEquals(30, range.getUpperBound().asInt());
    // Check Max Running Time Seconds
    assertEquals(MAX_RUNNING_TIME_SECONDS, request.getMaxRunningTimeSeconds());
}
Also used : CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) DecisionInput(org.kie.kogito.trusty.storage.api.model.decision.DecisionInput) BeforeEach(org.junit.jupiter.api.BeforeEach) LIMEExplainabilityResult(org.kie.kogito.explainability.api.LIMEExplainabilityResult) BaseExplainabilityRequest(org.kie.kogito.explainability.api.BaseExplainabilityRequest) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) CounterfactualSearchDomainValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainValue) CounterfactualDomainRange(org.kie.kogito.explainability.api.CounterfactualDomainRange) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) CounterfactualExplainerServiceHandler(org.kie.kogito.trusty.service.common.handlers.CounterfactualExplainerServiceHandler) Map(java.util.Map) CounterfactualExplainabilityResultsManagerSlidingWindow(org.kie.kogito.trusty.service.common.handlers.CounterfactualExplainabilityResultsManagerSlidingWindow) JsonNode(com.fasterxml.jackson.databind.JsonNode) Instance(javax.enterprise.inject.Instance) CounterfactualExplainabilityResultsManagerDuplicates(org.kie.kogito.trusty.service.common.handlers.CounterfactualExplainabilityResultsManagerDuplicates) CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) ExplainabilityStatus(org.kie.kogito.explainability.api.ExplainabilityStatus) UUID(java.util.UUID) 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) ExplainabilityRequestProducer(org.kie.kogito.trusty.service.common.messaging.outgoing.ExplainabilityRequestProducer) OffsetDateTime(java.time.OffsetDateTime) Stream(java.util.stream.Stream) CollectionValue(org.kie.kogito.tracing.typedvalue.CollectionValue) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) CounterfactualExplainabilityResult(org.kie.kogito.explainability.api.CounterfactualExplainabilityResult) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain) StorageImplMock(org.kie.kogito.trusty.service.common.mocks.StorageImplMock) DecisionOutcome(org.kie.kogito.trusty.storage.api.model.decision.DecisionOutcome) Mockito.mock(org.mockito.Mockito.mock) IntStream(java.util.stream.IntStream) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) 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) Query(org.kie.kogito.persistence.api.query.Query) StructureValue(org.kie.kogito.tracing.typedvalue.StructureValue) ArrayList(java.util.ArrayList) MatchedExecutionHeaders(org.kie.kogito.trusty.service.common.models.MatchedExecutionHeaders) CounterfactualSearchDomainStructureValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainStructureValue) ArgumentCaptor(org.mockito.ArgumentCaptor) ExplainerServiceHandler(org.kie.kogito.trusty.service.common.handlers.ExplainerServiceHandler) ExplainerServiceHandlerRegistry(org.kie.kogito.trusty.service.common.handlers.ExplainerServiceHandlerRegistry) Storage(org.kie.kogito.persistence.api.Storage) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) LIMEExplainerServiceHandler(org.kie.kogito.trusty.service.common.handlers.LIMEExplainerServiceHandler) TrustyStorageService(org.kie.kogito.trusty.storage.common.TrustyStorageService) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) CounterfactualDomain(org.kie.kogito.explainability.api.CounterfactualDomain) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) Assertions(org.junit.jupiter.api.Assertions) Collections(java.util.Collections) DMNModelWithMetadata(org.kie.kogito.trusty.storage.api.model.decision.DMNModelWithMetadata) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CounterfactualDomainRange(org.kie.kogito.explainability.api.CounterfactualDomainRange) DecisionOutcome(org.kie.kogito.trusty.storage.api.model.decision.DecisionOutcome) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Decision(org.kie.kogito.trusty.storage.api.model.decision.Decision) IntNode(com.fasterxml.jackson.databind.node.IntNode) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) BaseExplainabilityRequest(org.kie.kogito.explainability.api.BaseExplainabilityRequest) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) CounterfactualSearchDomainValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainValue) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain) Test(org.junit.jupiter.api.Test)

Example 10 with DecisionOutcome

use of org.kie.kogito.trusty.storage.api.model.decision.DecisionOutcome in project kogito-apps by kiegroup.

the class AbstractTrustyServiceIT method storeExecutionWithOutcomes.

private void storeExecutionWithOutcomes(String executionId, Long timestamp) {
    DecisionInput decisionInput = new DecisionInput();
    decisionInput.setId("inputId");
    decisionInput.setName("test");
    decisionInput.setValue(new UnitValue("number", "number", JsonNodeFactory.instance.numberNode(10)));
    DecisionOutcome decisionOutcome1 = new DecisionOutcome();
    decisionOutcome1.setOutcomeId("outcomeId1");
    decisionOutcome1.setOutcomeName("outcome1");
    decisionOutcome1.setOutcomeResult(new UnitValue("number", "number", JsonNodeFactory.instance.numberNode(20)));
    DecisionOutcome decisionOutcome2 = new DecisionOutcome();
    decisionOutcome2.setOutcomeId("outcomeId2");
    decisionOutcome2.setOutcomeName("outcome2");
    decisionOutcome2.setOutcomeResult(new UnitValue("string", "string", JsonNodeFactory.instance.textNode("food")));
    Decision decision = new Decision();
    decision.setExecutionId(executionId);
    decision.setExecutionTimestamp(timestamp);
    decision.setServiceUrl("serviceUrl");
    decision.setExecutedModelNamespace("executedModelNamespace");
    decision.setExecutedModelName("executedModelName");
    decision.setInputs(Collections.singletonList(decisionInput));
    decision.setOutcomes(List.of(decisionOutcome1, decisionOutcome2));
    trustyService.storeDecision(decision.getExecutionId(), decision);
}
Also used : DecisionInput(org.kie.kogito.trusty.storage.api.model.decision.DecisionInput) DecisionOutcome(org.kie.kogito.trusty.storage.api.model.decision.DecisionOutcome) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) Decision(org.kie.kogito.trusty.storage.api.model.decision.Decision)

Aggregations

DecisionOutcome (org.kie.kogito.trusty.storage.api.model.decision.DecisionOutcome)10 Decision (org.kie.kogito.trusty.storage.api.model.decision.Decision)9 UnitValue (org.kie.kogito.tracing.typedvalue.UnitValue)8 Test (org.junit.jupiter.api.Test)6 DecisionInput (org.kie.kogito.trusty.storage.api.model.decision.DecisionInput)6 ArrayList (java.util.ArrayList)5 IntNode (com.fasterxml.jackson.databind.node.IntNode)4 Collections (java.util.Collections)4 List (java.util.List)4 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)4 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)4 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)4 ExplainabilityStatus (org.kie.kogito.explainability.api.ExplainabilityStatus)4 LIMEExplainabilityResult (org.kie.kogito.explainability.api.LIMEExplainabilityResult)4 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)4 Mockito.when (org.mockito.Mockito.when)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 CounterfactualExplainabilityRequest (org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest)3 CounterfactualSearchDomain (org.kie.kogito.explainability.api.CounterfactualSearchDomain)3 NamedTypedValue (org.kie.kogito.explainability.api.NamedTypedValue)3