HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% [7 S- y( P+ P! H, [/ _
4 o S& a- ?9 n
public Object buildActions () {
& x4 \) L5 ~7 Z! y6 t! i" C super.buildActions();6 k2 Y( M: u. a- f! M; {
4 o0 z" B7 P7 F
// Create the list of simulation actions. We put these in
; Y _; A+ R7 h; k0 U // an action group, because we want these actions to be
2 O2 R( k# g p6 y* i; { // executed in a specific order, but these steps should( z8 i; \% |% U v% i6 l7 k
// take no (simulated) time. The M(foo) means "The message' k |3 i+ e, `0 V+ A; J
// called <foo>". You can send a message To a particular
3 k U& t( u6 Z7 u1 R% g // object, or ForEach object in a collection.3 t' J6 d) }9 P4 ^! N! n& T: t: O
; _4 ^2 {8 p' Q& f" M // Note we update the heatspace in two phases: first run z: b1 E+ H6 Z+ g5 w; z' g
// diffusion, then run "updateWorld" to actually enact the; K/ o' F4 i5 e- p
// changes the heatbugs have made. The ordering here is
/ H1 b; p7 G4 g) p( X" e2 { // significant!
$ J3 Z1 `: i. J6 i" P% F9 F2 V & X. \- c" B( w: C# d/ T
// Note also, that with the additional! X! ]# e' S1 C4 }7 D
// `randomizeHeatbugUpdateOrder' Boolean flag we can4 m- Y( \; X" q5 s
// randomize the order in which the bugs actually run+ o+ ~% o/ o5 ~* u* L @( r4 T0 f3 N
// their step rule. This has the effect of removing any- d9 k' U( n) R- G c: _
// systematic bias in the iteration throught the heatbug
/ i+ h6 Q. S+ u3 Z; o5 p, c // list from timestep to timestep P) {+ Y3 V) d9 S( p0 a9 a0 _
7 x* J5 a" V# T, F9 X
// By default, all `createActionForEach' modelActions have
; W1 I2 w2 Z% A# U3 q // a default order of `Sequential', which means that the8 q4 o1 k& K% k/ a6 W" ~
// order of iteration through the `heatbugList' will be
" E" a! e6 c2 b- m4 u // identical (assuming the list order is not changed
7 r( j' E+ u! p' o/ E* o! T // indirectly by some other process).8 x) k% ~" @8 Y& f+ { |
! p7 G, A3 E$ h" u) H7 q( V% \ modelActions = new ActionGroupImpl (getZone ());+ s( S0 K' }. N2 j
7 y1 [# w; _! n! a3 d try {8 R4 l+ M* C' Z# y( m8 U) u
modelActions.createActionTo$message W4 k. s7 I5 t9 L" O
(heat, new Selector (heat.getClass (), "stepRule", false));8 e. R( A9 q% Q; q
} catch (Exception e) {+ S" Z6 C6 L3 v) l* o1 \, q( g3 y
System.err.println ("Exception stepRule: " + e.getMessage ());
$ H$ l/ b, `7 x0 T2 H1 b$ ` }/ u; M+ q) Z9 N+ z
$ U; E: ], A7 ]
try {
6 m) }( d+ t5 L9 m6 F" t Heatbug proto = (Heatbug) heatbugList.get (0);
6 B! G7 o: i- }2 o* \. I) r, o! X Selector sel = / |+ D* K4 f/ R9 G. s
new Selector (proto.getClass (), "heatbugStep", false);
- }2 v6 V5 q; ]. k' C; N0 C actionForEach =2 s$ h! F& ~6 ^! S' q ^! a
modelActions.createFActionForEachHomogeneous$call& ^2 M; W1 H/ b& Y, l; f& _; D/ o
(heatbugList,$ u/ U g7 m- v: {: _. V8 C
new FCallImpl (this, proto, sel,$ Q( y4 a9 P9 R/ e9 V1 y
new FArgumentsImpl (this, sel)));8 ^' k: h( F: D/ V: U
} catch (Exception e) {; T6 W4 _; R" Q
e.printStackTrace (System.err);' V: L) c% C1 n
}
4 \2 F& @( `6 q8 y " A, H' ?/ A. |5 _8 G
syncUpdateOrder ();
, t7 R% ]( ?7 X
' \5 A" I/ I7 u$ I- f try {- S* \) |0 Q8 G+ j
modelActions.createActionTo$message 5 h. F. r) l% ^+ U8 Q
(heat, new Selector (heat.getClass (), "updateLattice", false));
% V9 P& X9 x. y% m' r, o } catch (Exception e) {
3 q! g( v# ^' q# ? System.err.println("Exception updateLattice: " + e.getMessage ());
2 A' J Q* l0 |- q) O: Q9 `5 }3 Z) \ }8 Y4 B6 N; j; ~# n6 i) b: b
9 B+ F* U$ Z4 K/ r // Then we create a schedule that executes the
6 N" d" j% k0 T2 N // modelActions. modelActions is an ActionGroup, by itself it% _0 n! ?' p" s; s5 Z
// has no notion of time. In order to have it executed in) W3 P, t/ ]; |0 w) r* y4 s5 y
// time, we create a Schedule that says to use the0 U2 @0 ^3 _/ J, \: h
// modelActions ActionGroup at particular times. This. w* ?8 S4 l2 {9 H0 e' e
// schedule has a repeat interval of 1, it will loop every3 Y( x; N5 [# `
// time step. The action is executed at time 0 relative to+ Z2 Q: h2 r4 i7 p4 s, y( M. k; G: [9 q8 M
// the beginning of the loop.
( @6 n# `: p4 U( J! s, q+ Q7 n- h
// This is a simple schedule, with only one action that is
) {' E2 O3 O" k // just repeated every time. See jmousetrap for more; f! U) M2 p, P: G
// complicated schedules.
; o4 V) P. W7 ?, U% v
0 h# F, }! O; y" O4 q- Z modelSchedule = new ScheduleImpl (getZone (), 1);
+ ^: ?/ P* |5 ?5 i _# H. E: ` modelSchedule.at$createAction (0, modelActions);
& n; e I$ L; o5 M/ I* V
M8 U5 V8 f4 t) V& ^ return this;
4 u: U. Y6 q7 [, e# i* P1 [ } |