HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 L: y) X" g7 H* q
' J2 _ `% O- J# n- U
public Object buildActions () {+ g6 s6 l5 Q! I% m) \- U( ~
super.buildActions();
, Q& M& S% m. ~) ^2 Z" i2 z
, p- i) N# c; \- X5 l6 S/ k$ X3 y+ U // Create the list of simulation actions. We put these in+ ~! j9 G+ v, ^" L
// an action group, because we want these actions to be4 M; X5 J2 u3 H1 |
// executed in a specific order, but these steps should
- P. L: u% p& P- | // take no (simulated) time. The M(foo) means "The message' T9 R' P0 v7 f/ }7 U1 t) i
// called <foo>". You can send a message To a particular
* K" U1 r* d$ e4 {7 G // object, or ForEach object in a collection.
& L* `) ?9 X1 Y% J% f
, _- i2 m' V4 Y4 S9 p" p" J // Note we update the heatspace in two phases: first run. e! l$ A+ m; n
// diffusion, then run "updateWorld" to actually enact the
' Q4 `/ `# f- H% p // changes the heatbugs have made. The ordering here is- h, C$ o" J* a# F
// significant!, t u5 e$ K2 E( A5 s. s4 ^
6 P4 Z5 x1 b; ~' }6 g6 q! [
// Note also, that with the additional% ~. m4 c+ \5 ~+ e
// `randomizeHeatbugUpdateOrder' Boolean flag we can
k9 ?4 N- w: ~- x6 i0 c+ G" _& b // randomize the order in which the bugs actually run
* B6 J) v' K: ]2 m# z; s7 H // their step rule. This has the effect of removing any8 u" [ T1 t" F! j" H
// systematic bias in the iteration throught the heatbug& ?# L- X" O% a' T
// list from timestep to timestep* u I/ { b! Z* Z" J0 [ G, `
0 _( K, \: u3 Q& X4 o" o0 a
// By default, all `createActionForEach' modelActions have# C$ a+ {4 ^+ E5 d
// a default order of `Sequential', which means that the. R j- }# T' `: E7 O
// order of iteration through the `heatbugList' will be- w$ T; a" R; g6 ?
// identical (assuming the list order is not changed1 c2 u! `' e2 K u/ M6 W+ X: K
// indirectly by some other process).
& W2 B, n: @1 b1 U; s
1 C, x3 T9 }9 J' b( f G( _ modelActions = new ActionGroupImpl (getZone ());
* h% m1 m) u- c! H/ a f# J: T& i7 N4 r0 G/ d
try {
8 L" a& x0 y. L modelActions.createActionTo$message2 T6 D0 G$ R2 w: q' y
(heat, new Selector (heat.getClass (), "stepRule", false));/ ]0 I5 S2 [" U2 @
} catch (Exception e) {" N' O9 q' _! Z8 s" Z. i+ ^
System.err.println ("Exception stepRule: " + e.getMessage ());
b$ A/ R2 |; X }
% E" p: b# w* h' Z
: @1 l+ @% |9 j try {4 Z; N% Z6 D& X
Heatbug proto = (Heatbug) heatbugList.get (0);
4 P' P( C* d& Y Selector sel = " [& R0 J5 J. ~% y
new Selector (proto.getClass (), "heatbugStep", false);
4 ^( y4 v7 i7 I' V C actionForEach =' N4 O: D4 E/ B8 a
modelActions.createFActionForEachHomogeneous$call
! m: e( L( `# L/ D2 Q' e (heatbugList,7 A. B5 V' B% e/ }
new FCallImpl (this, proto, sel,
4 u. I( D- `% u new FArgumentsImpl (this, sel)));+ n- ^. r" e( X1 I$ P
} catch (Exception e) {* a7 G& R) f3 @& v( y& Y2 L9 f& |# r
e.printStackTrace (System.err);
G; q2 Y" e E7 G( X8 I2 I }
7 i+ v' W) k6 R- q& W " F0 E# X9 n! z/ J$ z" @- _" E J2 p3 } M
syncUpdateOrder ();, y! v3 ^! M. z' ]) {5 j
$ q2 G! k2 g; N; _; z' x& |2 G: K try {2 j. X) j, E- y# e4 g# l7 E
modelActions.createActionTo$message 7 t4 U! i. m8 D& v3 o3 C/ E3 R( `
(heat, new Selector (heat.getClass (), "updateLattice", false));
$ n! c- G. Q: t% J8 i _ } catch (Exception e) {( M0 A1 e$ M# h/ C2 f
System.err.println("Exception updateLattice: " + e.getMessage ());: d$ x0 V# x3 ~* O. {
}
8 q& o3 ? U2 ^: _; s
8 {! `* n8 H" L. Z* c9 ]( h // Then we create a schedule that executes the/ i" H( l0 ` k9 ^4 a0 x
// modelActions. modelActions is an ActionGroup, by itself it
6 `7 k3 n- q! K6 S# [ // has no notion of time. In order to have it executed in; R7 T$ b1 S. b, J) G- {9 e
// time, we create a Schedule that says to use the
1 J x+ \/ P S) Q( q s9 Z$ k) L8 z // modelActions ActionGroup at particular times. This
$ \4 r0 d' r2 {! z: ^$ V // schedule has a repeat interval of 1, it will loop every
4 y5 r( `2 j) N& Y // time step. The action is executed at time 0 relative to
( F* V; r7 | D$ K3 R* s // the beginning of the loop.' o- d/ @ Y" L `
* U' k. u& S% H" u& D! k // This is a simple schedule, with only one action that is) ~" J; Q- K. Z
// just repeated every time. See jmousetrap for more' e" @ N' K* v
// complicated schedules.; F( N% \; }4 O( J, w
+ `: ~8 r' R/ d# }3 R. y modelSchedule = new ScheduleImpl (getZone (), 1);
* z, {9 o7 ~ L) I J, _: _ modelSchedule.at$createAction (0, modelActions);
6 O9 _% J ]& z$ ]: L
8 o# h0 s6 k3 n) ^* D return this; H# T$ O6 ]) P: |/ p8 D3 I
} |