HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. D. Y5 G6 L! p1 p! \& X
# N% j- W- I7 L public Object buildActions () {
8 s) L% X" Z0 s) ]* t+ f" o: T super.buildActions();8 T: F# G4 W( J8 C8 v
* J' F5 Q* L% D" u* N9 ~ // Create the list of simulation actions. We put these in
8 A* q; k, o5 u$ e5 |/ C; W // an action group, because we want these actions to be) S! G2 T; E$ s: z
// executed in a specific order, but these steps should
; u) A8 o; I- O+ T: W // take no (simulated) time. The M(foo) means "The message7 @: x% H$ O% Z" C
// called <foo>". You can send a message To a particular
( `- ]6 _( b, V h- { // object, or ForEach object in a collection.( j4 }/ T: Y8 |) N6 _8 x
8 @, F$ l2 k# L" d' Q
// Note we update the heatspace in two phases: first run
/ P: H1 _4 x3 W1 R' _1 P // diffusion, then run "updateWorld" to actually enact the# Z% f/ u: Y; p8 h8 m
// changes the heatbugs have made. The ordering here is
' g9 y ~2 ?: ]9 a // significant!, n2 Y( @- R, p" f9 ]; R
" s# f! d% L; e% Y% `" ? // Note also, that with the additional- M$ X9 T7 |- R$ C' \; Q
// `randomizeHeatbugUpdateOrder' Boolean flag we can i- p: f9 U5 `0 H
// randomize the order in which the bugs actually run
9 D" W- C+ \ U" S3 h$ x) m // their step rule. This has the effect of removing any
6 c5 ]# P5 r+ y // systematic bias in the iteration throught the heatbug
: C8 h; x- ~1 \( u // list from timestep to timestep
8 j7 q# w2 a. m5 I) [8 i$ h+ ` ' n b3 u- a7 [) j' O0 Q
// By default, all `createActionForEach' modelActions have
' ]% f$ k2 Y5 P) m/ U$ y5 i // a default order of `Sequential', which means that the
P0 J( D8 F1 M' H // order of iteration through the `heatbugList' will be
" |( z& K& s, g& s7 l6 K // identical (assuming the list order is not changed
1 t: Y( O6 o& s // indirectly by some other process). l0 i2 U! Z: B; m) \5 u
\3 r+ X" l, { B+ ]5 b. f modelActions = new ActionGroupImpl (getZone ());
, A% W% ^% G% ?6 L0 A
# b! R; u( {6 O- L; F# E$ v$ C) X try {: v' X3 j' d# z% Q- {- u U! l
modelActions.createActionTo$message" C- {- a" ~& \$ p9 V8 w
(heat, new Selector (heat.getClass (), "stepRule", false));4 i! N/ G" v4 S( O& [) z
} catch (Exception e) {
+ t$ S' e; O V3 b0 \+ { System.err.println ("Exception stepRule: " + e.getMessage ());
' L; S# x& Z1 c( R }
, U( x6 A2 s$ S% U+ G6 N8 _* g. }) `6 v" h
try {" s( _. x/ i1 j1 D- A1 ^
Heatbug proto = (Heatbug) heatbugList.get (0);
, g9 X" O4 y+ S Selector sel = % W, Q* i; X$ X5 @
new Selector (proto.getClass (), "heatbugStep", false);
I1 a" C7 h$ R, ]- ] actionForEach =
/ y6 l5 e( ?2 p* L0 ^9 \0 H4 E- U modelActions.createFActionForEachHomogeneous$call- Q1 e8 {! }% [
(heatbugList,
# P0 L0 j$ ]- M0 M4 `8 O" c( m new FCallImpl (this, proto, sel,
% H9 J# k( u. A- ]. J new FArgumentsImpl (this, sel)));
& y+ e+ Z) n6 m' H" |7 @8 w6 E } catch (Exception e) {( P7 |3 h F1 y
e.printStackTrace (System.err);
; h4 X* J; o: x# \9 x+ W }2 ?, p- j! C% s" E, \3 \5 y3 f
5 s# z, T+ ?9 z/ i, W+ Y syncUpdateOrder ();" [" U0 O% q& S. O* |+ L
) R. [- Z4 s Y7 ^6 t2 \ try {
5 R) g( G, m/ m3 m: j modelActions.createActionTo$message
5 |0 I. d2 x2 O7 M1 g9 m8 m$ ]1 N% ` (heat, new Selector (heat.getClass (), "updateLattice", false));# d( S9 Z: h, s1 C \; `
} catch (Exception e) {! ]) `& |' @! A" m
System.err.println("Exception updateLattice: " + e.getMessage ());3 G* m4 f7 y# r9 `; U* v
}
9 E/ C; R1 I3 _8 u0 A' [4 j & u& r) k2 `, \9 h3 b
// Then we create a schedule that executes the
. | u8 W2 t7 k9 `! g: W3 n6 k // modelActions. modelActions is an ActionGroup, by itself it; i5 {+ J$ @) E/ G ]
// has no notion of time. In order to have it executed in3 a4 G1 P7 J4 l, ^* x
// time, we create a Schedule that says to use the
3 _5 G4 m& ?2 S; Q // modelActions ActionGroup at particular times. This
& ?* q8 o5 F6 m# F9 ~* b // schedule has a repeat interval of 1, it will loop every
/ ?6 D7 m: s. r- ` // time step. The action is executed at time 0 relative to* p0 x J; Z7 h- V/ b
// the beginning of the loop.
! p, @- d% J* D
# q3 n4 I7 i& ` // This is a simple schedule, with only one action that is
" c6 {( Y1 x4 I0 u! f% ^ // just repeated every time. See jmousetrap for more* W7 y4 K( X4 U
// complicated schedules.
+ y$ N e1 j7 `' u
; R) a& O3 g( E( a8 f- e modelSchedule = new ScheduleImpl (getZone (), 1);+ i5 ~0 }2 M# t" s# J7 [) t$ V
modelSchedule.at$createAction (0, modelActions);
" V- F0 C; r; z6 u# i
' ^2 R1 s9 _' O# Y) N return this;
: p- y5 G0 m- ?. @ } |