Search in sources :

Example 21 with VariableScopeInstance

use of org.jbpm.process.instance.context.variable.VariableScopeInstance in project jbpm by kiegroup.

the class ProcessSubProcessTest method testVariableMapping.

@Test
public void testVariableMapping() throws Exception {
    KieSession workingMemory = createStatefulKnowledgeSessionFromRule(true);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("x", "x-value");
    org.jbpm.process.instance.ProcessInstance processInstance = (org.jbpm.process.instance.ProcessInstance) workingMemory.startProcess("com.sample.ruleflow", map);
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    assertEquals(2, workingMemory.getProcessInstances().size());
    for (ProcessInstance p : workingMemory.getProcessInstances()) {
        VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((org.jbpm.process.instance.ProcessInstance) p).getContextInstance(VariableScope.VARIABLE_SCOPE);
        if ("com.sample.ruleflow".equals(p.getProcessId())) {
            assertEquals("x-value", variableScopeInstance.getVariable("x"));
        } else if ("com.sample.subflow".equals(p.getProcessId())) {
            assertEquals("x-value", variableScopeInstance.getVariable("y"));
            assertEquals("z-value", variableScopeInstance.getVariable("z"));
            assertEquals(7, variableScopeInstance.getVariable("n"));
            assertEquals(10, variableScopeInstance.getVariable("o"));
        }
    }
    workingMemory.insert(new Person());
    workingMemory.fireAllRules();
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) processInstance.getContextInstance(VariableScope.VARIABLE_SCOPE);
    assertEquals("z-value", variableScopeInstance.getVariable("x"));
    assertEquals(10, variableScopeInstance.getVariable("m"));
    assertEquals(0, workingMemory.getProcessInstances().size());
}
Also used : HashMap(java.util.HashMap) VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Person(org.jbpm.integrationtests.test.Person) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 22 with VariableScopeInstance

use of org.jbpm.process.instance.context.variable.VariableScopeInstance in project jbpm by kiegroup.

the class ProcessMarshallingTest method testVariablePersistenceMarshallingStrategies.

@Test
public void testVariablePersistenceMarshallingStrategies() throws Exception {
    String process = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + "    xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "    xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + "    type=\"RuleFlow\" name=\"ruleflow\" id=\"org.test.ruleflow\" package-name=\"org.test\" >\n" + "  <header>\n" + "    <variables>\n" + "      <variable name=\"myVariable\" >\n" + "        <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "        <value>OldValue</value>\n" + "      </variable>\n" + "      <variable name=\"myPerson\" >\n" + "        <type name=\"org.jbpm.process.core.datatype.impl.type.ObjectDataType\" className=\"org.jbpm.integrationtests.test.Person\"/>\n" + "      </variable>\n" + "    </variables>\n" + "  </header>\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <workItem id=\"2\" name=\"Email\" >\n" + "      <work name=\"Report\" >\n" + "        <parameter name=\"Subject\" >\n" + "          <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "          <value>Mail</value>\n" + "        </parameter>\n" + "        <parameter name=\"Subject\" >\n" + "          <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "          <value>Mail</value>\n" + "        </parameter>\n" + "      </work>\n" + "    </workItem>\n" + "    <end id=\"3\" name=\"End\" />\n" + "  </nodes>\n" + "  <connections>\n" + "    <connection from=\"1\" to=\"2\"/>\n" + "    <connection from=\"2\" to=\"3\"/>\n" + "  </connections>\n" + "</process>";
    builder.addProcessFromXml(new StringReader(process));
    KieSession session = createKieSession(builder.getPackages());
    TestWorkItemHandler handler = new TestWorkItemHandler();
    session.getWorkItemManager().registerWorkItemHandler("Report", handler);
    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put("myVariable", "ThisIsMyValue");
    Person myPerson = new Person("Nikola Tesla", 156);
    variables.put("myPerson", myPerson);
    session.startProcess("org.test.ruleflow", variables);
    assertEquals(1, session.getProcessInstances().size());
    assertTrue(handler.getWorkItem() != null);
    session = getSerialisedStatefulKnowledgeSession(session);
    assertEquals(1, session.getProcessInstances().size());
    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((ProcessInstance) session.getProcessInstances().iterator().next()).getContextInstance(VariableScope.VARIABLE_SCOPE);
    assertEquals("ThisIsMyValue", variableScopeInstance.getVariable("myVariable"));
    assertEquals(myPerson, variableScopeInstance.getVariable("myPerson"));
    session.getWorkItemManager().completeWorkItem(handler.getWorkItem().getId(), null);
    assertEquals(0, session.getProcessInstances().size());
}
Also used : TestWorkItemHandler(org.jbpm.integrationtests.handler.TestWorkItemHandler) VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) HashMap(java.util.HashMap) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) Person(org.jbpm.integrationtests.test.Person) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 23 with VariableScopeInstance

