HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# |/ u. v% i" O
# `" @6 e* l) U$ \( e/ K$ }6 P( W4 p0 L public Object buildActions () {* i( {5 s0 k$ q; Z
super.buildActions();7 C a: x: ^5 Y; `7 {; H
. s6 C+ B7 ^7 U& K% N
// Create the list of simulation actions. We put these in
9 V& M2 i k+ N, @ // an action group, because we want these actions to be
9 Y3 o- }7 R4 x" Q0 j- O: E$ m // executed in a specific order, but these steps should
D& r* [8 G, ` // take no (simulated) time. The M(foo) means "The message) O% S3 V$ ~1 U; j& X6 x
// called <foo>". You can send a message To a particular
. v- J, z. a/ C$ r7 Y: O* m // object, or ForEach object in a collection.
9 ` A% c; h% `
, q" m" f4 n) H, @) B // Note we update the heatspace in two phases: first run. s& I( R: l3 y: n9 ]# j* E1 z
// diffusion, then run "updateWorld" to actually enact the
6 r& A8 I; O5 R9 e // changes the heatbugs have made. The ordering here is( S" G2 T8 w- N; ~7 k7 k; ^
// significant!1 L% y% X5 b5 i
" u' o/ ]. \% C
// Note also, that with the additional
. n, B6 A! e* L- I2 l // `randomizeHeatbugUpdateOrder' Boolean flag we can
& b0 ]& r) @% |+ H: M6 e/ A // randomize the order in which the bugs actually run
! D z- s% w4 b% j+ ? // their step rule. This has the effect of removing any
5 ?: z% G' b6 z* P$ n0 T1 N7 c' I // systematic bias in the iteration throught the heatbug
7 L9 o, @: l$ t+ \ W // list from timestep to timestep
7 u6 l4 V K9 @7 o0 A8 T }$ Y - P7 t+ h& z5 @- A- ~
// By default, all `createActionForEach' modelActions have+ m, Q2 o6 e! m- o- X
// a default order of `Sequential', which means that the
- Y1 Q+ {$ [, K2 ~+ w // order of iteration through the `heatbugList' will be
+ [2 m* u: G0 o6 E3 [ // identical (assuming the list order is not changed
5 N8 a+ z0 T9 c9 y3 w8 @$ y# I // indirectly by some other process).
) w+ y$ e+ J. j9 K Y" b( z2 `, u
# O- c+ F* \ q% W1 q modelActions = new ActionGroupImpl (getZone ());8 ]5 r9 l) G& {9 E
: U+ t* O( G! o1 `9 O# l% u
try {
( ]1 ^' O% S6 s0 Y7 } modelActions.createActionTo$message8 ^& w' i' Q2 f$ W2 ]
(heat, new Selector (heat.getClass (), "stepRule", false));5 l! O8 c* z% r; O& [1 r
} catch (Exception e) {/ ~; N3 g$ _8 R# U2 N5 H
System.err.println ("Exception stepRule: " + e.getMessage ());; @, N% @( o- z e8 J2 I- I4 m2 S
}
( Z0 c( \1 a" Q& ^9 B( [2 G! b8 ]. L% Z6 B3 v
try {
: D0 O, V8 U0 n) q: Q; G Heatbug proto = (Heatbug) heatbugList.get (0);
2 A- L2 q6 C/ x3 I( ^6 J Selector sel =
; i0 Q, U8 S1 ~$ V/ l2 z new Selector (proto.getClass (), "heatbugStep", false);+ B R: [. h( I, H- b
actionForEach =* J8 v8 y! s' L1 M( V9 e
modelActions.createFActionForEachHomogeneous$call
) S; _ l& W3 Z! n. I( N. t1 |! m (heatbugList,
7 }! d J6 Q4 k) x6 A new FCallImpl (this, proto, sel,$ J( x5 ~4 b$ a+ n5 P$ } l
new FArgumentsImpl (this, sel)));
. k+ g. b2 A& w3 l- q, M; N } catch (Exception e) {
6 d& U# b+ S$ A- M7 ~+ d/ @, r e.printStackTrace (System.err);
8 G* v+ A) `8 `; H4 n }
1 {' I: W( k, ]- X+ O - p0 N' l' H5 R ^- f3 o
syncUpdateOrder ();' }& ]9 i1 R+ Q# t) L
2 \6 S3 v: F, N+ W1 Z# ^8 l1 u
try {
" _/ l2 J' p) V; J modelActions.createActionTo$message 9 o$ j/ L! x! \/ n4 k
(heat, new Selector (heat.getClass (), "updateLattice", false));
( |" ?( w& j- l/ C1 X: Z' F } catch (Exception e) {
, d* e" i/ z9 q' n. t- L System.err.println("Exception updateLattice: " + e.getMessage ());6 _6 D5 V9 F9 }5 z }: A% ~
}
4 k# Z& P- T7 q3 |4 |# }' W- d3 Q
q, i( ]4 e& o, H8 _3 j3 k) B2 M$ p // Then we create a schedule that executes the
/ _3 @5 x8 B' Q( ]' s& w // modelActions. modelActions is an ActionGroup, by itself it
1 @+ {; f3 B3 R3 L9 V // has no notion of time. In order to have it executed in) J+ g6 \3 X. ^8 r( ^3 M
// time, we create a Schedule that says to use the- n' O3 A. e/ X+ _: x& I1 k
// modelActions ActionGroup at particular times. This5 v, E) N" S! G+ o" [
// schedule has a repeat interval of 1, it will loop every% k j9 o2 C$ _
// time step. The action is executed at time 0 relative to
" ^6 Q: H9 s/ M; {& p1 J e2 z // the beginning of the loop.
5 | V5 D( E9 _
, c. U6 `' I5 H" G% V3 K, ?+ c# C // This is a simple schedule, with only one action that is; _3 M1 `+ o+ P
// just repeated every time. See jmousetrap for more3 ^9 ?# G) L5 i- R0 P
// complicated schedules.
0 A7 I$ b4 c5 L7 Y! g : J) f: |$ v* W; b3 @/ [' B H
modelSchedule = new ScheduleImpl (getZone (), 1);
4 D0 u4 b/ U0 p modelSchedule.at$createAction (0, modelActions);
5 u5 T0 Q8 y# f1 K4 z5 r/ ~ r ' `. X+ `4 n7 T0 t7 Z! I8 z# ]& p- q
return this;; v, r) L. b1 f8 p% Q: I: `/ Y
} |