[#0x002D] variable的name和mapped-name

  看似是一個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所示。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章