HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
6 _! M# e+ @5 S' B( A
& @- x8 b r+ Q- o! {# a public Object buildActions () {
# W3 U' o4 B5 M8 R) C super.buildActions();% X/ K$ K' F) X/ I& p1 J
- B6 I4 @& V: w2 k
// Create the list of simulation actions. We put these in
$ s+ S9 W, ]8 k% u) E# S7 f // an action group, because we want these actions to be
( Y% R! p' l8 f, I // executed in a specific order, but these steps should/ q8 I5 s, \8 c0 O1 S
// take no (simulated) time. The M(foo) means "The message; r0 l# O( G# e
// called <foo>". You can send a message To a particular
: i6 \2 h. A* ^ // object, or ForEach object in a collection.) N& g8 m) M) a" r; L; s& I; p
5 B/ j% W, N' P6 C" F. n, i$ O
// Note we update the heatspace in two phases: first run- l" ~ J" T; S1 e! x- [
// diffusion, then run "updateWorld" to actually enact the
; l; O; w' d( Q- u( F% C+ f" n! W // changes the heatbugs have made. The ordering here is% v. j! |2 P9 }6 A" q& h3 O
// significant!8 \4 |* N( X* f9 V; |0 x% u g
6 b( E. V# k) D. a
// Note also, that with the additional5 q8 _& X7 z# I* U% L; M- f
// `randomizeHeatbugUpdateOrder' Boolean flag we can
- F: j9 |: `) p8 e6 t // randomize the order in which the bugs actually run4 @* `$ z8 |5 i6 x$ y
// their step rule. This has the effect of removing any' G' l) `. V7 o9 W
// systematic bias in the iteration throught the heatbug0 C! _7 e, l8 g2 _$ h7 {
// list from timestep to timestep( \2 I, Q) I3 ~( r
' H! F/ G F: p' n2 Y, v* R! s // By default, all `createActionForEach' modelActions have
. N" [" o# C$ U" I2 a3 I" O5 C // a default order of `Sequential', which means that the
, C; g2 Z. g, t1 R8 J // order of iteration through the `heatbugList' will be
; \/ E7 y' E, } i: ^7 @/ E$ ~$ \( [0 F // identical (assuming the list order is not changed+ u1 j2 W u- U% }
// indirectly by some other process).8 o; E+ x. {7 v% w5 p. l
0 S7 r$ [0 \5 Q+ S modelActions = new ActionGroupImpl (getZone ());* c% `/ N# W6 f" D: T K
2 N4 t# W* ~' c
try {
7 q5 @; l5 X1 i# S( k+ y5 G: V! j. v modelActions.createActionTo$message
8 g6 k# X9 f# S: v0 p! w7 Q (heat, new Selector (heat.getClass (), "stepRule", false));
) O: j( u7 M0 q i( b; O( W } catch (Exception e) {1 g# a$ }! X2 ~, `
System.err.println ("Exception stepRule: " + e.getMessage ());
. \6 }$ j$ |6 ^! W% j$ O }$ M# _ r6 f* |0 |; Q& o
$ M$ v* B( U; C. j* t
try {. c f& ]3 P" z' ~ h0 o* o% @
Heatbug proto = (Heatbug) heatbugList.get (0);" q1 x+ `! b" Q* B! j4 Y7 ]3 x
Selector sel = 0 m/ K! p# S! ]) X: N+ ~5 F& t
new Selector (proto.getClass (), "heatbugStep", false);5 }' T, x1 q0 N' a7 u
actionForEach =+ I$ n% O! M) Z
modelActions.createFActionForEachHomogeneous$call* m# M+ f! u5 P( a) A3 X$ H
(heatbugList,1 q+ o! K8 j e& Y. k
new FCallImpl (this, proto, sel,
9 l, {$ n+ m7 {3 o- M0 k new FArgumentsImpl (this, sel)));
8 U' M) b' V7 Y$ N( |' H3 E% r" [- v } catch (Exception e) {
8 D; M1 V- o y1 a e.printStackTrace (System.err);. i% `* I3 T2 X& I& h. z2 B0 Z
}5 ?1 g. j6 ~! ?9 I/ k/ R
6 w8 J9 Y8 E9 d+ x2 m syncUpdateOrder ();
2 a J W9 l, g* t$ y) T) e7 X) u* x: e' I3 e) S1 ?& P: ~
try {6 E- v8 D- g- @1 t* k* s
modelActions.createActionTo$message 9 X: ~8 d% [1 O/ G3 r3 _7 U! J
(heat, new Selector (heat.getClass (), "updateLattice", false));
, f2 u6 W6 b- Q) `8 N9 F } catch (Exception e) {! X# B2 J( W; X
System.err.println("Exception updateLattice: " + e.getMessage ());
' x) n3 t- G9 S }
% P9 g* m7 b- W
7 V4 x! U; J7 ~' F) ` // Then we create a schedule that executes the
$ U' P4 [+ H7 O c" ~/ Y // modelActions. modelActions is an ActionGroup, by itself it
7 t, x/ C8 g' | // has no notion of time. In order to have it executed in
" ~( L* L5 a T5 \3 d9 W+ S // time, we create a Schedule that says to use the* z6 B2 ^3 T* ]( `1 U/ L% ~
// modelActions ActionGroup at particular times. This |3 E+ ?: b& p* q2 L+ ]1 u
// schedule has a repeat interval of 1, it will loop every, \6 c- w& J6 O, u) K: M
// time step. The action is executed at time 0 relative to6 |* l/ Z" B; m
// the beginning of the loop.( Q9 Y# b5 c l( [% Z% i
# [4 p# _0 i; r* ? // This is a simple schedule, with only one action that is
0 f; D* l* B. z2 A% T // just repeated every time. See jmousetrap for more
/ c- ^ M3 L7 k, j# ~5 T // complicated schedules.
* b; F6 B6 ^- L 5 L' w1 I" A+ `6 R+ W* Y9 \) T
modelSchedule = new ScheduleImpl (getZone (), 1); y: I: D/ U' H) a; x$ q6 K
modelSchedule.at$createAction (0, modelActions);0 F& [4 K6 a4 L" u( P& ^5 s7 b
- {" [( L) B9 z+ D
return this;
7 p3 b# t P7 S2 f } |