Search in sources :

Example 1 with DecisionInput

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

the class DecisionMarshallerTest method testWriteAndRead.

@Test
public void testWriteAndRead() throws IOException {
    List<DecisionInput> inputs = Collections.singletonList(new DecisionInput("id", "in", new UnitValue("nameIn", "number", JsonNodeFactory.instance.numberNode(10))));
    List<DecisionOutcome> outcomes = Collections.singletonList(new DecisionOutcome("id", "out", DMNDecisionResult.DecisionEvaluationStatus.SUCCEEDED.toString(), new UnitValue("nameOut", "number", JsonNodeFactory.instance.numberNode(10)), List.of(new NamedTypedValue("nameOut", new UnitValue("number", "number", JsonNodeFactory.instance.numberNode(10)))), new ArrayList<>()));
    Decision decision = new Decision("executionId", "source", "serviceUrl", 0L, true, "executor", "model", "namespace", inputs, outcomes);
    DecisionMarshaller marshaller = new DecisionMarshaller(new ObjectMapper());
    marshaller.writeTo(writer, decision);
    Decision retrieved = marshaller.readFrom(reader);
    Assertions.assertEquals(decision.getExecutionId(), retrieved.getExecutionId());
    Assertions.assertEquals(decision.getSourceUrl(), retrieved.getSourceUrl());
    Assertions.assertEquals(decision.getServiceUrl(), retrieved.getServiceUrl());
    Assertions.assertEquals(decision.getExecutedModelName(), retrieved.getExecutedModelName());
    Assertions.assertEquals(inputs.get(0).getName(), retrieved.getInputs().stream().findFirst().get().getName());
    Assertions.assertEquals(inputs.get(0).getValue().getType(), retrieved.getInputs().stream().findFirst().get().getValue().getType());
    Assertions.assertEquals(inputs.get(0).getValue().toUnit().getBaseType(), retrieved.getInputs().stream().findFirst().get().getValue().toUnit().getBaseType());
    Assertions.assertEquals(outcomes.get(0).getOutcomeId(), retrieved.getOutcomes().stream().findFirst().get().getOutcomeId());
    Assertions.assertTrue(retrieved.getOutcomes().stream().findFirst().get().getMessages().isEmpty());
}
Also used : DecisionInput(org.kie.kogito.trusty.storage.api.model.decision.DecisionInput) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) 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) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 2 with DecisionInput

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

the class TrustyServiceTest method givenADecisionToProcessWhenExplainabilityIsEnabledThenRequestIsSent.

@Test
@SuppressWarnings("unchecked")
void givenADecisionToProcessWhenExplainabilityIsEnabledThenRequestIsSent() throws JsonProcessingException {
    trustyService.enableExplainability();
    Decision decision = new Decision(TEST_EXECUTION_ID, TEST_SOURCE_URL, TEST_SERVICE_URL, 1591692950000L, true, null, "model", "modelNamespace", List.of(new DecisionInput("1", "Input1", new CollectionValue("string", List.of(new UnitValue("string", "string", toJsonNode("\"ONE\"")), new UnitValue("string", "string", toJsonNode("\"TWO\""))))), new DecisionInput("2", "Input2", new StructureValue("Person", Map.of("Name", new UnitValue("string", "string", toJsonNode("\"George Orwell\"")), "Age", new UnitValue("number", "number", toJsonNode("45")))))), List.of(new DecisionOutcome("OUT1", "Result", "SUCCEEDED", new UnitValue("string", "string", toJsonNode("\"YES\"")), Collections.emptyList(), Collections.emptyList())));
    Storage<String, Decision> decisionStorageMock = mock(Storage.class);
    when(decisionStorageMock.containsKey(eq(TEST_EXECUTION_ID))).thenReturn(false);
    when(trustyStorageServiceMock.getDecisionsStorage()).thenReturn(decisionStorageMock);
    trustyService.processDecision(TEST_EXECUTION_ID, decision);
    verify(explainabilityRequestProducerMock).sendEvent(any());
}
Also used : DecisionInput(org.kie.kogito.trusty.storage.api.model.decision.DecisionInput) CollectionValue(org.kie.kogito.tracing.typedvalue.CollectionValue) DecisionOutcome(org.kie.kogito.trusty.storage.api.model.decision.DecisionOutcome) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) StructureValue(org.kie.kogito.tracing.typedvalue.StructureValue) CounterfactualSearchDomainStructureValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainStructureValue) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Decision(org.kie.kogito.trusty.storage.api.model.decision.Decision) Test(org.junit.jupiter.api.Test)

Example 3 with DecisionInput

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

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

use of org.kie.kogito.trusty.storage.api.model.decision.DecisionInput 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)7 DecisionInput (org.kie.kogito.trusty.storage.api.model.decision.DecisionInput)7 UnitValue (org.kie.kogito.tracing.typedvalue.UnitValue)6 DecisionOutcome (org.kie.kogito.trusty.storage.api.model.decision.DecisionOutcome)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Test (org.junit.jupiter.api.Test)3 NamedTypedValue (org.kie.kogito.explainability.api.NamedTypedValue)3 ArrayList (java.util.ArrayList)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 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 IntNode (com.fasterxml.jackson.databind.node.IntNode)1 TextNode (com.fasterxml.jackson.databind.node.TextNode)1 OffsetDateTime (java.time.OffsetDateTime)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1