KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > context > exe > ContextInstance


1 package org.jbpm.context.exe;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Map JavaDoc;
5
6 import org.jbpm.graph.exe.Token;
7 import org.jbpm.module.exe.ModuleInstance;
8
9 /**
10  * maintains all the key-variable pairs for a process instance. You can obtain a
11  * ContextInstance from a processInstance from a process instance like this :
12  * <pre>
13  * ProcessInstance processInstance = ...;
14  * ContextInstance contextInstance = processInstance.getContextInstance();
15  * </pre>
16  * More information on context and process variables can be found in
17  * <a HREF="../../../../../userguide/en/html/reference.html#context">the userguide, section context</a>
18  */

19 public class ContextInstance extends ModuleInstance {
20
21   private static final long serialVersionUID = 1L;
22
23   // maps Token's to TokenVariableMap's
24
protected Map JavaDoc tokenVariableMaps = null;
25   // maps variablenames (String) to values (Object)
26
protected transient Map JavaDoc transientVariables = null;
27
28   public ContextInstance() {
29   }
30
31   // normal variables (persistent)
32
/////////////////////////////////////////////////////////////////////////////
33

34   /**
35    * creates a variable on the root-token (= process-instance scope) and
36    * calculates the actual VariableInstance-type from the value.
37    */

38   public void createVariable(String JavaDoc name, Object JavaDoc value) {
39     createVariable(name, value, getRootToken());
40   }
41
42   /**
43    * creates a variable in the scope of the given token and calculates the
44    * actual VariableInstance-type from the value.
45    */

46   public void createVariable(String JavaDoc name, Object JavaDoc value, Token token) {
47     TokenVariableMap tokenVariableMap = getOrCreateTokenVariableMap(token);
48     tokenVariableMap.createVariableInstance(name, value);
49   }
50
51   /**
52    * gets all the variables on the root-token (= process-instance scope).
53    */

54   public Map JavaDoc getVariables() {
55     return getVariables(getRootToken());
56   }
57
58   /**
59    * retrieves all the variables in scope of the given token.
60    */

61   public Map JavaDoc getVariables(Token token) {
62     Map JavaDoc variables = new HashMap JavaDoc();
63
64     TokenVariableMap tokenVariableMap = getTokenVariableMap(token);
65     if (tokenVariableMap != null) {
66       tokenVariableMap.collectAllVariables(variables);
67     }
68
69     return variables;
70   }
71
72   /**
73    * adds all the variables on the root-token (= process-instance scope).
74    */

75   public void addVariables(Map JavaDoc variables) {
76     addVariables(variables, getRootToken());
77   }
78
79   /**
80    * adds all the variables to the scope of the given token.
81    */

82   public void addVariables(Map JavaDoc variables, Token token) {
83     TokenVariableMap tokenVariableMap = getOrCreateTokenVariableMap(token);
84     tokenVariableMap.addVariables(variables);
85   }
86
87   /**
88    * gets the variable with the given name on the root-token (= process-instance
89    * scope).
90    */

91   public Object JavaDoc getVariable(String JavaDoc name) {
92     return getVariable(name, getRootToken());
93   }
94
95   /**
96    * retrieves a variable in the scope of the token. If the given token does not
97    * have a variable for the given name, the variable is searched for up the
98    * token hierarchy.
99    */

100   public Object JavaDoc getVariable(String JavaDoc name, Token token) {
101     Object JavaDoc variable = null;
102     TokenVariableMap tokenVariableMap = getTokenVariableMap(token);
103     if (tokenVariableMap != null) {
104       variable = tokenVariableMap.getVariable(name);
105     }
106     return variable;
107   }
108
109   /**
110    * retrieves a variable which is local to the token.
111    */

112   public Object JavaDoc getLocalVariable(String JavaDoc name, Token token) {
113     Object JavaDoc variable = null;
114     if (tokenVariableMaps!=null && tokenVariableMaps.containsKey(token)) {
115       TokenVariableMap tokenVariableMap = (TokenVariableMap) tokenVariableMaps.get(token);
116       if (tokenVariableMap != null) {
117         VariableInstance variableInstance = tokenVariableMap.getVariableInstance(name);
118         if(variableInstance != null) {
119           variable = variableInstance.getValue();
120         }
121       }
122     }
123     return variable;
124   }
125
126   /**
127    * sets a variable on the process instance scope.
128    */

129   public void setVariable(String JavaDoc name, Object JavaDoc value) {
130     setVariable(name, value, getRootToken());
131   }
132
133   /**
134    * sets a variable. If a variable exists in the scope given by the token, that
135    * variable is updated. Otherwise, the variable is created on the root token
136    * (=process instance scope).
137    */

138   public void setVariable(String JavaDoc name, Object JavaDoc value, Token token) {
139     TokenVariableMap tokenVariableMap = getOrCreateTokenVariableMap(token);
140     tokenVariableMap.setVariable(name, value);
141   }
142
143   /**
144    * checks if a variable is present with the given name on the root-token (=
145    * process-instance scope).
146    */

147   public boolean hasVariable(String JavaDoc name) {
148     return hasVariable(name, getRootToken());
149   }
150
151   /**
152    * checks if a variable is present with the given name in the scope of the
153    * token.
154    */

155   public boolean hasVariable(String JavaDoc name, Token token) {
156     boolean hasVariable = false;
157     TokenVariableMap tokenVariableMap = getTokenVariableMap(token);
158     if (tokenVariableMap != null) {
159       hasVariable = tokenVariableMap.hasVariable(name);
160     }
161     return hasVariable;
162   }
163
164   /**
165    * deletes the given variable on the root-token (=process-instance scope).
166    */

167   public void deleteVariable(String JavaDoc name) {
168     deleteVariable(name, getRootToken());
169   }
170
171   /**
172    * deletes a variable from the given token. For safety reasons, this method does
173    * not propagate the deletion to parent tokens in case the given token does not contain
174    * the variable.
175    */

176   public void deleteVariable(String JavaDoc name, Token token) {
177     TokenVariableMap tokenVariableMap = getTokenVariableMap(token);
178     if (tokenVariableMap != null) {
179       tokenVariableMap.deleteVariable(name);
180     }
181   }
182
183   // transient variables
184
/////////////////////////////////////////////////////////////////////////////
185

186   /**
187    * retrieves the transient variable for the given name.
188    */

189   public Object JavaDoc getTransientVariable(String JavaDoc name) {
190     Object JavaDoc transientVariable = null;
191     if (transientVariables!= null) {
192       transientVariable = transientVariables.get(name);
193     }
194     return transientVariable;
195   }
196
197   /**
198    * sets the transient variable for the given name to the given value.
199    */

200   public void setTransientVariable(String JavaDoc name, Object JavaDoc value) {
201     if (transientVariables == null) {
202       transientVariables = new HashMap JavaDoc();
203     }
204     transientVariables.put(name, value);
205   }
206
207   /**
208    * tells if a transient variable with the given name is present.
209    */

210   public boolean hasTransientVariable(String JavaDoc name) {
211     if (transientVariables == null) {
212       return false;
213     }
214     return transientVariables.containsKey(name);
215   }
216
217   /**
218    * retrieves all the transient variables map. note that no deep copy is
219    * performed, changing the map leads to changes in the transient variables of
220    * this context instance.
221    */

222   public Map JavaDoc getTransientVariables() {
223     return transientVariables;
224   }
225
226   /**
227    * replaces the transient variables with the given map.
228    */

229   public void setTransientVariables(Map JavaDoc transientVariables) {
230     this.transientVariables = transientVariables;
231   }
232
233   /**
234    * removes the transient variable.
235    */

236   public void deleteTransientVariable(String JavaDoc name) {
237     if (transientVariables == null)
238       return;
239     transientVariables.remove(name);
240   }
241
242   
243   Token getRootToken() {
244     return processInstance.getRootToken();
245   }
246
247   /**
248    * looks up the token-variable-map for the given token
249    * and creates it if it doesn't exist. This method also
250    * contains all the token maps for all the token's parents.
251    */

252   TokenVariableMap getOrCreateTokenVariableMap(Token token) {
253     TokenVariableMap tokenVariableMap = null;
254     if (tokenVariableMaps==null) {
255       tokenVariableMaps = new HashMap JavaDoc();
256     }
257     if (tokenVariableMaps.containsKey(token)) {
258       tokenVariableMap = (TokenVariableMap) tokenVariableMaps.get(token);
259     } else {
260       tokenVariableMap = new TokenVariableMap(token, this);
261       tokenVariableMaps.put( token, tokenVariableMap );
262
263       Token parent = token.getParent();
264       TokenVariableMap parentVariableMap = null;
265       while (parent!=null) {
266         if (! tokenVariableMaps.containsKey(parent) ) {
267           parentVariableMap = new TokenVariableMap(parent, this);
268           tokenVariableMaps.put( parent, parentVariableMap );
269         }
270         parent = parent.getParent();
271       }
272     }
273     return tokenVariableMap;
274   }
275
276   /**
277    * looks for the first token-variable-map that is found
278    * up the token-parent hirarchy.
279    */

280   TokenVariableMap getTokenVariableMap(Token token) {
281     TokenVariableMap tokenVariableMap = null;
282     if (tokenVariableMaps!=null) {
283       if (tokenVariableMaps.containsKey(token)) {
284         tokenVariableMap = (TokenVariableMap) tokenVariableMaps.get(token);
285       } else if (! token.isRoot()) {
286         tokenVariableMap = getTokenVariableMap(token.getParent());
287       }
288     }
289     return tokenVariableMap;
290   }
291 }
292
Popular Tags