HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& v$ h' n& f* K S' Z" R! d g; A
/ ]# w1 y+ x0 D% x6 k; \& _, C public Object buildActions () {+ Y2 J! e) }% o! n7 E' T6 n
super.buildActions();; l( y* X( r) i2 a+ j
7 B1 J3 r' Y, q, `
// Create the list of simulation actions. We put these in3 a9 ~( N% a1 W# o8 U: z6 ^
// an action group, because we want these actions to be
, H, Y* l2 |- T( f- a7 g5 w // executed in a specific order, but these steps should
# \. k6 R1 V) ?( P // take no (simulated) time. The M(foo) means "The message# M V5 I/ `& @; X! Z
// called <foo>". You can send a message To a particular7 N* ]; a+ ^* B
// object, or ForEach object in a collection.
* ~$ B4 U* _# X$ R9 M
+ u1 f$ D0 U5 E% ]% J7 [! \( e# Y3 P // Note we update the heatspace in two phases: first run% \0 A# d8 o$ g, `* ^% q! {
// diffusion, then run "updateWorld" to actually enact the
7 k' V+ D E7 P! N // changes the heatbugs have made. The ordering here is5 U5 d" f; j1 |. n0 d7 j- q- t! a
// significant!
: _7 I9 o, {. E * R; Z; ?9 f! o4 O- _+ y$ \ Q
// Note also, that with the additional8 P$ }6 ^8 E: h8 t3 u
// `randomizeHeatbugUpdateOrder' Boolean flag we can) i, V0 u. m$ M0 J% L u9 ^) u
// randomize the order in which the bugs actually run
& Y$ r9 w6 z5 O7 I2 t d // their step rule. This has the effect of removing any
: v* V+ `4 Z9 e0 s // systematic bias in the iteration throught the heatbug
7 I! N! |. G) K1 I6 Z // list from timestep to timestep, E# G0 K2 L' G" r" V
* i# Y5 L1 _' @/ z. J+ Q% _
// By default, all `createActionForEach' modelActions have
# k+ A+ K/ }' S9 N1 y+ }( Z J // a default order of `Sequential', which means that the' s" D) D8 C( p6 Y7 N4 a3 E2 A
// order of iteration through the `heatbugList' will be- F- z: {8 L8 R/ b
// identical (assuming the list order is not changed6 M6 w) j9 ~" ~3 b" n
// indirectly by some other process).
+ q8 v# ^- o& J* L# ?8 Z+ _
9 W/ |; n' m; j7 d, Y modelActions = new ActionGroupImpl (getZone ());
" S5 r3 W4 E- w* \5 S
. Z0 d% i" P$ h9 ` try {
: W' K \+ S; U0 `8 m modelActions.createActionTo$message5 j$ F W0 k3 o1 k) J, S! J
(heat, new Selector (heat.getClass (), "stepRule", false));
G$ I4 r9 A% }! F+ V$ F } catch (Exception e) { H7 a/ W2 l" a0 A& o0 s& O% i' D
System.err.println ("Exception stepRule: " + e.getMessage ());
4 ^! ^. G# y6 z$ e; u }
3 x3 x1 ?1 T% G; K) K3 E* P" e1 g
! y9 o2 y- M" k; f- B& v2 E/ _( q try {7 e" ?* e: `7 i) M9 ~8 L6 _
Heatbug proto = (Heatbug) heatbugList.get (0); u+ |/ f) B3 d# g9 b
Selector sel =
; v6 l) G' R* Y2 }; v new Selector (proto.getClass (), "heatbugStep", false);$ j# Z0 q# ] ^& U: X y" Q
actionForEach =$ ^6 p+ ^6 u/ H' L
modelActions.createFActionForEachHomogeneous$call0 m0 R4 I& Y1 h! H% Z
(heatbugList,7 `1 Q5 P ]8 W7 x' m, P
new FCallImpl (this, proto, sel,( r% c( P/ O5 P; s2 U. T$ b
new FArgumentsImpl (this, sel)));* C) x: d: T! {/ v9 @5 L, |
} catch (Exception e) {
/ |2 ^8 ?: I% Q3 z6 m: ^' R e.printStackTrace (System.err);
3 J% P6 A- `+ C6 ]1 v; M4 m }+ z* L9 G5 h$ e
5 s* e$ q) i( Y7 K; B- j
syncUpdateOrder ();6 l$ }; N" E4 N6 V+ A/ ] T: S, T
" v9 Y5 x$ p1 Y1 P% u try {4 x/ x% D6 c' K! K+ L
modelActions.createActionTo$message . {! A1 f+ h# G3 t4 a' D
(heat, new Selector (heat.getClass (), "updateLattice", false));
+ N* d' S9 y4 `( a$ ]! h' H. H } catch (Exception e) {
( n% N' C* Q! y! @. n3 Y) p System.err.println("Exception updateLattice: " + e.getMessage ());/ e% D( s; G9 s
}% A% g( B, S% v% F& l5 }6 L) p1 a
, ~6 {) B, P! o9 s5 W; j& g1 ? // Then we create a schedule that executes the7 r7 b( c" N0 i# N; B
// modelActions. modelActions is an ActionGroup, by itself it
2 ~5 R& R' I: m5 p8 p3 _ D // has no notion of time. In order to have it executed in
$ v5 d n! }1 V) ]6 [4 P0 n9 v4 ~1 a9 C // time, we create a Schedule that says to use the
" h0 ~& y% Q9 {1 L$ m/ t5 B // modelActions ActionGroup at particular times. This
* {- a. B6 s i3 M6 c // schedule has a repeat interval of 1, it will loop every
# d: T& t/ ?$ n // time step. The action is executed at time 0 relative to
8 {% f2 I2 E5 W2 `- j/ s$ ~' y // the beginning of the loop.$ s* k" Z. o! L7 ~
* S6 y7 f4 a( ?) J
// This is a simple schedule, with only one action that is
6 w/ n- ?0 N4 { x" J" K3 e: n q // just repeated every time. See jmousetrap for more. y% k. u* F/ t+ W% R) ~ K
// complicated schedules.% D& n! e) y3 `
3 Q. W" K( X7 i# J: O! ^: `
modelSchedule = new ScheduleImpl (getZone (), 1);8 @+ _ A2 x2 [
modelSchedule.at$createAction (0, modelActions);* V6 {' K; X! R' l- o$ K* x
0 @" v5 U1 n; F* ~; A& w7 T% [+ o2 \
return this;
4 u2 q* t& ]5 N% V8 N/ d$ ? } |