Search in sources :

Example 11 with NamedTypedValue

use of org.kie.kogito.explainability.api.NamedTypedValue 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 NamedTypedValue

use of org.kie.kogito.explainability.api.NamedTypedValue 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 13 with NamedTypedValue

use of org.kie.kogito.explainability.api.NamedTypedValue 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 14 with NamedTypedValue

use of org.kie.kogito.explainability.api.NamedTypedValue 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 15 with NamedTypedValue

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

the class CounterfactualExplainabilityResultMarshallerTest method testWriteAndRead.

@Test
public void testWriteAndRead() throws IOException {
    List<NamedTypedValue> inputs = List.of(new NamedTypedValue("unitIn", new UnitValue("number", "number", JsonNodeFactory.instance.numberNode(10))));
    List<NamedTypedValue> outputs = List.of(new NamedTypedValue("unitOut", new UnitValue("number", "number", JsonNodeFactory.instance.numberNode(55))));
    CounterfactualExplainabilityResult explainabilityResult = new CounterfactualExplainabilityResult("executionId", "counterfactualId", "solutionId", 0L, ExplainabilityStatus.SUCCEEDED, "statusDetail", true, CounterfactualExplainabilityResult.Stage.FINAL, inputs, outputs);
    CounterfactualExplainabilityResultMarshaller marshaller = new CounterfactualExplainabilityResultMarshaller(new ObjectMapper());
    marshaller.writeTo(writer, explainabilityResult);
    CounterfactualExplainabilityResult retrieved = marshaller.readFrom(reader);
    List<NamedTypedValue> retrievedInputs = List.of(retrieved.getInputs().toArray(new NamedTypedValue[0]));
    List<NamedTypedValue> retrievedOutputs = List.of(retrieved.getOutputs().toArray(new NamedTypedValue[0]));
    Assertions.assertEquals(explainabilityResult.getExecutionId(), retrieved.getExecutionId());
    Assertions.assertEquals(explainabilityResult.getCounterfactualId(), retrieved.getCounterfactualId());
    Assertions.assertEquals(explainabilityResult.getSolutionId(), retrieved.getSolutionId());
    Assertions.assertEquals(explainabilityResult.getSequenceId(), retrieved.getSequenceId());
    Assertions.assertEquals(explainabilityResult.getStatus(), retrieved.getStatus());
    Assertions.assertEquals(explainabilityResult.getStatusDetails(), retrieved.getStatusDetails());
    Assertions.assertEquals(explainabilityResult.getStage(), retrieved.getStage());
    Assertions.assertEquals(1, retrievedInputs.size());
    Assertions.assertEquals(inputs.get(0).getName(), retrievedInputs.get(0).getName());
    Assertions.assertEquals(inputs.get(0).getValue().getKind(), retrievedInputs.get(0).getValue().getKind());
    Assertions.assertEquals(inputs.get(0).getValue().getType(), retrievedInputs.get(0).getValue().getType());
    Assertions.assertEquals(inputs.get(0).getValue().toUnit().getValue(), retrievedInputs.get(0).getValue().toUnit().getValue());
    Assertions.assertEquals(1, retrievedOutputs.size());
    Assertions.assertEquals(outputs.get(0).getName(), retrievedOutputs.get(0).getName());
    Assertions.assertEquals(outputs.get(0).getValue().getKind(), retrievedOutputs.get(0).getValue().getKind());
    Assertions.assertEquals(outputs.get(0).getValue().getType(), retrievedOutputs.get(0).getValue().getType());
    Assertions.assertEquals(outputs.get(0).getValue().toUnit().getValue(), retrievedOutputs.get(0).getValue().toUnit().getValue());
}
Also used : NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) CounterfactualExplainabilityResult(org.kie.kogito.explainability.api.CounterfactualExplainabilityResult) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Aggregations

NamedTypedValue (org.kie.kogito.explainability.api.NamedTypedValue)33 Test (org.junit.jupiter.api.Test)27 UnitValue (org.kie.kogito.tracing.typedvalue.UnitValue)22 IntNode (com.fasterxml.jackson.databind.node.IntNode)19 CounterfactualExplainabilityRequest (org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest)19 CounterfactualSearchDomain (org.kie.kogito.explainability.api.CounterfactualSearchDomain)19 CounterfactualSearchDomainUnitValue (org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue)16 CounterfactualDomainRange (org.kie.kogito.explainability.api.CounterfactualDomainRange)12 CounterfactualExplainabilityResult (org.kie.kogito.explainability.api.CounterfactualExplainabilityResult)11 StructureValue (org.kie.kogito.tracing.typedvalue.StructureValue)11 List (java.util.List)9 CounterfactualSearchDomainStructureValue (org.kie.kogito.explainability.api.CounterfactualSearchDomainStructureValue)9 Prediction (org.kie.kogito.explainability.model.Prediction)9 CollectionValue (org.kie.kogito.tracing.typedvalue.CollectionValue)9 Map (java.util.Map)8 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)8 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)8 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)8 ModelIdentifier (org.kie.kogito.explainability.api.ModelIdentifier)8 CounterfactualPrediction (org.kie.kogito.explainability.model.CounterfactualPrediction)8