Search in sources :

Example 1 with DecisionModels

use of org.kie.kogito.decision.DecisionModels in project kogito-runtimes by kiegroup.

the class DecisionExplainabilityResourceExecutorTest method testGetDecisionModel.

@ParameterizedTest
@MethodSource("getDecisionModelTestProvider")
public void testGetDecisionModel(String[] args) {
    String namespace = args[0];
    String name = args[1];
    DecisionExplainabilityResourceExecutor executor = new DecisionExplainabilityResourceExecutor();
    DecisionModels decisionModels = mock(DecisionModels.class);
    DecisionModel model = new DmnDecisionModel(generateDMNRuntime(namespace, name), namespace, name);
    when(decisionModels.getDecisionModel(eq(namespace), eq(name))).thenReturn(model);
    ModelIdentifier modelIdentifier = new ModelIdentifier("dmn", String.format("%s%s%s", namespace, RESOURCE_ID_SEPARATOR, name));
    DecisionModel decisionModelResponse = executor.getDecisionModel(decisionModels, modelIdentifier);
    Assertions.assertNotNull(decisionModelResponse);
    Assertions.assertEquals(namespace, decisionModelResponse.getDMNModel().getNamespace());
    Assertions.assertEquals(name, decisionModelResponse.getDMNModel().getName());
}
Also used : ModelIdentifier(org.kie.kogito.explainability.model.ModelIdentifier) DmnDecisionModel(org.kie.kogito.dmn.DmnDecisionModel) DecisionModel(org.kie.kogito.decision.DecisionModel) DmnDecisionModel(org.kie.kogito.dmn.DmnDecisionModel) DecisionModels(org.kie.kogito.decision.DecisionModels) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with DecisionModels

use of org.kie.kogito.decision.DecisionModels in project kogito-runtimes by kiegroup.

the class ExplainabilityServiceTest method testPerturbedExecution.

@Test
public void testPerturbedExecution() {
    DecisionModels decisionModels = (namespace, name) -> {
        if (MODEL_NAMESPACE.equals(namespace) && MODEL_NAME.equals(name)) {
            return decisionModel;
        }
        throw new RuntimeException("Model " + namespace + ":" + name + " not found.");
    };
    Map<String, Object> perturbedRequest = createRequest();
    PredictInput predictInput = new PredictInput(new ModelIdentifier("dmn", String.format("%s%s%s", MODEL_NAMESPACE, RESOURCE_ID_SEPARATOR, MODEL_NAME)), perturbedRequest);
    StaticApplication application = new StaticApplication(null, null, null, decisionModels, null);
    ExplainabilityService explainabilityService = ExplainabilityService.INSTANCE;
    List<PredictOutput> predictOutputs = explainabilityService.processRequest(application, singletonList(predictInput));
    Assertions.assertEquals(1, predictOutputs.size());
    PredictOutput predictOutput = predictOutputs.get(0);
    Assertions.assertNotNull(predictOutput);
    Assertions.assertNotNull(predictOutput.getResult());
    Map<String, Object> perturbedResult = predictOutput.getResult();
    Assertions.assertTrue(perturbedResult.containsKey("Should the driver be suspended?"));
    Assertions.assertEquals("No", perturbedResult.get("Should the driver be suspended?"));
    Assertions.assertTrue(perturbedResult.containsKey("Fine"));
    Assertions.assertNull(perturbedResult.get("Fine"));
    Assertions.assertTrue(decisionModel.getEvaluationSkipMonitoringHistory().stream().allMatch(x -> x.equals(true)));
}
Also used : RESOURCE_ID_SEPARATOR(org.kie.kogito.explainability.model.ModelIdentifier.RESOURCE_ID_SEPARATOR) ModelIdentifier(org.kie.kogito.explainability.model.ModelIdentifier) PredictOutput(org.kie.kogito.explainability.model.PredictOutput) HashMap(java.util.HashMap) InputStreamReader(java.io.InputStreamReader) DMNKogito(org.kie.kogito.dmn.DMNKogito) Collections.singletonList(java.util.Collections.singletonList) Test(org.junit.jupiter.api.Test) List(java.util.List) Map(java.util.Map) Assertions(org.junit.jupiter.api.Assertions) StaticApplication(org.kie.kogito.StaticApplication) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DecisionModels(org.kie.kogito.decision.DecisionModels) PredictInput(org.kie.kogito.explainability.model.PredictInput) ModelIdentifier(org.kie.kogito.explainability.model.ModelIdentifier) DecisionModels(org.kie.kogito.decision.DecisionModels) PredictOutput(org.kie.kogito.explainability.model.PredictOutput) PredictInput(org.kie.kogito.explainability.model.PredictInput) StaticApplication(org.kie.kogito.StaticApplication) Test(org.junit.jupiter.api.Test)

