Search in sources :

Example 11 with UnitValue

use of org.kie.kogito.tracing.typedvalue.UnitValue in project kogito-apps by kiegroup.

the class TrustyServiceTest method doGivenStoredExecutionWhenCounterfactualRequestIsMadeThenRequestIsStoredTest.

@SuppressWarnings("unchecked")
void doGivenStoredExecutionWhenCounterfactualRequestIsMadeThenRequestIsStoredTest(CounterfactualDomain domain) {
    Storage<String, Decision> decisionStorage = mock(Storage.class);
    Storage<String, CounterfactualExplainabilityRequest> counterfactualStorage = mock(Storage.class);
    ArgumentCaptor<CounterfactualExplainabilityRequest> counterfactualArgumentCaptor = ArgumentCaptor.forClass(CounterfactualExplainabilityRequest.class);
    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(TrustyServiceTestUtils.buildCorrectDecision(TEST_EXECUTION_ID));
    // The Goals structures must be comparable to the original decisions outcomes.
    // The Search Domain structures must be identical those of the original decision inputs.
    trustyService.requestCounterfactuals(TEST_EXECUTION_ID, List.of(new NamedTypedValue("Fine", new StructureValue("tFine", Map.of("Amount", new UnitValue("number", "number", new IntNode(0)), "Points", new UnitValue("number", "number", new IntNode(0))))), new NamedTypedValue("Should the driver be suspended?", new UnitValue("string", "string", new TextNode("No")))), List.of(new CounterfactualSearchDomain("Violation", new CounterfactualSearchDomainStructureValue("tViolation", Map.of("Type", new CounterfactualSearchDomainUnitValue("string", "string", true, domain), "Actual Speed", new CounterfactualSearchDomainUnitValue("number", "number", true, domain), "Speed Limit", new CounterfactualSearchDomainUnitValue("number", "number", true, domain)))), new CounterfactualSearchDomain("Driver", new CounterfactualSearchDomainStructureValue("tDriver", Map.of("Age", new CounterfactualSearchDomainUnitValue("number", "number", true, domain), "Points", new CounterfactualSearchDomainUnitValue("number", "number", true, domain))))));
    verify(counterfactualStorage).put(anyString(), counterfactualArgumentCaptor.capture());
    CounterfactualExplainabilityRequest counterfactual = counterfactualArgumentCaptor.getValue();
    assertNotNull(counterfactual);
    assertEquals(TEST_EXECUTION_ID, counterfactual.getExecutionId());
}
Also used : CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) TextNode(com.fasterxml.jackson.databind.node.TextNode) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Decision(org.kie.kogito.trusty.storage.api.model.decision.Decision) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) IntNode(com.fasterxml.jackson.databind.node.IntNode) StructureValue(org.kie.kogito.tracing.typedvalue.StructureValue) CounterfactualSearchDomainStructureValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainStructureValue) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain) CounterfactualSearchDomainStructureValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainStructureValue)

Example 12 with UnitValue

use of org.kie.kogito.tracing.typedvalue.UnitValue 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 13 with UnitValue

use of org.kie.kogito.tracing.typedvalue.UnitValue in project kogito-apps by kiegroup.

the class AbstractTrustyServiceIT method testStoreExplainabilityResult_Counterfactual.

@Test
public void testStoreExplainabilityResult_Counterfactual() {
    String executionId = "myCFExecution1Store";
    NamedTypedValue input1 = new NamedTypedValue("field1", new UnitValue("typeRef1", "typeRef1", new IntNode(25)));
    NamedTypedValue input2 = new NamedTypedValue("field2", new UnitValue("typeRef2", "typeRef2", new IntNode(99)));
    NamedTypedValue output1 = new NamedTypedValue("field3", new UnitValue("typeRef3", "typeRef3", new IntNode(200)));
    NamedTypedValue output2 = new NamedTypedValue("field4", new UnitValue("typeRef4", "typeRef4", new IntNode(1000)));
    trustyService.storeExplainabilityResult(executionId, new CounterfactualExplainabilityResult(executionId, "counterfactualId", "solutionId", 0L, ExplainabilityStatus.SUCCEEDED, "status", true, CounterfactualExplainabilityResult.Stage.FINAL, List.of(input1, input2), List.of(output1, output2)));
    CounterfactualExplainabilityResult result = trustyService.getExplainabilityResultById(executionId, CounterfactualExplainabilityResult.class);
    assertNotNull(result);
}
Also used : NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) IntNode(com.fasterxml.jackson.databind.node.IntNode) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) CounterfactualExplainabilityResult(org.kie.kogito.explainability.api.CounterfactualExplainabilityResult) Test(org.junit.jupiter.api.Test)

