Search in sources :

Example 1 with JavaDialect

use of org.drools.mvel.java.JavaDialect in project jbpm by kiegroup.

the class JavaActionBuilder method getAnalysis.

protected AnalysisResult getAnalysis(final PackageBuildContext context, final ActionDescr actionDescr) {
    JavaDialect dialect = (JavaDialect) context.getDialect("java");
    Map<String, Class<?>> variables = new HashMap<String, Class<?>>();
    BoundIdentifiers boundIdentifiers = new BoundIdentifiers(variables, context);
    AnalysisResult analysis = dialect.analyzeBlock(context, actionDescr, actionDescr.getText(), boundIdentifiers);
    return analysis;
}
Also used : HashMap(java.util.HashMap) JavaDialect(org.drools.mvel.java.JavaDialect) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers)

Example 2 with JavaDialect

use of org.drools.mvel.java.JavaDialect 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.mvel.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 3 with JavaDialect

use of org.drools.mvel.java.JavaDialect in project kogito-runtimes by kiegroup.

the class JavaActionBuilder method getAnalysis.

protected AnalysisResult getAnalysis(final PackageBuildContext context, final ActionDescr actionDescr) {
    JavaDialect dialect = (JavaDialect) context.getDialect("java");
    Map<String, Class<?>> variables = new HashMap<String, Class<?>>();
    BoundIdentifiers boundIdentifiers = new BoundIdentifiers(variables, context);
    AnalysisResult analysis = dialect.analyzeBlock(context, actionDescr, actionDescr.getText(), boundIdentifiers);
    return analysis;
}
Also used : HashMap(java.util.HashMap) JavaDialect(org.drools.mvel.java.JavaDialect) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers)

Example 4 with JavaDialect

use of org.drools.mvel.java.JavaDialect 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);
    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) 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 5 with JavaDialect

use of org.drools.mvel.java.JavaDialect in project kogito-runtimes by kiegroup.

the class JavaReturnValueConstraintEvaluatorBuilderTest method testSimpleReturnValueConstraintEvaluator.

@Test
public void testSimpleReturnValueConstraintEvaluator() throws Exception {
    final InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("pkg1");
    ProcessDescr processDescr = new ProcessDescr();
    processDescr.setClassName("Process1");
    processDescr.setName("Process1");
    WorkflowProcessImpl process = new WorkflowProcessImpl();
    process.setName("Process1");
    process.setPackageName("pkg1");
    ReturnValueDescr descr = new ReturnValueDescr();
    descr.setText("return value;");
    builder = new KnowledgeBuilderImpl(pkg);
    DialectCompiletimeRegistry dialectRegistry = builder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    JavaDialect javaDialect = (JavaDialect) dialectRegistry.getDialect("java");
    ProcessBuildContext context = new ProcessBuildContext(builder, pkg, process, processDescr, dialectRegistry, javaDialect);
    builder.addPackageFromDrl(new StringReader("package pkg1;\nglobal Boolean value;"));
    ReturnValueConstraintEvaluator node = new ReturnValueConstraintEvaluator();
    final JavaReturnValueEvaluatorBuilder evaluatorBuilder = new JavaReturnValueEvaluatorBuilder();
    evaluatorBuilder.build(context, node, descr, null);
    ProcessDialectRegistry.getDialect(JavaDialect.ID).addProcess(context);
    javaDialect.compileAll();
    assertEquals(0, javaDialect.getResults().size());
    KogitoProcessRuntime kruntime = createKogitoProcessRuntime();
    kruntime.getKieSession().setGlobal("value", true);
    RuleFlowProcessInstance processInstance = new RuleFlowProcessInstance();
    processInstance.setKnowledgeRuntime((InternalKnowledgeRuntime) kruntime.getKieSession());
    SplitInstance splitInstance = new SplitInstance();
    splitInstance.setProcessInstance(processInstance);
    assertTrue(node.evaluate(splitInstance, null, null));
    kruntime.getKieSession().setGlobal("value", false);
    assertFalse(node.evaluate(splitInstance, null, null));
}
Also used : RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) ReturnValueDescr(org.drools.drl.ast.descr.ReturnValueDescr) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) KogitoProcessRuntime(org.kie.kogito.internal.process.runtime.KogitoProcessRuntime) ProcessDescr(org.drools.drl.ast.descr.ProcessDescr) ReturnValueConstraintEvaluator(org.jbpm.process.instance.impl.ReturnValueConstraintEvaluator) WorkflowProcessImpl(org.jbpm.workflow.core.impl.WorkflowProcessImpl) JavaDialect(org.drools.mvel.java.JavaDialect) SplitInstance(org.jbpm.workflow.instance.node.SplitInstance) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StringReader(java.io.StringReader) JavaReturnValueEvaluatorBuilder(org.jbpm.process.builder.dialect.java.JavaReturnValueEvaluatorBuilder) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.jupiter.api.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Aggregations

JavaDialect (org.drools.mvel.java.JavaDialect)10 StringReader (java.io.StringReader)4 HashMap (java.util.HashMap)4 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)4 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)4 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)4 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)4 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)4 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)4 WorkflowProcessImpl (org.jbpm.workflow.core.impl.WorkflowProcessImpl)4 ProcessDescr (org.drools.compiler.lang.descr.ProcessDescr)3 ProcessDescr (org.drools.drl.ast.descr.ProcessDescr)3 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)2 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)2 JavaDialectRuntimeData (org.drools.core.rule.JavaDialectRuntimeData)2 Wireable (org.drools.core.spi.Wireable)2 ProcessErrorHandler (org.jbpm.process.builder.ProcessErrorHandler)2 ProcessInvokerErrorHandler (org.jbpm.process.builder.ProcessInvokerErrorHandler)2