Search in sources :

Example 1 with ReturnValueDescr

use of org.drools.drl.ast.descr.ReturnValueDescr 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 2 with ReturnValueDescr

use of org.drools.drl.ast.descr.ReturnValueDescr 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)

Example 3 with ReturnValueDescr

use of org.drools.drl.ast.descr.ReturnValueDescr in project kogito-runtimes by kiegroup.

the class MultiConditionalSequenceFlowNodeBuilder method build.

public void build(Process process, ProcessDescr processDescr, ProcessBuildContext context, Node node) {
    Map<ConnectionRef, Constraint> constraints = ((NodeImpl) node).getConstraints();
    // exclude split as it is handled with separate builder and nodes with non conditional sequence flows
    if (node instanceof Split || constraints.size() == 0) {
        return;
    }
    // we need to clone the map, so we can update the original while iterating.
    Map<ConnectionRef, Constraint> map = new HashMap<ConnectionRef, Constraint>(constraints);
    for (Iterator<Map.Entry<ConnectionRef, Constraint>> it = map.entrySet().iterator(); it.hasNext(); ) {
        Map.Entry<ConnectionRef, Constraint> entry = it.next();
        ConnectionRef connection = entry.getKey();
        ConstraintImpl constraint = (ConstraintImpl) entry.getValue();
        Connection outgoingConnection = null;
        for (Connection out : ((NodeImpl) node).getDefaultOutgoingConnections()) {
            if (out.getToType().equals(connection.getToType()) && out.getTo().getId() == connection.getNodeId()) {
                outgoingConnection = out;
            }
        }
        if (outgoingConnection == null) {
            throw new IllegalArgumentException("Could not find outgoing connection");
        }
        if ("rule".equals(constraint.getType())) {
            RuleConstraintEvaluator ruleConstraint = new RuleConstraintEvaluator();
            ruleConstraint.setDialect(constraint.getDialect());
            ruleConstraint.setName(constraint.getName());
            ruleConstraint.setPriority(constraint.getPriority());
            ruleConstraint.setDefault(constraint.isDefault());
            ((NodeImpl) node).setConstraint(outgoingConnection, ruleConstraint);
        } else if ("code".equals(constraint.getType())) {
            ReturnValueConstraintEvaluator returnValueConstraint = new ReturnValueConstraintEvaluator();
            returnValueConstraint.setDialect(constraint.getDialect());
            returnValueConstraint.setName(constraint.getName());
            returnValueConstraint.setPriority(constraint.getPriority());
            returnValueConstraint.setDefault(constraint.isDefault());
            ((NodeImpl) node).setConstraint(outgoingConnection, returnValueConstraint);
            ReturnValueDescr returnValueDescr = new ReturnValueDescr();
            returnValueDescr.setText(constraint.getConstraint());
            returnValueDescr.setResource(processDescr.getResource());
            ProcessDialect dialect = ProcessDialectRegistry.getDialect(constraint.getDialect());
            dialect.getReturnValueEvaluatorBuilder().build(context, returnValueConstraint, returnValueDescr, (NodeImpl) node);
        }
    }
}
Also used : NodeImpl(org.jbpm.workflow.core.impl.NodeImpl) Constraint(org.jbpm.workflow.core.Constraint) HashMap(java.util.HashMap) ReturnValueDescr(org.drools.drl.ast.descr.ReturnValueDescr) Connection(org.kie.api.definition.process.Connection) ReturnValueConstraintEvaluator(org.jbpm.process.instance.impl.ReturnValueConstraintEvaluator) ProcessDialect(org.jbpm.process.builder.dialect.ProcessDialect) ConstraintImpl(org.jbpm.workflow.core.impl.ConstraintImpl) RuleConstraintEvaluator(org.jbpm.process.instance.impl.RuleConstraintEvaluator) Split(org.jbpm.workflow.core.node.Split) ConnectionRef(org.jbpm.workflow.core.impl.ConnectionRef) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with ReturnValueDescr

use of org.drools.drl.ast.descr.ReturnValueDescr in project kogito-runtimes by kiegroup.

the class SplitNodeBuilder method build.

