HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; h0 l6 v I9 A7 q
) w. `5 p+ ^% i' ^ public Object buildActions () {
5 {# G! g5 l; A super.buildActions();
( [! o% B# v: N' _( o$ b
2 o0 n9 p# m. V9 d% D // Create the list of simulation actions. We put these in. y# _" K3 Q# U" o
// an action group, because we want these actions to be, g6 E( u8 _" d4 j4 B$ t$ o
// executed in a specific order, but these steps should
% }0 P, z1 t2 C+ E: @ // take no (simulated) time. The M(foo) means "The message7 T( J7 B! H0 [. P
// called <foo>". You can send a message To a particular
8 m& t0 l# }, }2 H // object, or ForEach object in a collection.
( r% ~* |' T2 E6 u% ^% S( ] , g5 u' X7 I4 ]2 c; m0 Z) Q
// Note we update the heatspace in two phases: first run
6 J! G0 C$ r5 E9 P# B7 `7 Z // diffusion, then run "updateWorld" to actually enact the
. ]1 O( E2 |9 ^/ I: m$ Y // changes the heatbugs have made. The ordering here is! d- H. ?6 O9 O* }1 N/ m
// significant!# J( D) j3 i! k
+ F; b; {, ~6 g( f
// Note also, that with the additional
2 O* B1 M3 f! Q3 j // `randomizeHeatbugUpdateOrder' Boolean flag we can, F) f+ E( z& I) N+ H" [9 n
// randomize the order in which the bugs actually run# s2 N M! u0 P8 n2 H
// their step rule. This has the effect of removing any, s6 z8 G; f' E& x4 g' I( R
// systematic bias in the iteration throught the heatbug
. P# p& }6 k$ Y4 }7 P // list from timestep to timestep0 O4 l5 k) U5 o0 \, v) {: Q$ r% G# f
7 x* c8 _6 d7 i# a* X; Z3 x // By default, all `createActionForEach' modelActions have/ A: p+ ^$ H! T& p0 k
// a default order of `Sequential', which means that the
9 U( x; z1 j1 F3 N& @9 w // order of iteration through the `heatbugList' will be" K% C9 z* `* f+ J+ l
// identical (assuming the list order is not changed5 N Q! Y6 e* k! a
// indirectly by some other process).9 Z- Y% M7 }. i6 ]5 S
: y" T' G, r' s. r! d modelActions = new ActionGroupImpl (getZone ());3 w% m- C+ N0 C; H
4 `. r8 R! L- n s) |/ W
try {# H1 P2 c9 Q& U' C4 R
modelActions.createActionTo$message1 }7 P2 k) W$ ]# y( b9 d
(heat, new Selector (heat.getClass (), "stepRule", false));
1 W0 b9 e/ f! s2 f1 U } catch (Exception e) {
) t2 F; k2 _2 G9 d. @$ A System.err.println ("Exception stepRule: " + e.getMessage ());4 Q$ P6 E6 t$ v% I
}
( Q- J- Y2 o6 B( \. E& Q8 C2 S) K$ N) N9 a1 O- ]( i
try {
" T* ]: i0 H; M3 D+ I$ z' ] Heatbug proto = (Heatbug) heatbugList.get (0);: X' V0 h5 l) H5 |' X( Y
Selector sel =
' ?- c; h0 [, } Y new Selector (proto.getClass (), "heatbugStep", false);6 x0 [2 Y( C2 d! q D, s
actionForEach =
3 V9 y: X- N0 I/ @: E modelActions.createFActionForEachHomogeneous$call) |0 |/ o' m2 \7 {. e6 x' L
(heatbugList,$ K } P, C. x' K4 S
new FCallImpl (this, proto, sel,; r- w6 U% p. s2 \$ Q* J, N
new FArgumentsImpl (this, sel)));
9 h+ ]) n% r) G) S' p2 N" z } catch (Exception e) {. D( i/ B( V4 J5 K5 d
e.printStackTrace (System.err);( R( D) v6 J h d% p; f
}
# H9 p% [" X9 l3 {$ k: y- ~
+ D6 d6 E* b$ X$ w7 l" c syncUpdateOrder ();( l4 z) u8 o4 k' k# P
$ k' T: t' E6 I1 E try {/ n) X& T. g$ L5 g
modelActions.createActionTo$message
# b; e9 R) p- K (heat, new Selector (heat.getClass (), "updateLattice", false));0 ]: A. N% e6 ?0 R6 }* O! ^
} catch (Exception e) {$ @) _2 H9 O" f% i# {; ?! [
System.err.println("Exception updateLattice: " + e.getMessage ());/ ?. H' v* v1 A% o+ Z! [- q9 ^
}+ M* I. \; ~! w
7 J5 T! E6 K1 V& H3 o, e! ?
// Then we create a schedule that executes the
# s2 y4 v9 H0 M1 ~6 y3 y // modelActions. modelActions is an ActionGroup, by itself it
! X) z4 O) g$ q( ~ // has no notion of time. In order to have it executed in G L& I8 |+ Y& V% m4 M
// time, we create a Schedule that says to use the
- T( \$ ]" z4 N& d // modelActions ActionGroup at particular times. This
$ D( d3 T2 W: Q& k+ g8 I" C // schedule has a repeat interval of 1, it will loop every6 }. @* B7 B' n
// time step. The action is executed at time 0 relative to4 _6 @5 u; ]7 W; Z
// the beginning of the loop.
. k4 n8 H' i: X6 q3 Q9 [( [( E, ^' \, F8 z0 I
// This is a simple schedule, with only one action that is$ R! G4 ^. B, @3 Y* N/ Y/ D
// just repeated every time. See jmousetrap for more4 Z' t# ]2 Q) K( \$ t9 p* ~
// complicated schedules.
# o6 I `6 V+ u* j5 E 3 S3 Q4 m f) [) G m
modelSchedule = new ScheduleImpl (getZone (), 1);
) z8 C, Y& W- W modelSchedule.at$createAction (0, modelActions);
5 R" d# A" [+ V E! }5 s . Q- N6 J5 ?% v& i4 ]7 _
return this;5 Y! q! Q8 p# z+ z0 R. _# ]( r3 a
} |