HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 w8 r0 d4 x9 K$ z/ U* g" |, J
* n; ~: s4 j$ [* W* I1 G( k public Object buildActions () {
3 V, A5 S, S4 O( R- ~, U) b super.buildActions();
( e9 K) f+ Y. L2 a8 f
" r. Y0 {( X: @! J3 q. V l // Create the list of simulation actions. We put these in
/ i! H$ u9 F& S0 M // an action group, because we want these actions to be
2 ?. @5 D6 _2 ]* D c% e // executed in a specific order, but these steps should4 y2 H0 k+ y- X3 t7 _0 `6 C' `
// take no (simulated) time. The M(foo) means "The message1 J( k) @2 n5 R3 @
// called <foo>". You can send a message To a particular0 b# B c( v d
// object, or ForEach object in a collection.0 R& C' K' B9 d* _4 f# m% @4 i" e, }
. z5 {" Z s/ ^. k. _ // Note we update the heatspace in two phases: first run" ?7 _& ]! l0 g& s
// diffusion, then run "updateWorld" to actually enact the
! A ]5 u/ h: L+ G0 A4 n4 J t // changes the heatbugs have made. The ordering here is
& L) l) I, E) E5 t- _7 I. C // significant!
1 I2 J! a" _3 M1 n0 [$ _ # x$ g8 D4 E% @: X0 N! O
// Note also, that with the additional, U3 D: n2 Q7 [+ j
// `randomizeHeatbugUpdateOrder' Boolean flag we can& p" P4 z; m8 p. \! G
// randomize the order in which the bugs actually run1 a, K3 Z( l9 @8 `1 m) _; c
// their step rule. This has the effect of removing any) G2 b7 l2 e5 o
// systematic bias in the iteration throught the heatbug
- Z. U) g2 j A* _+ _1 L // list from timestep to timestep! \' [1 S( X8 a, }& \' J( W
& k, z0 i0 V: A( }
// By default, all `createActionForEach' modelActions have
) k$ S& i v3 B2 r. F. r! w# z // a default order of `Sequential', which means that the
3 o, ^$ r2 D7 J1 t/ K+ H- M // order of iteration through the `heatbugList' will be
8 K# g, L/ J' F; x5 C8 l // identical (assuming the list order is not changed
0 s- P @/ ]" r* _5 k" y // indirectly by some other process).
( Y1 |9 O4 i" {5 L/ M$ e7 ^ ! O$ D- |$ C' e. C
modelActions = new ActionGroupImpl (getZone ()); f/ W/ X/ A& k+ L% E) W: P9 D Q1 ?
# f( L+ t: l4 }5 Z try {
- O% p4 s8 h9 \1 N# v1 a; j( f modelActions.createActionTo$message; W7 W' Y* V& Z) p1 s8 H1 l z
(heat, new Selector (heat.getClass (), "stepRule", false));3 s; E4 a) O4 G3 y; J8 Y3 V* s3 C7 P
} catch (Exception e) {
, W8 r6 I8 C5 ~6 U) i3 ?5 i4 ^ System.err.println ("Exception stepRule: " + e.getMessage ());
8 n- ?# k( S h" f# A }
0 E9 S' ~6 E) S% i! i8 B4 A5 ^
) h7 \7 B6 Y7 Y try {
: U# c* |; i+ k( u# ? ^ Heatbug proto = (Heatbug) heatbugList.get (0);* h3 T1 v g* m B% P! w! J. |
Selector sel =
: Y: @+ ~, H/ }2 @9 X/ Y) H' |- q new Selector (proto.getClass (), "heatbugStep", false);
6 t+ G; i9 o3 h b7 H9 Y# P actionForEach = x/ \! n* h" @. Z& n& _" w
modelActions.createFActionForEachHomogeneous$call) Z8 f6 q, ^/ C; A" n
(heatbugList,
6 P1 p) E7 ^& r% Q new FCallImpl (this, proto, sel,+ e8 t9 w/ E, T) _' |6 N2 O" m1 B
new FArgumentsImpl (this, sel)));3 C/ R+ C* o) g: h8 c/ `
} catch (Exception e) {1 a. x) {& X& c7 g9 ~7 j
e.printStackTrace (System.err);
9 T( e% z% Y" G: d4 A }1 {, G9 j Y" R- k$ R9 o
6 d$ Z4 D8 R F$ V6 r; @ syncUpdateOrder ();
' }' `3 l: o1 ^8 r, G+ I$ m( ]7 H2 M- B& }
try {, i2 D# u5 X* L
modelActions.createActionTo$message
) O( N7 u# P8 ^, Y: |6 L# { (heat, new Selector (heat.getClass (), "updateLattice", false));& B9 _ _: y% t- T2 |# [, ~
} catch (Exception e) {
. M# ~. \, v7 R6 C/ y1 E' _# R System.err.println("Exception updateLattice: " + e.getMessage ());
! _: a7 G) n4 T4 ] }
$ z- G* A4 c; Y- _ 2 D& L4 B$ z9 U. b1 _; g
// Then we create a schedule that executes the
7 K0 N# V5 Z' S // modelActions. modelActions is an ActionGroup, by itself it3 b$ T1 T8 m: Y4 p
// has no notion of time. In order to have it executed in
# d5 j) { Y* U! ]% d( c0 ~9 Q // time, we create a Schedule that says to use the
, b0 T* A; C( [ // modelActions ActionGroup at particular times. This
^# ^3 ~, ^) H0 R" {5 `% A3 w // schedule has a repeat interval of 1, it will loop every
u5 @& Q7 @) [3 i4 j7 `7 }& { J; C // time step. The action is executed at time 0 relative to
8 K! i4 w) [" D4 r2 O1 N // the beginning of the loop.
) N e( j& A1 T9 R+ P1 h3 s H
7 N" p+ ], m8 K' V7 A& h, B4 v( C // This is a simple schedule, with only one action that is; E6 s3 o" T: f# j, l) D7 Z5 v
// just repeated every time. See jmousetrap for more* B' I' q* v c3 y4 O, |. s6 {
// complicated schedules.3 w5 v) e1 k" C
2 q* S0 T& s* x. v% d: k modelSchedule = new ScheduleImpl (getZone (), 1);9 G7 m% V ]! g. o
modelSchedule.at$createAction (0, modelActions);
+ G7 R4 A( c4 x9 y7 n$ W7 C
8 W& b5 `- X) p return this;; f; o0 o- d9 r _
} |