use of org.jbpm.process.instance.impl.Action in project jbpm by kiegroup.
the class ActionNodeInstance method internalTrigger.
public void internalTrigger(final NodeInstance from, String type) {
if (!org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE.equals(type)) {
throw new IllegalArgumentException("An ActionNode only accepts default incoming connections!");
}
Action action = (Action) getActionNode().getAction().getMetaData("Action");
try {
ProcessContext context = new ProcessContext(getProcessInstance().getKnowledgeRuntime());
context.setNodeInstance(this);
executeAction(action);
} catch (WorkflowRuntimeException wre) {
throw wre;
} catch (Exception e) {
// - or context.setNodeInstance(this)
throw new WorkflowRuntimeException(this, getProcessInstance(), "Unable to execute Action: " + e.getMessage(), e);
}
triggerCompleted();
}
use of org.jbpm.process.instance.impl.Action in project jbpm by kiegroup.
the class SubProcessTest method testSynchronousSubProcess.
@Test
public void testSynchronousSubProcess() {
RuleFlowProcess process = new RuleFlowProcess();
process.setId("org.drools.core.process.process");
process.setName("Process");
StartNode startNode = new StartNode();
startNode.setName("Start");
startNode.setId(1);
process.addNode(startNode);
EndNode endNode = new EndNode();
endNode.setName("EndNode");
endNode.setId(2);
process.addNode(endNode);
SubProcessNode subProcessNode = new SubProcessNode();
subProcessNode.setName("SubProcessNode");
subProcessNode.setId(3);
subProcessNode.setProcessId("org.drools.core.process.subprocess");
process.addNode(subProcessNode);
new ConnectionImpl(startNode, Node.CONNECTION_DEFAULT_TYPE, subProcessNode, Node.CONNECTION_DEFAULT_TYPE);
new ConnectionImpl(subProcessNode, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE);
RuleFlowProcess subprocess = new RuleFlowProcess();
subprocess.setId("org.drools.core.process.subprocess");
subprocess.setName("SubProcess");
startNode = new StartNode();
startNode.setName("Start");
startNode.setId(1);
subprocess.addNode(startNode);
endNode = new EndNode();
endNode.setName("EndNode");
endNode.setId(2);
subprocess.addNode(endNode);
ActionNode actionNode = new ActionNode();
actionNode.setName("Action");
DroolsAction action = new DroolsConsequenceAction("java", null);
action.setMetaData("Action", new Action() {
public void execute(ProcessContext context) throws Exception {
logger.info("Executed action");
executed = true;
}
});
actionNode.setAction(action);
actionNode.setId(3);
subprocess.addNode(actionNode);
new ConnectionImpl(startNode, Node.CONNECTION_DEFAULT_TYPE, actionNode, Node.CONNECTION_DEFAULT_TYPE);
new ConnectionImpl(actionNode, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE);
KieSession ksession = createKieSession(process, subprocess);
TestProcessEventListener procEventListener = new TestProcessEventListener();
ksession.addEventListener(procEventListener);
ksession.startProcess("org.drools.core.process.process");
assertTrue(executed);
assertEquals(0, ksession.getProcessInstances().size());
verifyEventHistory(syncEventorder, procEventListener.getEventHistory());
}
use of org.jbpm.process.instance.impl.Action in project jbpm by kiegroup.
the class JavaActionBuilderTest method testSimpleAction.
@Test
public void testSimpleAction() throws Exception {
final InternalKnowledgePackage pkg = new KnowledgePackageImpl("pkg1");
ActionDescr actionDescr = new ActionDescr();
actionDescr.setText("list.add( \"hello world\" );");
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.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(pkgBuilder, pkgBuilder.getPackage("pkg1"), null, processDescr, dialectRegistry, javaDialect);
context.init(pkgBuilder, pkg, null, dialectRegistry, javaDialect, null);
pkgBuilder.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());
final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(Arrays.asList(pkgBuilder.getPackages()));
final KieSession wm = kbase.newKieSession();
List<String> list = new ArrayList<String>();
wm.setGlobal("list", list);
ProcessContext processContext = new ProcessContext(((InternalWorkingMemory) wm).getKnowledgeRuntime());
((Action) actionNode.getAction().getMetaData("Action")).execute(processContext);
assertEquals("hello world", list.get(0));
}
use of org.jbpm.process.instance.impl.Action in project jbpm by kiegroup.
the class JavaScriptActionBuilderTest method testSimpleAction.
@Test
public void testSimpleAction() throws Exception {
final InternalKnowledgePackage pkg = new KnowledgePackageImpl("pkg1");
ActionDescr actionDescr = new ActionDescr();
actionDescr.setText("var testString; print('Hello')");
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
ProcessDescr processDescr = new ProcessDescr();
processDescr.setClassName("Process1");
processDescr.setName("Process1");
WorkflowProcessImpl process = new WorkflowProcessImpl();
process.setName("Process1");
process.setPackageName("pkg1");
ProcessBuildContext context = new ProcessBuildContext(pkgBuilder, pkgBuilder.getPackage("pkg1"), null, processDescr, dialectRegistry, null);
context.init(pkgBuilder, pkg, null, dialectRegistry, null, null);
pkgBuilder.addPackageFromDrl(new StringReader("package pkg1;\nglobal String testField;\n"));
ActionNode actionNode = new ActionNode();
DroolsAction action = new DroolsConsequenceAction("JavaScript", null);
actionNode.setAction(action);
ProcessDialect dialect = ProcessDialectRegistry.getDialect("JavaScript");
dialect.getActionBuilder().build(context, action, actionDescr, actionNode);
dialect.addProcess(context);
final JavaScriptActionBuilder builder = new JavaScriptActionBuilder();
builder.build(context, action, actionDescr, actionNode);
final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(Arrays.asList(pkgBuilder.getPackages()));
final KieSession wm = kbase.newKieSession();
wm.setGlobal("testField", "vagon");
ProcessContext processContext = new ProcessContext(((InternalWorkingMemory) wm).getKnowledgeRuntime());
((Action) actionNode.getAction().getMetaData("Action")).execute(processContext);
assertEquals("vagon", wm.getGlobal("testField").toString());
}
use of org.jbpm.process.instance.impl.Action in project jbpm by kiegroup.
the class MVELDecisionBuilderTest method testSimpleAction.
@Test
public void testSimpleAction() throws Exception {
final InternalKnowledgePackage pkg = new KnowledgePackageImpl("pkg1");
ActionDescr actionDescr = new ActionDescr();
actionDescr.setText("list.add( 'hello world' )");
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
PackageRegistry pkgReg = pkgBuilder.getPackageRegistry(pkg.getName());
MVELDialect mvelDialect = (MVELDialect) pkgReg.getDialectCompiletimeRegistry().getDialect("mvel");
PackageBuildContext context = new PackageBuildContext();
context.init(pkgBuilder, pkg, null, pkgReg.getDialectCompiletimeRegistry(), mvelDialect, null);
pkgBuilder.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 builder = new MVELActionBuilder();
builder.build(context, action, actionDescr, actionNode);
final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(Arrays.asList(pkgBuilder.getPackages()));
final KieSession wm = kbase.newKieSession();
List<String> list = new ArrayList<String>();
wm.setGlobal("list", list);
MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkgBuilder.getPackage("pkg1").getDialectRuntimeRegistry().getDialectData("mvel");
ProcessContext processContext = new ProcessContext(((InternalWorkingMemory) wm).getKnowledgeRuntime());
((MVELAction) actionNode.getAction().getMetaData("Action")).compile(data);
((Action) actionNode.getAction().getMetaData("Action")).execute(processContext);
assertEquals("hello world", list.get(0));
}
Aggregations