HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& N% n% E' r) y( ]
f8 T4 \4 ?8 r3 N; F3 O" O public Object buildActions () {
* }' s. U5 b( m% Z3 ^, N3 f super.buildActions();
0 m: X+ D& `& I5 U
2 {( G% X8 l# m' s( S, K // Create the list of simulation actions. We put these in
8 X# T3 D! {: V* m u( ` // an action group, because we want these actions to be
( C4 x9 r# A9 R+ b: _+ g7 z // executed in a specific order, but these steps should" K& W5 R+ \0 O) y4 p
// take no (simulated) time. The M(foo) means "The message a4 L; v; T Q
// called <foo>". You can send a message To a particular* y# V; g. O7 s3 O7 Y
// object, or ForEach object in a collection.; O1 Y8 Y; ]6 E3 ^" G5 Y4 m( U
! c/ v: S1 d g- K
// Note we update the heatspace in two phases: first run
7 |$ W u. |, @0 D z2 o; Q0 y // diffusion, then run "updateWorld" to actually enact the
: c/ `: l6 X% W D // changes the heatbugs have made. The ordering here is
) g' y) f5 x( g. p: h6 } // significant!
! W! ~+ O: x- ~' V3 {6 [ ; d/ O+ k- K0 K3 c$ B' N
// Note also, that with the additional
& d4 g: Z! p! U! B( S& B // `randomizeHeatbugUpdateOrder' Boolean flag we can: B, d% @" T: r5 m1 E8 m0 c) @
// randomize the order in which the bugs actually run
+ U1 u" L1 z4 A% q5 l' | // their step rule. This has the effect of removing any% ]. Z4 p. R, s' X1 R
// systematic bias in the iteration throught the heatbug# Y8 J7 P' q: v$ H7 u$ b' m& C
// list from timestep to timestep
, Y _ G& l$ o ; e) I- Q2 T5 a7 L) p# r* _ E
// By default, all `createActionForEach' modelActions have* R# m) s; e, y9 U2 p
// a default order of `Sequential', which means that the& I4 m8 b+ H( z% F
// order of iteration through the `heatbugList' will be3 O# _/ O: C3 C5 O' I9 b
// identical (assuming the list order is not changed
( \* z0 ?8 M4 y; X // indirectly by some other process).8 R1 x. h7 \* p6 B+ A2 N* S8 T
% Y9 U9 k& ~1 T6 D modelActions = new ActionGroupImpl (getZone ());
5 v |" w- g+ U) K V, j- S8 |( Z% ~5 Y$ M
try {
2 O5 r6 H0 V# \4 R modelActions.createActionTo$message
" ~ f" d) ?5 w (heat, new Selector (heat.getClass (), "stepRule", false));
& r" o: z @4 k' V* Y: Y } catch (Exception e) {
( [- I( x# I7 J7 R* n; ? System.err.println ("Exception stepRule: " + e.getMessage ());
1 M3 Q M0 Y/ u9 B }
* o4 j9 o1 K5 F& f" w3 v3 C$ ^* R
; x/ ]: p9 M; q* S4 a7 I try {9 e; g9 Z& b5 f0 D% e6 \6 v
Heatbug proto = (Heatbug) heatbugList.get (0);
7 V' A/ F5 @, c7 _+ Z Selector sel = 6 B6 j7 K# X- y- G8 _, m7 q7 m( C/ k
new Selector (proto.getClass (), "heatbugStep", false);
) E8 {" \+ I$ u7 g& F a7 b actionForEach =1 m! I8 D4 t) [9 ~1 o' J7 O% v
modelActions.createFActionForEachHomogeneous$call
3 B! \2 I! k8 U4 n (heatbugList,
2 f1 B! S5 n. k( n5 t' N6 {3 W0 h new FCallImpl (this, proto, sel,
3 Q* l* Z( K; T7 l5 L new FArgumentsImpl (this, sel)));
$ V0 V" M1 d' s" m x1 E6 x/ _1 b1 y } catch (Exception e) {
5 y5 j- G, v( j e.printStackTrace (System.err);" p3 Z Z4 U' P+ q
}: e m1 ^3 w9 p* {' ` }
% U! A" W+ S9 _7 Z$ G
syncUpdateOrder ();
) _5 G' h4 w1 X. @
% H" ~1 C8 G5 _1 {7 D& ~: R. D try {2 n5 P+ E4 a: N9 t$ \7 B
modelActions.createActionTo$message
3 z6 R+ z7 a7 n1 N: B (heat, new Selector (heat.getClass (), "updateLattice", false));6 D/ S* ~' C) z2 a
} catch (Exception e) {$ m1 P; o7 [1 c2 |( E$ i7 R
System.err.println("Exception updateLattice: " + e.getMessage ());
" v* M( j/ I+ f! O' I# E, v }
! ^( {& G# a& b9 n [ $ F9 @! y: h7 c4 z3 U
// Then we create a schedule that executes the! G F. e0 X5 c' j8 v# i
// modelActions. modelActions is an ActionGroup, by itself it
6 d+ T, Q7 M# O) Z // has no notion of time. In order to have it executed in
2 P- P9 `6 H# v1 p0 n& V // time, we create a Schedule that says to use the9 ~0 J% `4 l: c2 h- s/ {# K- y1 X
// modelActions ActionGroup at particular times. This
0 c9 Q2 X# o5 u( u // schedule has a repeat interval of 1, it will loop every
/ Y* w& a7 ^9 Y7 C) T! B$ }& f. J+ q // time step. The action is executed at time 0 relative to' A6 e f! O7 j2 Z$ O) ?4 K& x
// the beginning of the loop.
) a* t3 Q; S; W9 l9 _% d" l, z# ^" A9 }/ D# {+ ]5 I& b$ U1 ?9 H
// This is a simple schedule, with only one action that is
1 g2 R; g% D# c1 _4 y& ]) H // just repeated every time. See jmousetrap for more
2 u" i/ H2 f8 \0 H7 v) q' f // complicated schedules.
0 i% }9 u) D3 _. J4 U2 A $ G+ b. _2 M7 _! w0 Q! ]: K
modelSchedule = new ScheduleImpl (getZone (), 1);3 m4 i8 @+ T8 w3 z c5 Z+ h
modelSchedule.at$createAction (0, modelActions);& Y3 i n6 c1 O7 Y
7 Y; M. T, P% D
return this;
7 [8 |5 _" X3 l9 R* B } |