Example 3 with DecisionModels

use of org.kie.kogito.decision.DecisionModels in project kogito-runtimes by kiegroup.

the class EventDrivenDecisionControllerTest method testSubscribe.

@Test
void testSubscribe() {
    DecisionModels decisionModelsMock = mock(DecisionModels.class);
    ConfigBean configMock = mock(ConfigBean.class);
    EventEmitter eventEmitterMock = mock(EventEmitter.class);
    EventReceiver eventReceiverMock = mock(EventReceiver.class);
    // option #1: parameters via constructor + parameterless setup
    EventDrivenDecisionController controller1 = new EventDrivenDecisionController(decisionModelsMock, configMock, eventEmitterMock, eventReceiverMock);
    controller1.subscribe();
    verify(eventReceiverMock).subscribe(any(), any());
    reset(eventReceiverMock);
    // option #2: parameterless via constructor + parameters via setup (introduced for Quarkus CDI)
    EventDrivenDecisionController controller2 = new EventDrivenDecisionController();
    controller2.init(decisionModelsMock, configMock, eventEmitterMock, eventReceiverMock);
    controller2.subscribe();
    verify(eventReceiverMock).subscribe(any(), any());
}
Also used : EventEmitter(org.kie.kogito.event.EventEmitter) ConfigBean(org.kie.kogito.conf.ConfigBean) DecisionModels(org.kie.kogito.decision.DecisionModels) EventReceiver(org.kie.kogito.event.EventReceiver) Test(org.junit.jupiter.api.Test)

Example 4 with DecisionModels

use of org.kie.kogito.decision.DecisionModels in project kogito-runtimes by kiegroup.

the class BaseSpringBootDecisionTracingTest method testCollector.

@SuppressWarnings("unchecked")
private void testCollector(List<EvaluateEvent> events, DecisionModel model) throws IOException {
    final DecisionModels mockedDecisionModels = mock(DecisionModels.class);
    when(mockedDecisionModels.getDecisionModel(getTestModelNameSpace(), getTestModelName())).thenReturn(model);
    final Application mockedApplication = mock(Application.class);
    when(mockedApplication.get(any())).thenReturn(mockedDecisionModels);
    final ConfigBean configBean = new StaticConfigBean(TEST_SERVICE_URL, true, null);
    final KafkaTemplate<String, String> mockedTemplate = mock(KafkaTemplate.class);
    final SpringBootTraceEventEmitter eventEmitter = new SpringBootTraceEventEmitter(mockedTemplate, TEST_KAFKA_TOPIC);
    SpringBootDecisionTracingCollector collector = new SpringBootDecisionTracingCollector(eventEmitter, configBean, mockedApplication);
    events.forEach(collector::onApplicationEvent);
    ArgumentCaptor<String> payloadCaptor = ArgumentCaptor.forClass(String.class);
    verify(mockedTemplate).send(eq(TEST_KAFKA_TOPIC), payloadCaptor.capture());
    CloudEvent cloudEvent = CloudEventUtils.decode(payloadCaptor.getValue()).orElseThrow(() -> new IllegalStateException("Can't decode CloudEvent"));
    assertEquals(TEST_EXECUTION_ID, cloudEvent.getId());
    assertNotNull(cloudEvent.getData());
    TraceEvent traceEvent = MAPPER.readValue(cloudEvent.getData().toBytes(), TraceEvent.class);
    assertNotNull(traceEvent);
    assertEquals(TEST_SERVICE_URL, traceEvent.getHeader().getResourceId().getServiceUrl());
}
Also used : StaticConfigBean(org.kie.kogito.conf.StaticConfigBean) ConfigBean(org.kie.kogito.conf.ConfigBean) StaticConfigBean(org.kie.kogito.conf.StaticConfigBean) DecisionModels(org.kie.kogito.decision.DecisionModels) Application(org.kie.kogito.Application) CloudEvent(io.cloudevents.CloudEvent) TraceEvent(org.kie.kogito.tracing.event.trace.TraceEvent)

