HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 r& _3 o' I- H2 m6 x# S; w! g: `% l
public Object buildActions () {3 M0 I+ Y$ A! q( T1 V2 f
super.buildActions();
0 t4 p" y; I! | Q/ I7 a& K + p1 u$ D: a, i, u. v5 _1 j
// Create the list of simulation actions. We put these in \' H2 o8 c8 j
// an action group, because we want these actions to be/ N W2 d' S7 h5 n6 c
// executed in a specific order, but these steps should' F4 p8 B) r0 w+ ~% a& ?' j8 e
// take no (simulated) time. The M(foo) means "The message( r/ F. m, W U" g$ K- a8 E' I* \; m: a/ X
// called <foo>". You can send a message To a particular/ P' M) H* \5 C' t! B, a- V! D
// object, or ForEach object in a collection.
: a* R% W5 G! C3 c/ L
j5 L5 Y( |% h/ F( b // Note we update the heatspace in two phases: first run4 j9 y# }, d- M! K0 R3 ^5 Y, f! M
// diffusion, then run "updateWorld" to actually enact the
* X6 O: l: z7 w3 s' x$ X& q // changes the heatbugs have made. The ordering here is
8 G+ j: }% n4 {, p2 W // significant!& s3 q2 h4 X6 k7 P B: D
( v0 `& x Z1 t! q5 z2 P; b* j# l
// Note also, that with the additional1 o. V& F! u9 f+ U3 B0 _2 t
// `randomizeHeatbugUpdateOrder' Boolean flag we can- b5 I& _% l5 v$ e' F \& U. z
// randomize the order in which the bugs actually run: x; o+ M' r* s7 J8 Z. [
// their step rule. This has the effect of removing any
6 `- o- O- y" d7 d8 b: m$ i5 _ // systematic bias in the iteration throught the heatbug$ s4 q k4 Y3 C* |8 T, C8 M# g
// list from timestep to timestep
8 \' N$ q1 d: `4 S0 I2 d
/ d! z4 s' C0 V# u // By default, all `createActionForEach' modelActions have8 w& E' W8 a+ W0 K& V
// a default order of `Sequential', which means that the7 B3 k _6 o1 f/ t9 |. s
// order of iteration through the `heatbugList' will be
* F) X; S2 K0 w, R, y( y // identical (assuming the list order is not changed
3 A ~5 R% C2 A5 v& p2 U0 g // indirectly by some other process).) J+ J9 U3 @3 Y3 b' w
/ `8 L# U& m+ U. [* ]
modelActions = new ActionGroupImpl (getZone ());7 c- x: C9 m$ z% J/ c
, F' ]6 H) {" Z( t4 o
try {
# _, T4 {8 @! E( B2 w modelActions.createActionTo$message
/ d5 J% J, A3 T/ S (heat, new Selector (heat.getClass (), "stepRule", false));
Y# c, E; K% |1 J- F' A8 l } catch (Exception e) {, [0 F8 J9 j& F' R+ I
System.err.println ("Exception stepRule: " + e.getMessage ());, P1 b a. P2 h* K* J5 d
}
" D# q% }2 U- O4 O) T8 ?+ { [0 c: g7 ~# i( Q, ], {
try {
7 ], F2 d6 U2 R( r( h+ d& l2 e5 T3 l" X Heatbug proto = (Heatbug) heatbugList.get (0);/ V- T4 ?4 g9 J S1 ~5 Y! D+ a Z
Selector sel =
@3 @" r7 E4 _6 ^7 \3 a Q new Selector (proto.getClass (), "heatbugStep", false);+ H* A! Q. g+ \ V& B
actionForEach =% ?7 _# C9 g; A6 y
modelActions.createFActionForEachHomogeneous$call. E0 U' _0 [: Y
(heatbugList,
) g: X$ c! K: D: k new FCallImpl (this, proto, sel,4 R5 G4 ^; ]) p% X4 U- i
new FArgumentsImpl (this, sel)));8 M6 S! D2 ]7 P/ f! N w2 J) w( N
} catch (Exception e) {
0 _( U3 u# K7 r6 } A9 Q& H7 y2 x e.printStackTrace (System.err);0 K7 r" Z: H: v/ c
}0 o$ w, k( H* r
# E% f# b3 x" Z
syncUpdateOrder ();
1 A& O- d" \/ h" U
4 n2 g0 j( x4 g) M. ` try {
! y* w2 ]+ g5 D& o+ T* _0 N0 r modelActions.createActionTo$message 2 w% T) E3 r2 b. k! m# E: X; N% O" M- j) p6 `
(heat, new Selector (heat.getClass (), "updateLattice", false));
. l) n3 _* x+ i1 A% R5 m$ n } catch (Exception e) { @. @) @' K5 Y3 g: f
System.err.println("Exception updateLattice: " + e.getMessage ());
* N* W" C. ?, s( o4 H4 R+ S8 g }! Y# X+ K1 T0 L
+ S# U# z- N) v5 c1 H // Then we create a schedule that executes the
# l) i3 v# f f) E5 ^+ Y; @ // modelActions. modelActions is an ActionGroup, by itself it- P0 E! e- F6 ^5 Y- P" S$ P
// has no notion of time. In order to have it executed in
7 j/ W" j8 ~' ^4 Z D5 D // time, we create a Schedule that says to use the- p T/ d4 P) ^. H! m
// modelActions ActionGroup at particular times. This) g- _( d$ Q6 m
// schedule has a repeat interval of 1, it will loop every! l# a, F, v. M- I
// time step. The action is executed at time 0 relative to; s: ]+ c- d/ q# b+ O F" y
// the beginning of the loop.7 H4 s7 B' P* a9 V; N( M. ?
# W2 X# ^( ^( Q // This is a simple schedule, with only one action that is1 z1 R% ?3 B( }. Y# N
// just repeated every time. See jmousetrap for more$ l6 I( k" H, d/ f, L' K
// complicated schedules.; ` \% D& \8 Q4 G
* E+ f( t; c& F, j; R$ q! X modelSchedule = new ScheduleImpl (getZone (), 1);& y2 C, a, s7 u
modelSchedule.at$createAction (0, modelActions);
" N7 O* M. j# i$ w& L) N& ~/ q- D
5 D. }7 Q: _8 r* ^& w$ ~1 E! i return this;
3 Q. M' Y4 M! q; c6 `/ {5 f; I! q } |