Search in sources :

Example 1 with KogitoProcessContext

use of org.kie.kogito.internal.process.runtime.KogitoProcessContext in project kogito-runtimes by kiegroup.

the class MVELDecisionBuilderTest method testSimpleAction.

@Test
public void testSimpleAction() throws Exception {
    final InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("pkg1");
    ActionDescr actionDescr = new ActionDescr();
    actionDescr.setText("list.add( 'hello world' )");
    builder = new KnowledgeBuilderImpl(pkg, new SemanticKnowledgeBuilderConfigurationImpl());
    PackageRegistry pkgReg = builder.getPackageRegistry(pkg.getName());
    MVELDialect mvelDialect = (MVELDialect) pkgReg.getDialectCompiletimeRegistry().getDialect("mvel");
    PackageBuildContext context = new PackageBuildContext();
    context.init(builder, pkg, null, pkgReg.getDialectCompiletimeRegistry(), mvelDialect, null);
    builder.addPackageFromDrl(new StringReader("package pkg1;\nglobal java.util.List list;\n"));
    ActionNode actionNode = new ActionNode();
    DroolsAction action = new DroolsConsequenceAction("java", null);
    actionNode.setAction(action);
    final MVELActionBuilder actionBuilder = new MVELActionBuilder();
    actionBuilder.build(context, action, actionDescr, actionNode);
    KogitoProcessRuntime kruntime = createKogitoProcessRuntime();
    List<String> list = new ArrayList<String>();
    kruntime.getKieSession().setGlobal("list", list);
    MVELDialectRuntimeData data = (MVELDialectRuntimeData) builder.getPackage("pkg1").getDialectRuntimeRegistry().getDialectData("mvel");
    KogitoProcessContext processContext = new KogitoProcessContextImpl(kruntime.getKieSession());
    ((MVELAction) actionNode.getAction().getMetaData("Action")).compile(data);
    ((Action) actionNode.getAction().getMetaData("Action")).execute(processContext);
    assertEquals("hello world", list.get(0));
}
Also used : DroolsAction(org.jbpm.workflow.core.DroolsAction) MVELActionBuilder(org.jbpm.process.builder.dialect.mvel.MVELActionBuilder) MVELAction(org.jbpm.process.instance.impl.MVELAction) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) Action(org.jbpm.process.instance.impl.Action) DroolsAction(org.jbpm.workflow.core.DroolsAction) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) KogitoProcessRuntime(org.kie.kogito.internal.process.runtime.KogitoProcessRuntime) ActionDescr(org.drools.drl.ast.descr.ActionDescr) ActionNode(org.jbpm.workflow.core.node.ActionNode) ArrayList(java.util.ArrayList) MVELDialect(org.drools.mvel.builder.MVELDialect) SemanticKnowledgeBuilderConfigurationImpl(org.jbpm.compiler.xml.compiler.SemanticKnowledgeBuilderConfigurationImpl) MVELDialectRuntimeData(org.drools.mvel.MVELDialectRuntimeData) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) PackageBuildContext(org.drools.compiler.rule.builder.PackageBuildContext) KogitoProcessContext(org.kie.kogito.internal.process.runtime.KogitoProcessContext) MVELAction(org.jbpm.process.instance.impl.MVELAction) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StringReader(java.io.StringReader) KogitoProcessContextImpl(org.jbpm.process.instance.KogitoProcessContextImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.jupiter.api.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 2 with KogitoProcessContext

use of org.kie.kogito.internal.process.runtime.KogitoProcessContext in project kogito-runtimes by kiegroup.

the class JavaActionBuilderTest method testSimpleAction.

@Test
public void testSimpleAction() throws Exception {
    final InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("pkg1");
    ActionDescr actionDescr = new ActionDescr();
    actionDescr.setText("list.add( \"hello world\" );");
    builder = new KnowledgeBuilderImpl(pkg, new SemanticKnowledgeBuilderConfigurationImpl());
    DialectCompiletimeRegistry dialectRegistry = builder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    JavaDialect javaDialect = (JavaDialect) dialectRegistry.getDialect("java");
    ProcessDescr processDescr = new ProcessDescr();
    processDescr.setClassName("Process1");
    processDescr.setName("Process1");
    WorkflowProcessImpl process = new WorkflowProcessImpl();
    process.setName("Process1");
    process.setPackageName("pkg1");
    ProcessBuildContext context = new ProcessBuildContext(builder, builder.getPackage("pkg1"), null, processDescr, dialectRegistry, javaDialect);
    context.init(builder, pkg, null, dialectRegistry, javaDialect, null);
    builder.addPackageFromDrl(new StringReader("package pkg1;\nglobal java.util.List list;\n"));
    ActionNode actionNode = new ActionNode();
    DroolsAction action = new DroolsConsequenceAction("java", null);
    actionNode.setAction(action);
    ProcessDialect dialect = ProcessDialectRegistry.getDialect("java");
    dialect.getActionBuilder().build(context, action, actionDescr, actionNode);
    dialect.addProcess(context);
    javaDialect.compileAll();
    assertEquals(0, javaDialect.getResults().size());
    KogitoProcessRuntime kruntime = createKogitoProcessRuntime();
    List<String> list = new ArrayList<String>();
    kruntime.getKieSession().setGlobal("list", list);
    KogitoProcessContext processContext = new KogitoProcessContextImpl(kruntime.getKieRuntime());
    ((Action) actionNode.getAction().getMetaData("Action")).execute(processContext);
    assertEquals("hello world", list.get(0));
}
Also used : DroolsAction(org.jbpm.workflow.core.DroolsAction) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) Action(org.jbpm.process.instance.impl.Action) DroolsAction(org.jbpm.workflow.core.DroolsAction) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) KogitoProcessRuntime(org.kie.kogito.internal.process.runtime.KogitoProcessRuntime) ActionDescr(org.drools.drl.ast.descr.ActionDescr) ActionNode(org.jbpm.workflow.core.node.ActionNode) ArrayList(java.util.ArrayList) ProcessDescr(org.drools.drl.ast.descr.ProcessDescr) ProcessDialect(org.jbpm.process.builder.dialect.ProcessDialect) WorkflowProcessImpl(org.jbpm.workflow.core.impl.WorkflowProcessImpl) JavaDialect(org.drools.mvel.java.JavaDialect) SemanticKnowledgeBuilderConfigurationImpl(org.jbpm.compiler.xml.compiler.SemanticKnowledgeBuilderConfigurationImpl) KogitoProcessContext(org.kie.kogito.internal.process.runtime.KogitoProcessContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StringReader(java.io.StringReader) KogitoProcessContextImpl(org.jbpm.process.instance.KogitoProcessContextImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.jupiter.api.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 3 with KogitoProcessContext

use of org.kie.kogito.internal.process.runtime.KogitoProcessContext in project kogito-runtimes by kiegroup.

the class MVELActionBuilderTest method testSimpleAction.

@Test
public void testSimpleAction() throws Exception {
    final InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("pkg1");
    ActionDescr actionDescr = new ActionDescr();
    actionDescr.setText("list.add( 'hello world' )");
    builder = new KnowledgeBuilderImpl(pkg, new SemanticKnowledgeBuilderConfigurationImpl());
    DialectCompiletimeRegistry dialectRegistry = builder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
    PackageBuildContext context = new PackageBuildContext();
    context.init(builder, pkg, null, dialectRegistry, mvelDialect, null);
    builder.addPackageFromDrl(new StringReader("package pkg1;\nglobal java.util.List list;\n"));
    ActionNode actionNode = new ActionNode();
    DroolsAction action = new DroolsConsequenceAction("mvel", null);
    actionNode.setAction(action);
    final MVELActionBuilder actionBuilder = new MVELActionBuilder();
    actionBuilder.build(context, action, actionDescr, actionNode);
    KogitoProcessRuntime kruntime = createKogitoProcessRuntime();
    List<String> list = new ArrayList<String>();
    kruntime.getKieSession().setGlobal("list", list);
    MVELDialectRuntimeData data = (MVELDialectRuntimeData) builder.getPackage("pkg1").getDialectRuntimeRegistry().getDialectData("mvel");
    ((MVELAction) actionNode.getAction().getMetaData("Action")).compile(data);
    KogitoProcessContext processContext = new KogitoProcessContextImpl(kruntime.getKieRuntime());
    ((Action) actionNode.getAction().getMetaData("Action")).execute(processContext);
    assertEquals("hello world", list.get(0));
}
Also used : DroolsAction(org.jbpm.workflow.core.DroolsAction) MVELActionBuilder(org.jbpm.process.builder.dialect.mvel.MVELActionBuilder) MVELAction(org.jbpm.process.instance.impl.MVELAction) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) Action(org.jbpm.process.instance.impl.Action) DroolsAction(org.jbpm.workflow.core.DroolsAction) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) KogitoProcessRuntime(org.kie.kogito.internal.process.runtime.KogitoProcessRuntime) ActionDescr(org.drools.drl.ast.descr.ActionDescr) ActionNode(org.jbpm.workflow.core.node.ActionNode) ArrayList(java.util.ArrayList) MVELDialect(org.drools.mvel.builder.MVELDialect) SemanticKnowledgeBuilderConfigurationImpl(org.jbpm.compiler.xml.compiler.SemanticKnowledgeBuilderConfigurationImpl) MVELDialectRuntimeData(org.drools.mvel.MVELDialectRuntimeData) PackageBuildContext(org.drools.compiler.rule.builder.PackageBuildContext) KogitoProcessContext(org.kie.kogito.internal.process.runtime.KogitoProcessContext) MVELAction(org.jbpm.process.instance.impl.MVELAction) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StringReader(java.io.StringReader) KogitoProcessContextImpl(org.jbpm.process.instance.KogitoProcessContextImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.jupiter.api.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 4 with KogitoProcessContext

use of org.kie.kogito.internal.process.runtime.KogitoProcessContext in project kogito-runtimes by kiegroup.

the class CompensationTest method createNestedCompensationEventSubProcessProcess.

private RuleFlowProcess createNestedCompensationEventSubProcessProcess(String processId, String[] workItemNames, final List<String> eventList) throws Exception {
    RuleFlowProcess process = new RuleFlowProcess();
    process.setAutoComplete(true);
    process.setId(processId);
    process.setName("CESP Process");
    process.setMetaData("Compensation", true);
    NodeCreator<StartNode> startNodeCreator = new NodeCreator<StartNode>(process, StartNode.class);
    NodeCreator<WorkItemNode> workItemNodeCreator = new NodeCreator<WorkItemNode>(process, WorkItemNode.class);
    NodeCreator<CompositeContextNode> compNodeCreator = new NodeCreator<CompositeContextNode>(process, CompositeContextNode.class);
    NodeCreator<EndNode> endNodeCreator = new NodeCreator<EndNode>(process, EndNode.class);
    // outer process
    CompositeContextNode compositeNode = compNodeCreator.createNode("sub0");
    {
        StartNode startNode = startNodeCreator.createNode("start0");
        WorkItemNode workItemNode = workItemNodeCreator.createNode("work0-pre");
        workItemNode.getWork().setName(workItemNames[0]);
        connect(startNode, workItemNode);
        connect(workItemNode, compositeNode);
        EndNode endNode = endNodeCreator.createNode("end0");
        connect(compositeNode, endNode);
    }
    // 1rst level nested subprocess (contains compensation visibility scope)
    CompositeContextNode compensationScopeContainerNode = compositeNode;
    {
        startNodeCreator.setNodeContainer(compositeNode);
        workItemNodeCreator.setNodeContainer(compositeNode);
        compNodeCreator.setNodeContainer(compositeNode);
        endNodeCreator.setNodeContainer(compositeNode);
        StartNode startNode = startNodeCreator.createNode("start1");
        CompositeContextNode subCompNode = compNodeCreator.createNode("sub1");
        connect(startNode, subCompNode);
        WorkItemNode workItemNode = workItemNodeCreator.createNode("work1-post");
        workItemNode.getWork().setName(workItemNames[2]);
        connect(subCompNode, workItemNode);
        EndNode endNode = endNodeCreator.createNode("end1");
        connect(workItemNode, endNode);
        compositeNode = subCompNode;
    }
    // 2nd level nested subprocess
    {
        startNodeCreator.setNodeContainer(compositeNode);
        workItemNodeCreator.setNodeContainer(compositeNode);
        endNodeCreator.setNodeContainer(compositeNode);
        StartNode startNode = startNodeCreator.createNode("start2");
        WorkItemNode workItemNode = workItemNodeCreator.createNode("work2");
        workItemNode.getWork().setName(workItemNames[1]);
        connect(startNode, workItemNode);
        EndNode endNode = endNodeCreator.createNode("end2");
        connect(workItemNode, endNode);
    }
    // 3nd level nested event subprocess in 2nd level subprocess
    {
        NodeCreator<EventSubProcessNode> espNodeCreator = new NodeCreator<EventSubProcessNode>(compositeNode, EventSubProcessNode.class);
        EventSubProcessNode espNode = espNodeCreator.createNode("eventSub2");
        startNodeCreator.setNodeContainer(espNode);
        endNodeCreator.setNodeContainer(espNode);
        NodeCreator<ActionNode> actionNodeCreator = new NodeCreator<ActionNode>(espNode, ActionNode.class);
        EventTypeFilter eventFilter = new NonAcceptingEventTypeFilter();
        eventFilter.setType("Compensation");
        espNode.addEvent(eventFilter);
        addCompensationScope(espNode, compensationScopeContainerNode, (String) compositeNode.getMetaData("UniqueId"));
        StartNode startNode = startNodeCreator.createNode("start3*");
        ActionNode actionNode = actionNodeCreator.createNode("action3*");
        actionNode.setName("Execute");
        DroolsAction action = new DroolsConsequenceAction("java", null);
        action.setMetaData("Action", new Action() {

            public void execute(KogitoProcessContext context) throws Exception {
                eventList.add("Executed action");
            }
        });
        actionNode.setAction(action);
        connect(startNode, actionNode);
        EndNode endNode = endNodeCreator.createNode("end3*");
        connect(actionNode, endNode);
    }
    return process;
}
Also used : DroolsAction(org.jbpm.workflow.core.DroolsAction) RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) StartNode(org.jbpm.workflow.core.node.StartNode) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) DroolsAction(org.jbpm.workflow.core.DroolsAction) Action(org.jbpm.process.instance.impl.Action) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) CompositeContextNode(org.jbpm.workflow.core.node.CompositeContextNode) EventSubProcessNode(org.jbpm.workflow.core.node.EventSubProcessNode) ActionNode(org.jbpm.workflow.core.node.ActionNode) NonAcceptingEventTypeFilter(org.jbpm.process.core.event.NonAcceptingEventTypeFilter) NonAcceptingEventTypeFilter(org.jbpm.process.core.event.NonAcceptingEventTypeFilter) EventTypeFilter(org.jbpm.process.core.event.EventTypeFilter) KogitoProcessContext(org.kie.kogito.internal.process.runtime.KogitoProcessContext) EndNode(org.jbpm.workflow.core.node.EndNode) NodeCreator(org.jbpm.process.test.NodeCreator) WorkItemNode(org.jbpm.workflow.core.node.WorkItemNode)

