Search in sources :

Example 26 with DroolsAction

use of org.jbpm.workflow.core.DroolsAction 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 27 with DroolsAction

use of org.jbpm.workflow.core.DroolsAction in project jbpm by kiegroup.

the class EndEventHandler method handleSignalNode.

public void handleSignalNode(final Node node, final Element element, final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
    EndNode endNode = (EndNode) node;
    org.w3c.dom.Node xmlNode = element.getFirstChild();
    while (xmlNode != null) {
        String nodeName = xmlNode.getNodeName();
        if ("dataInput".equals(nodeName)) {
            String id = ((Element) xmlNode).getAttribute("id");
            String inputName = ((Element) xmlNode).getAttribute("name");
            dataInputs.put(id, inputName);
        } else if ("dataInputAssociation".equals(nodeName)) {
            readEndDataInputAssociation(xmlNode, endNode);
        } else if ("signalEventDefinition".equals(nodeName)) {
            String signalName = ((Element) xmlNode).getAttribute("signalRef");
            String variable = (String) endNode.getMetaData("MappingVariable");
            signalName = checkSignalAndConvertToRealSignalNam(parser, signalName);
            endNode.setMetaData("EventType", "signal");
            endNode.setMetaData("Ref", signalName);
            endNode.setMetaData("Variable", variable);
            // check if signal should be send async
            if (dataInputs.containsValue("async")) {
                signalName = "ASYNC-" + signalName;
            }
            String signalExpression = getSignalExpression(endNode, signalName, variable);
            List<DroolsAction> actions = new ArrayList<DroolsAction>();
            actions.add(new DroolsConsequenceAction("mvel", signalExpression));
            endNode.setActions(EndNode.EVENT_NODE_ENTER, actions);
        }
        xmlNode = xmlNode.getNextSibling();
    }
}
Also used : DroolsAction(org.jbpm.workflow.core.DroolsAction) EndNode(org.jbpm.workflow.core.node.EndNode) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList)

Example 28 with DroolsAction

use of org.jbpm.workflow.core.DroolsAction in project jbpm by kiegroup.

the class EndNodeHandler method writeNode.

