Search in sources :

Example 11 with CounterfactualExplainabilityResult

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

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

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

the class CounterfactualExplainabilityResultsManagerDuplicates method purge.

@Override
public void purge(String counterfactualId, Storage<String, CounterfactualExplainabilityResult> storage) {
    List<CounterfactualExplainabilityResult> results = new ArrayList<>(storage.query().sort(List.of(orderBy(CounterfactualExplainabilityResult.COUNTERFACTUAL_SEQUENCE_ID_FIELD, ASC))).filter(List.of(QueryFilterFactory.equalTo(CounterfactualExplainabilityResult.COUNTERFACTUAL_ID_FIELD, counterfactualId))).execute());
    if (results.size() < 2) {
        return;
    }
    final CounterfactualExplainabilityResult latestResult = results.get(results.size() - 1);
    final CounterfactualExplainabilityResult penultimateResult = results.get(results.size() - 2);
    final boolean inputsEqual = equals(latestResult.getInputs(), penultimateResult.getInputs());
    final boolean outputsEqual = equals(latestResult.getOutputs(), penultimateResult.getOutputs());
    if (inputsEqual && outputsEqual) {
        LOG.info("The latest two Counterfactual results are equal. Removing duplicate.");
        // It's plausible, although unlikely, that the FINAL result could have been received before the last INTERMEDIATE.
        if (latestResult.getStage().equals(CounterfactualExplainabilityResult.Stage.FINAL)) {
            removeSolution(storage, penultimateResult);
        } else {
            removeSolution(storage, latestResult);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) CounterfactualExplainabilityResult(org.kie.kogito.explainability.api.CounterfactualExplainabilityResult)

Example 14 with CounterfactualExplainabilityResult

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

the class CounterfactualExplainabilityResultsManagerSlidingWindow method purge.

@Override
public void purge(String counterfactualId, Storage<String, CounterfactualExplainabilityResult> storage) {
    List<CounterfactualExplainabilityResult> results = new ArrayList<>(storage.query().sort(List.of(orderBy(CounterfactualExplainabilityResult.COUNTERFACTUAL_SEQUENCE_ID_FIELD, ASC))).filter(List.of(QueryFilterFactory.equalTo(CounterfactualExplainabilityResult.COUNTERFACTUAL_ID_FIELD, counterfactualId))).execute());
    // Remove old results from window
    int overflow = results.size() - lengthOfWindow;
    while (overflow > 0) {
        CounterfactualExplainabilityResult result = results.get(--overflow);
        String solutionId = result.getSolutionId();
        if (LOG.isInfoEnabled()) {
            LOG.info(String.format("Counterfactual results overflow window with size %d by %d.", lengthOfWindow, overflow));
            LOG.info(String.format("Removing stale solution %s, sequence %d", solutionId, result.getSequenceId()));
        }
        storage.remove(solutionId);
    }
}
Also used : ArrayList(java.util.ArrayList) CounterfactualExplainabilityResult(org.kie.kogito.explainability.api.CounterfactualExplainabilityResult)

Example 15 with CounterfactualExplainabilityResult

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

CounterfactualExplainabilityResult (org.kie.kogito.explainability.api.CounterfactualExplainabilityResult)29 Test (org.junit.jupiter.api.Test)26 NamedTypedValue (org.kie.kogito.explainability.api.NamedTypedValue)8 BaseExplainabilityResult (org.kie.kogito.explainability.api.BaseExplainabilityResult)6 UnitValue (org.kie.kogito.tracing.typedvalue.UnitValue)6 IntNode (com.fasterxml.jackson.databind.node.IntNode)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Consumer (java.util.function.Consumer)4 CounterfactualExplainabilityRequest (org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest)4 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)3 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)3 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 CounterfactualSearchDomain (org.kie.kogito.explainability.api.CounterfactualSearchDomain)3 ExplainabilityStatus (org.kie.kogito.explainability.api.ExplainabilityStatus)3 ModelIdentifier (org.kie.kogito.explainability.api.ModelIdentifier)3 Prediction (org.kie.kogito.explainability.model.Prediction)3 Query (org.kie.kogito.persistence.api.query.Query)3 BooleanNode (com.fasterxml.jackson.databind.node.BooleanNode)2