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());
}
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());
}
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;
}
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());
}
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();
}
Aggregations