Search in sources :

Example 1 with SemanticKnowledgeBuilderConfigurationImpl

use of org.jbpm.compiler.xml.compiler.SemanticKnowledgeBuilderConfigurationImpl in project kogito-runtimes by kiegroup.

the class MVELDecisionBuilderTest 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, new SemanticKnowledgeBuilderConfigurationImpl());
    PackageRegistry pkgReg = builder.getPackageRegistry(pkg.getName());
    MVELDialect mvelDialect = (MVELDialect) pkgReg.getDialectCompiletimeRegistry().getDialect("mvel");
    PackageBuildContext context = new PackageBuildContext();
    context.init(builder, pkg, null, pkgReg.getDialectCompiletimeRegistry(), mvelDialect, 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);
    final MVELActionBuilder actionBuilder = new MVELActionBuilder();
    actionBuilder.build(context, action, actionDescr, actionNode);
    KogitoProcessRuntime kruntime = createKogitoProcessRuntime();
    List<String> list = new ArrayList<String>();
    kruntime.getKieSession().setGlobal("list", list);
    MVELDialectRuntimeData data = (MVELDialectRuntimeData) builder.getPackage("pkg1").getDialectRuntimeRegistry().getDialectData("mvel");
    KogitoProcessContext processContext = new KogitoProcessContextImpl(kruntime.getKieSession());
    ((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) Action(org.jbpm.process.instance.impl.Action) DroolsAction(org.jbpm.workflow.core.DroolsAction) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) 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) MVELDialect(org.drools.mvel.builder.MVELDialect) SemanticKnowledgeBuilderConfigurationImpl(org.jbpm.compiler.xml.compiler.SemanticKnowledgeBuilderConfigurationImpl) MVELDialectRuntimeData(org.drools.mvel.MVELDialectRuntimeData) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) PackageBuildContext(org.drools.compiler.rule.builder.PackageBuildContext) KogitoProcessContext(org.kie.kogito.internal.process.runtime.KogitoProcessContext) MVELAction(org.jbpm.process.instance.impl.MVELAction) 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 2 with SemanticKnowledgeBuilderConfigurationImpl

use of org.jbpm.compiler.xml.compiler.SemanticKnowledgeBuilderConfigurationImpl in project kogito-runtimes by kiegroup.

the class MVELReturnValueConstraintEvaluatorBuilderTest method testSimpleReturnValueConstraintEvaluator.

