HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:" P* x0 g/ B2 y% x! Z# h4 z( E. K
# z# ]( d% C% W public Object buildActions () {9 P8 T: g4 y) Z8 T2 u1 v, a2 c; B
super.buildActions();
7 E7 p# u3 B3 D; r
1 b7 g7 O9 s4 E; A* Y7 E$ c // Create the list of simulation actions. We put these in9 I& w d `! q! @6 ?
// an action group, because we want these actions to be
( q V' h3 i3 Y2 r0 P1 s // executed in a specific order, but these steps should6 f5 S# O5 g, M# N6 J
// take no (simulated) time. The M(foo) means "The message. P a* s$ k+ G) g5 b
// called <foo>". You can send a message To a particular' q# u- p* e- c0 v' B" g' M- X
// object, or ForEach object in a collection.
4 p% L" C. U! A5 R+ x( i
7 q$ ?4 v9 ^: G( T // Note we update the heatspace in two phases: first run6 O7 J: R8 r ~5 e5 _# a& r8 J
// diffusion, then run "updateWorld" to actually enact the
* r. I6 [9 k5 [+ U+ N) j // changes the heatbugs have made. The ordering here is
& W: p' b7 o+ k // significant!
: U% \3 e P* R, }4 n/ Y1 x
* Z/ D+ Q4 a7 g6 ^ // Note also, that with the additional5 j3 H `, u p) `7 S# d1 J# f8 L" p& I
// `randomizeHeatbugUpdateOrder' Boolean flag we can" ?3 C- I$ U: E/ G" z
// randomize the order in which the bugs actually run
0 \+ e2 {# L' B( ^* y. z& o // their step rule. This has the effect of removing any' R, R9 T1 c5 i7 C
// systematic bias in the iteration throught the heatbug w0 c7 ~4 r% N$ a9 k
// list from timestep to timestep
& B7 y% w. V# K w! S( q. |* V8 z 8 p' v8 V s: d0 J2 \
// By default, all `createActionForEach' modelActions have: k- x- f+ G5 i8 `
// a default order of `Sequential', which means that the2 G y+ h2 l" m7 }* z E. k- H
// order of iteration through the `heatbugList' will be
7 L: u. j+ t! x3 P; p6 S G# q // identical (assuming the list order is not changed
6 `. ] U' x2 P( a1 Z' ]5 q" ~+ D // indirectly by some other process).
2 K7 g9 s! l8 e# T' m7 e+ } , B- p! F% [. W4 i; ?
modelActions = new ActionGroupImpl (getZone ());0 Y3 s, ^- e9 ]- X8 Z# H
+ g+ c }' q8 ~) T( g2 O5 o8 @
try {
" x# `, R; q% |2 ?& X7 l2 f" l modelActions.createActionTo$message- x! w! ~- {' l0 E: z# c
(heat, new Selector (heat.getClass (), "stepRule", false));
2 J2 B7 M' r X+ L5 l/ ~/ l } catch (Exception e) {
7 w. c( e) @2 v6 U1 T2 Y System.err.println ("Exception stepRule: " + e.getMessage ());4 y" l8 `; P7 V5 N8 M
} `$ t+ f' T" c9 I: ~9 N
7 `1 J* k7 o; {: C
try {
+ ]" Z( S% L; S. ~ Heatbug proto = (Heatbug) heatbugList.get (0);
8 J1 M8 `+ w8 X) I+ R Selector sel = - g( S: m& x& c' `1 s* L. o; N3 g5 e0 C
new Selector (proto.getClass (), "heatbugStep", false);6 e5 L( D1 o3 `- Q
actionForEach =2 `9 ~ U# Q# w) s0 B0 W4 P
modelActions.createFActionForEachHomogeneous$call8 i C' V( O+ N+ n# a; X
(heatbugList,
) r3 H0 v4 f, _1 E new FCallImpl (this, proto, sel,
1 ^2 Y6 _, |: Y2 |2 t& t. ~) {. m new FArgumentsImpl (this, sel)));
& U3 q3 Z7 O6 ^" {; t } catch (Exception e) {- Z I( P+ C8 ^% h3 U* p; ?# Y* Y$ c% ~
e.printStackTrace (System.err);" j5 T1 n' Y% x& F- p
}1 b; x( r2 b4 W* b4 ?0 W
6 e4 ~, {( N& u5 ?6 S syncUpdateOrder ();+ d+ R$ E8 n. O# h/ J. j
3 n6 p- ~3 M% E4 Y2 M- I+ |- v
try {- u( p* k0 p) B' n- x; Z
modelActions.createActionTo$message ! S( A$ N9 l; U' s
(heat, new Selector (heat.getClass (), "updateLattice", false));
, a; k5 J! u" h" q% T' G } catch (Exception e) {
* l- b: x- Z5 b7 }; P+ s, B System.err.println("Exception updateLattice: " + e.getMessage ());
0 F$ u+ ~* h1 H: @3 g2 b }. ]+ c/ T3 ^! z
: K- ^3 w; Y; B' r
// Then we create a schedule that executes the) c5 l9 O( Q: x
// modelActions. modelActions is an ActionGroup, by itself it
! S3 p. B; O7 }4 v // has no notion of time. In order to have it executed in
/ C; G% o8 u* u$ h // time, we create a Schedule that says to use the
" I: s" r) {+ u, z4 V: ` // modelActions ActionGroup at particular times. This
8 y* @/ P$ D# ]3 e // schedule has a repeat interval of 1, it will loop every
) G+ {' Q& X" c6 B1 Y3 V, M3 S // time step. The action is executed at time 0 relative to
6 [1 S3 }5 ?( w8 f$ k8 l. |' a // the beginning of the loop.8 k: M; f$ [$ U
# R0 R4 _+ c7 }# z // This is a simple schedule, with only one action that is8 z! S# [0 c2 i. z. |% r) E# e
// just repeated every time. See jmousetrap for more
7 A8 E0 f( H2 I; [: l9 \, X // complicated schedules.
- R! ^: P8 o% C4 I
! X9 ^; z1 \/ i& ~ modelSchedule = new ScheduleImpl (getZone (), 1);
" C w8 T# q6 _ w# @- v4 Z modelSchedule.at$createAction (0, modelActions);" ~! T3 ]( F9 W; f2 v
: C/ }) B# L% J, F return this;# ]" |6 m( I7 \/ ?4 w7 R
} |