Search in sources :

Example 6 with Decision

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

the class TraceEventConverterTest method doTest.

private static void doTest(TraceEvent traceEvent, Decision expectedDecision) throws JsonProcessingException, JSONException {
    Decision actualDecision = TraceEventConverter.toDecision(traceEvent, TrustyServiceTestUtils.CLOUDEVENT_SOURCE, TrustyServiceTestUtils.CLOUDEVENT_SERVICE);
    JSONAssert.assertEquals(MAPPER.writeValueAsString(expectedDecision), MAPPER.writeValueAsString(actualDecision), false);
}
Also used : Decision(org.kie.kogito.trusty.storage.api.model.decision.Decision)

Example 7 with Decision

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

the class DecisionMarshallerTest method testWriteAndRead.

@Test
public void testWriteAndRead() throws IOException {
    List<DecisionInput> inputs = Collections.singletonList(new DecisionInput("id", "in", new UnitValue("nameIn", "number", JsonNodeFactory.instance.numberNode(10))));
    List<DecisionOutcome> outcomes = Collections.singletonList(new DecisionOutcome("id", "out", DMNDecisionResult.DecisionEvaluationStatus.SUCCEEDED.toString(), new UnitValue("nameOut", "number", JsonNodeFactory.instance.numberNode(10)), List.of(new NamedTypedValue("nameOut", new UnitValue("number", "number", JsonNodeFactory.instance.numberNode(10)))), new ArrayList<>()));
    Decision decision = new Decision("executionId", "source", "serviceUrl", 0L, true, "executor", "model", "namespace", inputs, outcomes);
    DecisionMarshaller marshaller = new DecisionMarshaller(new ObjectMapper());
    marshaller.writeTo(writer, decision);
    Decision retrieved = marshaller.readFrom(reader);
    Assertions.assertEquals(decision.getExecutionId(), retrieved.getExecutionId());
    Assertions.assertEquals(decision.getSourceUrl(), retrieved.getSourceUrl());
    Assertions.assertEquals(decision.getServiceUrl(), retrieved.getServiceUrl());
    Assertions.assertEquals(decision.getExecutedModelName(), retrieved.getExecutedModelName());
    Assertions.assertEquals(inputs.get(0).getName(), retrieved.getInputs().stream().findFirst().get().getName());
    Assertions.assertEquals(inputs.get(0).getValue().getType(), retrieved.getInputs().stream().findFirst().get().getValue().getType());
    Assertions.assertEquals(inputs.get(0).getValue().toUnit().getBaseType(), retrieved.getInputs().stream().findFirst().get().getValue().toUnit().getBaseType());
    Assertions.assertEquals(outcomes.get(0).getOutcomeId(), retrieved.getOutcomes().stream().findFirst().get().getOutcomeId());
    Assertions.assertTrue(retrieved.getOutcomes().stream().findFirst().get().getMessages().isEmpty());
}
Also used : DecisionInput(org.kie.kogito.trusty.storage.api.model.decision.DecisionInput) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) 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) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 8 with Decision

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

the class TrustyServiceTest method doGivenStoredExecutionWhenCounterfactualRequestIsMadeThenExplainabilityEventIsEmittedTest.

