看似是一个variable有name和mapped-name两个名称,其实情况很复杂。看例子。
假定我们有一个极其简单的流程vartest (surprisingly it also means V-Artest, but i must say that i'm not a Laker fan) 。在start-state的controller里添加一个variable,name为"var",mapped-name为"variable",如下:
<?xml version="1.0" encoding="UTF-8"?> <process-definition xmlns="" name="vartest"> <start-state name="start-state1"> <task> <controller> <variable access="read,write" name="var" mapped-name="variable"></variable> </controller> </task> <transition to="task-node1"></transition> </start-state> <task-node name="task-node1" <transition to="end-state1"></transition> </task-node> <end-state name="end-state1"></end-state> </process-definition>
然后我们来写一个Test Case:
public class AccessVariable extends TestCase
{
public void testPayProcess() throws Exception
{
JbpmContext jc = JbpmConfiguration.getInstance().
createJbpmContext();
ProcessDefinition pd = jc.getGraphSession().
findLatestProcessDefinition("vartest");
ProcessInstance pi = pd.createProcessInstance();
ContextInstance ci = pi.getContextInstance();
TaskInstance ti = pi.getTaskMgmtInstance().createStartTaskInstance()
//hint 1:
//ci can create new variable (not declared in <controller>)
//and ti can access the variable ci created
ci.setVariable("abc", "ABC");
System.out.println(ti.getVariable("abc")); //ABC
ti.setVariable("abc", "CBA");
System.out.println(ci.getVariable("abc")); //CBA
//hint 1:
//also, ti can create this kind of variables
//and ci can access the variable ti created
ti.setVariable("def", "DEF");
System.out.println(ci.getVariable("def")); //DEF
ci.setVariable("def", "FED");
System.out.println(ti.getVariable("def")); //FED
//hint 2:
//for the variables declared in <controller>, it's the same situation
//both ci and ti can create and access this kind of variable
ci.setVariable("var", "VAR");
System.out.println(ti.getVariable("var")); //VAR
ti.setVariable("var", "RAV");
System.out.println(ci.getVariable("var")); //RAV
//hint 3:
//"var" doesn't affect "variable"
System.out.println(ti.getVariable("variable")); //null
System.out.println(ci.getVariable("variable")); //null
//hint 4:
//things becaome a little different when using mapped-name
ti.setVariable("variable", "VARIABLE");
System.out.println(ti.getVariable("variable")); //VARIABLE
System.out.println(ci.getVariable("variable")); //null
//hint 4:
//it's clear that ti and ci both holds an "variable", respectively
ci.setVariable("variable", "ELBAIRAV");
System.out.println(ti.getVariable("variable")); //VARIABLE
System.out.println(ci.getVariable("variable")); //ELBAIRAV
//hint 3:
//and "variable" doesn't affect "var"
System.out.println(ti.getVariable("var")); //RAV
System.out.println(ci.getVariable("var")); //RAV
ti.end();
pi.end();
jc.close();
}
}
输出结果如注释所示。
可以看出,除了mapped-name之外,ti和ci可以随意SetVariable(key, value),SetVariable的过程包含了create的过程,key可以是name,也可以是其他字符串,且这种类型的变量(名称为name或是任意字符串,只要不是mapped-name)ti和ci可以随意访问,这类变量对ti和ci的作用域是一样的(类似于全局变量)。
但name和mapped-name变量没有任何关系,如hint 3所示,"var"的值不会影响"variable",反过来"variable"的值也不会影响"var"。
且mapped-name变量对ti和ci来说更像是2个变量,其作用域严格区分,没有任何交集,如hint 4所示。