use of org.kie.kogito.tracing.typedvalue.UnitValue 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.tracing.typedvalue.UnitValue in project kogito-apps by kiegroup.
the class TrustyServiceTest method givenStoredExecutionWhenCounterfactualRequestIsMadeThenExplainabilityEventHasCorrectPayload.
@Test
@SuppressWarnings("unchecked")
void givenStoredExecutionWhenCounterfactualRequestIsMadeThenExplainabilityEventHasCorrectPayload() {
Storage<String, Decision> decisionStorage = mock(Storage.class);
Storage<String, CounterfactualExplainabilityRequest> counterfactualStorage = mock(Storage.class);
ArgumentCaptor<BaseExplainabilityRequest> explainabilityEventArgumentCaptor = ArgumentCaptor.forClass(BaseExplainabilityRequest.class);
Decision decision = new Decision(TEST_EXECUTION_ID, TEST_SOURCE_URL, TEST_SERVICE_URL, 0L, true, null, "model", "modelNamespace", List.of(new DecisionInput("IN1", "yearsOfService", new UnitValue("integer", "integer", new IntNode(10)))), List.of(new DecisionOutcome("OUT1", "salary", "SUCCEEDED", new UnitValue("integer", "integer", new IntNode(1000)), Collections.emptyList(), Collections.emptyList())));
when(decisionStorage.containsKey(eq(TEST_EXECUTION_ID))).thenReturn(true);
when(trustyStorageServiceMock.getDecisionsStorage()).thenReturn(decisionStorage);
when(trustyStorageServiceMock.getCounterfactualRequestStorage()).thenReturn(counterfactualStorage);
when(decisionStorage.get(eq(TEST_EXECUTION_ID))).thenReturn(decision);
trustyService.requestCounterfactuals(TEST_EXECUTION_ID, List.of(new NamedTypedValue("salary", new UnitValue("integer", "integer", new IntNode(2000)))), List.of(new CounterfactualSearchDomain("yearsOfService", new CounterfactualSearchDomainUnitValue("integer", "integer", false, new CounterfactualDomainRange(new IntNode(10), new IntNode(30))))));
verify(explainabilityRequestProducerMock).sendEvent(explainabilityEventArgumentCaptor.capture());
BaseExplainabilityRequest event = explainabilityEventArgumentCaptor.getValue();
CounterfactualExplainabilityRequest request = (CounterfactualExplainabilityRequest) event;
assertEquals(TEST_EXECUTION_ID, request.getExecutionId());
assertEquals(TEST_SERVICE_URL, request.getServiceUrl());
// Check original input value has been copied into CF request
assertEquals(1, request.getOriginalInputs().size());
assertTrue(request.getOriginalInputs().stream().anyMatch(i -> i.getName().equals("yearsOfService")));
// It is safe to use the iterator unchecked as the collection only contains one item
assertEquals(decision.getInputs().iterator().next().getValue().toUnit().getValue().asInt(), request.getOriginalInputs().iterator().next().getValue().toUnit().getValue().asInt());
// Check CF goals have been copied into CF request
assertEquals(1, request.getGoals().size());
assertTrue(request.getGoals().stream().anyMatch(g -> g.getName().equals("salary")));
// It is safe to use the iterator unchecked as the collection only contains one item
assertEquals(2000, request.getGoals().iterator().next().getValue().toUnit().getValue().asInt());
// Check CF search domains have been copied into CF request
assertEquals(1, request.getSearchDomains().size());
assertTrue(request.getSearchDomains().stream().anyMatch(sd -> sd.getName().equals("yearsOfService")));
// It is safe to use the iterator unchecked as the collection only contains one item
CounterfactualSearchDomainValue searchDomain = request.getSearchDomains().iterator().next().getValue();
assertTrue(searchDomain instanceof CounterfactualSearchDomainUnitValue);
CounterfactualSearchDomainUnitValue unit = (CounterfactualSearchDomainUnitValue) searchDomain;
assertFalse(unit.isFixed());
assertNotNull(unit.getDomain());
assertTrue(unit.getDomain() instanceof CounterfactualDomainRange);
CounterfactualDomainRange range = (CounterfactualDomainRange) unit.getDomain();
assertEquals(10, range.getLowerBound().asInt());
assertEquals(30, range.getUpperBound().asInt());
// Check Max Running Time Seconds
assertEquals(MAX_RUNNING_TIME_SECONDS, request.getMaxRunningTimeSeconds());
}
use of org.kie.kogito.tracing.typedvalue.UnitValue in project kogito-apps by kiegroup.
the class AbstractTrustyServiceIT method testStoreExplainabilityResult_Counterfactual_DuplicateRemoval_FinalThenIntermediate.
@Test
public void testStoreExplainabilityResult_Counterfactual_DuplicateRemoval_FinalThenIntermediate() {
String executionId = "myCFExecution1Store";
String counterfactualId = "myCFCounterfactualId";
NamedTypedValue input1 = new NamedTypedValue("field1", new UnitValue("typeRef1", "typeRef1", new IntNode(25)));
NamedTypedValue input2 = new NamedTypedValue("field2", new UnitValue("typeRef2", "typeRef2", new IntNode(99)));
NamedTypedValue output1 = new NamedTypedValue("field3", new UnitValue("typeRef3", "typeRef3", new IntNode(200)));
NamedTypedValue output2 = new NamedTypedValue("field4", new UnitValue("typeRef4", "typeRef4", new IntNode(1000)));
// First solution is the FINAL (for whatever reason, e.g. messaging delays, the INTERMEDIATE is received afterwards)
trustyService.storeExplainabilityResult(executionId, new CounterfactualExplainabilityResult(executionId, counterfactualId, "solutionId1", 0L, ExplainabilityStatus.SUCCEEDED, "status", true, CounterfactualExplainabilityResult.Stage.FINAL, List.of(input1, input2), List.of(output1, output2)));
List<CounterfactualExplainabilityResult> result1 = trustyService.getCounterfactualResults(executionId, counterfactualId);
assertNotNull(result1);
assertEquals(1, result1.size());
assertEquals("solutionId1", result1.get(0).getSolutionId());
assertEquals(CounterfactualExplainabilityResult.Stage.FINAL, result1.get(0).getStage());
trustyService.storeExplainabilityResult(executionId, new CounterfactualExplainabilityResult(executionId, counterfactualId, "solutionId2", 0L, ExplainabilityStatus.SUCCEEDED, "status", true, CounterfactualExplainabilityResult.Stage.INTERMEDIATE, List.of(input1, input2), List.of(output1, output2)));
List<CounterfactualExplainabilityResult> result2 = trustyService.getCounterfactualResults(executionId, counterfactualId);
assertNotNull(result2);
assertEquals(1, result1.size());
assertEquals("solutionId1", result1.get(0).getSolutionId());
assertEquals(CounterfactualExplainabilityResult.Stage.FINAL, result1.get(0).getStage());
}
use of org.kie.kogito.tracing.typedvalue.UnitValue in project kogito-apps by kiegroup.
the class AbstractTrustyServiceIT method storeExecutionWithOutcomes.
private void storeExecutionWithOutcomes(String executionId, Long timestamp) {
DecisionInput decisionInput = new DecisionInput();
decisionInput.setId("inputId");
decisionInput.setName("test");
decisionInput.setValue(new UnitValue("number", "number", JsonNodeFactory.instance.numberNode(10)));
DecisionOutcome decisionOutcome1 = new DecisionOutcome();
decisionOutcome1.setOutcomeId("outcomeId1");
decisionOutcome1.setOutcomeName("outcome1");
decisionOutcome1.setOutcomeResult(new UnitValue("number", "number", JsonNodeFactory.instance.numberNode(20)));
DecisionOutcome decisionOutcome2 = new DecisionOutcome();
decisionOutcome2.setOutcomeId("outcomeId2");
decisionOutcome2.setOutcomeName("outcome2");
decisionOutcome2.setOutcomeResult(new UnitValue("string", "string", JsonNodeFactory.instance.textNode("food")));
Decision decision = new Decision();
decision.setExecutionId(executionId);
decision.setExecutionTimestamp(timestamp);
decision.setServiceUrl("serviceUrl");
decision.setExecutedModelNamespace("executedModelNamespace");
decision.setExecutedModelName("executedModelName");
decision.setInputs(Collections.singletonList(decisionInput));
decision.setOutcomes(List.of(decisionOutcome1, decisionOutcome2));
trustyService.storeDecision(decision.getExecutionId(), decision);
}
use of org.kie.kogito.tracing.typedvalue.UnitValue in project kogito-runtimes by kiegroup.
the class TraceEventTest method getTraceInputValue.
private TraceInputValue getTraceInputValue(int id) {
String valueType = "type-" + id;
TypedValue value = new UnitValue(valueType);
List<Message> messages = IntStream.range(id + 1, id + 3).mapToObj(DecisionMessageTest::getDecisionMessage).collect(Collectors.toList());
return new TraceInputValue("id-" + id, "name-" + id, value, messages);
}
Aggregations