ludc
2025-01-16 986aa62ed00bee39363bab41b4eeb8259d446efd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jbpm.pvm.internal.model;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.jbpm.api.activity.ActivityBehaviour;
import org.jbpm.pvm.internal.util.ReflectUtil;
import org.jbpm.pvm.internal.wire.Descriptor;
 
/**
 * @author Tom Baeyens
 */
public class ActivityImpl extends CompositeElementImpl implements Activity {
 
  private static final long serialVersionUID = 1L;
  
  protected ActivityBehaviour activityBehaviour;
  protected boolean isActivityBehaviourStateful = false;
  protected Descriptor activityBehaviourDescriptor;
  
  protected List<TransitionImpl> outgoingTransitions = new ArrayList<TransitionImpl>();
  protected List<TransitionImpl> incomingTransitions = new ArrayList<TransitionImpl>();
  protected TransitionImpl defaultOutgoingTransition;
  protected ActivityImpl parentActivity;
 
  protected String type;
  protected Continuation continuation = Continuation.SYNCHRONOUS;
 
  protected ActivityCoordinatesImpl coordinates;
  
  // Do not initialize. Caching is based on the nullity of this map
  transient protected Map<String, TransitionImpl> outgoingTransitionsMap = null;
  
  /**
   * Use {@link ProcessDefinitionImpl#createActivity()} or {@link ActivityImpl#createActivity()} instead.
   */
  public ActivityImpl() {
    super();
  }
  
  // specialized activity containment methods /////////////////////////////////////
  
  public ActivityImpl addActivity(ActivityImpl activity) {
    activity.setParentActivity(this);
    super.addActivity(activity);
    return activity;
  }
  
  public ActivityImpl findActivity(String activityName) {
    if (activityName==null) {
      if (name==null) {
        return this;
      }
    } else if (activityName.equals(name)) {
      return this;
    }
    return super.findActivity(activityName);
  }
 
  // outgoing transitions //////////////////////////////////////////////////////
 
  /** creates an outgoing transition from this activity. */
  public TransitionImpl createOutgoingTransition() {
    // create a new transition
    TransitionImpl transition = new TransitionImpl();
    transition.setProcessDefinition(processDefinition);
    
    // wire it between the source and destination
    addOutgoingTransition(transition);
 
    // if there is no default transition yet
    if (defaultOutgoingTransition==null) {
      // make this the default outgoing transition
      defaultOutgoingTransition = transition;
    }
    
    return transition;
  }
  
  /**
   * adds the given transition as a leaving transition to this activity.
   * Also the source of the transition is set to this activity.
   * Adding a transition that is already contained in the leaving 
   * transitions has no effect. 
   * @return the added transition. 
   * @throws NullPointerException if transition is null.
   */
  public Transition addOutgoingTransition(TransitionImpl transition) {
    if (! outgoingTransitions.contains(transition)) {
      transition.setSource(this);
      transition.setSourceIndex(outgoingTransitions.size());
      outgoingTransitions.add(transition);
      clearOutgoingTransitionsMap();
    }
    return transition;
  }
 
  /**
   * removes the given transition from the leaving transitions.
   * Also the transition's source will be nulled.
   * This method will do nothing if the transition is null or if 
   * the given transition is not in the list of this activity's leaving 
   * transitions.
   * In case this is the transition that was in the 
   * outgoingTransitionsMap and another transition exists with the same
   * name, that transition (the first) will be put in the 
   * outgoingTransitionsMap as a replacement for the removed transition.
   * If the transition is actually removed from the list of 
   * leaving transitions, the transition's source will be nulled. 
   */
  public boolean removeOutgoingTransition(TransitionImpl transition) {
    if (transition!=null) {
      boolean isRemoved = outgoingTransitions.remove(transition);
      if (isRemoved) {
        transition.setSource(null);
        clearOutgoingTransitionsMap();
      }
      return isRemoved;
    }
    return false;
  }
 
  /** the first leaving transition with the given name or null of no
   * such leaving transition exists.
   */
  public TransitionImpl getOutgoingTransition(String transitionName) {
    return (getOutgoingTransitionsMap()!=null ? outgoingTransitionsMap.get(transitionName) : null);
  }
  
  /** searches for the given transitionName in this activity and then up the 
   * parent chain. Returns null if no such transition is found. */
  public TransitionImpl findOutgoingTransition(String transitionName) {
    TransitionImpl transition = getOutgoingTransition(transitionName);
    if (transition!=null) {
      return transition;
    }
    if (parentActivity!=null) {
      return parentActivity.findOutgoingTransition(transitionName);
    }
    return null;
  }
  
