Search in sources :

Example 1 with PackageBuildContext

use of org.drools.compiler.rule.builder.PackageBuildContext in project drools by kiegroup.

the class TypeDeclarationConfigurator method getMvelAnalysisResult.

private static MVELAnalysisResult getMvelAnalysisResult(KnowledgeBuilderImpl kbuilder, BaseDescr typeDescr, TypeDeclaration type, PackageRegistry pkgRegistry, String durationField, InternalKnowledgePackage pkg) {
    MVELDialect dialect = (MVELDialect) pkgRegistry.getDialectCompiletimeRegistry().getDialect("mvel");
    PackageBuildContext context = new PackageBuildContext();
    context.init(kbuilder, pkg, typeDescr, pkgRegistry.getDialectCompiletimeRegistry(), dialect, null);
    if (!type.isTypesafe()) {
        context.setTypesafe(false);
    }
    return (MVELAnalysisResult) context.getDialect().analyzeExpression(context, typeDescr, durationField, new BoundIdentifiers(type.getTypeClass()));
}
Also used : PackageBuildContext(org.drools.compiler.rule.builder.PackageBuildContext) MVELAnalysisResult(org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult) MVELDialect(org.drools.compiler.rule.builder.dialect.mvel.MVELDialect) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers)

Example 2 with PackageBuildContext

use of org.drools.compiler.rule.builder.PackageBuildContext 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 3 with PackageBuildContext

use of org.drools.compiler.rule.builder.PackageBuildContext in project jbpm by kiegroup.

the class MVELReturnValueConstraintEvaluatorBuilderTest method testSimpleReturnValueConstraintEvaluator.

@Test
public void testSimpleReturnValueConstraintEvaluator() throws Exception {
    final InternalKnowledgePackage pkg = new KnowledgePackageImpl("pkg1");
    ReturnValueDescr descr = new ReturnValueDescr();
    descr.setText("return value");
    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 Boolean value;"));
    ReturnValueConstraintEvaluator node = new ReturnValueConstraintEvaluator();
    final MVELReturnValueEvaluatorBuilder builder = new MVELReturnValueEvaluatorBuilder();
    builder.build(context, node, descr, null);
    final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Arrays.asList(pkgBuilder.getPackages()));
    final KieSession ksession = kbase.newKieSession();
    ksession.setGlobal("value", true);
    RuleFlowProcessInstance processInstance = new RuleFlowProcessInstance();
    processInstance.setKnowledgeRuntime((InternalKnowledgeRuntime) ksession);
    SplitInstance splitInstance = new SplitInstance();
    splitInstance.setProcessInstance(processInstance);
    MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkgBuilder.getPackage("pkg1").getDialectRuntimeRegistry().getDialectData("mvel");
    ((MVELReturnValueEvaluator) node.getReturnValueEvaluator()).compile(data);
    assertTrue(node.evaluate(splitInstance, null, null));
    ksession.setGlobal("value", false);
    assertFalse(node.evaluate(splitInstance, null, null));
}
Also used : RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) ReturnValueDescr(org.drools.compiler.compiler.ReturnValueDescr) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) MVELReturnValueEvaluator(org.jbpm.process.instance.impl.MVELReturnValueEvaluator) MVELDialect(org.drools.compiler.rule.builder.dialect.mvel.MVELDialect) ReturnValueConstraintEvaluator(org.jbpm.process.instance.impl.ReturnValueConstraintEvaluator) MVELReturnValueEvaluatorBuilder(org.jbpm.process.builder.dialect.mvel.MVELReturnValueEvaluatorBuilder) SplitInstance(org.jbpm.workflow.instance.node.SplitInstance) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) PackageBuildContext(org.drools.compiler.rule.builder.PackageBuildContext) 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 4 with PackageBuildContext

use of org.drools.compiler.rule.builder.PackageBuildContext 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

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