@Test
public void testSimpleReturnValueConstraintEvaluator() throws Exception {
    final InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("pkg1");
    ReturnValueDescr descr = new ReturnValueDescr();
    descr.setText("return value");
    builder = new KnowledgeBuilderImpl(pkg, new SemanticKnowledgeBuilderConfigurationImpl());
    DialectCompiletimeRegistry dialectRegistry = builder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
    PackageBuildContext context = new PackageBuildContext();
    context.init(builder, pkg, null, dialectRegistry, mvelDialect, null);
    builder.addPackageFromDrl(new StringReader("package pkg1;\nglobal Boolean value;"));
    ReturnValueConstraintEvaluator node = new ReturnValueConstraintEvaluator();
    final MVELReturnValueEvaluatorBuilder evaluatorBuilder = new MVELReturnValueEvaluatorBuilder();
    evaluatorBuilder.build(context, node, descr, null);
    KogitoProcessRuntime kruntime = createKogitoProcessRuntime();
    kruntime.getKieSession().setGlobal("value", true);
    RuleFlowProcessInstance processInstance = new RuleFlowProcessInstance();
    processInstance.setKnowledgeRuntime((InternalKnowledgeRuntime) kruntime.getKieSession());
    SplitInstance splitInstance = new SplitInstance();
    splitInstance.setProcessInstance(processInstance);
    MVELDialectRuntimeData data = (MVELDialectRuntimeData) builder.getPackage("pkg1").getDialectRuntimeRegistry().getDialectData("mvel");
    ((MVELReturnValueEvaluator) node.getReturnValueEvaluator()).compile(data);
    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) MVELReturnValueEvaluator(org.jbpm.process.instance.impl.MVELReturnValueEvaluator) MVELDialect(org.drools.mvel.builder.MVELDialect) ReturnValueConstraintEvaluator(org.jbpm.process.instance.impl.ReturnValueConstraintEvaluator) MVELReturnValueEvaluatorBuilder(org.jbpm.process.builder.dialect.mvel.MVELReturnValueEvaluatorBuilder) SplitInstance(org.jbpm.workflow.instance.node.SplitInstance) SemanticKnowledgeBuilderConfigurationImpl(org.jbpm.compiler.xml.compiler.SemanticKnowledgeBuilderConfigurationImpl) MVELDialectRuntimeData(org.drools.mvel.MVELDialectRuntimeData) PackageBuildContext(org.drools.compiler.rule.builder.PackageBuildContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StringReader(java.io.StringReader) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.jupiter.api.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 3 with SemanticKnowledgeBuilderConfigurationImpl

use of org.jbpm.compiler.xml.compiler.SemanticKnowledgeBuilderConfigurationImpl in project kogito-runtimes by kiegroup.

the class DumperTestHelper method XmlFile.

public static void XmlFile(String filename) throws Exception {
    SemanticKnowledgeBuilderConfigurationImpl conf = new SemanticKnowledgeBuilderConfigurationImpl();
    XmlPackageReader xmlPackageReader = new XmlPackageReader(conf.getSemanticModules());
    xmlPackageReader.getParser().setClassLoader(DumperTestHelper.class.getClassLoader());
    xmlPackageReader.read(new InputStreamReader(DumperTestHelper.class.getResourceAsStream(filename)));
    final PackageDescr pkgOriginal = xmlPackageReader.getPackageDescr();
    final XmlDumper dumper = new XmlDumper();
    final String result = dumper.dump(pkgOriginal);
    String buffer = readFile(filename);
    System.out.println(buffer);
    System.out.println(result);
    assertThat(buffer).isEqualToIgnoringWhitespace(result);
    assertThat(result).isNotNull();
}
Also used : SemanticKnowledgeBuilderConfigurationImpl(org.jbpm.compiler.xml.compiler.SemanticKnowledgeBuilderConfigurationImpl) InputStreamReader(java.io.InputStreamReader) PackageDescr(org.drools.drl.ast.descr.PackageDescr) XmlDumper(org.jbpm.compiler.xml.compiler.XmlDumper) XmlPackageReader(org.jbpm.compiler.xml.compiler.XmlPackageReader)

Example 4 with SemanticKnowledgeBuilderConfigurationImpl

use of org.jbpm.compiler.xml.compiler.SemanticKnowledgeBuilderConfigurationImpl 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, new SemanticKnowledgeBuilderConfigurationImpl());
    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) SemanticKnowledgeBuilderConfigurationImpl(org.jbpm.compiler.xml.compiler.SemanticKnowledgeBuilderConfigurationImpl) 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 SemanticKnowledgeBuilderConfigurationImpl

use of org.jbpm.compiler.xml.compiler.SemanticKnowledgeBuilderConfigurationImpl 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, new SemanticKnowledgeBuilderConfigurationImpl());
    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) SemanticKnowledgeBuilderConfigurationImpl(org.jbpm.compiler.xml.compiler.SemanticKnowledgeBuilderConfigurationImpl) 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

SemanticKnowledgeBuilderConfigurationImpl (org.jbpm.compiler.xml.compiler.SemanticKnowledgeBuilderConfigurationImpl)15 StringReader (java.io.StringReader)10 Test (org.junit.jupiter.api.Test)10 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)9 KogitoProcessRuntime (org.kie.kogito.internal.process.runtime.KogitoProcessRuntime)8 ArrayList (java.util.ArrayList)6 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)5 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)5 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)4 PackageBuildContext (org.drools.compiler.rule.builder.PackageBuildContext)3 ActionDescr (org.drools.drl.ast.descr.ActionDescr)3 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)3 MVELDialectRuntimeData (org.drools.mvel.MVELDialectRuntimeData)3 MVELDialect (org.drools.mvel.builder.MVELDialect)3 ReaderResource (org.drools.util.io.ReaderResource)3 TestWorkItemHandler (org.jbpm.integrationtests.handler.TestWorkItemHandler)3 Person (org.jbpm.integrationtests.test.Person)3 KogitoProcessContextImpl (org.jbpm.process.instance.KogitoProcessContextImpl)3 Action (org.jbpm.process.instance.impl.Action)3 DroolsAction (org.jbpm.workflow.core.DroolsAction)3