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);
}
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());
}
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);
}
}
}
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);
}
}
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());
}
Aggregations