HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 m& c2 B1 j3 H
9 Q2 x% u( s8 u! g4 l
public Object buildActions () {
; I5 d9 \$ d( \0 p super.buildActions();2 k; |6 @ I4 i
: t$ K& y9 a- @" o! V // Create the list of simulation actions. We put these in) `/ d9 ^2 O% C( [& A* |9 x; n" i, \
// an action group, because we want these actions to be" h0 T4 c. M2 V- R8 i0 J
// executed in a specific order, but these steps should- ^5 d" m8 m& Y
// take no (simulated) time. The M(foo) means "The message
0 C8 B8 A p. X% m. l9 Y // called <foo>". You can send a message To a particular
$ J( w0 d, {6 b$ W // object, or ForEach object in a collection.6 I( a/ v) C+ `# E Q8 r% M# f5 T
# [# t( L- P, c
// Note we update the heatspace in two phases: first run
0 L/ G* L `* Z' [( v4 \ // diffusion, then run "updateWorld" to actually enact the
T' ]6 k% H+ b8 u" x // changes the heatbugs have made. The ordering here is
$ M% ^8 r$ P# X1 E- B/ o0 E' A2 d // significant!6 V! z1 @) T2 ]/ A3 E; v% y- q
2 e% ~& d" h- t8 }8 R' ^ // Note also, that with the additional( z5 I! K* k( m0 Z( y
// `randomizeHeatbugUpdateOrder' Boolean flag we can3 I( a3 F& d8 U" Z$ I. H
// randomize the order in which the bugs actually run1 i* d+ J2 O* T3 ^4 f" d
// their step rule. This has the effect of removing any
3 p) [/ B# W X Y // systematic bias in the iteration throught the heatbug
6 J( x$ a3 r3 h: S8 x& F2 p // list from timestep to timestep
! v8 |8 D/ x' [ c4 \2 V$ w) O " {$ m) b" U, g# E
// By default, all `createActionForEach' modelActions have
t4 ?! O7 R1 ^: d: I; ~ // a default order of `Sequential', which means that the* F) M& ^, h M1 O8 @3 A
// order of iteration through the `heatbugList' will be, f+ m3 w* W) w1 S" O! d
// identical (assuming the list order is not changed( Q) l) U! y3 s% E4 y- o/ p2 H
// indirectly by some other process).8 o" A7 T7 `. ]) D
' G1 Q, M/ d7 l$ L6 O' B
modelActions = new ActionGroupImpl (getZone ());$ v( S' y. @5 l
7 m- |/ T! Y! o2 s8 K' ]$ W
try {! C3 \+ W9 h% @" B. `/ _' ~
modelActions.createActionTo$message
) B8 I2 E2 g+ T+ z (heat, new Selector (heat.getClass (), "stepRule", false));* ^* w6 |* V& y* \0 N- q: W1 s
} catch (Exception e) {/ i/ [# P$ O# u$ Q9 `
System.err.println ("Exception stepRule: " + e.getMessage ());
7 ~" `- s# }& i/ B# {6 y2 ` }
A+ x7 U: J5 X* ?# j- Z5 R2 Q' q9 O# L
try {
8 G) o. h G: f+ v% v5 p Heatbug proto = (Heatbug) heatbugList.get (0);% s) w' V7 ^2 G2 w g2 J! t% I
Selector sel =
9 M. X7 w0 _2 B' v9 @# @! l N new Selector (proto.getClass (), "heatbugStep", false);* W& |& E5 ~$ ]6 R0 b9 W
actionForEach =1 Q; \+ y2 ?0 Z Q! O' w) L8 i
modelActions.createFActionForEachHomogeneous$call: n4 s. I' t+ t0 S& x% e( w
(heatbugList,7 Q: c9 o- N5 Y
new FCallImpl (this, proto, sel,8 i) c' K/ o1 e. S
new FArgumentsImpl (this, sel)));/ w7 i/ E) z6 \# n) J
} catch (Exception e) {
( x+ A1 d7 y- z7 M- f) `8 _ e.printStackTrace (System.err);
: H3 t1 s5 V2 o$ X& p/ r* f6 u }5 d6 G5 H# H( b, ?9 i& q! J) p7 h
9 r/ k0 W# _3 o# Q7 ^
syncUpdateOrder ();
+ y! c; E. u7 e( m: [- P
$ b# R/ r5 K4 e; B try {0 Q1 L, b" R' x: P! Q( C8 t
modelActions.createActionTo$message
1 _% E& x4 Q7 N: i# u0 { T (heat, new Selector (heat.getClass (), "updateLattice", false));
2 c/ w+ c0 v/ K } catch (Exception e) {6 L" E1 _ h: r0 x$ d
System.err.println("Exception updateLattice: " + e.getMessage ());
' F/ ]/ u" M. g" |) T }
# }5 S) `3 T% }9 |! I, X/ q7 {7 w- F/ g# ~3 B
! Z- C$ L7 D* W. r9 n' g // Then we create a schedule that executes the( I V4 s' [# p6 N1 [$ f
// modelActions. modelActions is an ActionGroup, by itself it
. U7 |* O( D- C. [& o9 t // has no notion of time. In order to have it executed in
( H* O, D7 l* E) o# r7 ~ // time, we create a Schedule that says to use the' B" r$ D) X Z4 u* _
// modelActions ActionGroup at particular times. This9 X) ^9 F# q0 ~% c% h" Y
// schedule has a repeat interval of 1, it will loop every4 l6 G: X* T' U4 [' X* U
// time step. The action is executed at time 0 relative to/ `# a$ c. n: e2 g
// the beginning of the loop.$ p3 z4 j$ g5 K5 v3 F: a; l0 j4 b& J
# R- \$ ?& z/ z Q M // This is a simple schedule, with only one action that is
9 Y1 E5 S7 O5 o0 h // just repeated every time. See jmousetrap for more
) s/ J' w: u2 b; O: `' L+ M, u1 \+ L // complicated schedules.
F5 j( B+ Q$ A& ^- a! h: d& q
7 _$ K: e8 V) s) x" d2 B; o modelSchedule = new ScheduleImpl (getZone (), 1);! Y+ C. _) K2 D/ }6 ~# J- z
modelSchedule.at$createAction (0, modelActions);; _' Q4 a4 t4 I' k5 |; B! g
2 ]0 a8 F& n) F2 T- V return this;
3 p4 Y& @/ H6 S } |