public void build(Process process, ProcessDescr processDescr, ProcessBuildContext context, Node node) {
    Split splitNode = (Split) node;
    if (splitNode.getType() != Split.TYPE_XOR && splitNode.getType() != Split.TYPE_OR) {
        // we only process or/xor
        return;
    }
    // we need to clone the map, so we can update the original while iterating.
    Map<ConnectionRef, Constraint> map = new HashMap<ConnectionRef, Constraint>(splitNode.getConstraints());
    for (Iterator<Map.Entry<ConnectionRef, Constraint>> it = map.entrySet().iterator(); it.hasNext(); ) {
        Map.Entry<ConnectionRef, Constraint> entry = it.next();
        ConnectionRef connection = entry.getKey();
        ConstraintImpl constraint = (ConstraintImpl) entry.getValue();
        Connection outgoingConnection = null;
        for (Connection out : splitNode.getDefaultOutgoingConnections()) {
            if (out.getToType().equals(connection.getToType()) && out.getTo().getId() == connection.getNodeId()) {
                outgoingConnection = out;
            }
        }
        if (outgoingConnection == null) {
            throw new IllegalArgumentException("Could not find outgoing connection");
        }
        if (constraint == null && splitNode.isDefault(outgoingConnection)) {
        // do nothing since conditions are ignored for default sequence flow
        } else if (constraint != null && "rule".equals(constraint.getType())) {
            RuleConstraintEvaluator ruleConstraint = new RuleConstraintEvaluator();
            ruleConstraint.setDialect(constraint.getDialect());
            ruleConstraint.setName(constraint.getName());
            ruleConstraint.setPriority(constraint.getPriority());
            ruleConstraint.setDefault(constraint.isDefault());
            ruleConstraint.setType(constraint.getType());
            ruleConstraint.setConstraint(constraint.getConstraint());
            splitNode.setConstraint(outgoingConnection, ruleConstraint);
        } else if (constraint != null && "code".equals(constraint.getType())) {
            ReturnValueConstraintEvaluator returnValueConstraint = new ReturnValueConstraintEvaluator();
            returnValueConstraint.setDialect(constraint.getDialect());
            returnValueConstraint.setName(constraint.getName());
            returnValueConstraint.setPriority(constraint.getPriority());
            returnValueConstraint.setDefault(constraint.isDefault());
            returnValueConstraint.setType(constraint.getType());
            returnValueConstraint.setConstraint(constraint.getConstraint());
            splitNode.setConstraint(outgoingConnection, returnValueConstraint);
            ReturnValueDescr returnValueDescr = new ReturnValueDescr();
            returnValueDescr.setText(constraint.getConstraint());
            returnValueDescr.setResource(processDescr.getResource());
            ProcessDialect dialect = ProcessDialectRegistry.getDialect(constraint.getDialect());
            dialect.getReturnValueEvaluatorBuilder().build(context, returnValueConstraint, returnValueDescr, (NodeImpl) node);
        }
    }
}
Also used : Constraint(org.jbpm.workflow.core.Constraint) HashMap(java.util.HashMap) ReturnValueDescr(org.drools.drl.ast.descr.ReturnValueDescr) Connection(org.kie.api.definition.process.Connection) ReturnValueConstraintEvaluator(org.jbpm.process.instance.impl.ReturnValueConstraintEvaluator) ProcessDialect(org.jbpm.process.builder.dialect.ProcessDialect) ConstraintImpl(org.jbpm.workflow.core.impl.ConstraintImpl) RuleConstraintEvaluator(org.jbpm.process.instance.impl.RuleConstraintEvaluator) Split(org.jbpm.workflow.core.node.Split) ConnectionRef(org.jbpm.workflow.core.impl.ConnectionRef) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ReturnValueDescr (org.drools.drl.ast.descr.ReturnValueDescr)4 ReturnValueConstraintEvaluator (org.jbpm.process.instance.impl.ReturnValueConstraintEvaluator)4 StringReader (java.io.StringReader)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)2 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)2 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)2 SemanticKnowledgeBuilderConfigurationImpl (org.jbpm.compiler.xml.compiler.SemanticKnowledgeBuilderConfigurationImpl)2 ProcessDialect (org.jbpm.process.builder.dialect.ProcessDialect)2 RuleConstraintEvaluator (org.jbpm.process.instance.impl.RuleConstraintEvaluator)2 RuleFlowProcessInstance (org.jbpm.ruleflow.instance.RuleFlowProcessInstance)2 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)2 Constraint (org.jbpm.workflow.core.Constraint)2 ConnectionRef (org.jbpm.workflow.core.impl.ConnectionRef)2 ConstraintImpl (org.jbpm.workflow.core.impl.ConstraintImpl)2 Split (org.jbpm.workflow.core.node.Split)2 SplitInstance (org.jbpm.workflow.instance.node.SplitInstance)2 Test (org.junit.jupiter.api.Test)2 Connection (org.kie.api.definition.process.Connection)2