HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# F; D, j& u- s$ N" t' V
3 l! _+ A) [, W% ?9 [: X% Q6 ?
public Object buildActions () {
0 E) N# _1 }- X" T7 a+ a super.buildActions();
, B: I5 h3 p: @ * ~! h$ H: v" b& W1 g# T
// Create the list of simulation actions. We put these in3 T7 t7 O8 o) c' l/ h1 V! v- S
// an action group, because we want these actions to be b" x0 s$ i8 I! v/ I5 E
// executed in a specific order, but these steps should3 d& }9 N" U! B+ p: _& M( ]
// take no (simulated) time. The M(foo) means "The message
$ @7 E ^% u) f3 C m( z // called <foo>". You can send a message To a particular) ^$ m% V. C$ L, L. I, C$ B* p
// object, or ForEach object in a collection., z8 U+ h6 o4 ]. a' [
o! S3 j) k' p0 @% s, {( {
// Note we update the heatspace in two phases: first run
( P5 _: c1 Q0 e# Z7 W // diffusion, then run "updateWorld" to actually enact the
* n H- R+ z( ~, K/ R0 Y // changes the heatbugs have made. The ordering here is
8 O, W2 O9 i& w. |% q, K3 o // significant!
4 E* E" g8 u* @ P% M. }1 [
5 T3 @) R2 X+ Z" v // Note also, that with the additional. C' P( y2 P3 t' C+ i& v
// `randomizeHeatbugUpdateOrder' Boolean flag we can
H3 S2 u; }" R0 q& y5 n // randomize the order in which the bugs actually run8 u5 W V' u. \! z" D( z
// their step rule. This has the effect of removing any
2 U4 w7 k; G8 V& d5 X, b // systematic bias in the iteration throught the heatbug4 g% k+ h$ h2 c" m/ ~9 v6 |- O
// list from timestep to timestep
! X. O/ [' k. d( d
8 @$ I0 }8 v V# O$ D // By default, all `createActionForEach' modelActions have
& _- ^ d4 z* Q9 Z, H9 q5 x // a default order of `Sequential', which means that the- \7 w: H; Q3 g4 m7 c8 Z
// order of iteration through the `heatbugList' will be: ^2 M6 z0 ]8 N9 W% t
// identical (assuming the list order is not changed
- c8 C' ]% q: f: g3 n" \ // indirectly by some other process).
, u4 I0 ^0 f" e! P( f! G
W+ x6 q9 U. k modelActions = new ActionGroupImpl (getZone ());
1 J. M8 I [: g
7 B. N+ @1 r. u: O! \ try {0 ~( D$ X$ K2 `2 q) N
modelActions.createActionTo$message
' w& s a; Q8 `1 ] (heat, new Selector (heat.getClass (), "stepRule", false));3 u, `7 f$ G/ C5 c. I0 S
} catch (Exception e) {2 W0 H: L( z- x! A, |4 ^0 B$ r" L
System.err.println ("Exception stepRule: " + e.getMessage ());) x: L0 C. V; M/ U8 Q
}, v/ C4 R, U0 {* ^& m! p) E0 Q
! e+ {. P O( [5 E6 P- A5 c: S3 o
try {
4 s; b; M' A3 w7 D Heatbug proto = (Heatbug) heatbugList.get (0);
/ X1 c% B( A7 C, b" h2 C Selector sel =
! E: E& m! E- A new Selector (proto.getClass (), "heatbugStep", false);
# C4 q5 j& b$ {1 d/ O) G actionForEach =
$ c; Q& Y- J: C5 [* l: m- X: w$ J modelActions.createFActionForEachHomogeneous$call
1 K ?- p. \: }: p2 d8 U (heatbugList,+ Q% B; f$ _( X+ i4 h3 t( h# J
new FCallImpl (this, proto, sel,
) u+ N- @( O5 b/ x new FArgumentsImpl (this, sel)));
/ H$ |' p6 A# O# L. B9 t } catch (Exception e) {6 c; M# g( V( A: D4 k2 B
e.printStackTrace (System.err);
5 d A# g7 y* o& l4 ~, l }
# \' \: `6 P. E, A( l ) G: X# p& D- A+ _+ R8 s, v
syncUpdateOrder ();, q3 a8 b: `3 _5 a1 l
! r2 j3 y9 R; d) Q7 L" N
try {
3 _4 F7 ~) ~, X; q$ O) G+ A modelActions.createActionTo$message ( I! \8 T5 F* t* P( X* V$ E
(heat, new Selector (heat.getClass (), "updateLattice", false));
* m! [9 G5 T2 L9 a( y( }9 Y! e } catch (Exception e) {
( R/ y h3 E6 M: M" L8 k. Z System.err.println("Exception updateLattice: " + e.getMessage ());
+ E# j# P5 U @: j0 d) j% l }( v: t, p. |) @7 }6 P; h6 W
# R% Z9 ?. D' q( d // Then we create a schedule that executes the
- t( z( z/ f: B" X5 r3 k/ r // modelActions. modelActions is an ActionGroup, by itself it5 o; U' ^. g8 S; R* A
// has no notion of time. In order to have it executed in
/ z0 u, S2 @3 A i$ b _; R/ s/ X# |- g // time, we create a Schedule that says to use the
/ D: s/ r$ [- L% [& Z3 P* O) e0 E // modelActions ActionGroup at particular times. This0 m( t; Y) Z! e2 O6 @
// schedule has a repeat interval of 1, it will loop every
& d+ h1 e }/ Q3 A& q // time step. The action is executed at time 0 relative to
6 [* c8 W0 L* l // the beginning of the loop.1 `: R. O7 {. ]: y* j
3 O6 k6 t: D1 s% [; ~' G8 k // This is a simple schedule, with only one action that is
) e+ Z0 z. z' e3 `! j- q! z // just repeated every time. See jmousetrap for more
' C& { _; l& m+ d- b // complicated schedules.8 ?9 _5 ?. l: O L
7 B# L2 Y9 Q2 s& [- L8 L
modelSchedule = new ScheduleImpl (getZone (), 1);* r4 p2 r0 e Y0 q
modelSchedule.at$createAction (0, modelActions);
! ]: ]- b8 s* v8 r/ R 3 e: t! k) I' T6 r1 Q* `# v
return this;
; D B. Q, h1 @ H2 Z4 P. }3 o" U } |