HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ j: \, v5 S$ l
5 l- w/ x. F$ S! u' a public Object buildActions () {
) M1 X. n! Y W p. ? super.buildActions();8 t! U" w6 n8 h2 p$ f0 Y
. r8 m- J# f7 @/ M/ X9 B' a // Create the list of simulation actions. We put these in
8 s8 e* _0 p& i) ^+ T; T2 k // an action group, because we want these actions to be
3 U) ?( e5 u, }$ Y // executed in a specific order, but these steps should
2 v% `$ n8 Q$ H% m/ ~ // take no (simulated) time. The M(foo) means "The message
1 p) [. i( h4 ^# O. x // called <foo>". You can send a message To a particular
, a" J3 K# V+ d j9 F1 ]1 v // object, or ForEach object in a collection.' J. u8 ^) g) s& L! u3 h# \, u) F
5 l) | [% c A" ` // Note we update the heatspace in two phases: first run0 d! J. k( L, H
// diffusion, then run "updateWorld" to actually enact the
2 ?9 s+ g3 _5 ]; [4 x+ w1 J // changes the heatbugs have made. The ordering here is. d) Y' w7 E6 v2 Y" F' i
// significant!$ i% i: @3 w7 g7 F7 t
& H% I. l; q, O1 T
// Note also, that with the additional0 a0 F! z" y( \
// `randomizeHeatbugUpdateOrder' Boolean flag we can
; {$ z3 c6 F& {& X. B, Q# M6 `1 H5 L // randomize the order in which the bugs actually run: v) O8 a2 ~* @+ ]
// their step rule. This has the effect of removing any( r# ]2 ^8 D# b' T. m6 c
// systematic bias in the iteration throught the heatbug
. o. A; t5 V9 ^# } // list from timestep to timestep
2 Q8 t+ L/ i9 h" A, R: @+ N - ~ S; r& }# z6 }' i! w' S2 T
// By default, all `createActionForEach' modelActions have
. z, c5 ^" C9 v0 x // a default order of `Sequential', which means that the% f; [% ~& p1 Y; e6 H. Y0 s3 x
// order of iteration through the `heatbugList' will be8 h! }" ^) L$ }- o
// identical (assuming the list order is not changed1 x/ t6 G8 {2 A' j9 H
// indirectly by some other process).% k$ g3 {: j& ?1 U- {2 Y* G8 {" C
8 b) k0 M: `% } t2 @: Z/ s modelActions = new ActionGroupImpl (getZone ());$ K' y5 I: G. I4 k
+ U5 T3 R3 N" o
try {4 K# F7 U2 e7 G. ^( G0 @9 D0 {
modelActions.createActionTo$message8 f8 o1 n) A. @* S: d8 E
(heat, new Selector (heat.getClass (), "stepRule", false));0 x$ O d2 X9 r* U
} catch (Exception e) {
7 M8 Q, {8 |0 P; x' h# Y+ O1 S System.err.println ("Exception stepRule: " + e.getMessage ());) g1 d) N. _( W) P5 B0 e
}, A$ Y! T% v, J b
/ w7 U* o. F! u) x- e1 u try {5 h5 r6 q. z( w% _
Heatbug proto = (Heatbug) heatbugList.get (0);0 D5 P. [4 H( ~1 q9 a: s, z) X6 T
Selector sel = 0 R p! i$ Q9 ]$ x' \9 f2 s% b A
new Selector (proto.getClass (), "heatbugStep", false);
. c+ f' S6 ^- I7 ? actionForEach =
) x$ K+ L. g& u: r2 t: G modelActions.createFActionForEachHomogeneous$call& {$ Q/ o* {) J2 d" e. A1 J
(heatbugList,
. t# Q) L6 x0 h) R. G# k0 n! T new FCallImpl (this, proto, sel,
/ A; Z ^% b1 H1 }& W new FArgumentsImpl (this, sel)));
* M; w) z3 X" {1 A7 S } catch (Exception e) {
9 D6 n0 w& Z( p9 r% \2 I e.printStackTrace (System.err);
' l9 a6 U' F1 R }2 \" P0 z% }: M3 o; y" P! U
4 ^: o; ~. T3 y0 A1 d4 h
syncUpdateOrder ();
. V2 Y8 \- Y' B* F; M" ]: ]# z7 a5 i1 d9 T* ?
try {
( v1 V8 p, j) E# a1 x modelActions.createActionTo$message
# B! _+ K4 r, `! H% x( X (heat, new Selector (heat.getClass (), "updateLattice", false));
) F+ a S' k% g9 H8 O5 l- Q- i } catch (Exception e) {
$ t5 X3 c5 M+ q% U1 I System.err.println("Exception updateLattice: " + e.getMessage ());
9 ^5 H2 o3 D# z# r }
, i" [8 w& `# y; C& W, y9 g: B, s , b4 c9 [9 l8 U
// Then we create a schedule that executes the% D* E8 b: G: n) B6 x* s5 N
// modelActions. modelActions is an ActionGroup, by itself it
- Y+ {" p0 j: S& R- K# p // has no notion of time. In order to have it executed in1 r/ e2 ?; A- W( `- i* A
// time, we create a Schedule that says to use the
) L/ z) Q6 E4 K5 c' ~ F // modelActions ActionGroup at particular times. This
9 r: N7 q6 c- r' `8 k // schedule has a repeat interval of 1, it will loop every
( g# D/ ]- {7 S* ? // time step. The action is executed at time 0 relative to# _2 D; {, e/ C5 [
// the beginning of the loop.
" z/ d. x* w6 ]: k9 ~% _6 \6 K- H# A+ R/ k" w
// This is a simple schedule, with only one action that is8 D% {% W/ T4 Q+ O
// just repeated every time. See jmousetrap for more! P: U* z1 T- c' [; E
// complicated schedules.
( e/ {: {7 I5 I c! f) R
( w$ i& o( ^- e/ L% |6 Z; }0 s modelSchedule = new ScheduleImpl (getZone (), 1);
3 _: I0 H! M$ g) F! Z modelSchedule.at$createAction (0, modelActions);# b, T/ D% g$ {3 |& u; v& A" Y
% A; U/ J0 \0 ~1 ~
return this;
Y& I' e- W0 X* |9 a8 R6 | R } |