Search in sources :

Example 1 with ProcessDialect

use of org.jbpm.process.builder.dialect.ProcessDialect 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));
}
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) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) ActionDescr(org.drools.compiler.lang.descr.ActionDescr) ActionNode(org.jbpm.workflow.core.node.ActionNode) ArrayList(java.util.ArrayList) ProcessDescr(org.drools.compiler.lang.descr.ProcessDescr) ProcessDialect(org.jbpm.process.builder.dialect.ProcessDialect) WorkflowProcessImpl(org.jbpm.workflow.core.impl.WorkflowProcessImpl) JavaDialect(org.drools.compiler.rule.builder.dialect.java.JavaDialect) ProcessContext(org.drools.core.spi.ProcessContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 2 with ProcessDialect

use of org.jbpm.process.builder.dialect.ProcessDialect 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());
}
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) ActionDescr(org.drools.compiler.lang.descr.ActionDescr) ActionNode(org.jbpm.workflow.core.node.ActionNode) ProcessDescr(org.drools.compiler.lang.descr.ProcessDescr) ProcessDialect(org.jbpm.process.builder.dialect.ProcessDialect) WorkflowProcessImpl(org.jbpm.workflow.core.impl.WorkflowProcessImpl) JavaScriptActionBuilder(org.jbpm.process.builder.dialect.javascript.JavaScriptActionBuilder) ProcessContext(org.drools.core.spi.ProcessContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 3 with ProcessDialect

use of org.jbpm.process.builder.dialect.ProcessDialect in project jbpm by kiegroup.

the class StoreNodeBuilder method build.

public void build(Process process, ProcessDescr processDescr, ProcessBuildContext context, Node node) {
    ActionNode actionNode = (ActionNode) node;
    DroolsConsequenceAction action = (DroolsConsequenceAction) actionNode.getAction();
    ActionDescr actionDescr = new ActionDescr();
    actionDescr.setText(action.getConsequence());
    actionDescr.setResource(processDescr.getResource());
    ProcessDialect dialect = ProcessDialectRegistry.getDialect(action.getDialect());
    dialect.getActionBuilder().build(context, action, actionDescr, (NodeImpl) node);
}
Also used : DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) ActionDescr(org.drools.compiler.lang.descr.ActionDescr) ActionNode(org.jbpm.workflow.core.node.ActionNode) ProcessDialect(org.jbpm.process.builder.dialect.ProcessDialect)

Example 4 with ProcessDialect

use of org.jbpm.process.builder.dialect.ProcessDialect in project jbpm by kiegroup.

the class WorkItemNodeBuilder method build.

public void build(Process process, ProcessDescr processDescr, ProcessBuildContext context, Node node) {
    super.build(process, processDescr, context, node);
    WorkflowProcess wfProcess = (WorkflowProcess) process;
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("imports", wfProcess.getImports());
    parameters.put("classloader", context.getConfiguration().getClassLoader());
    for (DataAssociation dataAssociation : ((WorkItemNode) node).getInAssociations()) {
        Transformation transformation = dataAssociation.getTransformation();
        if (transformation != null) {
            DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
            transformation.setCompiledExpression(transformer.compile(transformation.getExpression(), parameters));
        }
        List<Assignment> assignments = dataAssociation.getAssignments();
        if (assignments != null) {
            for (Assignment assignment : assignments) {
                ProcessDialect dialect = ProcessDialectRegistry.getDialect(assignment.getDialect());
                dialect.getAssignmentBuilder().build(context, assignment, dataAssociation.getSources().get(0), dataAssociation.getTarget(), ((WorkItemNode) node), true);
            }
        }
    }
    for (DataAssociation dataAssociation : ((WorkItemNode) node).getOutAssociations()) {
        Transformation transformation = dataAssociation.getTransformation();
        if (transformation != null) {
            DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
            transformation.setCompiledExpression(transformer.compile(transformation.getExpression(), parameters));
        }
        List<Assignment> assignments = dataAssociation.getAssignments();
        if (assignments != null) {
            for (Assignment assignment : assignments) {
                ProcessDialect dialect = ProcessDialectRegistry.getDialect(assignment.getDialect());
                dialect.getAssignmentBuilder().build(context, assignment, dataAssociation.getSources().get(0), dataAssociation.getTarget(), ((WorkItemNode) node), false);
            }
        }
    }
}
Also used : Assignment(org.jbpm.workflow.core.node.Assignment) Transformation(org.jbpm.workflow.core.node.Transformation) DataTransformer(org.kie.api.runtime.process.DataTransformer) HashMap(java.util.HashMap) DataAssociation(org.jbpm.workflow.core.node.DataAssociation) WorkItemNode(org.jbpm.workflow.core.node.WorkItemNode) ProcessDialect(org.jbpm.process.builder.dialect.ProcessDialect) WorkflowProcess(org.jbpm.workflow.core.WorkflowProcess)

Example 5 with ProcessDialect

use of org.jbpm.process.builder.dialect.ProcessDialect in project jbpm by kiegroup.

the class ActionNodeBuilder method build.

public void build(Process process, ProcessDescr processDescr, ProcessBuildContext context, Node node) {
    super.build(process, processDescr, context, node);
    ActionNode actionNode = (ActionNode) node;
    DroolsConsequenceAction action = (DroolsConsequenceAction) actionNode.getAction();
    ActionDescr actionDescr = new ActionDescr();
    actionDescr.setText(action.getConsequence());
    actionDescr.setResource(processDescr.getResource());
    ProcessDialect dialect = ProcessDialectRegistry.getDialect(action.getDialect());
    dialect.getActionBuilder().build(context, action, actionDescr, (NodeImpl) node);
    Transformation transformation = (Transformation) node.getMetaData().get("Transformation");
    if (transformation != null) {
        WorkflowProcess wfProcess = (WorkflowProcess) process;
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("imports", wfProcess.getImports());
        parameters.put("classloader", context.getConfiguration().getClassLoader());
        DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
        transformation.setCompiledExpression(transformer.compile(transformation.getExpression(), parameters));
    }
}
Also used : Transformation(org.jbpm.workflow.core.node.Transformation) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) DataTransformer(org.kie.api.runtime.process.DataTransformer) HashMap(java.util.HashMap) ActionDescr(org.drools.compiler.lang.descr.ActionDescr) ActionNode(org.jbpm.workflow.core.node.ActionNode) ProcessDialect(org.jbpm.process.builder.dialect.ProcessDialect) WorkflowProcess(org.jbpm.workflow.core.WorkflowProcess)

Aggregations

ProcessDialect (org.jbpm.process.builder.dialect.ProcessDialect)9 ActionDescr (org.drools.compiler.lang.descr.ActionDescr)6 DroolsConsequenceAction (org.jbpm.workflow.core.impl.DroolsConsequenceAction)6 HashMap (java.util.HashMap)4 ActionNode (org.jbpm.workflow.core.node.ActionNode)4 StringReader (java.io.StringReader)2 Map (java.util.Map)2 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)2 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)2 ReturnValueDescr (org.drools.compiler.compiler.ReturnValueDescr)2 ProcessDescr (org.drools.compiler.lang.descr.ProcessDescr)2 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)2 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)2 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)2 ProcessContext (org.drools.core.spi.ProcessContext)2 Action (org.jbpm.process.instance.impl.Action)2 ReturnValueConstraintEvaluator (org.jbpm.process.instance.impl.ReturnValueConstraintEvaluator)2 RuleConstraintEvaluator (org.jbpm.process.instance.impl.RuleConstraintEvaluator)2 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)2 Constraint (org.jbpm.workflow.core.Constraint)2