HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
{1 e! R$ C: N! N* `2 ^8 U& R0 `, |" c9 R" Z5 I: ^
public Object buildActions () { r: U- F n; e6 o) s& C
super.buildActions();
# Q) j+ e' @( i [/ O" o 2 g4 |. F; D2 j0 W% X. r, z1 }
// Create the list of simulation actions. We put these in+ B: w& p0 S& S+ Z# |7 Y) E
// an action group, because we want these actions to be
o6 q& S1 k$ H- v. d // executed in a specific order, but these steps should: N! @* E8 M1 B `+ _
// take no (simulated) time. The M(foo) means "The message" P2 k2 p- u0 K w
// called <foo>". You can send a message To a particular( j6 o& s8 C3 m. M V( u
// object, or ForEach object in a collection.6 {6 G8 I2 Z" m g& ^: |. {$ F
& \# Y% L& u J0 i/ Q // Note we update the heatspace in two phases: first run
6 E3 z \& P+ S3 k // diffusion, then run "updateWorld" to actually enact the2 E4 c7 z5 _1 r s' q" @( r
// changes the heatbugs have made. The ordering here is
! ~+ v$ p, }; s, k* P5 p% g( u3 k, D- j // significant!
/ i' L7 N r6 I# K
) V( O* R5 m8 |0 ]/ ?" N // Note also, that with the additional
6 z, N; X& n& ]4 |9 _- f% c // `randomizeHeatbugUpdateOrder' Boolean flag we can, r1 A( h! C, T" g. g
// randomize the order in which the bugs actually run
# e! J, s, G# Q" K5 D) p // their step rule. This has the effect of removing any
3 m Y+ ^2 p. e9 E, S; t // systematic bias in the iteration throught the heatbug, U2 g! [) d* `# W
// list from timestep to timestep
3 H3 {0 W/ ~" ~4 l2 n1 a/ G
5 d9 c) p9 l, y* }9 ?, v) n# ? // By default, all `createActionForEach' modelActions have
' u! c; [# I4 l2 L; y# C // a default order of `Sequential', which means that the
% Y+ l" ^% @6 I8 D9 e // order of iteration through the `heatbugList' will be
' H, Y- s+ \) m- G, j, a- B // identical (assuming the list order is not changed
5 J0 h& F4 @ m- L2 p# e. E // indirectly by some other process).' a. z. F; d% ?% F! I- B
+ r' |* q3 y( P! B modelActions = new ActionGroupImpl (getZone ());
3 l4 C6 W$ [6 p9 m i4 C: f: g
: F% r) n9 p( w4 J$ z! k6 b, a8 q try {& S% ^2 H' Q$ C- u# f6 Y5 M* ^
modelActions.createActionTo$message. K% \! B! S E. E" L$ d
(heat, new Selector (heat.getClass (), "stepRule", false));
, I, Q! y% `3 s1 a, Q } catch (Exception e) {
( |3 E/ K' [; c5 U System.err.println ("Exception stepRule: " + e.getMessage ());
- W& L5 U d) w1 S }
f& m, ~0 J4 K2 H; @
7 l( U& @& s- M0 [, V3 y& x) l/ Q) | try {
0 A3 G, ?' F9 X0 s% r y Heatbug proto = (Heatbug) heatbugList.get (0);# N& c2 Z8 \1 v
Selector sel =
" w2 _+ \4 i) ]6 p2 m new Selector (proto.getClass (), "heatbugStep", false);& f( `6 W/ W, X
actionForEach =
# Z% J. h2 D+ K$ S9 K' G. l7 k modelActions.createFActionForEachHomogeneous$call
7 u2 v, p! P. M. u! N# s6 z3 q7 ~ (heatbugList,) Z; e; O% }; K
new FCallImpl (this, proto, sel,
1 t& ^" D( I$ w( A/ H new FArgumentsImpl (this, sel)));' {# p( @8 V* P% p# q
} catch (Exception e) {6 O3 u9 k/ l4 X3 P, Q: _
e.printStackTrace (System.err);
7 k! Z$ s+ z: C9 g6 \ }
4 R( U, W+ S |4 _' Q2 q
& p* [. ?& @7 v2 G& H8 w% y syncUpdateOrder ();: Y: c+ R: u# {$ C2 G. n
1 e5 k& T1 q/ o
try {4 r3 h5 ?4 t/ Z! ~
modelActions.createActionTo$message
# h$ D% u5 W6 O% l' W! J5 a (heat, new Selector (heat.getClass (), "updateLattice", false));
4 p% h: d% w' `/ H } catch (Exception e) {7 G; g- g' w# `3 R; T
System.err.println("Exception updateLattice: " + e.getMessage ());* [+ ~4 d! y( A6 \
}. C( V8 M( J. b3 N
. d# A/ [# {& U9 O0 l9 i* T3 \+ o8 [% E5 H; N
// Then we create a schedule that executes the# p# o/ X3 u' v7 O. Z$ I2 l/ ^: [
// modelActions. modelActions is an ActionGroup, by itself it
$ p6 P$ c/ Z8 h4 J, b // has no notion of time. In order to have it executed in
. N! {1 F. |9 ?$ G // time, we create a Schedule that says to use the, h0 r+ U& S. a/ U( J
// modelActions ActionGroup at particular times. This" \7 Q ^1 X. j5 ?/ Q8 p! [
// schedule has a repeat interval of 1, it will loop every
6 F5 Y H3 X; P8 f; _ // time step. The action is executed at time 0 relative to
: [ A. m% }' F1 ^& }' Q // the beginning of the loop.
. X0 H0 F5 [4 S! e2 Z* H
- A4 q' Y) f6 ^8 k // This is a simple schedule, with only one action that is5 |( g2 \& @5 t9 O0 i T
// just repeated every time. See jmousetrap for more
" S2 T* V# d7 D$ w // complicated schedules.+ n7 T. \" U% t) {" D% g
B7 G% r0 V' E7 V1 J2 x/ r, P
modelSchedule = new ScheduleImpl (getZone (), 1);
( ~ S( C7 E; U j: l modelSchedule.at$createAction (0, modelActions);9 v3 `7 Y/ W( d
9 t F" J3 B- |$ X: Q6 e) |# ?) w0 \ return this;
( i3 n8 {* T/ z% S- B% S% T i } |