use of org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest in project kogito-apps by kiegroup.
the class ExplainabilityApiV1Test method testGetCounterfactualResultsWhenExecutionDoesExistAndResultsHaveBeenCreated.
@Test
public void testGetCounterfactualResultsWhenExecutionDoesExistAndResultsHaveBeenCreated() {
NamedTypedValue goal = buildGoalUnit("unit", "string", new TextNode("hello"));
CounterfactualSearchDomain searchDomain = buildSearchDomainUnit("unit", "string", new CounterfactualDomainCategorical(List.of(new TextNode("hello"), new TextNode("goodbye"))));
CounterfactualExplainabilityResult solution1 = new CounterfactualExplainabilityResult(EXECUTION_ID, COUNTERFACTUAL_ID, "solution1", 0L, ExplainabilityStatus.SUCCEEDED, "", true, CounterfactualExplainabilityResult.Stage.INTERMEDIATE, Collections.emptyList(), Collections.emptyList());
CounterfactualExplainabilityResult solution2 = new CounterfactualExplainabilityResult(EXECUTION_ID, COUNTERFACTUAL_ID, "solution2", 1L, ExplainabilityStatus.SUCCEEDED, "", true, CounterfactualExplainabilityResult.Stage.FINAL, Collections.emptyList(), Collections.emptyList());
when(trustyService.getCounterfactualRequest(anyString(), anyString())).thenReturn(new CounterfactualExplainabilityRequest(EXECUTION_ID, SERVICE_URL, new ModelIdentifier("resourceType", "resourceIdentifier"), COUNTERFACTUAL_ID, Collections.emptyList(), List.of(goal), List.of(searchDomain), MAX_RUNNING_TIME_SECONDS));
when(trustyService.getCounterfactualResults(anyString(), anyString())).thenReturn(List.of(solution1, solution2));
Response response = explainabilityEndpoint.getCounterfactualDetails(EXECUTION_ID, COUNTERFACTUAL_ID);
assertNotNull(response);
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
Object entity = response.getEntity();
assertNotNull(entity);
assertTrue(entity instanceof CounterfactualResultsResponse);
CounterfactualResultsResponse resultsResponse = (CounterfactualResultsResponse) entity;
assertEquals(EXECUTION_ID, resultsResponse.getExecutionId());
assertEquals(COUNTERFACTUAL_ID, resultsResponse.getCounterfactualId());
assertEquals(MAX_RUNNING_TIME_SECONDS, resultsResponse.getMaxRunningTimeSeconds());
assertEquals(1, resultsResponse.getGoals().size());
assertEquals(goal, resultsResponse.getGoals().iterator().next());
assertEquals(1, resultsResponse.getSearchDomains().size());
assertEquals(searchDomain, resultsResponse.getSearchDomains().iterator().next());
assertEquals(2, resultsResponse.getSolutions().size());
assertEquals(solution1, resultsResponse.getSolutions().get(0));
assertEquals(solution2, resultsResponse.getSolutions().get(1));
}
use of org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest in project kogito-apps by kiegroup.
the class ExplainabilityApiV1Test method testGetCounterfactualResultsWhenExecutionDoesExist.
@Test
public void testGetCounterfactualResultsWhenExecutionDoesExist() {
NamedTypedValue goal = buildGoalUnit("unit", "string", new TextNode("hello"));
CounterfactualSearchDomain searchDomain = buildSearchDomainUnit("unit", "string", new CounterfactualDomainCategorical(List.of(new TextNode("hello"), new TextNode("goodbye"))));
when(trustyService.getCounterfactualRequest(anyString(), anyString())).thenReturn(new CounterfactualExplainabilityRequest(EXECUTION_ID, SERVICE_URL, new ModelIdentifier("resourceType", "resourceIdentifier"), COUNTERFACTUAL_ID, Collections.emptyList(), List.of(goal), List.of(searchDomain), MAX_RUNNING_TIME_SECONDS));
Response response = explainabilityEndpoint.getCounterfactualDetails(EXECUTION_ID, COUNTERFACTUAL_ID);
assertNotNull(response);
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
Object entity = response.getEntity();
assertNotNull(entity);
assertTrue(entity instanceof CounterfactualResultsResponse);
CounterfactualResultsResponse resultsResponse = (CounterfactualResultsResponse) entity;
assertEquals(EXECUTION_ID, resultsResponse.getExecutionId());
assertEquals(COUNTERFACTUAL_ID, resultsResponse.getCounterfactualId());
assertEquals(MAX_RUNNING_TIME_SECONDS, resultsResponse.getMaxRunningTimeSeconds());
assertEquals(1, resultsResponse.getGoals().size());
assertEquals(goal, resultsResponse.getGoals().iterator().next());
assertEquals(1, resultsResponse.getSearchDomains().size());
assertEquals(searchDomain, resultsResponse.getSearchDomains().iterator().next());
assertTrue(resultsResponse.getSolutions().isEmpty());
}
use of org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest in project kogito-apps by kiegroup.
the class ExplainabilityApiV1Test method testGetAllCounterfactualsWhenExecutionDoesExist.
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testGetAllCounterfactualsWhenExecutionDoesExist() {
when(trustyService.getCounterfactualRequests(anyString())).thenReturn(List.of(new CounterfactualExplainabilityRequest(EXECUTION_ID, SERVICE_URL, new ModelIdentifier("resourceType", "resourceIdentifier"), COUNTERFACTUAL_ID, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), MAX_RUNNING_TIME_SECONDS)));
Response response = explainabilityEndpoint.getAllCounterfactualsSummary(EXECUTION_ID);
assertNotNull(response);
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
Object entity = response.getEntity();
assertNotNull(entity);
assertTrue(entity instanceof List);
List<CounterfactualRequestResponse> counterfactualRequestResponse = (List) entity;
assertEquals(1, counterfactualRequestResponse.size());
CounterfactualRequestResponse counterfactual = counterfactualRequestResponse.get(0);
assertEquals(EXECUTION_ID, counterfactual.getExecutionId());
assertEquals(COUNTERFACTUAL_ID, counterfactual.getCounterfactualId());
assertEquals(MAX_RUNNING_TIME_SECONDS, counterfactual.getMaxRunningTimeSeconds());
}
use of org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest in project kogito-apps by kiegroup.
the class TrustyServiceTest method givenNoStoredCounterfactualRequestWhenGetCounterfactualRequestThenIllegalArgumentExceptionIsThrown.
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
void givenNoStoredCounterfactualRequestWhenGetCounterfactualRequestThenIllegalArgumentExceptionIsThrown() {
Storage<String, CounterfactualExplainabilityRequest> counterfactualStorage = mock(Storage.class);
Query queryMock = mock(Query.class);
when(queryMock.filter(any(List.class))).thenReturn(queryMock);
when(queryMock.execute()).thenReturn(new ArrayList<>());
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 TrustyServiceTest method givenSingleStoredCounterfactualRequestWhenGetCounterfactualRequestThenRequestIsReturned.
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
void givenSingleStoredCounterfactualRequestWhenGetCounterfactualRequestThenRequestIsReturned() {
Storage<String, CounterfactualExplainabilityRequest> counterfactualStorage = mock(Storage.class);
CounterfactualExplainabilityRequest request = mock(CounterfactualExplainabilityRequest.class);
Query queryMock = mock(Query.class);
when(queryMock.filter(any(List.class))).thenReturn(queryMock);
when(queryMock.execute()).thenReturn(List.of(request));
when(counterfactualStorage.query()).thenReturn(queryMock);
when(trustyStorageServiceMock.getCounterfactualRequestStorage()).thenReturn(counterfactualStorage);
assertEquals(request, trustyService.getCounterfactualRequest(TEST_EXECUTION_ID, TEST_COUNTERFACTUAL_ID));
}
Aggregations