Search in sources :

Example 21 with Decision

use of org.kie.kogito.trusty.storage.api.model.decision.Decision in project kogito-apps by kiegroup.

the class TrustyServiceImpl method requestCounterfactuals.

@Override
public CounterfactualExplainabilityRequest requestCounterfactuals(String executionId, List<NamedTypedValue> goals, List<CounterfactualSearchDomain> searchDomains) {
    Storage<String, Decision> storage = storageService.getDecisionsStorage();
    if (!storage.containsKey(executionId)) {
        throw new IllegalArgumentException(String.format("A decision with ID %s is not present in the storage. Counterfactuals cannot be requested.", executionId));
    }
    CounterfactualExplainabilityRequest request = makeCounterfactualRequest(executionId, goals, searchDomains, maxRunningTimeSeconds);
    storeCounterfactualRequest(request);
    sendCounterfactualRequestEvent(request);
    return request;
}
Also used : CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) Decision(org.kie.kogito.trusty.storage.api.model.decision.Decision)

Example 22 with Decision

use of org.kie.kogito.trusty.storage.api.model.decision.Decision in project kogito-apps by kiegroup.

the class TrustyServiceImpl method getExecutionHeaders.

@Override
public MatchedExecutionHeaders getExecutionHeaders(OffsetDateTime from, OffsetDateTime to, int limit, int offset, String prefix) {
    Storage<String, Decision> storage = storageService.getDecisionsStorage();
    List<AttributeFilter<?>> filters = new ArrayList<>();
    filters.add(QueryFilterFactory.like(Execution.EXECUTION_ID_FIELD, prefix + "*"));
    filters.add(QueryFilterFactory.greaterThanEqual(Execution.EXECUTION_TIMESTAMP_FIELD, from.toInstant().toEpochMilli()));
    filters.add(QueryFilterFactory.lessThanEqual(Execution.EXECUTION_TIMESTAMP_FIELD, to.toInstant().toEpochMilli()));
    ArrayList result = new ArrayList<>(storage.query().sort(asList(orderBy(Execution.EXECUTION_TIMESTAMP_FIELD, DESC))).filter(filters).execute());
    if (result.size() < offset) {
        throw new IllegalArgumentException("Out of bound start offset in result");
    }
    return new MatchedExecutionHeaders(result.subList(offset, Math.min(offset + limit, result.size())), result.size());
}
Also used : MatchedExecutionHeaders(org.kie.kogito.trusty.service.common.models.MatchedExecutionHeaders) AttributeFilter(org.kie.kogito.persistence.api.query.AttributeFilter) ArrayList(java.util.ArrayList) Decision(org.kie.kogito.trusty.storage.api.model.decision.Decision)

Example 23 with Decision

use of org.kie.kogito.trusty.storage.api.model.decision.Decision in project kogito-apps by kiegroup.

the class ExplainerServiceHandlerRegistryTest method setup.

@BeforeEach
@SuppressWarnings("unchecked")
public void setup() {
    TrustyStorageService trustyStorage = mock(TrustyStorageService.class);
    limeExplainerServiceHandler = spy(new LIMEExplainerServiceHandler(trustyStorage));
    counterfactualExplainerServiceHandler = spy(new CounterfactualExplainerServiceHandler(trustyStorage, mock(CounterfactualExplainabilityResultsManagerSlidingWindow.class), mock(CounterfactualExplainabilityResultsManagerDuplicates.class)));
    Instance<ExplainerServiceHandler<?>> explanationHandlers = mock(Instance.class);
    when(explanationHandlers.stream()).thenReturn(Stream.of(limeExplainerServiceHandler, counterfactualExplainerServiceHandler));
    registry = new ExplainerServiceHandlerRegistry(explanationHandlers);
    storageLIME = mock(Storage.class);
    storageCounterfactual = mock(Storage.class);
    when(trustyStorage.getLIMEResultStorage()).thenReturn(storageLIME);
    when(trustyStorage.getCounterfactualResultStorage()).thenReturn(storageCounterfactual);
    decision = mock(Decision.class);
}
Also used : Storage(org.kie.kogito.persistence.api.Storage) TrustyStorageService(org.kie.kogito.trusty.storage.common.TrustyStorageService) Decision(org.kie.kogito.trusty.storage.api.model.decision.Decision) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 24 with Decision