@SuppressWarnings("unchecked")
void doGivenStoredExecutionWhenCounterfactualRequestIsMadeThenExplainabilityEventIsEmittedTest(CounterfactualDomain domain) {
    Storage<String, Decision> decisionStorage = mock(Storage.class);
    Storage<String, CounterfactualExplainabilityRequest> counterfactualStorage = mock(Storage.class);
    ArgumentCaptor<BaseExplainabilityRequest> explainabilityEventArgumentCaptor = ArgumentCaptor.forClass(BaseExplainabilityRequest.class);
    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(TrustyServiceTestUtils.buildCorrectDecision(TEST_EXECUTION_ID));
    // The Goals structures must be comparable to the original decisions outcomes.
    // The Search Domain structures must be identical those of the original decision inputs.
    trustyService.requestCounterfactuals(TEST_EXECUTION_ID, List.of(new NamedTypedValue("Fine", new StructureValue("tFine", Map.of("Amount", new UnitValue("number", "number", new IntNode(0)), "Points", new UnitValue("number", "number", new IntNode(0))))), new NamedTypedValue("Should the driver be suspended?", new UnitValue("string", "string", new TextNode("No")))), List.of(new CounterfactualSearchDomain("Violation", new CounterfactualSearchDomainStructureValue("tViolation", Map.of("Type", new CounterfactualSearchDomainUnitValue("string", "string", true, domain), "Actual Speed", new CounterfactualSearchDomainUnitValue("number", "number", true, domain), "Speed Limit", new CounterfactualSearchDomainUnitValue("number", "number", true, domain)))), new CounterfactualSearchDomain("Driver", new CounterfactualSearchDomainStructureValue("tDriver", Map.of("Age", new CounterfactualSearchDomainUnitValue("number", "number", true, domain), "Points", new CounterfactualSearchDomainUnitValue("number", "number", true, domain))))));
    verify(explainabilityRequestProducerMock).sendEvent(explainabilityEventArgumentCaptor.capture());
    BaseExplainabilityRequest event = explainabilityEventArgumentCaptor.getValue();
    assertNotNull(event);
    assertTrue(event instanceof CounterfactualExplainabilityRequest);
    CounterfactualExplainabilityRequest request = (CounterfactualExplainabilityRequest) event;
    assertEquals(TEST_EXECUTION_ID, request.getExecutionId());
}
Also used : CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) TextNode(com.fasterxml.jackson.databind.node.TextNode) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Decision(org.kie.kogito.trusty.storage.api.model.decision.Decision) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) IntNode(com.fasterxml.jackson.databind.node.IntNode) BaseExplainabilityRequest(org.kie.kogito.explainability.api.BaseExplainabilityRequest) StructureValue(org.kie.kogito.tracing.typedvalue.StructureValue) CounterfactualSearchDomainStructureValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainStructureValue) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain) CounterfactualSearchDomainStructureValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainStructureValue)

Example 9 with Decision

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

the class TrustyServiceTest method givenADecisionWhenADecisionIsStoredAndRetrievedByIdThenTheOriginalObjectIsReturned.

@Test
@SuppressWarnings("unchecked")
void givenADecisionWhenADecisionIsStoredAndRetrievedByIdThenTheOriginalObjectIsReturned() {
    Decision decision = new Decision();
    decision.setExecutionId(TEST_EXECUTION_ID);
    @SuppressWarnings("unchecked") Storage storageMock = new StorageImplMock(Decision.class);
    when(trustyStorageServiceMock.getDecisionsStorage()).thenReturn(storageMock);
    trustyService.storeDecision(TEST_EXECUTION_ID, decision);
    Decision result = trustyService.getDecisionById(TEST_EXECUTION_ID);
    assertEquals(TEST_EXECUTION_ID, result.getExecutionId());
}
Also used : StorageImplMock(org.kie.kogito.trusty.service.common.mocks.StorageImplMock) Storage(org.kie.kogito.persistence.api.Storage) Decision(org.kie.kogito.trusty.storage.api.model.decision.Decision) Test(org.junit.jupiter.api.Test)

Example 10 with Decision

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

the class TrustyServiceTest method givenManyExecutionsThenPaginationWorksProperly.

@Test
@SuppressWarnings("unchecked")
void givenManyExecutionsThenPaginationWorksProperly() {
    List<Decision> decisions = new ArrayList<>();
    IntStream.range(0, 10).forEach(x -> {
        Decision d = new Decision();
        d.setExecutionId(String.valueOf(x));
        decisions.add(d);
    });
    Query queryMock = mock(Query.class);
    when(queryMock.filter(any(List.class))).thenReturn(queryMock);
    when(queryMock.sort(any(List.class))).thenReturn(queryMock);
    when(queryMock.execute()).thenReturn(decisions);
    Storage storageMock = mock(Storage.class);
    decisions.forEach(x -> {
        when(storageMock.put(eq(x.getExecutionId()), any(Object.class))).thenReturn(x);
        when(storageMock.containsKey(eq(x.getExecutionId()))).thenReturn(false);
    });
    when(storageMock.query()).thenReturn(queryMock);
    when(trustyStorageServiceMock.getDecisionsStorage()).thenReturn(storageMock);
    decisions.forEach(x -> trustyService.storeDecision(x.getExecutionId(), x));
    MatchedExecutionHeaders result = trustyService.getExecutionHeaders(OffsetDateTime.now().minusDays(1), OffsetDateTime.now(), 3, 5, "");
    assertEquals(3, result.getExecutions().size());
    assertEquals(decisions.size(), result.getAvailableResults());
    result = trustyService.getExecutionHeaders(OffsetDateTime.now().minusDays(1), OffsetDateTime.now(), 100, 5, "");
    assertEquals(5, result.getExecutions().size());
    assertEquals(decisions.size(), result.getAvailableResults());
}
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) ArrayList(java.util.ArrayList) 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