HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% E7 p; z, X7 o
* r( W( c3 D. O+ |( i+ ^
public Object buildActions () {
( c8 t: a* f3 H+ ?+ @, o6 B! K$ @! y super.buildActions();
3 z2 W* Q% o2 k) h/ [3 m0 d
2 ~8 r! j% t! H i; ~ // Create the list of simulation actions. We put these in2 S9 G" u# |* P
// an action group, because we want these actions to be
* F3 K( V v0 t+ ^1 q8 f // executed in a specific order, but these steps should- t' Q( ^, [/ r1 n' `
// take no (simulated) time. The M(foo) means "The message
# H# e3 @- n( i, H // called <foo>". You can send a message To a particular
0 `8 `( f2 U* _: K! v& O, w // object, or ForEach object in a collection.
9 n% q3 o+ K# O3 h8 T+ s) }. q0 q, u1 h) X
0 |$ X; J0 p" o$ v7 d( {6 c$ M // Note we update the heatspace in two phases: first run( L/ @+ Y- t, v' I
// diffusion, then run "updateWorld" to actually enact the
) @5 ?) T" j1 I" ^ // changes the heatbugs have made. The ordering here is
; Q" j& l0 r% r. t' [: ] // significant!
3 t @6 u; m0 M( P* \
: E+ Z0 v: C/ r5 C; b // Note also, that with the additional6 T7 U2 u9 O' g3 n2 W
// `randomizeHeatbugUpdateOrder' Boolean flag we can% ^; `' Y. x* P2 j3 r7 X( V5 f
// randomize the order in which the bugs actually run
+ X, R9 L" |! o0 {% M7 p // their step rule. This has the effect of removing any
, S* {& S3 ^3 B // systematic bias in the iteration throught the heatbug
, `9 n) n, D; }9 S // list from timestep to timestep
/ R4 q; x1 G1 M6 H5 p. t2 y ' A W1 D' x) C0 T
// By default, all `createActionForEach' modelActions have9 j+ U- r+ y2 M1 G4 l( f+ B% ~- E
// a default order of `Sequential', which means that the
) e+ y0 O* L9 S3 P, j // order of iteration through the `heatbugList' will be
2 n8 ^8 j1 m: z2 j // identical (assuming the list order is not changed3 M, k s Z2 q8 q( i0 A# x
// indirectly by some other process).
) I5 f q6 h3 G/ z4 Q7 E 8 Q) a) c' p4 G6 P5 J) V
modelActions = new ActionGroupImpl (getZone ());
- }% w- i) W0 C" A, @: S: ]0 g: s+ m; L0 }: O0 n' Z# y
try {
3 |+ G2 I9 ?& m" _ modelActions.createActionTo$message
( I1 H `8 L e/ n w (heat, new Selector (heat.getClass (), "stepRule", false));
8 y0 k6 `: Z! Z& S& F P; x- _/ p } catch (Exception e) {) [3 Y' ~! Z3 D c3 k5 Y/ J' ~& C
System.err.println ("Exception stepRule: " + e.getMessage ());& }7 L( }: {' h- C0 u* f
}; O+ j5 A+ e- h' m
8 t2 q/ L) w1 E2 ~& J4 |: w
try {0 f! I8 W3 E3 W8 t
Heatbug proto = (Heatbug) heatbugList.get (0);
, g* c& O, y; _ Selector sel =
9 r6 Q* m5 y9 H" x+ z9 H$ s* K3 z new Selector (proto.getClass (), "heatbugStep", false);) r) n' o( z! I! g- x# C. X
actionForEach =9 o0 z( X, |& ^9 u* |$ W: |* F* s
modelActions.createFActionForEachHomogeneous$call' v0 ?0 S% C1 E/ r5 K8 L
(heatbugList,3 i5 p; ~+ V: h% }2 }2 ?+ ^$ Y; B$ [
new FCallImpl (this, proto, sel,
% x5 k- c0 b# [( s; n+ V2 {8 d new FArgumentsImpl (this, sel)));
" ?" o7 i: J2 l } catch (Exception e) {! J$ t) ^) D+ b
e.printStackTrace (System.err);; S; B0 l m* ^$ ]2 a+ D6 J$ C
}
: d+ O% @% T- Z" o. w" g. q 6 D& G, { ~# e" F
syncUpdateOrder ();& \, n) b' i. @& u! M7 N/ u7 B; ?8 X
: X" O7 f2 }% M! f1 R8 O1 V! D) N) ] try {* A1 A) z/ | u. U0 X0 j
modelActions.createActionTo$message
! f# u( N6 k0 r/ L (heat, new Selector (heat.getClass (), "updateLattice", false));
. M' e& Z* M" W( B } catch (Exception e) {- k; \; k7 K( ]' D3 Q b2 a
System.err.println("Exception updateLattice: " + e.getMessage ());
* b, H( ]8 X/ T/ A+ m0 O+ P }
/ D- j- D7 [9 m8 g: A
) f) E% k5 f' }: t) a. H7 X6 W // Then we create a schedule that executes the
, a m0 \# U/ S // modelActions. modelActions is an ActionGroup, by itself it: o9 B1 J' r8 _; ?+ Z
// has no notion of time. In order to have it executed in
* P; m: e! R R* X // time, we create a Schedule that says to use the1 y& A8 Q4 k1 S M
// modelActions ActionGroup at particular times. This
( [% m$ K- E, J3 y. }8 `: b. | // schedule has a repeat interval of 1, it will loop every
% n9 D; I7 _3 l3 f, y // time step. The action is executed at time 0 relative to; P- f0 J$ u! [) H; }
// the beginning of the loop.: v. E- Y3 G" N3 E
: P+ _- g3 @3 {- |% l, ^# Z // This is a simple schedule, with only one action that is
' R: N8 i: ?- W* z1 f6 k4 B // just repeated every time. See jmousetrap for more/ p/ H6 w/ l2 t( c& j7 z: {! s
// complicated schedules.) |: g' v* r* W
+ S( S' q; f/ } modelSchedule = new ScheduleImpl (getZone (), 1);. @/ R$ p' M/ ?( W; t
modelSchedule.at$createAction (0, modelActions);* X' p. z. o4 F% _$ }+ R" }, X X
6 C- I8 ~% @1 Q$ Y' J1 p. K9 o
return this;
@; G# n& J# u& }* j8 k7 h } |