HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- ?( y$ d- g! }- F+ ?3 M
' `; z6 o7 M* r7 \' J7 d public Object buildActions () {' `2 \, j1 i8 f# K
super.buildActions();
' `. n: h. R" P
7 p3 r* g( \; s // Create the list of simulation actions. We put these in* X# h# y* L& V
// an action group, because we want these actions to be
' X( H6 W( Z' c4 {& C0 f2 T0 ]7 C9 t // executed in a specific order, but these steps should$ W9 m, V' X4 K2 l1 ~ B
// take no (simulated) time. The M(foo) means "The message' i+ e2 z& V5 y) C% S5 ~
// called <foo>". You can send a message To a particular- s& |- ?# ?5 X
// object, or ForEach object in a collection.' j5 B" C# g1 r5 P! N( A+ ]
2 A- l) l/ w+ B6 R4 r
// Note we update the heatspace in two phases: first run6 |- I( w8 s( }
// diffusion, then run "updateWorld" to actually enact the ~5 d8 N& D/ M& q& Z) {
// changes the heatbugs have made. The ordering here is7 W+ L/ e4 _- d0 b; Z# A
// significant!& W7 o% N* ~+ U/ _9 a
& \% ^, _3 p& g! o& [ // Note also, that with the additional
" M2 D' ?- l! f# Q i( D: Z& t // `randomizeHeatbugUpdateOrder' Boolean flag we can
) O% v8 D4 P5 l% c# m // randomize the order in which the bugs actually run; D( r* X( r4 l- F+ P7 a$ ?
// their step rule. This has the effect of removing any5 r1 s$ i* G' `# s& S$ m
// systematic bias in the iteration throught the heatbug6 O% U* P% x+ U5 H
// list from timestep to timestep
u7 Y1 |9 t7 ?+ S % p/ `$ Q' K: [7 N7 N
// By default, all `createActionForEach' modelActions have4 N- V! H/ K3 o: Q! C1 ^# \$ Y. n# q$ M
// a default order of `Sequential', which means that the, \* a" Z& O, U8 ]
// order of iteration through the `heatbugList' will be
7 N$ d7 I( {. W // identical (assuming the list order is not changed! {6 g# ^. c. l. \& O% B
// indirectly by some other process)./ x7 A1 s- P! u, K0 ?! w
9 w7 I, B. h' O0 k2 h9 Y modelActions = new ActionGroupImpl (getZone ());( k1 X2 ]) I0 p2 A1 a
3 Y4 g* C+ L- l( M0 |& ^ try {- A! \! R0 }$ I
modelActions.createActionTo$message
7 g# i8 y& N# C3 a2 ^3 y! Z (heat, new Selector (heat.getClass (), "stepRule", false));
J T; P2 b- E0 i8 J1 ^) s- m- P } catch (Exception e) {
1 h! g/ [& e" u* j, C System.err.println ("Exception stepRule: " + e.getMessage ());, j2 ~8 [& e0 n- s' N9 k
}
+ A: [8 c1 C! s5 [ Z, y( J# n) b8 {. d
try {/ H6 r# [4 N' [$ m+ r6 }. b
Heatbug proto = (Heatbug) heatbugList.get (0);
% f W- y. k/ l Selector sel = * e/ U7 `% G, _6 q8 {
new Selector (proto.getClass (), "heatbugStep", false);7 P" L4 N; _ x5 Q5 E
actionForEach =
; j! q. Y* \: A( v# g& J5 I; m modelActions.createFActionForEachHomogeneous$call
: S$ C* H- V+ Q" x5 t ^1 z. s (heatbugList,
$ R+ x- k, X( {+ Q. j+ }8 c/ x new FCallImpl (this, proto, sel,! Z5 t1 f2 R$ M# s' ^8 g) t
new FArgumentsImpl (this, sel)));
- r L) T' I3 B$ _" X2 c3 b9 b } catch (Exception e) {6 K( I; Q: S$ i1 q
e.printStackTrace (System.err);0 V/ z$ Q" d3 w q, X* y, ^
}0 Z5 Q9 M- F5 ]/ D# r2 b' s
$ N: N z; ?1 _$ L# _
syncUpdateOrder ();% {: {5 k! c7 l+ S3 h
) e- S. B6 r4 A# z. o) n
try {5 E! h) s3 Y: y* H1 c
modelActions.createActionTo$message
: J$ ~% k7 l2 R (heat, new Selector (heat.getClass (), "updateLattice", false));5 e, S5 m6 q: @- A
} catch (Exception e) {- \# a& p! A3 s
System.err.println("Exception updateLattice: " + e.getMessage ());
0 [$ q4 a/ Z% A9 P7 F' V" m& X }
! V* G$ \- `# z 2 _5 @' v, c7 D, Y+ y- r* e
// Then we create a schedule that executes the. t- u2 { t1 h w% |. a
// modelActions. modelActions is an ActionGroup, by itself it2 E" s) N0 O" f4 @' @ x
// has no notion of time. In order to have it executed in
0 I5 s5 U# t% B4 S* l // time, we create a Schedule that says to use the& }: l/ _* n- P# w
// modelActions ActionGroup at particular times. This5 j2 G+ O5 b1 _( M r
// schedule has a repeat interval of 1, it will loop every
$ j2 Q1 {# x' O0 M. m // time step. The action is executed at time 0 relative to; u7 P# d# k, S* R* B8 i; z2 d! F
// the beginning of the loop.( q8 i* F# R7 X) {" \
6 N( I1 I1 X% J
// This is a simple schedule, with only one action that is
9 W c6 j( s# ?2 v$ e2 x1 {% g // just repeated every time. See jmousetrap for more
3 N! b* B" Y; h; X# b // complicated schedules.& Y) Y. d- T" N: X) E
$ J0 }. o$ N6 l modelSchedule = new ScheduleImpl (getZone (), 1);+ O8 E0 o" D2 z" Q1 U+ ]* C: `
modelSchedule.at$createAction (0, modelActions);
2 v ]. c0 _( W9 \' ]% w" \* @ r$ V7 y0 U2 |5 B; O: H8 O4 j; C
return this;
- x& Y9 `2 y+ d9 R% d, G } |