Search in sources :

Example 6 with CounterfactualDomainCategorical

use of org.kie.kogito.explainability.api.CounterfactualDomainCategorical 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 7 with CounterfactualDomainCategorical

use of org.kie.kogito.explainability.api.CounterfactualDomainCategorical in project kogito-apps by kiegroup.

the class AbstractTrustyServiceIT method assertCounterfactualDomainCategorical.

private void assertCounterfactualDomainCategorical(CounterfactualDomain expectedDomain, CounterfactualDomain actualDomain) {
    assertTrue(expectedDomain instanceof CounterfactualDomainCategorical);
    assertTrue(actualDomain instanceof CounterfactualDomainCategorical);
    CounterfactualDomainCategorical expectedDomainCategorical = (CounterfactualDomainCategorical) expectedDomain;
    CounterfactualDomainCategorical actualDomainCategorical = (CounterfactualDomainCategorical) actualDomain;
    assertEquals(expectedDomainCategorical.getCategories().size(), actualDomainCategorical.getCategories().size());
    assertTrue(expectedDomainCategorical.getCategories().containsAll(actualDomainCategorical.getCategories()));
}
Also used : CounterfactualDomainCategorical(org.kie.kogito.explainability.api.CounterfactualDomainCategorical)

Example 8 with CounterfactualDomainCategorical

use of org.kie.kogito.explainability.api.CounterfactualDomainCategorical in project kogito-apps by kiegroup.

the class AbstractTrustyServiceIT method testCounterfactuals_StoreSingleAndRetrieveSingleWithSearchDomainCategorical.

@Test
public void testCounterfactuals_StoreSingleAndRetrieveSingleWithSearchDomainCategorical() {
    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.
    Collection<JsonNode> categories = List.of(new TextNode("1"), new TextNode("2"));
    CounterfactualSearchDomain searchDomain = buildSearchDomainUnit("test", "number", new CounterfactualDomainCategorical(categories));
    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());
    assertCounterfactualSearchDomainCategorical(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());
    assertCounterfactualSearchDomainCategorical(searchDomain, resultSearchDomains.get(0));
}
Also used : CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) CounterfactualDomainCategorical(org.kie.kogito.explainability.api.CounterfactualDomainCategorical) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain) Test(org.junit.jupiter.api.Test)

Example 9 with CounterfactualDomainCategorical

use of org.kie.kogito.explainability.api.CounterfactualDomainCategorical in project kogito-apps by kiegroup.

the class CounterfactualDomainSerialisationTest method testCounterfactualSearchDomain_Categorical_RoundTrip.

@Test
public void testCounterfactualSearchDomain_Categorical_RoundTrip() throws Exception {
    CounterfactualDomainCategorical domainCategorical = new CounterfactualDomainCategorical(List.of(new TextNode("A"), new TextNode("B")));
    CounterfactualSearchDomain searchDomain = new CounterfactualSearchDomain("age", new CounterfactualSearchDomainUnitValue("integer", "integer", Boolean.TRUE, domainCategorical));
    mapper.writeValue(writer, searchDomain);
    String searchDomainJson = writer.toString();
    assertNotNull(searchDomainJson);
    CounterfactualSearchDomain roundTrippedSearchDomain = mapper.readValue(searchDomainJson, CounterfactualSearchDomain.class);
    assertTrue(roundTrippedSearchDomain.getValue() instanceof CounterfactualSearchDomainUnitValue);
    assertEquals(searchDomain.getValue().getKind(), roundTrippedSearchDomain.getValue().getKind());
    assertEquals(searchDomain.getName(), roundTrippedSearchDomain.getName());
    assertEquals(searchDomain.getValue().getType(), roundTrippedSearchDomain.getValue().getType());
    assertEquals(searchDomain.getValue().toUnit().getBaseType(), roundTrippedSearchDomain.getValue().toUnit().getBaseType());
    assertEquals(searchDomain.getValue().toUnit().isFixed(), roundTrippedSearchDomain.getValue().toUnit().isFixed());
    assertTrue(roundTrippedSearchDomain.getValue().toUnit().getDomain() instanceof CounterfactualDomainCategorical);
    CounterfactualDomainCategorical roundTrippedDomainCategorical = (CounterfactualDomainCategorical) roundTrippedSearchDomain.getValue().toUnit().getDomain();
    assertEquals(domainCategorical.getCategories().size(), roundTrippedDomainCategorical.getCategories().size());
    assertTrue(roundTrippedDomainCategorical.getCategories().containsAll(domainCategorical.getCategories()));
}
Also used : CounterfactualDomainCategorical(org.kie.kogito.explainability.api.CounterfactualDomainCategorical) TextNode(com.fasterxml.jackson.databind.node.TextNode) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain) Test(org.junit.jupiter.api.Test)

Aggregations

CounterfactualDomainCategorical (org.kie.kogito.explainability.api.CounterfactualDomainCategorical)9 Test (org.junit.jupiter.api.Test)7 CounterfactualSearchDomain (org.kie.kogito.explainability.api.CounterfactualSearchDomain)6 TextNode (com.fasterxml.jackson.databind.node.TextNode)4 NamedTypedValue (org.kie.kogito.explainability.api.NamedTypedValue)4 CounterfactualRequestResponse (org.kie.kogito.trusty.service.common.responses.CounterfactualRequestResponse)4 ArrayList (java.util.ArrayList)3 CounterfactualDomainRange (org.kie.kogito.explainability.api.CounterfactualDomainRange)3 CounterfactualExplainabilityRequest (org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 QuarkusTest (io.quarkus.test.junit.QuarkusTest)2 RequestLoggingFilter (io.restassured.filter.log.RequestLoggingFilter)2 ResponseLoggingFilter (io.restassured.filter.log.ResponseLoggingFilter)2 List (java.util.List)2 Response (javax.ws.rs.core.Response)2 CounterfactualSearchDomainUnitValue (org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue)2 ModelIdentifier (org.kie.kogito.explainability.api.ModelIdentifier)2 CounterfactualResultsResponse (org.kie.kogito.trusty.service.common.responses.CounterfactualResultsResponse)2 Map (java.util.Map)1 CounterfactualExplainabilityResult (org.kie.kogito.explainability.api.CounterfactualExplainabilityResult)1