Search in sources :

Example 31 with CounterfactualExplainabilityRequest

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));
}
Also used : CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) CounterfactualRequestResponse(org.kie.kogito.trusty.service.common.responses.CounterfactualRequestResponse) Response(javax.ws.rs.core.Response) CounterfactualResultsResponse(org.kie.kogito.trusty.service.common.responses.CounterfactualResultsResponse) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) ModelIdentifier(org.kie.kogito.explainability.api.ModelIdentifier) TextNode(com.fasterxml.jackson.databind.node.TextNode) CounterfactualDomainCategorical(org.kie.kogito.explainability.api.CounterfactualDomainCategorical) CounterfactualExplainabilityResult(org.kie.kogito.explainability.api.CounterfactualExplainabilityResult) CounterfactualResultsResponse(org.kie.kogito.trusty.service.common.responses.CounterfactualResultsResponse) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain) Test(org.junit.jupiter.api.Test)

Example 32 with CounterfactualExplainabilityRequest

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());
}
Also used : CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) CounterfactualRequestResponse(org.kie.kogito.trusty.service.common.responses.CounterfactualRequestResponse) Response(javax.ws.rs.core.Response) CounterfactualResultsResponse(org.kie.kogito.trusty.service.common.responses.CounterfactualResultsResponse) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) ModelIdentifier(org.kie.kogito.explainability.api.ModelIdentifier) TextNode(com.fasterxml.jackson.databind.node.TextNode) CounterfactualDomainCategorical(org.kie.kogito.explainability.api.CounterfactualDomainCategorical) CounterfactualResultsResponse(org.kie.kogito.trusty.service.common.responses.CounterfactualResultsResponse) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain) Test(org.junit.jupiter.api.Test)

Example 33 with CounterfactualExplainabilityRequest

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());
}
Also used : CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) CounterfactualRequestResponse(org.kie.kogito.trusty.service.common.responses.CounterfactualRequestResponse) Response(javax.ws.rs.core.Response) CounterfactualResultsResponse(org.kie.kogito.trusty.service.common.responses.CounterfactualResultsResponse) ModelIdentifier(org.kie.kogito.explainability.api.ModelIdentifier) List(java.util.List) CounterfactualRequestResponse(org.kie.kogito.trusty.service.common.responses.CounterfactualRequestResponse) Test(org.junit.jupiter.api.Test)

Example 34 with CounterfactualExplainabilityRequest

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));
}
Also used : CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) Query(org.kie.kogito.persistence.api.query.Query) List(java.util.List) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 35 with CounterfactualExplainabilityRequest

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));
}
Also used : CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) Query(org.kie.kogito.persistence.api.query.Query) List(java.util.List) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Aggregations

CounterfactualExplainabilityRequest (org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest)41 Test (org.junit.jupiter.api.Test)37 CounterfactualSearchDomain (org.kie.kogito.explainability.api.CounterfactualSearchDomain)23 IntNode (com.fasterxml.jackson.databind.node.IntNode)22 NamedTypedValue (org.kie.kogito.explainability.api.NamedTypedValue)21 CounterfactualSearchDomainUnitValue (org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue)18 CounterfactualDomainRange (org.kie.kogito.explainability.api.CounterfactualDomainRange)17 UnitValue (org.kie.kogito.tracing.typedvalue.UnitValue)17 List (java.util.List)11 CounterfactualSearchDomainStructureValue (org.kie.kogito.explainability.api.CounterfactualSearchDomainStructureValue)11 StructureValue (org.kie.kogito.tracing.typedvalue.StructureValue)10 ArrayList (java.util.ArrayList)9 ModelIdentifier (org.kie.kogito.explainability.api.ModelIdentifier)9 TextNode (com.fasterxml.jackson.databind.node.TextNode)8 CounterfactualExplainabilityResult (org.kie.kogito.explainability.api.CounterfactualExplainabilityResult)8 CounterfactualSearchDomainCollectionValue (org.kie.kogito.explainability.api.CounterfactualSearchDomainCollectionValue)8 CounterfactualPrediction (org.kie.kogito.explainability.model.CounterfactualPrediction)8 Prediction (org.kie.kogito.explainability.model.Prediction)8 CollectionValue (org.kie.kogito.tracing.typedvalue.CollectionValue)8 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7