use of org.drools.core.spi.ProcessContext 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.drools.core.spi.ProcessContext 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.drools.core.spi.ProcessContext 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));
}
use of org.drools.core.spi.ProcessContext in project jbpm by kiegroup.
the class WorkItemNodeInstance method handleAssignment.
private void handleAssignment(Assignment assignment) {
AssignmentAction action = (AssignmentAction) assignment.getMetaData("Action");
try {
ProcessContext context = new ProcessContext(getProcessInstance().getKnowledgeRuntime());
context.setNodeInstance(this);
action.execute(getWorkItem(), context);
} catch (Exception e) {
throw new RuntimeException("unable to execute Assignment", e);
}
}
use of org.drools.core.spi.ProcessContext in project jbpm by kiegroup.
the class MVELActionBuilderTest 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();
MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
PackageBuildContext context = new PackageBuildContext();
context.init(pkgBuilder, pkg, null, dialectRegistry, mvelDialect, null);
pkgBuilder.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 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");
((MVELAction) actionNode.getAction().getMetaData("Action")).compile(data);
ProcessContext processContext = new ProcessContext(((InternalWorkingMemory) wm).getKnowledgeRuntime());
((Action) actionNode.getAction().getMetaData("Action")).execute(processContext);
assertEquals("hello world", list.get(0));
}
Aggregations