HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
* v* V* |. I, {( c6 L+ l6 {9 m6 C/ v2 j4 M+ Z7 Q0 v
public Object buildActions () {
- O8 V4 ~: w( j! ? super.buildActions();
6 o8 L; H) `9 i9 M" `
% k( B3 B5 m+ p" M& D // Create the list of simulation actions. We put these in! `% Q( s! i, i( d4 \: m4 H$ v
// an action group, because we want these actions to be
8 f% z. F. F5 }, [: Q. i // executed in a specific order, but these steps should) ^* j8 b' @ p
// take no (simulated) time. The M(foo) means "The message5 g1 Z. }$ j1 I/ H( y( ~) e" t8 G
// called <foo>". You can send a message To a particular
& ?, x, \; v; w- f+ X, }& p // object, or ForEach object in a collection. B, \# x! o$ ~9 D( U8 O3 e7 n
; [- t) [0 r' x // Note we update the heatspace in two phases: first run
X% T/ q6 W# X- Q // diffusion, then run "updateWorld" to actually enact the* ]# R7 ?& t5 W3 |9 S0 y- d) f
// changes the heatbugs have made. The ordering here is) e9 C' z( }0 S( f+ X. W
// significant!
& s. A$ Z9 v! P
1 \- I) v2 S5 x3 ^; p // Note also, that with the additional
. T6 ^$ _( A& s2 s% }; [' A // `randomizeHeatbugUpdateOrder' Boolean flag we can$ z) y0 W* O, x8 E7 j
// randomize the order in which the bugs actually run
( b3 z9 A. m$ s8 B4 d // their step rule. This has the effect of removing any
- Y* r( t" C% d: z; Y) D // systematic bias in the iteration throught the heatbug
2 Z$ z$ m' I s // list from timestep to timestep
& F( ?5 Q" R! S6 Q
) C q( c. \3 q. }; h // By default, all `createActionForEach' modelActions have
6 t& G. S( J$ U+ j/ B0 H // a default order of `Sequential', which means that the
0 M) S3 }) M- t1 a6 F( _ // order of iteration through the `heatbugList' will be! U5 n' r. h0 w9 c! v5 w
// identical (assuming the list order is not changed7 w8 I9 t$ I' X8 ]
// indirectly by some other process).
/ G: b4 X' x% S: n$ ~
: ?* M0 M, d1 S& R! t modelActions = new ActionGroupImpl (getZone ());/ ~" A" S8 S; s/ H4 K, t# h
& p/ f+ e5 \+ h7 G6 C1 J try {6 m% q% P* q& O" z
modelActions.createActionTo$message; g. ]3 W# ^: y# G; u9 v# `# Y
(heat, new Selector (heat.getClass (), "stepRule", false));
( n- ^* J a, e/ }& M, t ?, G } catch (Exception e) {. d- U/ H8 q, L
System.err.println ("Exception stepRule: " + e.getMessage ());1 g! C9 O V& R2 Q
}# c9 v' w3 ]* r8 E* C4 I3 k
' \: D3 B ]9 U( Z try {4 ^8 T X8 M2 k- u
Heatbug proto = (Heatbug) heatbugList.get (0);
, `1 f2 f3 n- G% {1 H Selector sel = : I, X8 h/ X( s$ S
new Selector (proto.getClass (), "heatbugStep", false);5 @+ H# J! e8 N. O2 W3 V$ t: `+ Z
actionForEach =8 w! R. k. U# K. Q1 [
modelActions.createFActionForEachHomogeneous$call
5 t4 f( x, R b V: S (heatbugList,. e3 }+ l* D/ ]8 A+ P
new FCallImpl (this, proto, sel,
1 k4 |! A0 ~+ m; t new FArgumentsImpl (this, sel)));
4 \; k7 T) L# H% Y4 Y! M' N$ N4 `: s% h } catch (Exception e) {
0 i w& ~7 a8 h% m e.printStackTrace (System.err);
; n; f. b! D2 Q8 Z3 Z }
1 {7 l& m! L6 J, r% h
7 `% U' W! [: `' L& F; w' Q( F syncUpdateOrder ();* b+ R* k; `6 T }5 t0 t& T' w
. v0 O5 X( Z( K9 G* R# T3 l try {
" ~) [0 w. {* f) A; h modelActions.createActionTo$message ) ~; B" K; ` k6 J+ L8 f& a5 q
(heat, new Selector (heat.getClass (), "updateLattice", false));
: I5 }& E: }$ }4 X } catch (Exception e) {6 n4 c& E. S5 V! H2 j6 E& a
System.err.println("Exception updateLattice: " + e.getMessage ());
( A- H( k8 D; Y8 t' V- f' b }& Z+ m' y) W/ o2 h- U' Z
v+ N b- e5 h; A0 i3 f // Then we create a schedule that executes the
, K I8 I: E6 w5 {* K6 P8 [+ F$ _! A U // modelActions. modelActions is an ActionGroup, by itself it6 _1 d, l: d' s
// has no notion of time. In order to have it executed in; n% m- p4 M0 k, b" q8 @
// time, we create a Schedule that says to use the
+ E! D2 H6 A, q" }: s- U8 N- Q // modelActions ActionGroup at particular times. This
d# H# T* V; h9 s# ?5 {4 m- I // schedule has a repeat interval of 1, it will loop every) I6 d R5 ?3 ^) G
// time step. The action is executed at time 0 relative to$ ]+ H4 f p5 x5 G* W
// the beginning of the loop." A1 d' V$ o* O8 U: |. d$ Q5 u
. M: _' Q6 t# u, a7 N7 x% Y3 o) v // This is a simple schedule, with only one action that is$ ?. W, x+ N! W7 P$ B
// just repeated every time. See jmousetrap for more
! e5 A" |& c0 r7 f // complicated schedules.
, C1 K) \( O) S9 p' G
5 k4 n# v& x- z1 x3 j) n l3 |& o modelSchedule = new ScheduleImpl (getZone (), 1);1 {* y+ u* n; Z, V
modelSchedule.at$createAction (0, modelActions);9 l$ C H% o! Q2 V% N
. p. Q6 f N: J- G% c) P% p return this;
1 C5 \9 o' ^: K! K. S( ?8 E } |