HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! M9 p! N$ f3 Z; M0 J
; }% D8 Y5 C6 W% a! g! [' h public Object buildActions () {5 |6 p/ U5 T2 O" d- G" Y
super.buildActions();
- l* j4 X5 b/ X! K( s / P6 S* R3 D) i- X% U
// Create the list of simulation actions. We put these in
! U9 y" U! D9 `" A' r4 ? // an action group, because we want these actions to be. `* W# d: R/ ^5 n
// executed in a specific order, but these steps should
( B# T+ }, ^$ { U* }! z // take no (simulated) time. The M(foo) means "The message# D. d! i2 R; m2 C& e0 g+ l6 S3 S0 K( ]
// called <foo>". You can send a message To a particular C2 i8 `" g0 O( O5 m7 J% k$ Y
// object, or ForEach object in a collection.
8 f2 o* p5 m+ [" m( ^4 l" w
. `; V5 T3 F: M3 o // Note we update the heatspace in two phases: first run
$ o+ G# z# l# [1 v // diffusion, then run "updateWorld" to actually enact the2 ]( ]+ `- h1 k% ~; [
// changes the heatbugs have made. The ordering here is
% D- {4 o; d8 L: X // significant!. G# t1 M( e6 k. a [
" h* F0 p8 e3 a4 a W
// Note also, that with the additional' S4 i2 m/ z9 g& ~- |, Z( F9 ?
// `randomizeHeatbugUpdateOrder' Boolean flag we can' T! A. G4 p: I# F
// randomize the order in which the bugs actually run
4 a3 I3 a7 T7 [2 e* o // their step rule. This has the effect of removing any' U7 n- Y4 X, d2 H' |5 ~/ G: o
// systematic bias in the iteration throught the heatbug; t$ I Y9 H, T5 V
// list from timestep to timestep
9 a7 m% b2 W! I/ \
0 _( N% I7 Z% ^/ L1 [ // By default, all `createActionForEach' modelActions have2 l ~5 X; }/ g1 l4 d
// a default order of `Sequential', which means that the2 X8 T2 p. x) Y+ f# |9 ~( N
// order of iteration through the `heatbugList' will be3 ]. C4 o4 w; t7 |' d2 i! Q
// identical (assuming the list order is not changed& e# O' u( z x+ P5 m t
// indirectly by some other process).
5 a& ~/ z6 L9 ~; N. {& v _* c) L
1 c' J& U3 l ?& r2 i modelActions = new ActionGroupImpl (getZone ());+ {, x& A. {' y. P8 V! n3 \$ _
& U; M* n& U* L: n) n9 ?, j2 A. T try {0 H/ n; Z" c' n/ d/ z
modelActions.createActionTo$message
/ z# d" Y- o+ | I3 v2 x( G7 S/ h% t (heat, new Selector (heat.getClass (), "stepRule", false));
) b. H/ [+ j5 a8 R } catch (Exception e) {2 k; A! e, v* n0 c9 _
System.err.println ("Exception stepRule: " + e.getMessage ());
8 y7 ?2 Y( P. |. f! r: }: M7 c( ? }# X/ Z% X: N/ @5 [3 y3 }3 P2 e4 \" j5 U
8 F( K6 R' F9 |$ f' T' r( Q7 p3 O8 @
try {8 \7 n8 n! s' r8 \( T% s
Heatbug proto = (Heatbug) heatbugList.get (0);- U) Z Z8 p$ m$ n J) s, e' |
Selector sel =
+ G+ I1 |2 L. e/ w% S/ B9 O new Selector (proto.getClass (), "heatbugStep", false);
" v. C% c* Z5 \1 B0 d actionForEach =/ R0 z' B& |. g
modelActions.createFActionForEachHomogeneous$call0 M+ g- c3 A% c. ? g9 A
(heatbugList,
: C9 C3 e" @$ N" h! w# y new FCallImpl (this, proto, sel,& H! f. d* u2 n7 t
new FArgumentsImpl (this, sel)));0 c4 X1 K" ?& J v; W
} catch (Exception e) {
: H% J- ?( F& o0 @4 ~ e.printStackTrace (System.err);
9 o i+ |8 i& h9 z6 W/ q) F% y4 T }
/ [" i% D, i- i2 l$ A
2 ^3 O+ y; B4 P syncUpdateOrder ();
% }. v3 r* V8 e7 v; j# c# P5 X. _7 { O% p5 v
try {! `7 `/ s$ q- p) F: Y
modelActions.createActionTo$message ; Z% u9 H' A* r9 L9 N% Z3 x3 _
(heat, new Selector (heat.getClass (), "updateLattice", false));9 b. H2 D3 ]- L' e
} catch (Exception e) {
/ j! s0 e- S8 ~: R% T% F) Q7 r System.err.println("Exception updateLattice: " + e.getMessage ());7 f0 g% R: O6 S; }+ k1 I7 ^
}
$ o/ w5 s% K1 n' t7 i % T% q" R2 ? K& f/ z' h! s
// Then we create a schedule that executes the
. F8 C, }$ x, H! f* N // modelActions. modelActions is an ActionGroup, by itself it1 ?. Q* |5 M/ N, r/ _7 F. p. \
// has no notion of time. In order to have it executed in, n9 S" e- T; D% g# a
// time, we create a Schedule that says to use the( t- {( J2 R4 {9 m. g! u) e* Z
// modelActions ActionGroup at particular times. This6 _3 x/ L: s: I8 S1 A- u- o
// schedule has a repeat interval of 1, it will loop every
! p5 O# ]( i$ s, { // time step. The action is executed at time 0 relative to
' }, B! A. ?) W7 y1 C8 X# m // the beginning of the loop.
7 b- I8 ]/ \9 i. F" p" ]: H3 ?- L9 p Q
// This is a simple schedule, with only one action that is& R% e; B$ A V! m* U8 Z1 W( y
// just repeated every time. See jmousetrap for more
/ f! B1 `% o8 x8 {- E% x; J% }3 Q // complicated schedules.
" G4 L) p0 r; s) L/ j: b; L% w
2 h- N" ]- N* X7 z modelSchedule = new ScheduleImpl (getZone (), 1); F' d9 h7 K4 ?8 j8 u8 ~, X. |- L" S
modelSchedule.at$createAction (0, modelActions);
3 w' h" Q x- L- x * ]3 C+ L% v7 b3 d
return this;6 z$ e8 Q9 r$ f) v
} |