HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& O! n) `6 g5 j' I% r
# }8 U% ~6 a! K$ f ^: d; R
public Object buildActions () {0 T) i# q0 n6 V& e) B2 L
super.buildActions();
, L- X' f" ?8 X+ F8 c$ C& J
; X, R: g+ u$ r: z: ` // Create the list of simulation actions. We put these in6 R" M2 P: l3 c* d/ b4 l
// an action group, because we want these actions to be
) A, Y( @7 T& i. X6 t4 G( X% T // executed in a specific order, but these steps should- [6 {. }$ I7 e
// take no (simulated) time. The M(foo) means "The message
" i. R4 p0 r) X) i1 Y, g // called <foo>". You can send a message To a particular2 {/ s' y; e3 C+ `0 F) [( _, B3 G
// object, or ForEach object in a collection.
1 A! R# F2 e: y' |3 d% H1 f4 K& V* x
$ E- A& w5 f& M2 {: i* l) g // Note we update the heatspace in two phases: first run
1 q% E0 o: T C% |# G7 Z. S // diffusion, then run "updateWorld" to actually enact the
# p' f" p# _9 h2 f5 M // changes the heatbugs have made. The ordering here is
! C; a& I: v& e* |+ i, z // significant!
) i! q5 I/ a% i1 _ J$ p5 v% }: q / u- r+ t0 s9 [3 a" x
// Note also, that with the additional
, ]. ~5 p! M% K3 L l: g$ Y, g // `randomizeHeatbugUpdateOrder' Boolean flag we can
5 N1 F8 K' o" h( E' d, B // randomize the order in which the bugs actually run
0 a; w+ e, [. f1 q3 y // their step rule. This has the effect of removing any, E9 b# n C2 N4 ^
// systematic bias in the iteration throught the heatbug
: z+ p+ P' t: K/ M- r' X N/ { // list from timestep to timestep
3 w3 O: [: a4 F3 }2 a: k. D ) N" l3 k; o8 p0 b5 [5 [$ N, C
// By default, all `createActionForEach' modelActions have$ w& \' p( N) W' ^2 q5 P
// a default order of `Sequential', which means that the4 s* S' _( k8 M# y
// order of iteration through the `heatbugList' will be2 T9 G5 ?9 G+ Y( ?- J
// identical (assuming the list order is not changed
# P/ r0 s+ [2 T" H" T; @4 M // indirectly by some other process).7 E- l0 P1 O1 {; [% q
0 p+ T5 f' `# B8 G modelActions = new ActionGroupImpl (getZone ());
6 w9 x" B/ m* V8 ~) P" O9 Q/ l+ G" x
try {
( C5 c) s% F( ^) o! z1 Z modelActions.createActionTo$message
$ H: y; Z/ s* ^5 p- p (heat, new Selector (heat.getClass (), "stepRule", false));3 [. v! G7 I' L9 ?, \. N A
} catch (Exception e) {: m. h$ \% \& A/ u, z
System.err.println ("Exception stepRule: " + e.getMessage ());
0 _: b) b6 q: ^* J. E- W* H }# U6 l) l; a! L, N7 d' Q& a
: w! t/ ^8 |: J
try {
5 u+ E6 {! ?6 A# Z0 M! I Heatbug proto = (Heatbug) heatbugList.get (0);: {2 ]5 X" C" c0 ]
Selector sel =
1 C( W) b5 v. k/ h4 O3 k9 m( f# ?, { new Selector (proto.getClass (), "heatbugStep", false);
& s3 {+ A2 }" q7 }+ B actionForEach =
. O5 G: K8 s; n. H% ^5 I modelActions.createFActionForEachHomogeneous$call
$ p- q/ j1 {3 K0 S (heatbugList,
1 w9 M- t* a% L, } new FCallImpl (this, proto, sel,
! `( j( Y s/ u2 H. K" g% B/ H4 \ new FArgumentsImpl (this, sel)));
2 l H- K2 y) l7 O' Z; [: ~( l! A } catch (Exception e) {6 G3 v( j6 V' D6 V( t/ ?/ w9 J/ p/ I q
e.printStackTrace (System.err);
% b* j& l. h" p }7 c9 }' J# J9 E X9 \; r
3 `, [6 i4 n2 n# R6 S! L
syncUpdateOrder ();
% E% {; o q) t M/ d; T' {# N" |, q" k5 [3 F* g6 y
try {: e! b# ~) b0 w p+ X
modelActions.createActionTo$message
" @9 H* m% Q1 ]. U! a4 |3 l (heat, new Selector (heat.getClass (), "updateLattice", false));
4 D2 k% {! P! C" E M2 x } catch (Exception e) {
# q5 }( _, A' K6 Z+ S/ g System.err.println("Exception updateLattice: " + e.getMessage ());
0 W" d! u" i$ c) I2 D: V# o }
: @ }9 Y% Y+ h# G" j7 K9 ^) U ! j* z$ x' H+ H7 x v* {* |
// Then we create a schedule that executes the# F4 C3 F. F5 B5 D6 B
// modelActions. modelActions is an ActionGroup, by itself it- S h" ?' i' _6 W$ A
// has no notion of time. In order to have it executed in2 Q+ M, _ q6 z p2 l8 h4 d9 _6 c
// time, we create a Schedule that says to use the: W# ~" ~# V5 a! q" @4 L: T. B' P! @
// modelActions ActionGroup at particular times. This
7 Z" J0 c( D! s9 e" E, X& \, L // schedule has a repeat interval of 1, it will loop every
+ X7 `* L$ k% q // time step. The action is executed at time 0 relative to& O. c9 l7 P5 R/ A. S* L- @
// the beginning of the loop.9 x* ~: }1 \( J2 i! ?) N4 S, K+ |
5 g8 e; h3 f2 |- l // This is a simple schedule, with only one action that is
" N+ z3 w( P( g/ k% F3 b. E5 S; x // just repeated every time. See jmousetrap for more. R. `1 ~! C. m: V$ i- z
// complicated schedules.
! ^: D' A, ^! q6 C/ O : y( Z% Q! c; \1 L
modelSchedule = new ScheduleImpl (getZone (), 1);& j1 B! F" P6 G: d2 ~
modelSchedule.at$createAction (0, modelActions);, d+ O$ D) C+ s- K* a& ?
/ a2 R# `+ @, f( A return this;' _& R/ L5 t, w& j0 w$ P7 B; Q, I0 q
} |