Search in sources :

Example 1 with StaticApplication

use of org.kie.kogito.StaticApplication 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 2 with StaticApplication

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

the class BpmnProcessCompiler method from.

public List<BpmnProcess> from(ProcessConfig config, Resource... resources) {
    try {
        BpmnProcesses bpmnProcesses = new BpmnProcesses();
        StaticApplication application = new StaticApplication(new StaticConfig(null, config), bpmnProcesses);
        List<Process> processes = new ArrayList<>();
        XmlProcessReader xmlReader = new XmlProcessReader(getSemanticModules(), Thread.currentThread().getContextClassLoader());
        configureProcessReader(xmlReader, config);
        for (Resource resource : resources) {
            processes.addAll(xmlReader.read(resource.getReader()));
        }
        List<BpmnProcess> bpmnProcessesList = processes.stream().map(p -> create(p, config, application)).collect(Collectors.toList());
        bpmnProcessesList.forEach(p -> {
            for (Node node : ((WorkflowProcess) p.get()).getNodesRecursively()) {
                processNode(node, bpmnProcessesList);
            }
        });
        return bpmnProcessesList;
    } catch (Exception e) {
        throw new BpmnProcessReaderException(e);
    }
}
Also used : XmlProcessReader(org.jbpm.compiler.xml.XmlProcessReader) WorkflowProcess(org.jbpm.workflow.core.WorkflowProcess) ProcessConfig(org.kie.kogito.process.ProcessConfig) Process(org.kie.api.definition.process.Process) StaticConfig(org.kie.kogito.StaticConfig) SemanticModule(org.drools.core.xml.SemanticModule) Application(org.kie.kogito.Application) Collectors(java.util.stream.Collectors) BPMNDISemanticModule(org.jbpm.bpmn2.xml.BPMNDISemanticModule) ArrayList(java.util.ArrayList) Resource(org.kie.api.io.Resource) BPMNExtensionsSemanticModule(org.jbpm.bpmn2.xml.BPMNExtensionsSemanticModule) List(java.util.List) BPMNSemanticModule(org.jbpm.bpmn2.xml.BPMNSemanticModule) Node(org.kie.api.definition.process.Node) StaticApplication(org.kie.kogito.StaticApplication) SemanticModules(org.drools.core.xml.SemanticModules) XmlProcessReader(org.jbpm.compiler.xml.XmlProcessReader) Node(org.kie.api.definition.process.Node) StaticConfig(org.kie.kogito.StaticConfig) ArrayList(java.util.ArrayList) Resource(org.kie.api.io.Resource) WorkflowProcess(org.jbpm.workflow.core.WorkflowProcess) Process(org.kie.api.definition.process.Process) StaticApplication(org.kie.kogito.StaticApplication) WorkflowProcess(org.jbpm.workflow.core.WorkflowProcess)

Example 3 with StaticApplication

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

the class ActivityGenerationModelTest method createProcesses.

protected Map<String, BpmnProcess> createProcesses(Map<String, String> classData, Map<String, KogitoWorkItemHandler> handlers) throws Exception {
    MemoryFileSystem srcMfs = new MemoryFileSystem();
    MemoryFileSystem trgMfs = new MemoryFileSystem();
    String[] sources = new String[classData.size()];
    int index = 0;
    for (Entry<String, String> entry : classData.entrySet()) {
        String fileName = entry.getKey().replaceAll("\\.", "/") + ".java";
        sources[index++] = fileName;
        srcMfs.write(fileName, entry.getValue().getBytes());
    }
    CompilationResult result = JAVA_COMPILER.compile(sources, srcMfs, trgMfs, this.getClass().getClassLoader());
    assertThat(result).isNotNull();
    assertThat(result.getErrors()).hasSize(0);
    CachedWorkItemHandlerConfig wiConfig = new CachedWorkItemHandlerConfig();
    for (Entry<String, KogitoWorkItemHandler> entry : handlers.entrySet()) {
        wiConfig.register(entry.getKey(), entry.getValue());
    }
    ProcessConfig config = new StaticProcessConfig(wiConfig, new DefaultProcessEventListenerConfig(), new DefaultUnitOfWorkManager(new CollectingUnitOfWorkFactory()), null);
    TestClassLoader cl = new TestClassLoader(this.getClass().getClassLoader(), trgMfs.getMap());
    Map<String, BpmnProcess> processes = new HashMap<>();
    BpmnProcesses bpmnProcesses = new BpmnProcesses();
    StaticApplication application = new StaticApplication(new StaticConfig(null, config), bpmnProcesses);
    for (String className : classData.keySet()) {
        Class<?> processClass = Class.forName(className, true, cl);
        Method processMethod = processClass.getMethod("process");
        Process process = (Process) processMethod.invoke(null);
        assertThat(process).isNotNull();
        processes.put(process.getId(), new BpmnProcess(process, config, application));
    }
    return processes;
}
Also used : BpmnProcess(org.kie.kogito.process.bpmn2.BpmnProcess) ProcessConfig(org.kie.kogito.process.ProcessConfig) StaticProcessConfig(org.kie.kogito.process.impl.StaticProcessConfig) StaticProcessConfig(org.kie.kogito.process.impl.StaticProcessConfig) HashMap(java.util.HashMap) KogitoWorkItemHandler(org.kie.kogito.internal.process.runtime.KogitoWorkItemHandler) StaticConfig(org.kie.kogito.StaticConfig) BpmnProcess(org.kie.kogito.process.bpmn2.BpmnProcess) Process(org.kie.api.definition.process.Process) WorkflowProcess(org.kie.api.definition.process.WorkflowProcess) Method(java.lang.reflect.Method) BpmnProcesses(org.kie.kogito.process.bpmn2.BpmnProcesses) StaticApplication(org.kie.kogito.StaticApplication) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) CachedWorkItemHandlerConfig(org.kie.kogito.process.impl.CachedWorkItemHandlerConfig) CollectingUnitOfWorkFactory(org.kie.kogito.services.uow.CollectingUnitOfWorkFactory) DefaultProcessEventListenerConfig(org.kie.kogito.process.impl.DefaultProcessEventListenerConfig) CompilationResult(org.kie.memorycompiler.CompilationResult) DefaultUnitOfWorkManager(org.kie.kogito.services.uow.DefaultUnitOfWorkManager)

Aggregations

StaticApplication (org.kie.kogito.StaticApplication)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Process (org.kie.api.definition.process.Process)2 StaticConfig (org.kie.kogito.StaticConfig)2 ProcessConfig (org.kie.kogito.process.ProcessConfig)2 InputStreamReader (java.io.InputStreamReader)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Collections.singletonList (java.util.Collections.singletonList)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 MemoryFileSystem (org.drools.compiler.compiler.io.memory.MemoryFileSystem)1 SemanticModule (org.drools.core.xml.SemanticModule)1 SemanticModules (org.drools.core.xml.SemanticModules)1 BPMNDISemanticModule (org.jbpm.bpmn2.xml.BPMNDISemanticModule)1 BPMNExtensionsSemanticModule (org.jbpm.bpmn2.xml.BPMNExtensionsSemanticModule)1 BPMNSemanticModule (org.jbpm.bpmn2.xml.BPMNSemanticModule)1 XmlProcessReader (org.jbpm.compiler.xml.XmlProcessReader)1 WorkflowProcess (org.jbpm.workflow.core.WorkflowProcess)1