Search in sources :

Example 26 with CounterfactualSearchDomain

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

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

Example 28 with CounterfactualSearchDomain

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

the class CounterfactualDomainSerialisationTest method testCounterfactualSearchDomain_Range_RoundTrip.

@Test
public void testCounterfactualSearchDomain_Range_RoundTrip() throws Exception {
    CounterfactualDomainRange domainRange = new CounterfactualDomainRange(new IntNode(18), new IntNode(65));
    CounterfactualSearchDomain searchDomain = new CounterfactualSearchDomain("age", new CounterfactualSearchDomainUnitValue("integer", "integer", Boolean.TRUE, domainRange));
    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 CounterfactualDomainRange);
    CounterfactualDomainRange roundTrippedDomainRange = (CounterfactualDomainRange) roundTrippedSearchDomain.getValue().toUnit().getDomain();
    assertEquals(domainRange.getLowerBound(), roundTrippedDomainRange.getLowerBound());
    assertEquals(domainRange.getUpperBound(), roundTrippedDomainRange.getUpperBound());
}
Also used : IntNode(com.fasterxml.jackson.databind.node.IntNode) CounterfactualDomainRange(org.kie.kogito.explainability.api.CounterfactualDomainRange) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain) Test(org.junit.jupiter.api.Test)

Aggregations

CounterfactualSearchDomain (org.kie.kogito.explainability.api.CounterfactualSearchDomain)28 Test (org.junit.jupiter.api.Test)22 CounterfactualDomainRange (org.kie.kogito.explainability.api.CounterfactualDomainRange)19 CounterfactualExplainabilityRequest (org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest)19 NamedTypedValue (org.kie.kogito.explainability.api.NamedTypedValue)17 IntNode (com.fasterxml.jackson.databind.node.IntNode)16 CounterfactualSearchDomainUnitValue (org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue)12 TextNode (com.fasterxml.jackson.databind.node.TextNode)11 ArrayList (java.util.ArrayList)9 CounterfactualDomainCategorical (org.kie.kogito.explainability.api.CounterfactualDomainCategorical)9 UnitValue (org.kie.kogito.tracing.typedvalue.UnitValue)9 List (java.util.List)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 Map (java.util.Map)4 CounterfactualDomain (org.kie.kogito.explainability.api.CounterfactualDomain)4 Feature (org.kie.kogito.explainability.model.Feature)4 StructureValue (org.kie.kogito.tracing.typedvalue.StructureValue)4 CounterfactualRequestResponse (org.kie.kogito.trusty.service.common.responses.CounterfactualRequestResponse)4 Decision (org.kie.kogito.trusty.storage.api.model.decision.Decision)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4