Search in sources :

Example 16 with Decision

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

the class DecisionsApiV1IT method buildValidDecision.

private Decision buildValidDecision(ListStatus inputsStatus, ListStatus outcomesStatus) throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    Decision decision = new Decision();
    decision.setExecutionId(TEST_EXECUTION_ID);
    decision.setSourceUrl(TEST_SOURCE_URL);
    decision.setExecutionTimestamp(TEST_EXECUTION_TIMESTAMP);
    decision.setSuccess(true);
    decision.setExecutedModelName(TEST_MODEL_NAME);
    decision.setExecutedModelNamespace(TEST_MODEL_NAMESPACE);
    switch(inputsStatus) {
        case EMPTY:
            decision.setInputs(List.of());
            break;
        case FULL:
            decision.setInputs(List.of(new DecisionInput("1", "first", new UnitValue("string", "string", mapper.readTree("\"Hello\""))), new DecisionInput("2", "second", new UnitValue("number", "number", mapper.readTree("12345")))));
    }
    switch(outcomesStatus) {
        case EMPTY:
            decision.setOutcomes(List.of());
            break;
        case FULL:
            decision.setOutcomes(List.of(new DecisionOutcome(TEST_OUTCOME_ID, "ONE", "SUCCEEDED", new UnitValue("string", "string", mapper.readTree("\"The First " + "Outcome\"")), Collections.emptyList(), List.of(getMessage(MessageLevel.WARNING, MessageCategory.INTERNAL, "TEST", "testSrc", "Test message", getMessageExceptionField("TestException", "Test exception message", getMessageExceptionField("TestExceptionCause", "Test exception " + "cause " + "message", null)))))));
    }
    return 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) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Decision(org.kie.kogito.trusty.storage.api.model.decision.Decision)

Example 17 with Decision

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

the class AbstractTrustyServiceIT method storeExecution.

private void storeExecution(String executionId, Long timestamp) {
    DecisionInput decisionInput = new DecisionInput();
    decisionInput.setId("inputId");
    decisionInput.setName("test");
    decisionInput.setValue(new UnitValue("number", "number", JsonNodeFactory.instance.numberNode(10)));
    Decision decision = new Decision();
    decision.setExecutionId(executionId);
    decision.setExecutionTimestamp(timestamp);
    decision.setServiceUrl("serviceUrl");
    decision.setExecutedModelNamespace("executedModelNamespace");
    decision.setExecutedModelName("executedModelName");
    decision.setInputs(Collections.singletonList(decisionInput));
    trustyService.storeDecision(decision.getExecutionId(), decision);
}
Also used : DecisionInput(org.kie.kogito.trusty.storage.api.model.decision.DecisionInput) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) Decision(org.kie.kogito.trusty.storage.api.model.decision.Decision)

Example 18 with Decision

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

the class AbstractTrustyServiceIT method givenAnExecutionWhenGetDecisionByIdThenTheComponentsInUnitTypesIsNull.

@Test
public void givenAnExecutionWhenGetDecisionByIdThenTheComponentsInUnitTypesIsNull() {
    String executionId = "myExecution";
    storeExecution(executionId, 1591692950000L);
    Decision result = trustyService.getDecisionById(executionId);
    Assertions.assertTrue(result.getInputs().stream().findFirst().get().getValue().isUnit());
}
Also used : Decision(org.kie.kogito.trusty.storage.api.model.decision.Decision) Test(org.junit.jupiter.api.Test)

Example 19 with Decision

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

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

Decision (org.kie.kogito.trusty.storage.api.model.decision.Decision)30 Test (org.junit.jupiter.api.Test)18 UnitValue (org.kie.kogito.tracing.typedvalue.UnitValue)12 DecisionOutcome (org.kie.kogito.trusty.storage.api.model.decision.DecisionOutcome)10 ArrayList (java.util.ArrayList)9 DecisionInput (org.kie.kogito.trusty.storage.api.model.decision.DecisionInput)8 IntNode (com.fasterxml.jackson.databind.node.IntNode)7 List (java.util.List)7 CounterfactualExplainabilityRequest (org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest)6 NamedTypedValue (org.kie.kogito.explainability.api.NamedTypedValue)6 Storage (org.kie.kogito.persistence.api.Storage)6 MatchedExecutionHeaders (org.kie.kogito.trusty.service.common.models.MatchedExecutionHeaders)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 Collections (java.util.Collections)5 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)5 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)5 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 CounterfactualSearchDomain (org.kie.kogito.explainability.api.CounterfactualSearchDomain)5 ExplainabilityStatus (org.kie.kogito.explainability.api.ExplainabilityStatus)5