HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:. u J2 S( s4 q& B1 y' K0 A
& J; I, g! z5 _: k' b0 R$ ^" D; U public Object buildActions () {1 q. c9 T( \' l6 Q6 P( `
super.buildActions();) g% ~8 _# L* X' T
% S) j- ^ t. X" {% m4 M // Create the list of simulation actions. We put these in' `: ?2 G# ^! Z3 s: j
// an action group, because we want these actions to be. z( {' k1 o* C9 W7 D0 W
// executed in a specific order, but these steps should
$ f" x! X. Q* k1 E1 y // take no (simulated) time. The M(foo) means "The message( e0 B5 U1 w2 m- Q+ ] t% h
// called <foo>". You can send a message To a particular- m& H, t0 G% _2 V7 U( O8 P, [
// object, or ForEach object in a collection.
/ U* f+ A" z$ G1 V( e. J6 X $ P# Y H8 N7 `; M* R: v7 X
// Note we update the heatspace in two phases: first run
9 i! Z& I2 n! E9 F1 U) a D // diffusion, then run "updateWorld" to actually enact the
' K0 z% U& x% t$ h# c) s! p$ c // changes the heatbugs have made. The ordering here is/ B% w& [) C' D/ I
// significant!; _- d1 y9 }/ E7 R% ]# A
% a4 E1 y$ d) Q/ y
// Note also, that with the additional$ W3 U+ ]( B2 ^. J0 i0 R6 e9 ?
// `randomizeHeatbugUpdateOrder' Boolean flag we can. {2 \0 b" _6 c1 R, q: s( Z7 b
// randomize the order in which the bugs actually run* H9 R/ V: _7 t9 y1 x
// their step rule. This has the effect of removing any
9 f$ R4 H. v! o- F5 G& e // systematic bias in the iteration throught the heatbug
: C) Z' C. [4 H% `4 l // list from timestep to timestep
' Q8 g9 a1 l4 g# W4 I @; P/ I 1 f2 g) T s5 J
// By default, all `createActionForEach' modelActions have" u5 u$ j. K) t* z+ y! i2 R
// a default order of `Sequential', which means that the t" v8 H* x+ D# V$ S
// order of iteration through the `heatbugList' will be3 C( m. j, ~* u2 o, e
// identical (assuming the list order is not changed/ D8 S! f( q1 \) T \
// indirectly by some other process).2 P% `0 a+ ~1 |. p
: d& r }. ]/ q, x# @; K g' ~( h modelActions = new ActionGroupImpl (getZone ());
0 |; r; b7 n' ?0 @: n" C
# v7 V# p- q- L: q# Q try {( I# v" E- q j3 }; q/ @
modelActions.createActionTo$message
, _! e. z c& j! `: B0 }8 B (heat, new Selector (heat.getClass (), "stepRule", false));! C5 l4 U8 I* R, B
} catch (Exception e) {: Z6 P- k* j- M- b9 f% Y" b& V, u
System.err.println ("Exception stepRule: " + e.getMessage ()); Y$ Z, }* U, K& N, l
}
3 p+ [8 ?1 r* a2 G3 @+ O7 V! z& m/ f1 w5 V! V/ m4 d9 \
try {
* Q3 i, D2 p. }5 B( _ Heatbug proto = (Heatbug) heatbugList.get (0);/ l: r2 \) g [# W4 s
Selector sel = 6 q' u. p- f9 _- O) }8 N" f
new Selector (proto.getClass (), "heatbugStep", false);4 W, I2 q. g @4 K! W$ `( f
actionForEach =; s" G, W& H {) I' ~: f
modelActions.createFActionForEachHomogeneous$call4 y! `7 K, a6 T! H: S5 k
(heatbugList,# O1 N! E; A7 q0 d$ C' @
new FCallImpl (this, proto, sel,
3 F7 y) H N: Q+ J5 @% T6 `0 x new FArgumentsImpl (this, sel)));
) g( \9 I0 i4 C- l) v1 u& R* n } catch (Exception e) {
0 \' N: r3 ]/ k- [6 F) x4 C e.printStackTrace (System.err);2 v9 n; m+ R6 N7 K( y9 {8 P/ B, X$ Z
}. ~ T# u; V! i% T
: Z" T6 M; f. F3 t5 o
syncUpdateOrder ();8 ^1 e0 T0 j! t# U
7 i3 v% _8 l5 S! h3 u: j try {
3 G/ a |: ?9 G modelActions.createActionTo$message
S E( @5 @ l5 J" R! Y1 ^ (heat, new Selector (heat.getClass (), "updateLattice", false));
8 j$ I/ U# W7 y0 U } catch (Exception e) {
- d+ i9 s( ~; J; u System.err.println("Exception updateLattice: " + e.getMessage ());4 d4 i& |$ X$ h5 A- b
}
. K+ }: t r1 ~' A& c) U4 ]; ~' C1 i + g( G) a% A' Z9 U, R2 T3 R# Z
// Then we create a schedule that executes the
0 m* V) i# G5 U: G& G5 V& j // modelActions. modelActions is an ActionGroup, by itself it
8 P2 F2 o& E6 L% j // has no notion of time. In order to have it executed in
3 O. D* p% X- p& m& V Q a) ~ // time, we create a Schedule that says to use the! ^2 b$ l" h l, [* y
// modelActions ActionGroup at particular times. This
! H3 M5 |* X6 M" r3 D9 u$ m // schedule has a repeat interval of 1, it will loop every
9 T ^ ?- P- Y j6 y1 ^ // time step. The action is executed at time 0 relative to& X @ E7 _* Q( \% _
// the beginning of the loop.! n4 b( L: w5 f
: e( c1 M+ }9 l* a' y+ m // This is a simple schedule, with only one action that is
1 W. V( ~- ]/ M1 {$ \ // just repeated every time. See jmousetrap for more }: @# n* Q" y. }. Q" z; L
// complicated schedules.
( z5 X0 y K. W+ D2 q6 S/ H
9 C4 P* N: R+ m& g modelSchedule = new ScheduleImpl (getZone (), 1);2 F; `0 F! e% P9 j5 c8 o' q1 S. ^% }: A- d
modelSchedule.at$createAction (0, modelActions);
# b7 c: \) T4 Z$ h
' I; O9 A1 R* p. ]% z/ P8 T return this;
. U: n0 t. l- a) @7 r h/ N, g } |