use of org.kie.kogito.trusty.storage.api.model.decision.Decision in project kogito-apps by kiegroup.

the class LIMESaliencyConverterTest method testFromResult_DecisionExists_WhenOutcomeNameNotFound.

@Test
public void testFromResult_DecisionExists_WhenOutcomeNameNotFound() {
    LIMEExplainabilityResult result = LIMEExplainabilityResult.buildSucceeded(EXECUTION_ID, List.of(new SaliencyModel("outcomeName1", List.of(new FeatureImportanceModel("feature1", 1.0))), new SaliencyModel("outcomeName2", List.of(new FeatureImportanceModel("feature2", 2.0)))));
    Decision decision = new Decision(EXECUTION_ID, "sourceUrl", "serviceUrl", 0L, true, "executorName", "executorModelName", "executorModelNamespace", new ArrayList<>(), new ArrayList<>());
    decision.getOutcomes().add(new DecisionOutcome("outcomeId1", "outcomeName1", ExplainabilityStatus.SUCCEEDED.name(), new UnitValue("type", new IntNode(1)), Collections.emptyList(), Collections.emptyList()));
    decision.getOutcomes().add(new DecisionOutcome("outcomeId2", "outcomeNameX", ExplainabilityStatus.SUCCEEDED.name(), new UnitValue("type2", new IntNode(2)), Collections.emptyList(), Collections.emptyList()));
    when(trustyService.getDecisionById(eq(EXECUTION_ID))).thenReturn(decision);
    SalienciesResponse response = converter.fromResult(EXECUTION_ID, result);
    assertNotNull(response);
    assertEquals(ExplainabilityStatus.SUCCEEDED.name(), response.getStatus());
    assertEquals(1, response.getSaliencies().size());
    List<SaliencyResponse> saliencyResponses = new ArrayList<>(response.getSaliencies());
    SaliencyResponse saliencyResponse1 = saliencyResponses.get(0);
    assertEquals("outcomeId1", saliencyResponse1.getOutcomeId());
    assertEquals("outcomeName1", saliencyResponse1.getOutcomeName());
    assertEquals(1, saliencyResponse1.getFeatureImportance().size());
    Optional<FeatureImportanceModel> oFeatureImportance1Model1 = saliencyResponse1.getFeatureImportance().stream().filter(fim -> fim.getFeatureName().equals("feature1")).findFirst();
    assertTrue(oFeatureImportance1Model1.isPresent());
    assertEquals(1.0, oFeatureImportance1Model1.get().getFeatureScore());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) BeforeEach(org.junit.jupiter.api.BeforeEach) LIMEExplainabilityResult(org.kie.kogito.explainability.api.LIMEExplainabilityResult) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) FeatureImportanceModel(org.kie.kogito.explainability.api.FeatureImportanceModel) Decision(org.kie.kogito.trusty.storage.api.model.decision.Decision) IntNode(com.fasterxml.jackson.databind.node.IntNode) Mock(org.mockito.Mock) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) SaliencyResponse(org.kie.kogito.trusty.service.common.responses.SaliencyResponse) ArrayList(java.util.ArrayList) SalienciesResponse(org.kie.kogito.trusty.service.common.responses.SalienciesResponse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ExplainabilityStatus(org.kie.kogito.explainability.api.ExplainabilityStatus) Mockito.when(org.mockito.Mockito.when) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) Test(org.junit.jupiter.api.Test) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) SaliencyModel(org.kie.kogito.explainability.api.SaliencyModel) Collections(java.util.Collections) TrustyService(org.kie.kogito.trusty.service.common.TrustyService) DecisionOutcome(org.kie.kogito.trusty.storage.api.model.decision.DecisionOutcome) SalienciesResponse(org.kie.kogito.trusty.service.common.responses.SalienciesResponse) LIMEExplainabilityResult(org.kie.kogito.explainability.api.LIMEExplainabilityResult) SaliencyResponse(org.kie.kogito.trusty.service.common.responses.SaliencyResponse) SaliencyModel(org.kie.kogito.explainability.api.SaliencyModel) DecisionOutcome(org.kie.kogito.trusty.storage.api.model.decision.DecisionOutcome) ArrayList(java.util.ArrayList) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) Decision(org.kie.kogito.trusty.storage.api.model.decision.Decision) IntNode(com.fasterxml.jackson.databind.node.IntNode) FeatureImportanceModel(org.kie.kogito.explainability.api.FeatureImportanceModel) Test(org.junit.jupiter.api.Test)

