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;
}
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());
}
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);
}
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());
}
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());
}
Aggregations