public void writeNode(Node node, StringBuilder xmlDump, int metaDataType) {
    EndNode endNode = (EndNode) node;
    String eventType = (String) endNode.getMetaData("EventType");
    String ref = (String) endNode.getMetaData("Ref");
    String variableRef = (String) endNode.getMetaData("Variable");
    writeNode("endEvent", endNode, xmlDump, metaDataType);
    if (endNode.isTerminate()) {
        xmlDump.append(">" + EOL);
        writeExtensionElements(endNode, xmlDump);
        xmlDump.append("        <terminateEventDefinition " + (endNode.getScope() == EndNode.PROCESS_SCOPE ? "tns:scope=\"process\"" : "") + "/>" + EOL);
        endNode("endEvent", xmlDump);
    } else {
        String scope = (String) endNode.getMetaData("customScope");
        List<DroolsAction> actions = endNode.getActions(EndNode.EVENT_NODE_ENTER);
        if (actions != null && !actions.isEmpty()) {
            if (actions.size() == 1) {
                DroolsConsequenceAction action = (DroolsConsequenceAction) actions.get(0);
                String s = action.getConsequence();
                if (s.startsWith("org.drools.core.process.instance.impl.WorkItemImpl workItem = new org.drools.core.process.instance.impl.WorkItemImpl();")) {
                    xmlDump.append(">" + EOL);
                    writeExtensionElements(endNode, xmlDump);
                    String variable = (String) endNode.getMetaData("MappingVariable");
                    if (variable != null) {
                        xmlDump.append("      <dataInput id=\"" + XmlBPMNProcessDumper.getUniqueNodeId(endNode) + "_Input\" />" + EOL + "      <dataInputAssociation>" + EOL + "        <sourceRef>" + XmlDumper.replaceIllegalChars(variable) + "</sourceRef>" + EOL + "        <targetRef>" + XmlBPMNProcessDumper.getUniqueNodeId(endNode) + "_Input</targetRef>" + EOL + "      </dataInputAssociation>" + EOL + "      <inputSet>" + EOL + "        <dataInputRefs>" + XmlBPMNProcessDumper.getUniqueNodeId(endNode) + "_Input</dataInputRefs>" + EOL + "      </inputSet>" + EOL);
                    }
                    xmlDump.append("      <messageEventDefinition messageRef=\"" + XmlBPMNProcessDumper.getUniqueNodeId(endNode) + "_Message\"/>" + EOL);
                    endNode("endEvent", xmlDump);
                } else if ("signal".equals(eventType)) {
                    xmlDump.append(">" + EOL);
                    writeExtensionElements(endNode, xmlDump);
                    if (!s.startsWith("null")) {
                        xmlDump.append("      <dataInput id=\"" + XmlBPMNProcessDumper.getUniqueNodeId(endNode) + "_Input\" />" + EOL + "      <dataInputAssociation>" + EOL + "        <sourceRef>" + XmlDumper.replaceIllegalChars(variableRef) + "</sourceRef>" + EOL + "        <targetRef>" + XmlBPMNProcessDumper.getUniqueNodeId(endNode) + "_Input</targetRef>" + EOL + "      </dataInputAssociation>" + EOL + "      <inputSet>" + EOL + "        <dataInputRefs>" + XmlBPMNProcessDumper.getUniqueNodeId(endNode) + "_Input</dataInputRefs>" + EOL + "      </inputSet>" + EOL);
                    }
                    xmlDump.append("      <signalEventDefinition signalRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(ref) + "\"/>" + EOL);
                    endNode("endEvent", xmlDump);
                } else if (s.startsWith(RUNTIME_SIGNAL_EVENT)) {
                    xmlDump.append(">" + EOL);
                    writeExtensionElements(endNode, xmlDump);
                    s = s.substring(44);
                    String type = s.substring(0, s.indexOf("\""));
                    s = s.substring(s.indexOf(",") + 2);
                    String variable = null;
                    if (!s.startsWith("null")) {
                        variable = s.substring(0, s.indexOf(")"));
                        xmlDump.append("      <dataInput id=\"" + XmlBPMNProcessDumper.getUniqueNodeId(endNode) + "_Input\" />" + EOL + "      <dataInputAssociation>" + EOL + "        <sourceRef>" + XmlDumper.replaceIllegalChars(variable) + "</sourceRef>" + EOL + "        <targetRef>" + XmlBPMNProcessDumper.getUniqueNodeId(endNode) + "_Input</targetRef>" + EOL + "      </dataInputAssociation>" + EOL + "      <inputSet>" + EOL + "        <dataInputRefs>" + XmlBPMNProcessDumper.getUniqueNodeId(endNode) + "_Input</dataInputRefs>" + EOL + "      </inputSet>" + EOL);
                    }
                    xmlDump.append("      <signalEventDefinition signalRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(type) + "\"/>" + EOL);
                    endNode("endEvent", xmlDump);
                } else if (s.startsWith(PROCESS_INSTANCE_SIGNAL_EVENT) && "processInstance".equals(scope)) {
                    xmlDump.append(">" + EOL);
                    writeExtensionElements(endNode, xmlDump);
                    s = s.substring(43);
                    String type = s.substring(0, s.indexOf("\""));
                    s = s.substring(s.indexOf(",") + 2);
                    String variable = null;
                    if (!s.startsWith("null")) {
                        variable = s.substring(0, s.indexOf(")"));
                        xmlDump.append("      <dataInput id=\"" + XmlBPMNProcessDumper.getUniqueNodeId(endNode) + "_Input\" />" + EOL + "      <dataInputAssociation>" + EOL + "        <sourceRef>" + XmlDumper.replaceIllegalChars(variable) + "</sourceRef>" + EOL + "        <targetRef>" + XmlBPMNProcessDumper.getUniqueNodeId(endNode) + "_Input</targetRef>" + EOL + "      </dataInputAssociation>" + EOL + "      <inputSet>" + EOL + "        <dataInputRefs>" + XmlBPMNProcessDumper.getUniqueNodeId(endNode) + "_Input</dataInputRefs>" + EOL + "      </inputSet>" + EOL);
                    }
                    xmlDump.append("      <signalEventDefinition signalRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(type) + "\"/>" + EOL);
                    endNode("endEvent", xmlDump);
                } else if (s.startsWith(PROCESS_INSTANCE_SIGNAL_EVENT)) {
                    xmlDump.append(">" + EOL);
                    writeExtensionElements(endNode, xmlDump);
                    int begin = (PROCESS_INSTANCE_SIGNAL_EVENT + "Compensation\", ").length() - 2;
                    int end = s.length() - 3;
                    String compensationEvent = s.substring(begin, end);
                    String activityRef = "";
                    if (!compensationEvent.startsWith(CompensationScope.IMPLICIT_COMPENSATION_PREFIX)) {
                        // specific
                        activityRef = "activityRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(activityRef) + "\" ";
                    }
                    // else general: activityRef = "" (above)
                    xmlDump.append("      <compensateEventDefinition " + activityRef + "/>" + EOL);
                    endNode("endEvent", xmlDump);
                } else {
                    throw new IllegalArgumentException("Unknown action " + s);
                }
            }
        } else {
            endNode(xmlDump);
        }
    }
}
Also used : DroolsAction(org.jbpm.workflow.core.DroolsAction) EndNode(org.jbpm.workflow.core.node.EndNode) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction)

