HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 }/ A( o: g; t( _
) }9 {7 \, j. l, y& i2 j public Object buildActions () {4 Z; a9 e* u# |/ q$ ^
super.buildActions();
1 [* T) ?. f2 J: i. v- u % q1 q' z0 ?8 z; c/ L
// Create the list of simulation actions. We put these in' @; h/ u! ^! C/ a
// an action group, because we want these actions to be
1 o8 f+ i! y( V% c9 U; [" b5 X // executed in a specific order, but these steps should" ^# V# m/ r/ G: ]/ P* F; r& Q
// take no (simulated) time. The M(foo) means "The message& \ n, D9 y( h' C V4 }+ ?7 u: Z
// called <foo>". You can send a message To a particular+ o3 ?2 e J d, ~0 i
// object, or ForEach object in a collection.. V$ I1 V9 n' ]( m$ Q
( J- h) b( T+ d+ v // Note we update the heatspace in two phases: first run
0 u4 V8 h5 [- K' F, Y // diffusion, then run "updateWorld" to actually enact the
. f2 c# E8 K; V# P // changes the heatbugs have made. The ordering here is
" h0 @% W% C4 Q/ D Y; i7 s // significant!' N( J _- K+ I1 S' v' d
3 v. w; m( }) Q5 p4 l3 y' N2 B
// Note also, that with the additional9 J4 |0 f9 L% v0 ~
// `randomizeHeatbugUpdateOrder' Boolean flag we can( q5 ~! N) g, s0 \; m2 T) V
// randomize the order in which the bugs actually run
3 h: v) O0 H/ C // their step rule. This has the effect of removing any2 V8 O' ]6 Z6 d# \% l" c" x
// systematic bias in the iteration throught the heatbug7 K* h r4 f$ m( O* L% h) l
// list from timestep to timestep0 b8 T: p: l8 I( Z4 L) S* L
4 E% j* ]" `( P- f' Q I, _
// By default, all `createActionForEach' modelActions have7 z( K4 \( X J5 z9 t. ^
// a default order of `Sequential', which means that the% G$ ?! g& j e/ w( I# S$ y7 l
// order of iteration through the `heatbugList' will be7 v2 V) m% g# J
// identical (assuming the list order is not changed
6 L. K9 U- c( U# A1 x // indirectly by some other process).
8 |0 c+ d# Y) X; B4 w$ s7 h z5 ^: [* ~" w- t. [0 ]8 N/ o
modelActions = new ActionGroupImpl (getZone ());- D0 t4 n" `, T& o6 _! w* }
' j" Q$ P+ v, y v8 T6 X
try {
k6 \ k7 E$ z5 w' j modelActions.createActionTo$message
, w# ?( K0 b" c& F. \ (heat, new Selector (heat.getClass (), "stepRule", false));, c5 T( n4 c( X! A* s. l
} catch (Exception e) {4 ^4 D" m( C! r
System.err.println ("Exception stepRule: " + e.getMessage ());
. W3 D1 [+ `" [( R% X: `5 y }; \: D- N2 `# v8 p0 {2 N. w
3 z4 n2 w9 M# B. A" u7 O$ l
try {
/ r [ E- v* |- P! v3 H Heatbug proto = (Heatbug) heatbugList.get (0);
! [# e- Y$ H: B/ ] Selector sel = 8 g4 \8 |! x; }+ A) ?0 o" y
new Selector (proto.getClass (), "heatbugStep", false);
# n% N2 \5 \* n+ k$ ] actionForEach =
" [0 l. G* N+ f: {( x/ n modelActions.createFActionForEachHomogeneous$call
, R2 Q: S% }2 C3 c (heatbugList,0 z, d' Z0 F! S0 q% ^+ F" t( ~
new FCallImpl (this, proto, sel," Z( g$ j* L! }& p* Y+ i* B$ s8 w
new FArgumentsImpl (this, sel)));
) m6 C7 v+ S: C( ?5 G- w- h) [# | } catch (Exception e) {
; C6 A5 N8 m/ I1 P* v e.printStackTrace (System.err);
( e2 c* L" w9 G' T$ } }
* ]7 ^) I+ o+ A+ ?1 |. ^ * @, s5 F7 ]% S- J3 f4 r
syncUpdateOrder ();4 g5 b4 @1 \3 V# m" C+ R; w
8 k" T; ]* E: L% _4 `$ e: x, w# Y try {( ?/ e% t: G W' V
modelActions.createActionTo$message
; b- n5 t Q5 e) ^5 l+ Z (heat, new Selector (heat.getClass (), "updateLattice", false));9 w6 n% z8 C# [4 ^
} catch (Exception e) {
# U e& @7 E# g System.err.println("Exception updateLattice: " + e.getMessage ());
3 F0 s7 ~' }, N }6 H; W- \# n9 ^2 A
9 U! ]: V. W- l( ~( o" i9 w! G
// Then we create a schedule that executes the1 ]4 {; S: H' W
// modelActions. modelActions is an ActionGroup, by itself it- d: t0 R( S, C/ B$ ]: q
// has no notion of time. In order to have it executed in
C0 S6 t1 P& l, s // time, we create a Schedule that says to use the# |" { z: S% D. S" _3 N0 M7 {4 F
// modelActions ActionGroup at particular times. This
9 j/ I% z5 k% x+ @4 G: ` // schedule has a repeat interval of 1, it will loop every( S& y9 @7 M) `6 `: b
// time step. The action is executed at time 0 relative to
: o. w& ?4 B& q" u // the beginning of the loop." W. d% U& l! K) {4 y/ M
" A1 h; x o: g // This is a simple schedule, with only one action that is
3 B; S% j @6 O5 _ // just repeated every time. See jmousetrap for more
0 W* t% V6 b* K9 a6 ?2 H4 I // complicated schedules.
, t# G4 D0 J( k
7 F. c4 R1 z2 s- F) @$ k modelSchedule = new ScheduleImpl (getZone (), 1);% E: N; ?4 q L/ |6 x
modelSchedule.at$createAction (0, modelActions);; V" ~' U, d2 T7 P
) R" f3 h# l- J: t1 R return this;& ], O' a7 T% q7 i
} |