Example 5 with KogitoProcessContext

use of org.kie.kogito.internal.process.runtime.KogitoProcessContext in project kogito-runtimes by kiegroup.

the class CompensationTest method createBoundaryEventCompensationHandler.

private void createBoundaryEventCompensationHandler(org.jbpm.workflow.core.NodeContainer nodeContainer, Node attachedToNode, final List<String> eventList, final String id) throws Exception {
    NodeCreator<BoundaryEventNode> boundaryNodeCreator = new NodeCreator<BoundaryEventNode>(nodeContainer, BoundaryEventNode.class);
    BoundaryEventNode boundaryNode = boundaryNodeCreator.createNode("boundary" + id);
    String attachedTo = (String) attachedToNode.getMetaData().get("UniqueId");
    boundaryNode.setMetaData("AttachedTo", attachedTo);
    boundaryNode.setAttachedToNodeId(attachedTo);
    EventTypeFilter eventFilter = new NonAcceptingEventTypeFilter();
    eventFilter.setType("Compensation");
    List<EventFilter> eventFilters = new ArrayList<EventFilter>();
    boundaryNode.setEventFilters(eventFilters);
    eventFilters.add(eventFilter);
    addCompensationScope(boundaryNode, nodeContainer, attachedTo);
    NodeCreator<ActionNode> actionNodeCreator = new NodeCreator<ActionNode>(nodeContainer, ActionNode.class);
    ActionNode actionNode = actionNodeCreator.createNode("handlerAction" + id);
    actionNode.setMetaData("isForCompensation", true);
    actionNode.setName("Execute");
    DroolsAction action = new DroolsConsequenceAction("java", null);
    action.setMetaData("Action", new Action() {

        public void execute(KogitoProcessContext context) throws Exception {
            eventList.add("action" + id);
        }
    });
    actionNode.setAction(action);
    connect(boundaryNode, actionNode);
}
Also used : DroolsAction(org.jbpm.workflow.core.DroolsAction) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) DroolsAction(org.jbpm.workflow.core.DroolsAction) Action(org.jbpm.process.instance.impl.Action) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) ArrayList(java.util.ArrayList) ActionNode(org.jbpm.workflow.core.node.ActionNode) NonAcceptingEventTypeFilter(org.jbpm.process.core.event.NonAcceptingEventTypeFilter) BoundaryEventNode(org.jbpm.workflow.core.node.BoundaryEventNode) EventFilter(org.jbpm.process.core.event.EventFilter) NonAcceptingEventTypeFilter(org.jbpm.process.core.event.NonAcceptingEventTypeFilter) EventTypeFilter(org.jbpm.process.core.event.EventTypeFilter) KogitoProcessContext(org.kie.kogito.internal.process.runtime.KogitoProcessContext) NodeCreator(org.jbpm.process.test.NodeCreator)

