Search in sources :

Example 1 with MVELAction

use of org.jbpm.process.instance.impl.MVELAction 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));
}
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) DroolsAction(org.jbpm.workflow.core.DroolsAction) Action(org.jbpm.process.instance.impl.Action) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) ActionDescr(org.drools.compiler.lang.descr.ActionDescr) ActionNode(org.jbpm.workflow.core.node.ActionNode) ArrayList(java.util.ArrayList) MVELDialect(org.drools.compiler.rule.builder.dialect.mvel.MVELDialect) ProcessContext(org.drools.core.spi.ProcessContext) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) PackageBuildContext(org.drools.compiler.rule.builder.PackageBuildContext) MVELAction(org.jbpm.process.instance.impl.MVELAction) 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 MVELAction

use of org.jbpm.process.instance.impl.MVELAction in project jbpm by kiegroup.

the class MVELActionBuilder method buildAction.

protected void buildAction(final PackageBuildContext context, final DroolsAction action, final ActionDescr actionDescr, final ContextResolver contextResolver, final MVELDialect dialect, final MVELAnalysisResult analysis, final String text, Map<String, Class<?>> variables) throws Exception {
    Set<String> variableNames = analysis.getNotBoundedIdentifiers();
    if (contextResolver != null) {
        for (String variableName : variableNames) {
            if (analysis.getMvelVariables().keySet().contains(variableName) || variableName.equals("kcontext") || variableName.equals("context")) {
                continue;
            }
            VariableScope variableScope = (VariableScope) contextResolver.resolveContext(VariableScope.VARIABLE_SCOPE, variableName);
            if (variableScope == null) {
                context.getErrors().add(new DescrBuildError(context.getParentDescr(), actionDescr, null, "Could not find variable '" + variableName + "' " + "for action '" + actionDescr.getText() + "'"));
            } else {
                variables.put(variableName, context.getDialect().getTypeResolver().resolveType(variableScope.findVariable(variableName).getType().getStringType()));
            }
        }
    }
    MVELCompilationUnit unit = dialect.getMVELCompilationUnit(text, analysis, null, null, variables, context, "context", org.kie.api.runtime.process.ProcessContext.class, false, MVELCompilationUnit.Scope.EXPRESSION);
    MVELAction expr = new MVELAction(unit, context.getDialect().getId());
    action.setMetaData("Action", expr);
    MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData(dialect.getId());
    data.addCompileable(action, expr);
    expr.compile(data);
    collectTypes("MVELDialect", analysis, (ProcessBuildContext) context);
}
Also used : MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) MVELAction(org.jbpm.process.instance.impl.MVELAction) MVELCompilationUnit(org.drools.core.base.mvel.MVELCompilationUnit) VariableScope(org.jbpm.process.core.context.variable.VariableScope)

Example 3 with MVELAction

use of org.jbpm.process.instance.impl.MVELAction 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));
}
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) 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) MVELDialect(org.drools.compiler.rule.builder.dialect.mvel.MVELDialect) ProcessContext(org.drools.core.spi.ProcessContext) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) PackageBuildContext(org.drools.compiler.rule.builder.PackageBuildContext) MVELAction(org.jbpm.process.instance.impl.MVELAction) 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)

Aggregations

MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)3 MVELAction (org.jbpm.process.instance.impl.MVELAction)3 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)2 ActionDescr (org.drools.compiler.lang.descr.ActionDescr)2 PackageBuildContext (org.drools.compiler.rule.builder.PackageBuildContext)2 MVELDialect (org.drools.compiler.rule.builder.dialect.mvel.MVELDialect)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 MVELActionBuilder (org.jbpm.process.builder.dialect.mvel.MVELActionBuilder)2 Action (org.jbpm.process.instance.impl.Action)2 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)2 DroolsAction (org.jbpm.workflow.core.DroolsAction)2 DroolsConsequenceAction (org.jbpm.workflow.core.impl.DroolsConsequenceAction)2 ActionNode (org.jbpm.workflow.core.node.ActionNode)2 Test (org.junit.Test)2 KieSession (org.kie.api.runtime.KieSession)2