Example 5 with DecisionModels

use of org.kie.kogito.decision.DecisionModels in project kogito-runtimes by kiegroup.

the class BaseQuarkusDecisionTracingTest method testCollector.

private void testCollector(List<EvaluateEvent> events, DecisionModel model) throws IOException {
    AssertSubscriber<String> subscriber = AssertSubscriber.create(1);
    final DecisionModels mockedDecisionModels = mock(DecisionModels.class);
    when(mockedDecisionModels.getDecisionModel(getTestModelNameSpace(), getTestModelName())).thenReturn(model);
    final Application mockedApplication = mock(Application.class);
    when(mockedApplication.get(any())).thenReturn(mockedDecisionModels);
    final ConfigBean configBean = new StaticConfigBean(TEST_SERVICE_URL, true, null);
    final QuarkusTraceEventEmitter eventEmitter = new QuarkusTraceEventEmitter();
    QuarkusDecisionTracingCollector collector = new QuarkusDecisionTracingCollector(eventEmitter, configBean, mockedApplication);
    eventEmitter.getEventPublisher().subscribe(subscriber);
    events.forEach(collector::onEvent);
    subscriber.assertNotTerminated();
    List<String> items = subscriber.getItems();
    assertEquals(1, items.size());
    CloudEvent cloudEvent = CloudEventUtils.decode(items.get(0)).orElseThrow(() -> new IllegalStateException("Can't decode CloudEvent"));
    assertEquals(TEST_EXECUTION_ID, cloudEvent.getId());
    assertNotNull(cloudEvent.getData());
    TraceEvent traceEvent = MAPPER.readValue(cloudEvent.getData().toBytes(), TraceEvent.class);
    assertNotNull(traceEvent);
    assertEquals(TEST_SERVICE_URL, traceEvent.getHeader().getResourceId().getServiceUrl());
}
Also used : StaticConfigBean(org.kie.kogito.conf.StaticConfigBean) ConfigBean(org.kie.kogito.conf.ConfigBean) StaticConfigBean(org.kie.kogito.conf.StaticConfigBean) DecisionModels(org.kie.kogito.decision.DecisionModels) Application(org.kie.kogito.Application) CloudEvent(io.cloudevents.CloudEvent) TraceEvent(org.kie.kogito.tracing.event.trace.TraceEvent)

Aggregations

DecisionModels (org.kie.kogito.decision.DecisionModels)5 ConfigBean (org.kie.kogito.conf.ConfigBean)3 CloudEvent (io.cloudevents.CloudEvent)2 Test (org.junit.jupiter.api.Test)2 Application (org.kie.kogito.Application)2 StaticConfigBean (org.kie.kogito.conf.StaticConfigBean)2 ModelIdentifier (org.kie.kogito.explainability.model.ModelIdentifier)2 TraceEvent (org.kie.kogito.tracing.event.trace.TraceEvent)2 InputStreamReader (java.io.InputStreamReader)1 Collections.singletonList (java.util.Collections.singletonList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Assertions (org.junit.jupiter.api.Assertions)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)1 StaticApplication (org.kie.kogito.StaticApplication)1 DecisionModel (org.kie.kogito.decision.DecisionModel)1 DMNKogito (org.kie.kogito.dmn.DMNKogito)1