HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ R1 F: ]/ y7 N7 l
# j: l+ t( e* ~$ P3 x/ j
public Object buildActions () {
3 E$ N$ D' Q5 E' S7 Q super.buildActions();5 }6 D% h1 W" h8 u6 y9 T2 {, T
: r" }( q; v5 c% o // Create the list of simulation actions. We put these in
4 E, x9 j' F: s // an action group, because we want these actions to be" h. e. X/ L" Z
// executed in a specific order, but these steps should) A- P7 ]& E4 y8 E8 _& Z( u
// take no (simulated) time. The M(foo) means "The message
1 w1 D: O* k# g! Z6 j; \4 Q // called <foo>". You can send a message To a particular
7 T, E9 T" g+ O# \' Y // object, or ForEach object in a collection.
1 F5 a t. c7 F& p4 K, ~
5 W; W, h0 m' x5 W // Note we update the heatspace in two phases: first run
3 C! O9 `1 M* @9 X- u // diffusion, then run "updateWorld" to actually enact the! t2 t' c1 U5 |2 g( @7 F
// changes the heatbugs have made. The ordering here is" H, {( x. R0 c. \) v+ J
// significant!% S% u. P# C. w! a) t- W
4 u4 \9 T4 |4 O5 p
// Note also, that with the additional
/ p5 r; V8 C& M/ @7 _3 W // `randomizeHeatbugUpdateOrder' Boolean flag we can w9 G9 Y) j, o+ @* j/ r
// randomize the order in which the bugs actually run
, v" o3 v( A3 E( S' @ // their step rule. This has the effect of removing any
$ w9 I: f; j: D& k! [4 k // systematic bias in the iteration throught the heatbug; S$ D# w, v( }( D" e" Z7 @
// list from timestep to timestep. c9 s8 v% g+ `1 X" ]3 Z2 g. ?
/ W$ Z3 M3 ^, z* V
// By default, all `createActionForEach' modelActions have& b, R* I, K/ W5 h; h, Y* Y
// a default order of `Sequential', which means that the3 y1 J! C+ y! k6 F' }* v
// order of iteration through the `heatbugList' will be0 c- w. J, ^- X3 P$ t$ Y& S4 P
// identical (assuming the list order is not changed
5 `2 C6 _9 ]4 N3 } // indirectly by some other process)., |0 V: K* }) ?* K9 @4 D
- h6 V$ t4 m! N/ o" b; Y9 U" } modelActions = new ActionGroupImpl (getZone ());
, e! A* G" O' M9 T! l1 {% }( D0 L- P5 e T2 Y
try {. b' K- N: u# P, ^0 u
modelActions.createActionTo$message
) v2 [& y7 Y) L6 u O2 J (heat, new Selector (heat.getClass (), "stepRule", false));
% j( `6 C! u8 g } catch (Exception e) {" Q. g3 y: t% I9 m: s' H" x
System.err.println ("Exception stepRule: " + e.getMessage ());$ i& X: `( J/ S+ v% c5 T) j! Z
}4 Y" T; e: Q# K7 N1 l
% a2 u1 l# d+ x3 h try {9 i+ z1 J6 t8 v" p6 `, U
Heatbug proto = (Heatbug) heatbugList.get (0);
U& b5 V( t+ m( o& x( S3 \- V Selector sel = % y) F) N/ N- b3 w
new Selector (proto.getClass (), "heatbugStep", false);
2 j0 d0 a8 q. |( h9 v& V actionForEach =
* I, e- I" ~: `( |6 E7 H( R# w modelActions.createFActionForEachHomogeneous$call" e' [' V8 _: ?
(heatbugList,$ L' q3 L9 N A: M' k' x
new FCallImpl (this, proto, sel,. _% d# c; d$ b- N3 A( _, n# t) \
new FArgumentsImpl (this, sel)));
* S6 C( S" k% K$ d7 ~ } catch (Exception e) { a1 E8 W) e( r1 v; x
e.printStackTrace (System.err);, E, j' M: m+ \: D
}
1 ]4 g) \. S3 _& n4 h5 Z 5 ]3 ~4 [, S, u8 _
syncUpdateOrder ();
1 t! N1 X" v+ ^: z
( E* R- o p$ o+ Q try {
$ k- T' W5 H- z8 f& ^ modelActions.createActionTo$message 7 \5 @+ A4 |$ E6 g2 k
(heat, new Selector (heat.getClass (), "updateLattice", false));+ n# B* U# a3 t8 I) c! t2 n! s" z- c
} catch (Exception e) {9 S/ {1 n0 ~, N1 O0 z
System.err.println("Exception updateLattice: " + e.getMessage ());
. ?$ `7 E: s* }' z. C( P* f8 i }7 i/ L8 y' v5 b
- A) } R9 t* @" a( {% t4 z // Then we create a schedule that executes the
4 n& C O; Y! b- l% A/ {6 [8 u // modelActions. modelActions is an ActionGroup, by itself it0 a5 L' r. c0 R' U
// has no notion of time. In order to have it executed in7 p5 ?/ \- C j0 W$ A& e
// time, we create a Schedule that says to use the
/ T/ r) U3 Z4 l; J // modelActions ActionGroup at particular times. This/ z" n. q! \+ d- |2 ^
// schedule has a repeat interval of 1, it will loop every$ |4 [3 f1 Q4 Y4 a8 e0 {7 d2 v1 S
// time step. The action is executed at time 0 relative to$ m; E, d3 h4 d
// the beginning of the loop.
' g: f5 q& \# K: V' p% Z# W$ ]
/ k. X$ d1 s0 k // This is a simple schedule, with only one action that is
' H6 V- f. \" C4 X5 D; q, v2 s+ } // just repeated every time. See jmousetrap for more
: j5 ?5 k2 }& v9 L' L) ~0 a! N // complicated schedules.9 ^; p. z% \- E3 Z
. \# ~ u+ H; p: g3 _ modelSchedule = new ScheduleImpl (getZone (), 1);
1 n6 z' Q- v3 H' r+ _; `6 b modelSchedule.at$createAction (0, modelActions);/ @9 L9 g( G& ?8 z. d$ ]
3 C E/ L# r$ f7 q$ W6 L( V
return this;
5 g# j( d# Z% M9 ~ } |