  /** searches for the default transition in this activity and then up the 
   * parent chain. Returns null if no such transition is found. */
  public TransitionImpl findDefaultTransition() {
    if (defaultOutgoingTransition!=null) {
      return defaultOutgoingTransition;
    }
    if (parentActivity!=null) {
      return parentActivity.findDefaultTransition();
    }
    return null;
  }
 
  
  /** the list of leaving transitions.
   * Beware: the actual member is returned.  No copy is made. 
   */
  public List<Transition> getOutgoingTransitions() {
    return (List) outgoingTransitions;
  }
 
  /** indicates if a leaving transition with the given transitionName exists. */
  public boolean hasOutgoingTransition(String transitionName) {
    return (getOutgoingTransition(transitionName)!=null);
  }
 
  /** indicates if this activity has leaving transitions */
  public boolean hasOutgoingTransitions() {
    return !outgoingTransitions.isEmpty();
  }
 
  /** sets the outgoingTransitions to the given list of outgoingTransitions.
   * A copy of the collection is made.  Also the outgoingTransitionsMap will 
   * be updated and the source of all the transitions in the given list will 
   * be set to this activity.
   * In case there was a leaving transitions list present, these transition's
   * source will be nulled.
   */
  public void setOutgoingTransitions(List<TransitionImpl> outgoingTransitions) {
    if (!this.outgoingTransitions.isEmpty()) {
      List<TransitionImpl> removedTransitions = new ArrayList<TransitionImpl>(outgoingTransitions);
      for (TransitionImpl removedTransition: removedTransitions) {
        removeOutgoingTransition(removedTransition);
      }
    }
    if (outgoingTransitions!=null) {
      this.outgoingTransitions = new ArrayList<TransitionImpl>();
      for (TransitionImpl addedTransition: outgoingTransitions) {
        addOutgoingTransition(addedTransition);
      }
    } else {
      this.outgoingTransitions = new ArrayList<TransitionImpl>();
    }
    clearOutgoingTransitionsMap();
  }
 
  // arriving transitions /////////////////////////////////////////////////////
  
  /**
   * adds the given transition as an arriving transition to this activity.
   * Also the source of the transition is set to this activity. 
   * @return the added transition. 
   * @throws NullPointerException if transition is null.
   */
  public Transition addIncomingTransition(TransitionImpl transition) {
    transition.setDestination(this);
    incomingTransitions.add(transition);
    return transition;
  }
 
  /** removes the given transition if it is contained in the arriving
   * transitions of this activity.  If this transition was actually removed,
   * its destination pointer is nulled.
   * @return true if a transition was removed.
   */
  public boolean removeIncomingTransition(TransitionImpl transition) {
    if ( (transition!=null) && (incomingTransitions.remove(transition))) {
      transition.setDestination(null);
      return true;
    }
    return false;
  }
 
  /** the list of arriving transitions.
   * Beware: the actual member is returned.  No copy is made.
   */ 
  public List<Transition> getIncomingTransitions() {
    return (List) incomingTransitions;
  }
 
  /** indicates if this activity has arriving transitions */
  public boolean hasIncomingTransitions() {
    return !incomingTransitions.isEmpty();
  }
 
 
  /** sets the incomingTransitions to the given list of incomingTransitions.
   * A copy of the collection is made.  Also the destination of all the transitions 
   * in the given list will be set to this activity.
   * In case there was an arriving transitions list present, these transition's
   * destination will be nulled.
   */
  public void setIncomingTransitions(List<TransitionImpl> incomingTransitions) {
    if (!this.incomingTransitions.isEmpty()) {
      for (TransitionImpl removedTransition: this.incomingTransitions) {
        removedTransition.setDestination(null);
      }
    }
    if (incomingTransitions!=null) {
      this.incomingTransitions = new ArrayList<TransitionImpl>(incomingTransitions);
      for (TransitionImpl addedTransition: incomingTransitions) {
        addedTransition.setDestination(this);
      }
    } else {
      this.incomingTransitions = null;
    }
  }
 