Example 29 with DroolsAction

use of org.jbpm.workflow.core.DroolsAction in project jbpm by kiegroup.

the class TimerHandler method end.

public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
    Element element = parser.endElementBuilder();
    StateBasedNode parent = (StateBasedNode) parser.getParent();
    String id = element.getAttribute("id");
    emptyAttributeCheck(localName, "id", id, parser);
    String delay = element.getAttribute("delay");
    String period = element.getAttribute("period");
    Timer timer = new Timer();
    timer.setId(new Long(id));
    if (delay != null && delay.length() != 0) {
        timer.setDelay(delay);
    }
    if (period != null && period.length() != 0) {
        timer.setPeriod(period);
    }
    org.w3c.dom.Node xmlNode = element.getFirstChild();
    DroolsAction action = null;
    if (xmlNode instanceof Element) {
        Element actionXml = (Element) xmlNode;
        action = AbstractNodeHandler.extractAction(actionXml);
    }
    parent.addTimer(timer, action);
    return null;
}
Also used : StateBasedNode(org.jbpm.workflow.core.node.StateBasedNode) DroolsAction(org.jbpm.workflow.core.DroolsAction) Timer(org.jbpm.process.core.timer.Timer) Element(org.w3c.dom.Element)

Example 30 with DroolsAction

use of org.jbpm.workflow.core.DroolsAction in project jbpm by kiegroup.

the class EventBasedNodeBuilder method build.

public void build(Process process, ProcessDescr processDescr, ProcessBuildContext context, Node node) {
    super.build(process, processDescr, context, node);
    Map<Timer, DroolsAction> timers = ((StateBasedNode) node).getTimers();
    if (timers != null) {
        for (DroolsAction action : timers.values()) {
            buildAction(action, context, (NodeImpl) node);
        }
    }
}
Also used : DroolsAction(org.jbpm.workflow.core.DroolsAction) StateBasedNode(org.jbpm.workflow.core.node.StateBasedNode) Timer(org.jbpm.process.core.timer.Timer)

Aggregations

DroolsAction (org.jbpm.workflow.core.DroolsAction)53 DroolsConsequenceAction (org.jbpm.workflow.core.impl.DroolsConsequenceAction)40 ArrayList (java.util.ArrayList)30 ActionNode (org.jbpm.workflow.core.node.ActionNode)25 Action (org.jbpm.process.instance.impl.Action)22 EndNode (org.jbpm.workflow.core.node.EndNode)22 RuleFlowProcess (org.jbpm.ruleflow.core.RuleFlowProcess)19 Test (org.junit.Test)18 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)17 StartNode (org.jbpm.workflow.core.node.StartNode)17 KieSession (org.kie.api.runtime.KieSession)17 ProcessContext (org.kie.api.runtime.process.ProcessContext)17 EventTypeFilter (org.jbpm.process.core.event.EventTypeFilter)15 EventNode (org.jbpm.workflow.core.node.EventNode)14 ConnectionImpl (org.jbpm.workflow.core.impl.ConnectionImpl)13 Variable (org.jbpm.process.core.context.variable.Variable)10 ObjectDataType (org.jbpm.process.core.datatype.impl.type.ObjectDataType)10 TestProcessEventListener (org.jbpm.process.test.TestProcessEventListener)9 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)8 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)8