Example 25 with Decision

use of org.kie.kogito.trusty.storage.api.model.decision.Decision in project kogito-apps by kiegroup.

the class TrustyServiceTest method givenADecisionWhenADecisionIsStoredAndRetrievedThenTheOriginalObjectIsReturned.

@Test
@SuppressWarnings("unchecked")
void givenADecisionWhenADecisionIsStoredAndRetrievedThenTheOriginalObjectIsReturned() {
    Decision decision = new Decision();
    decision.setExecutionId(TEST_EXECUTION_ID);
    Query queryMock = mock(Query.class);
    when(queryMock.filter(any(List.class))).thenReturn(queryMock);
    when(queryMock.offset(any(Integer.class))).thenReturn(queryMock);
    when(queryMock.sort(any(List.class))).thenReturn(queryMock);
    when(queryMock.execute()).thenReturn(List.of(decision));
    Storage storageMock = mock(Storage.class);
    when(storageMock.put(eq(TEST_EXECUTION_ID), any(Object.class))).thenReturn(decision);
    when(storageMock.containsKey(eq(TEST_EXECUTION_ID))).thenReturn(false);
    when(storageMock.query()).thenReturn(queryMock);
    when(trustyStorageServiceMock.getDecisionsStorage()).thenReturn(storageMock);
    trustyService.storeDecision(TEST_EXECUTION_ID, decision);
    MatchedExecutionHeaders result = trustyService.getExecutionHeaders(OffsetDateTime.now().minusDays(1), OffsetDateTime.now(), 100, 0, "");
    assertEquals(1, result.getExecutions().size());
    assertEquals(decision.getExecutionId(), result.getExecutions().get(0).getExecutionId());
}
Also used : Storage(org.kie.kogito.persistence.api.Storage) MatchedExecutionHeaders(org.kie.kogito.trusty.service.common.models.MatchedExecutionHeaders) Query(org.kie.kogito.persistence.api.query.Query) List(java.util.List) ArrayList(java.util.ArrayList) Decision(org.kie.kogito.trusty.storage.api.model.decision.Decision) Test(org.junit.jupiter.api.Test)

Aggregations

Decision (org.kie.kogito.trusty.storage.api.model.decision.Decision)30 Test (org.junit.jupiter.api.Test)18 UnitValue (org.kie.kogito.tracing.typedvalue.UnitValue)12 DecisionOutcome (org.kie.kogito.trusty.storage.api.model.decision.DecisionOutcome)10 ArrayList (java.util.ArrayList)9 DecisionInput (org.kie.kogito.trusty.storage.api.model.decision.DecisionInput)8 IntNode (com.fasterxml.jackson.databind.node.IntNode)7 List (java.util.List)7 CounterfactualExplainabilityRequest (org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest)6 NamedTypedValue (org.kie.kogito.explainability.api.NamedTypedValue)6 Storage (org.kie.kogito.persistence.api.Storage)6 MatchedExecutionHeaders (org.kie.kogito.trusty.service.common.models.MatchedExecutionHeaders)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 Collections (java.util.Collections)5 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)5 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)5 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 CounterfactualSearchDomain (org.kie.kogito.explainability.api.CounterfactualSearchDomain)5 ExplainabilityStatus (org.kie.kogito.explainability.api.ExplainabilityStatus)5