Example 14 with UnitValue

use of org.kie.kogito.tracing.typedvalue.UnitValue in project kogito-apps by kiegroup.

the class AbstractTrustyServiceIT method testStoreExplainabilityResult_Counterfactual_DuplicateRemoval_IntermediateThenFinal.

@Test
public void testStoreExplainabilityResult_Counterfactual_DuplicateRemoval_IntermediateThenFinal() {
    String executionId = "myCFExecution1Store";
    String counterfactualId = "myCFCounterfactualId";
    NamedTypedValue input1 = new NamedTypedValue("field1", new UnitValue("typeRef1", "typeRef1", new IntNode(25)));
    NamedTypedValue input2 = new NamedTypedValue("field2", new UnitValue("typeRef2", "typeRef2", new IntNode(99)));
    NamedTypedValue output1 = new NamedTypedValue("field3", new UnitValue("typeRef3", "typeRef3", new IntNode(200)));
    NamedTypedValue output2 = new NamedTypedValue("field4", new UnitValue("typeRef4", "typeRef4", new IntNode(1000)));
    // First solution is the INTERMEDIATE then the FINAL
    trustyService.storeExplainabilityResult(executionId, new CounterfactualExplainabilityResult(executionId, counterfactualId, "solutionId1", 0L, ExplainabilityStatus.SUCCEEDED, "status", true, CounterfactualExplainabilityResult.Stage.INTERMEDIATE, List.of(input1, input2), List.of(output1, output2)));
    List<CounterfactualExplainabilityResult> result1 = trustyService.getCounterfactualResults(executionId, counterfactualId);
    assertNotNull(result1);
    assertEquals(1, result1.size());
    assertEquals("solutionId1", result1.get(0).getSolutionId());
    assertEquals(CounterfactualExplainabilityResult.Stage.INTERMEDIATE, result1.get(0).getStage());
    trustyService.storeExplainabilityResult(executionId, new CounterfactualExplainabilityResult(executionId, counterfactualId, "solutionId2", 0L, ExplainabilityStatus.SUCCEEDED, "status", true, CounterfactualExplainabilityResult.Stage.FINAL, List.of(input1, input2), List.of(output1, output2)));
    List<CounterfactualExplainabilityResult> result2 = trustyService.getCounterfactualResults(executionId, counterfactualId);
    assertNotNull(result2);
    assertEquals(1, result2.size());
    assertEquals("solutionId2", result2.get(0).getSolutionId());
    assertEquals(CounterfactualExplainabilityResult.Stage.FINAL, result2.get(0).getStage());
}
Also used : NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) IntNode(com.fasterxml.jackson.databind.node.IntNode) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) CounterfactualExplainabilityResult(org.kie.kogito.explainability.api.CounterfactualExplainabilityResult) Test(org.junit.jupiter.api.Test)

Example 15 with UnitValue

use of org.kie.kogito.tracing.typedvalue.UnitValue 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)

Aggregations

UnitValue (org.kie.kogito.tracing.typedvalue.UnitValue)31 Test (org.junit.jupiter.api.Test)24 NamedTypedValue (org.kie.kogito.explainability.api.NamedTypedValue)23 IntNode (com.fasterxml.jackson.databind.node.IntNode)18 CounterfactualSearchDomainUnitValue (org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue)17 CounterfactualExplainabilityRequest (org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest)14 StructureValue (org.kie.kogito.tracing.typedvalue.StructureValue)12 List (java.util.List)10 CollectionValue (org.kie.kogito.tracing.typedvalue.CollectionValue)10 CounterfactualSearchDomain (org.kie.kogito.explainability.api.CounterfactualSearchDomain)9 ArrayList (java.util.ArrayList)8 CounterfactualExplainabilityResult (org.kie.kogito.explainability.api.CounterfactualExplainabilityResult)8 CounterfactualSearchDomainStructureValue (org.kie.kogito.explainability.api.CounterfactualSearchDomainStructureValue)8 TypedValue (org.kie.kogito.tracing.typedvalue.TypedValue)8 Decision (org.kie.kogito.trusty.storage.api.model.decision.Decision)8 Collections (java.util.Collections)7 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)7 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)7 CounterfactualDomainRange (org.kie.kogito.explainability.api.CounterfactualDomainRange)7 ExplainabilityStatus (org.kie.kogito.explainability.api.ExplainabilityStatus)7