Aggregations

KogitoProcessContext (org.kie.kogito.internal.process.runtime.KogitoProcessContext)9 Action (org.jbpm.process.instance.impl.Action)7 DroolsAction (org.jbpm.workflow.core.DroolsAction)6 DroolsConsequenceAction (org.jbpm.workflow.core.impl.DroolsConsequenceAction)6 ActionNode (org.jbpm.workflow.core.node.ActionNode)6 KogitoProcessContextImpl (org.jbpm.process.instance.KogitoProcessContextImpl)5 ArrayList (java.util.ArrayList)4 Test (org.junit.jupiter.api.Test)4 StringReader (java.io.StringReader)3 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)3 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)3 ActionDescr (org.drools.drl.ast.descr.ActionDescr)3 SemanticKnowledgeBuilderConfigurationImpl (org.jbpm.compiler.xml.compiler.SemanticKnowledgeBuilderConfigurationImpl)3 EventTypeFilter (org.jbpm.process.core.event.EventTypeFilter)3 NonAcceptingEventTypeFilter (org.jbpm.process.core.event.NonAcceptingEventTypeFilter)3 NodeCreator (org.jbpm.process.test.NodeCreator)3 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)3 KogitoProcessRuntime (org.kie.kogito.internal.process.runtime.KogitoProcessRuntime)3 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)2 PackageBuildContext (org.drools.compiler.rule.builder.PackageBuildContext)2