HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 G* ]* Q v. o& i: Z
( G; \* |& y# k/ f; {+ j6 j% A public Object buildActions () {' b* A y0 V- }# l; Y' s
super.buildActions();
1 |1 n+ a* M' t+ F2 o" `
! f) X' @# {2 \ p$ u // Create the list of simulation actions. We put these in& ~+ V7 L) u. @. L- T
// an action group, because we want these actions to be
; Y- q5 |% |" A! h$ c2 @2 Z; D# H // executed in a specific order, but these steps should
+ J# k! P$ X o9 C+ @% O // take no (simulated) time. The M(foo) means "The message
; m" m" o' H( L" V% h // called <foo>". You can send a message To a particular5 `2 U( s5 O/ b1 ~# q8 t
// object, or ForEach object in a collection.
$ D" P) |' N- ^& Z% z
7 ~* B+ y1 N- `$ Q // Note we update the heatspace in two phases: first run0 C6 q( Y% G" _0 l5 I
// diffusion, then run "updateWorld" to actually enact the
' A6 I: V- h3 ~9 T. O/ @5 m // changes the heatbugs have made. The ordering here is
' P+ S) q" p* t8 \: Y. B // significant!! G% E: e% Q2 G
6 ]( g8 D! l0 }: R X& I
// Note also, that with the additional
- `: r4 i+ l7 c2 e2 ?3 [1 v // `randomizeHeatbugUpdateOrder' Boolean flag we can
: }4 p7 J0 Z* {, K, a& { // randomize the order in which the bugs actually run
" M6 u' j8 \3 H1 Q) C // their step rule. This has the effect of removing any% y6 _3 h7 g$ x1 T1 r
// systematic bias in the iteration throught the heatbug
2 i* Y& j: P# p( k5 U; ~: _# `* `9 z // list from timestep to timestep
2 }9 B" J( R9 Z& H5 o
& v0 L# e+ {8 Z- X6 q- ^& n @ // By default, all `createActionForEach' modelActions have
\7 s- `; _. V7 B- \8 c) K // a default order of `Sequential', which means that the' s; E% B6 N* ]. A- X, W* v2 ~
// order of iteration through the `heatbugList' will be7 ], g2 z5 c' t' @/ O
// identical (assuming the list order is not changed
0 U* g ?+ L5 ?7 v O // indirectly by some other process)., y. D8 ] T7 s& ]# X
, D( p1 u; N" I+ t1 C R2 U
modelActions = new ActionGroupImpl (getZone ());1 c7 u3 K7 d9 E5 ^+ ]2 j' [+ V
" ]+ i+ v( Z5 H) X7 G8 W; T
try {
1 M) f$ a6 M- ]% K modelActions.createActionTo$message1 w6 y* ?2 h( |. ~5 U
(heat, new Selector (heat.getClass (), "stepRule", false));6 G7 P" U1 k+ l1 P; g
} catch (Exception e) {
( `$ g' s4 t1 x4 P' O% `9 E System.err.println ("Exception stepRule: " + e.getMessage ());
- U9 G: z7 O% ? }# O; m1 `9 l; S' ~4 C
4 K: t3 ?% u6 `
try {
, A' |7 X: G+ d0 X Heatbug proto = (Heatbug) heatbugList.get (0);7 a8 b0 b# P6 k! b3 s% d
Selector sel = + z: N# ~$ j9 t# B" q- H7 z/ C
new Selector (proto.getClass (), "heatbugStep", false);- g' _% C( D3 o- l/ L n
actionForEach =( z+ o! m- O' h* l% a
modelActions.createFActionForEachHomogeneous$call B! ~% G% G# S6 p+ u
(heatbugList,5 F8 r+ H. p5 p
new FCallImpl (this, proto, sel,/ M9 h9 b, K' r$ C1 o3 J$ A
new FArgumentsImpl (this, sel)));7 k& e( T2 t, @2 a2 [* O
} catch (Exception e) {' | L7 }+ Q7 Y2 b! x, V4 W- ]9 h' t
e.printStackTrace (System.err);
8 }6 w) {1 u) s: [' y& y& l }* |4 `8 @1 ]" l" }6 B, ]( w
" C, ?6 \# o C8 k6 u9 r
syncUpdateOrder ();
. g1 X, h& F e% M4 u6 T Z
# I: N6 q& b$ Q4 J% N( d try {1 I6 e) a4 A+ p
modelActions.createActionTo$message ! _" f0 ~9 O+ T, K( R+ X4 m
(heat, new Selector (heat.getClass (), "updateLattice", false));& s; a$ C' }6 ]: v; A1 V" Z% l
} catch (Exception e) {
0 E7 O, I$ |: y5 s1 E System.err.println("Exception updateLattice: " + e.getMessage ());
2 y" i, H1 @- o4 A1 Y o* b }
1 V2 T- M4 _8 O- M. n- x6 L
# M% v" I' m# Z" ]2 f5 h p // Then we create a schedule that executes the
" f+ H! L. b. Y5 `3 u // modelActions. modelActions is an ActionGroup, by itself it+ w$ u4 y: N; u8 f4 V
// has no notion of time. In order to have it executed in
: k% |8 d8 }+ l7 w1 O0 f // time, we create a Schedule that says to use the3 [% y) g4 A! V3 f
// modelActions ActionGroup at particular times. This
g0 k( a" O/ M5 Y // schedule has a repeat interval of 1, it will loop every
0 y4 j' O2 T; o* ^( R6 h) Q // time step. The action is executed at time 0 relative to
/ k) J! T$ |4 C+ u // the beginning of the loop." _8 I- K' Y9 ~. y
( o. Y; Q3 M0 `; w8 h1 R* t
// This is a simple schedule, with only one action that is
2 a' a$ i3 E7 H& j4 j/ R6 I // just repeated every time. See jmousetrap for more
: L1 L4 `, }: l5 k1 u // complicated schedules.' ~! ~3 f9 m, }! [5 t5 U
( a0 m5 _0 ^! K- ~" b K5 X _' H
modelSchedule = new ScheduleImpl (getZone (), 1);) d1 t; O0 V5 }* ]- u
modelSchedule.at$createAction (0, modelActions);. `: F9 D) \1 T9 L7 Z
2 D, k! I5 H+ h0 \
return this;
# P( L) [ M0 M3 x } |