use of org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest in project kogito-apps by kiegroup.
the class TrustyServiceTest method givenMultipleStoredCounterfactualRequestsWhenGetCounterfactualRequestThenIllegalArgumentExceptionIsThrown.
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
void givenMultipleStoredCounterfactualRequestsWhenGetCounterfactualRequestThenIllegalArgumentExceptionIsThrown() {
Storage<String, CounterfactualExplainabilityRequest> counterfactualStorage = mock(Storage.class);
CounterfactualExplainabilityRequest request1 = mock(CounterfactualExplainabilityRequest.class);
CounterfactualExplainabilityRequest request2 = mock(CounterfactualExplainabilityRequest.class);
Query queryMock = mock(Query.class);
when(queryMock.filter(any(List.class))).thenReturn(queryMock);
when(queryMock.execute()).thenReturn(List.of(request1, request2));
when(counterfactualStorage.query()).thenReturn(queryMock);
when(trustyStorageServiceMock.getCounterfactualRequestStorage()).thenReturn(counterfactualStorage);
assertThrows(IllegalArgumentException.class, () -> trustyService.getCounterfactualRequest(TEST_EXECUTION_ID, TEST_COUNTERFACTUAL_ID));
}
use of org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest in project kogito-apps by kiegroup.
the class AbstractTrustyServiceIT method testCounterfactuals_StoreSingleAndRetrieveSingleWithEmptyDefinition.
@Test
public void testCounterfactuals_StoreSingleAndRetrieveSingleWithEmptyDefinition() {
String executionId = "myCFExecution1";
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 request = trustyService.requestCounterfactuals(executionId, Collections.emptyList(), Collections.singletonList(searchDomain));
assertNotNull(request);
assertEquals(request.getExecutionId(), executionId);
assertNotNull(request.getCounterfactualId());
CounterfactualExplainabilityRequest result = trustyService.getCounterfactualRequest(executionId, request.getCounterfactualId());
assertNotNull(result);
assertEquals(request.getExecutionId(), result.getExecutionId());
assertEquals(request.getCounterfactualId(), result.getCounterfactualId());
}
use of org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest in project kogito-apps by kiegroup.
the class AbstractTrustyServiceIT method testCounterfactuals_StoreSingleAndRetrieveSingleWithSearchDomainRange.
@Test
public void testCounterfactuals_StoreSingleAndRetrieveSingleWithSearchDomainRange() {
String executionId = "myCFExecution1";
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 request = trustyService.requestCounterfactuals(executionId, Collections.emptyList(), Collections.singletonList(searchDomain));
assertNotNull(request);
assertEquals(request.getExecutionId(), executionId);
assertNotNull(request.getCounterfactualId());
assertEquals(1, request.getSearchDomains().size());
List<CounterfactualSearchDomain> requestSearchDomains = new ArrayList<>(request.getSearchDomains());
assertCounterfactualSearchDomainRange(searchDomain, requestSearchDomains.get(0));
CounterfactualExplainabilityRequest result = trustyService.getCounterfactualRequest(executionId, request.getCounterfactualId());
assertNotNull(result);
assertEquals(request.getExecutionId(), result.getExecutionId());
assertEquals(request.getCounterfactualId(), result.getCounterfactualId());
assertEquals(1, result.getSearchDomains().size());
List<CounterfactualSearchDomain> resultSearchDomains = new ArrayList<>(result.getSearchDomains());
assertCounterfactualSearchDomainRange(searchDomain, resultSearchDomains.get(0));
}
use of org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest in project kogito-apps by kiegroup.
the class AbstractTrustyServiceIT method testCounterfactuals_StoreMultipleAndRetrieveAllWithEmptyDefinition.
@Test
public void testCounterfactuals_StoreMultipleAndRetrieveAllWithEmptyDefinition() {
String executionId = "myCFExecution2";
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));
List<CounterfactualExplainabilityRequest> result = trustyService.getCounterfactualRequests(executionId);
assertNotNull(result);
assertEquals(2, result.size());
assertTrue(result.stream().anyMatch(c -> c.getCounterfactualId().equals(request1.getCounterfactualId())));
assertTrue(result.stream().anyMatch(c -> c.getCounterfactualId().equals(request2.getCounterfactualId())));
}
use of org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest 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);
}
Aggregations