  /** the leaving transitions, keyed by transition name.  If a transition with 
   * the same name occurs mutltiple times, the first one is returned.
   * Leaving transitions with a null value for their name are not included 
   * in the map.
   * Beware: the actual member is returned.  No copy is made. 
   */
  public Map<String, Transition> getOutgoingTransitionsMap() {
    if(outgoingTransitionsMap == null){
      this.outgoingTransitionsMap = new HashMap<String, TransitionImpl>();
      for (TransitionImpl transition: outgoingTransitions) {
        if (! this.outgoingTransitionsMap.containsKey(transition.getName())) {
          this.outgoingTransitionsMap.put(transition.getName(), transition);
        }
      }
    }
    return (Map) outgoingTransitionsMap;
  }
 
  void clearOutgoingTransitionsMap() {
    outgoingTransitionsMap = null;
  }
 
  // various helper methods ///////////////////////////////////////////////////
  
 
  static Map<String, ActivityImpl> getActivitiesMap(List<ActivityImpl> activities) {
    Map<String, ActivityImpl> map = null;
    if (activities!=null) {
      map = new HashMap<String, ActivityImpl>();
      for (ActivityImpl activity: activities) {
        if (! map.containsKey(activity.getName())) {
          map.put(activity.getName(), activity);
        }
      }
    }
    return map;
  }
 
  public String toString() { 
    if (name!=null) return "activity("+name+")";
    if (dbid!=0) return "activity("+dbid+")";
    return "activity("+System.identityHashCode(this)+")"; 
  }
 
  /** collects the full stack of parent in a list.  This activity is the 
   * first element in the chain.  The process definition will be the last element.
   * the chain will never be null. */
  public List<ObservableElementImpl> getParentChain() {
    List<ObservableElementImpl> chain = new ArrayList<ObservableElementImpl>();
    ObservableElementImpl processElement = this;
    while (processElement!=null) {
      chain.add(processElement);
      processElement = processElement.getParent();
    }
    return chain;
  }
 
  public boolean isAsync() {
    return ! (continuation==Continuation.SYNCHRONOUS);
  }
 
  public boolean contains(ActivityImpl activity) {
    while (activity!=null) {
      if (activity.getParent()==this) {
        return true;
      }
      activity = activity.getParentActivity();
    }
    return false;
  }
 
  // customized getters and setters ///////////////////////////////////////////
 
  public ActivityBehaviour getActivityBehaviour() {
    if (activityBehaviour!=null) {
      return activityBehaviour;
    }
    if (activityBehaviourDescriptor!=null) {
      ActivityBehaviour createdBehaviour = (ActivityBehaviour) ReflectUtil.instantiateUserCode(activityBehaviourDescriptor, processDefinition);
      if (!isActivityBehaviourStateful) {
        activityBehaviour = createdBehaviour;
      }
      return createdBehaviour;
    }
    return null;
  }
 
  // getters and setters //////////////////////////////////////////////////////
  
  public ObservableElementImpl getParent() {
    return (parentActivity!=null ? parentActivity : processDefinition);
  }
  
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public TransitionImpl getDefaultOutgoingTransition() {
    return defaultOutgoingTransition;
  }
  public void setDefaultOutgoingTransition(TransitionImpl defaultOutgoingTransition) {
    this.defaultOutgoingTransition = defaultOutgoingTransition;
  }
  public ActivityImpl getParentActivity() {
    return parentActivity;
  }
  public void setParentActivity(ActivityImpl parentActivity) {
    this.parentActivity = parentActivity;
  }
  public String getType() {
    return type;
  }
  public void setType(String type) {
    this.type = type;
  }
  public ActivityCoordinatesImpl getCoordinates() {
    return coordinates;
  }
  public void setCoordinates(ActivityCoordinatesImpl coordinates) {
    this.coordinates = coordinates;
  }
  public Continuation getContinuation() {
    return continuation;
  }
  public void setContinuation(Continuation continuation) {
    this.continuation = continuation;
  }
  public void setActivityBehaviour(ActivityBehaviour activityBehaviour) {
    this.activityBehaviour = activityBehaviour;
  }
  public Descriptor getActivityBehaviourDescriptor() {
    return activityBehaviourDescriptor;
  }
  public void setActivityBehaviourDescriptor(Descriptor activityBehaviourDescriptor) {
    this.activityBehaviourDescriptor = activityBehaviourDescriptor;
  }
  public boolean isActivityBehaviourStateful() {
    return isActivityBehaviourStateful;
  }
  public void setActivityBehaviourStateful(boolean isActivityBehaviourStateful) {
    this.isActivityBehaviourStateful = isActivityBehaviourStateful;
  }
}