HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:: f D/ J8 I4 @& Y1 a, ?
1 W: t; e" C g7 L0 x1 `3 e' S public Object buildActions () {
9 s( D, F( l9 B super.buildActions();
3 ~0 q' N) d; A# \9 ]
! `% s6 ^2 |. t5 |4 \7 B // Create the list of simulation actions. We put these in
' r3 j' |# G+ |; b3 g+ v // an action group, because we want these actions to be
4 X8 f# f2 j$ Q% I0 [ // executed in a specific order, but these steps should
* [8 u+ k& u2 k. A: W D. ? // take no (simulated) time. The M(foo) means "The message/ v7 ?9 ?, D$ W! U" x
// called <foo>". You can send a message To a particular
9 H! U3 D0 a9 t- u2 l. [ // object, or ForEach object in a collection.
% O; f0 y- Y$ i/ } 0 W. b9 D/ c& a+ A* D; }
// Note we update the heatspace in two phases: first run. G2 `: ^& x/ p: _0 {
// diffusion, then run "updateWorld" to actually enact the% @" G- ~6 @$ p
// changes the heatbugs have made. The ordering here is
. x( J( }6 H: P+ b // significant!
/ b$ |8 z/ ], G2 Z- s
; U- }. r' |( Z // Note also, that with the additional; W9 u# l. I- m) e2 A
// `randomizeHeatbugUpdateOrder' Boolean flag we can2 N" N" ]* ^+ Q" W$ [3 g5 B, b
// randomize the order in which the bugs actually run9 z+ s5 t9 f A8 A: r' P
// their step rule. This has the effect of removing any
2 {5 o, Z) Y' w5 ]' L" H0 t8 B // systematic bias in the iteration throught the heatbug
8 G c6 D6 I5 i6 f; l2 s, N // list from timestep to timestep3 r3 U6 {/ o' H
/ T7 G# Y3 y9 p3 ?0 p5 B1 h0 j
// By default, all `createActionForEach' modelActions have
# v: }: _ x, d) l( l% e3 G; C // a default order of `Sequential', which means that the
' Q# S' E5 `- \: M* J/ U% O' v // order of iteration through the `heatbugList' will be
0 N+ f0 n4 K2 P, b* | // identical (assuming the list order is not changed
p0 W9 n& t2 V // indirectly by some other process).
7 u" x* `+ L% m1 s; r" Q- S- N, t/ y 1 u7 M7 c6 i0 [! m
modelActions = new ActionGroupImpl (getZone ());
0 ^2 ^1 z3 U3 s% Q' e1 h7 b g, B! j4 X1 W
try {( s- |3 m; X0 F, t6 y$ s/ M/ S
modelActions.createActionTo$message
. ?: K$ ? N* U* C0 W# p (heat, new Selector (heat.getClass (), "stepRule", false));
6 i) v9 J/ L# A } catch (Exception e) {
" W A! ^3 O& f9 X! C System.err.println ("Exception stepRule: " + e.getMessage ());* O- I% H& S# b9 w2 t q- o2 C
}( y; X, }# Y% ? Y
! A: w0 H7 Z! u# X, M
try {- I m" G0 K4 d" G# l5 k6 G
Heatbug proto = (Heatbug) heatbugList.get (0);
: B6 x+ V/ x7 {- F# X+ Z* e( D Selector sel = . W) r, K( L: a! N0 g+ T2 z5 s1 I
new Selector (proto.getClass (), "heatbugStep", false);
. n7 m3 ]% l+ V& a: g* @ actionForEach =
& ~9 ^# o4 A1 [1 O% K; o modelActions.createFActionForEachHomogeneous$call
6 @4 n7 f# k' }3 K (heatbugList,
& h/ m- \# B9 ~. C9 X. D new FCallImpl (this, proto, sel,7 ~2 y0 o) f( l+ j
new FArgumentsImpl (this, sel)));
- ~# S6 Y# U2 D } catch (Exception e) {
4 U1 h: Z: `$ ^5 d e.printStackTrace (System.err);. F" C8 M- g, @* h% E, P9 q
}
. z% u, l0 K+ f% Y2 ?; h$ s n7 ^0 S1 I( R( l8 O+ _& |
syncUpdateOrder ();
' L# Z% ]2 t) J% q
. ]9 V7 {9 d2 Y. ]" ?+ m3 [ h3 x try {6 t y$ v) D& S& j# A
modelActions.createActionTo$message
, e* Z3 q0 ^3 w6 V$ w% @ (heat, new Selector (heat.getClass (), "updateLattice", false));! ^# P- W1 T/ W' ~4 D/ `
} catch (Exception e) {/ V u! Y; ^3 q: |$ a
System.err.println("Exception updateLattice: " + e.getMessage ());
" Q' R$ {7 m5 U+ ^/ i1 k9 [% q }9 z% H$ v* g, N0 h: i* b9 U
* |( ~- k# A2 J- m0 a% i
// Then we create a schedule that executes the3 m9 A+ A1 }* D% I n5 Z+ l" Q1 e
// modelActions. modelActions is an ActionGroup, by itself it- k6 d1 n3 B; n% P7 R
// has no notion of time. In order to have it executed in' @2 p% m" W& `% ?8 p n. j8 S
// time, we create a Schedule that says to use the7 ^$ \, r# K# D. B; r* _, d
// modelActions ActionGroup at particular times. This* F- Q4 I/ U+ D7 Q) u
// schedule has a repeat interval of 1, it will loop every
! k; N' B! Y- O& K0 L+ x8 F // time step. The action is executed at time 0 relative to7 J& Z4 S B$ X- Z+ ~
// the beginning of the loop.
; l. M" G- B$ T2 ^4 z( m
) G1 [2 }" f+ g/ H; {6 E. Q // This is a simple schedule, with only one action that is* A" G g* h) n7 [3 Z
// just repeated every time. See jmousetrap for more y5 I# e8 J/ M- J5 I
// complicated schedules.
5 L3 a: k6 h R- `# V& g 9 F3 k4 p9 y' b! p' T
modelSchedule = new ScheduleImpl (getZone (), 1);; f4 |3 r& H+ {3 R% s
modelSchedule.at$createAction (0, modelActions);. B/ @) F$ P% e9 \7 `. I: C; i
7 R+ `& C9 W7 v6 | I
return this;
- Q4 Z# V4 {9 t5 T/ @# b1 g* C' ? } |