use of org.jbpm.process.instance.context.variable.VariableScopeInstance in project jbpm by kiegroup.

the class WorkItemNodeInstance method getSourceParameters.

protected Map<String, Object> getSourceParameters(DataAssociation association) {
    Map<String, Object> parameters = new HashMap<String, Object>();
    for (String sourceParam : association.getSources()) {
        Object parameterValue = null;
        VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, sourceParam);
        if (variableScopeInstance != null) {
            parameterValue = variableScopeInstance.getVariable(sourceParam);
        } else {
            try {
                parameterValue = MVELSafeHelper.getEvaluator().eval(sourceParam, new NodeInstanceResolverFactory(this));
            } catch (Throwable t) {
                logger.warn("Could not find variable scope for variable {}", sourceParam);
            }
        }
        if (parameterValue != null) {
            parameters.put(association.getTarget(), parameterValue);
        }
    }
    return parameters;
}
Also used : VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) HashMap(java.util.HashMap) NodeInstanceResolverFactory(org.jbpm.workflow.instance.impl.NodeInstanceResolverFactory)

Example 24 with VariableScopeInstance

use of org.jbpm.process.instance.context.variable.VariableScopeInstance in project jbpm by kiegroup.

the class ForEachNodeInstance method evaluateCollectionExpression.

private Collection<?> evaluateCollectionExpression(String collectionExpression) {
    // TODO: should evaluate this expression using MVEL
    Object collection = null;
    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, collectionExpression);
    if (variableScopeInstance != null) {
        collection = variableScopeInstance.getVariable(collectionExpression);
    } else {
        try {
            collection = MVELSafeHelper.getEvaluator().eval(collectionExpression, new NodeInstanceResolverFactory(this));
        } catch (Throwable t) {
            throw new IllegalArgumentException("Could not find collection " + collectionExpression);
        }
    }
    if (collection == null) {
        return Collections.EMPTY_LIST;
    }
    if (collection instanceof Collection<?>) {
        return (Collection<?>) collection;
    }
    if (collection.getClass().isArray()) {
        List<Object> list = new ArrayList<Object>();
        for (Object o : (Object[]) collection) {
            list.add(o);
        }
        return list;
    }
    throw new IllegalArgumentException("Unexpected collection type: " + collection.getClass());
}
Also used : VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) NodeInstanceResolverFactory(org.jbpm.workflow.instance.impl.NodeInstanceResolverFactory) ArrayList(java.util.ArrayList) Collection(java.util.Collection)

Example 25 with VariableScopeInstance

use of org.jbpm.process.instance.context.variable.VariableScopeInstance in project jbpm by kiegroup.

the class StartNodeInstance method signalEvent.

public void signalEvent(String type, Object event) {
    String variableName = (String) getStartNode().getMetaData("TriggerMapping");
    if (variableName != null) {
        VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, variableName);
        if (variableScopeInstance == null) {
            throw new IllegalArgumentException("Could not find variable for start node: " + variableName);
        }
        EventTransformer transformer = getStartNode().getEventTransformer();
        if (transformer != null) {
            event = transformer.transformEvent(event);
        }
        variableScopeInstance.setVariable(variableName, event);
    }
    triggerCompleted();
}
Also used : VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) EventTransformer(org.jbpm.process.core.event.EventTransformer)

Aggregations

VariableScopeInstance (org.jbpm.process.instance.context.variable.VariableScopeInstance)38 HashMap (java.util.HashMap)17 Map (java.util.Map)12 NodeInstanceResolverFactory (org.jbpm.workflow.instance.impl.NodeInstanceResolverFactory)9 ExclusiveGroupInstance (org.jbpm.process.instance.context.exclusive.ExclusiveGroupInstance)8 NodeInstance (org.kie.api.runtime.process.NodeInstance)8 ArrayList (java.util.ArrayList)6 Matcher (java.util.regex.Matcher)6 SwimlaneContextInstance (org.jbpm.process.instance.context.swimlane.SwimlaneContextInstance)6 DataAssociation (org.jbpm.workflow.core.node.DataAssociation)6 Transformation (org.jbpm.workflow.core.node.Transformation)6 DataTransformer (org.kie.api.runtime.process.DataTransformer)6 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)6 ContextInstance (org.jbpm.process.instance.ContextInstance)5 WorkflowProcessInstanceImpl (org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl)5 Process (org.kie.api.definition.process.Process)5 MarshallerReaderContext (org.drools.core.marshalling.impl.MarshallerReaderContext)4 MarshallerWriteContext (org.drools.core.marshalling.impl.MarshallerWriteContext)4 Context (org.jbpm.process.core.Context)4 SwimlaneContext (org.jbpm.process.core.context.swimlane.SwimlaneContext)4