use of org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest in project kogito-apps by kiegroup.
the class TrustyServiceTest method givenStoredExecutionWhenCounterfactualRequestIsMadeThenExplainabilityEventHasCorrectPayload.
@Test
@SuppressWarnings("unchecked")
void givenStoredExecutionWhenCounterfactualRequestIsMadeThenExplainabilityEventHasCorrectPayload() {
Storage<String, Decision> decisionStorage = mock(Storage.class);
Storage<String, CounterfactualExplainabilityRequest> counterfactualStorage = mock(Storage.class);
ArgumentCaptor<BaseExplainabilityRequest> explainabilityEventArgumentCaptor = ArgumentCaptor.forClass(BaseExplainabilityRequest.class);
Decision decision = new Decision(TEST_EXECUTION_ID, TEST_SOURCE_URL, TEST_SERVICE_URL, 0L, true, null, "model", "modelNamespace", List.of(new DecisionInput("IN1", "yearsOfService", new UnitValue("integer", "integer", new IntNode(10)))), List.of(new DecisionOutcome("OUT1", "salary", "SUCCEEDED", new UnitValue("integer", "integer", new IntNode(1000)), Collections.emptyList(), Collections.emptyList())));
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(decision);
trustyService.requestCounterfactuals(TEST_EXECUTION_ID, List.of(new NamedTypedValue("salary", new UnitValue("integer", "integer", new IntNode(2000)))), List.of(new CounterfactualSearchDomain("yearsOfService", new CounterfactualSearchDomainUnitValue("integer", "integer", false, new CounterfactualDomainRange(new IntNode(10), new IntNode(30))))));
verify(explainabilityRequestProducerMock).sendEvent(explainabilityEventArgumentCaptor.capture());
BaseExplainabilityRequest event = explainabilityEventArgumentCaptor.getValue();
CounterfactualExplainabilityRequest request = (CounterfactualExplainabilityRequest) event;
assertEquals(TEST_EXECUTION_ID, request.getExecutionId());
assertEquals(TEST_SERVICE_URL, request.getServiceUrl());
// Check original input value has been copied into CF request
assertEquals(1, request.getOriginalInputs().size());
assertTrue(request.getOriginalInputs().stream().anyMatch(i -> i.getName().equals("yearsOfService")));
// It is safe to use the iterator unchecked as the collection only contains one item
assertEquals(decision.getInputs().iterator().next().getValue().toUnit().getValue().asInt(), request.getOriginalInputs().iterator().next().getValue().toUnit().getValue().asInt());
// Check CF goals have been copied into CF request
assertEquals(1, request.getGoals().size());
assertTrue(request.getGoals().stream().anyMatch(g -> g.getName().equals("salary")));
// It is safe to use the iterator unchecked as the collection only contains one item
assertEquals(2000, request.getGoals().iterator().next().getValue().toUnit().getValue().asInt());
// Check CF search domains have been copied into CF request
assertEquals(1, request.getSearchDomains().size());
assertTrue(request.getSearchDomains().stream().anyMatch(sd -> sd.getName().equals("yearsOfService")));
// It is safe to use the iterator unchecked as the collection only contains one item
CounterfactualSearchDomainValue searchDomain = request.getSearchDomains().iterator().next().getValue();
assertTrue(searchDomain instanceof CounterfactualSearchDomainUnitValue);
CounterfactualSearchDomainUnitValue unit = (CounterfactualSearchDomainUnitValue) searchDomain;
assertFalse(unit.isFixed());
assertNotNull(unit.getDomain());
assertTrue(unit.getDomain() instanceof CounterfactualDomainRange);
CounterfactualDomainRange range = (CounterfactualDomainRange) unit.getDomain();
assertEquals(10, range.getLowerBound().asInt());
assertEquals(30, range.getUpperBound().asInt());
// Check Max Running Time Seconds
assertEquals(MAX_RUNNING_TIME_SECONDS, request.getMaxRunningTimeSeconds());
}
use of org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest in project kogito-apps by kiegroup.
the class AbstractTrustyServiceIT method testCounterfactuals_StoreMultipleForMultipleExecutionsAndRetrieveSingleWithEmptyDefinition.
@Test
public void testCounterfactuals_StoreMultipleForMultipleExecutionsAndRetrieveSingleWithEmptyDefinition() {
String executionId1 = "myCFExecution1";
storeExecution(executionId1, 0L);
String executionId2 = "myCFExecution2";
storeExecution(executionId2, 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)));
CounterfactualExplainabilityRequest request1 = trustyService.requestCounterfactuals(executionId1, Collections.emptyList(), Collections.singletonList(searchDomain));
assertNotNull(request1);
assertEquals(request1.getExecutionId(), executionId1);
assertNotNull(request1.getCounterfactualId());
CounterfactualExplainabilityRequest request2 = trustyService.requestCounterfactuals(executionId2, Collections.emptyList(), Collections.singletonList(searchDomain));
assertNotNull(request2);
assertEquals(request2.getExecutionId(), executionId2);
assertNotNull(request2.getCounterfactualId());
CounterfactualExplainabilityRequest result1 = trustyService.getCounterfactualRequest(executionId1, request1.getCounterfactualId());
assertNotNull(result1);
assertEquals(request1.getExecutionId(), result1.getExecutionId());
assertEquals(request1.getCounterfactualId(), result1.getCounterfactualId());
}
use of org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest 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.CounterfactualExplainabilityRequest in project kogito-apps by kiegroup.
the class AbstractTrustyServiceIT method testCounterfactuals_WithDisparateGoalsStructure.
@Test
public void testCounterfactuals_WithDisparateGoalsStructure() {
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.
// For this test we're providing the correct structure for searchDomains but a sub-structure for goals.
CounterfactualSearchDomain searchDomain = buildSearchDomainUnit("test", "number", new CounterfactualDomainRange(new IntNode(1), new IntNode(2)));
CounterfactualExplainabilityRequest request = trustyService.requestCounterfactuals(executionId, Collections.emptyList(), Collections.singletonList(searchDomain));
assertNotNull(request);
assertEquals(request.getExecutionId(), executionId);
assertNotNull(request.getCounterfactualId());
assertTrue(request.getGoals().isEmpty());
CounterfactualExplainabilityRequest result = trustyService.getCounterfactualRequest(executionId, request.getCounterfactualId());
assertNotNull(result);
assertEquals(request.getExecutionId(), result.getExecutionId());
assertEquals(request.getCounterfactualId(), result.getCounterfactualId());
assertTrue(result.getGoals().isEmpty());
}
use of org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest in project kogito-apps by kiegroup.
the class AbstractTrustyServiceIT method testCounterfactuals_StoreMultipleAndRetrieveSingleWithEmptyDefinition.
@Test
public void testCounterfactuals_StoreMultipleAndRetrieveSingleWithEmptyDefinition() {
String executionId = "myCFExecution3";
storeExecution(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)));
CounterfactualExplainabilityRequest request1 = trustyService.requestCounterfactuals(executionId, Collections.emptyList(), Collections.singletonList(searchDomain));
CounterfactualExplainabilityRequest request2 = trustyService.requestCounterfactuals(executionId, Collections.emptyList(), Collections.singletonList(searchDomain));
CounterfactualExplainabilityRequest result1 = trustyService.getCounterfactualRequest(executionId, request1.getCounterfactualId());
assertNotNull(result1);
assertEquals(request1.getCounterfactualId(), result1.getCounterfactualId());
CounterfactualExplainabilityRequest result2 = trustyService.getCounterfactualRequest(executionId, request2.getCounterfactualId());
assertNotNull(result2);
assertEquals(request2.getCounterfactualId(), result2.getCounterfactualId());
}
Aggregations