use of org.kie.kogito.explainability.api.NamedTypedValue in project kogito-apps by kiegroup.
the class AbstractTrustyServiceIT method testCounterfactuals_StoreSingleAndRetrieveSingleWithGoals.
@Test
public void testCounterfactuals_StoreSingleAndRetrieveSingleWithGoals() {
String executionId = "myCFExecution1";
storeExecutionWithOutcomes(executionId, 0L);
// The Goals structures must be comparable to the original decisions outcomes.
// The Search Domain structures must be identical those of the original decision inputs.
CounterfactualSearchDomain searchDomain = buildSearchDomainUnit("test", "number", new CounterfactualDomainRange(new IntNode(1), new IntNode(2)));
NamedTypedValue goal1 = buildGoalUnit("outcome1", "number", new IntNode(25));
NamedTypedValue goal2 = buildGoalUnit("outcome2", "string", new TextNode("cheese"));
CounterfactualExplainabilityRequest request = trustyService.requestCounterfactuals(executionId, List.of(goal1, goal2), Collections.singletonList(searchDomain));
assertNotNull(request);
assertEquals(request.getExecutionId(), executionId);
assertNotNull(request.getCounterfactualId());
assertEquals(2, request.getGoals().size());
List<NamedTypedValue> requestGoals = new ArrayList<>(request.getGoals());
assertCounterfactualGoal(goal1, requestGoals.get(0));
assertCounterfactualGoal(goal2, requestGoals.get(1));
CounterfactualExplainabilityRequest result = trustyService.getCounterfactualRequest(executionId, request.getCounterfactualId());
assertNotNull(result);
assertEquals(request.getExecutionId(), result.getExecutionId());
assertEquals(request.getCounterfactualId(), result.getCounterfactualId());
assertEquals(2, result.getGoals().size());
List<NamedTypedValue> resultGoals = new ArrayList<>(request.getGoals());
assertCounterfactualGoal(goal1, resultGoals.get(0));
assertCounterfactualGoal(goal2, resultGoals.get(1));
}
use of org.kie.kogito.explainability.api.NamedTypedValue in project kogito-apps by kiegroup.
the class AbstractTrustyServiceIT method testStoreExplainabilityResult_Counterfactual_DuplicateRemoval_FinalThenIntermediate.
@Test
public void testStoreExplainabilityResult_Counterfactual_DuplicateRemoval_FinalThenIntermediate() {
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 FINAL (for whatever reason, e.g. messaging delays, the INTERMEDIATE is received afterwards)
trustyService.storeExplainabilityResult(executionId, new CounterfactualExplainabilityResult(executionId, counterfactualId, "solutionId1", 0L, ExplainabilityStatus.SUCCEEDED, "status", true, CounterfactualExplainabilityResult.Stage.FINAL, 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.FINAL, result1.get(0).getStage());
trustyService.storeExplainabilityResult(executionId, new CounterfactualExplainabilityResult(executionId, counterfactualId, "solutionId2", 0L, ExplainabilityStatus.SUCCEEDED, "status", true, CounterfactualExplainabilityResult.Stage.INTERMEDIATE, List.of(input1, input2), List.of(output1, output2)));
List<CounterfactualExplainabilityResult> result2 = trustyService.getCounterfactualResults(executionId, counterfactualId);
assertNotNull(result2);
assertEquals(1, result1.size());
assertEquals("solutionId1", result1.get(0).getSolutionId());
assertEquals(CounterfactualExplainabilityResult.Stage.FINAL, result1.get(0).getStage());
}
use of org.kie.kogito.explainability.api.NamedTypedValue in project kogito-apps by kiegroup.
the class TrustyServiceImpl method processDecision.
@Override
public void processDecision(String executionId, Decision decision) {
storeDecision(executionId, decision);
if (isExplainabilityEnabled) {
List<NamedTypedValue> inputs = decision.getInputs() != null ? decision.getInputs().stream().map(input -> new NamedTypedValue(input.getName(), input.getValue())).collect(Collectors.toList()) : Collections.emptyList();
List<NamedTypedValue> outputs = decision.getOutcomes() != null ? decision.getOutcomes().stream().map(output -> new NamedTypedValue(output.getOutcomeName(), output.getOutcomeResult())).collect(Collectors.toList()) : Collections.emptyList();
explainabilityRequestProducer.sendEvent(new LIMEExplainabilityRequest(executionId, decision.getServiceUrl(), createDecisionModelIdentifier(decision), inputs, outputs));
}
}
Aggregations