HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- V! `" l- a+ O' J2 f/ R5 R) R' U4 ?
public Object buildActions () {) W6 b0 h( Q: u+ T7 t
super.buildActions();
% m8 b" Q) y# w4 F$ i
/ [/ Z/ J$ z2 r // Create the list of simulation actions. We put these in
$ M# A2 Y8 L/ c8 K0 ` // an action group, because we want these actions to be% d4 |, I) ~3 |& ~* _8 T
// executed in a specific order, but these steps should) c+ h" Z+ T. Y$ n. J7 [( T' ~- x+ [
// take no (simulated) time. The M(foo) means "The message
; i' p: a1 ~0 C! } // called <foo>". You can send a message To a particular
: g, x- \4 k% k) u r& ~8 s V // object, or ForEach object in a collection.
% \- \+ L- O! j3 w' x% s! z7 _
$ b! |8 [: p: \% }: M: t8 O // Note we update the heatspace in two phases: first run- a2 l2 u" d, R2 T9 F$ N9 U8 G) v
// diffusion, then run "updateWorld" to actually enact the
% S8 p. R" S* Y, u, [- t; ] // changes the heatbugs have made. The ordering here is5 h* m) D4 f. G: p8 c3 Y( ~
// significant!5 j; L1 Y5 g" i, `* f4 H1 H( `
- ? c7 \4 g Y // Note also, that with the additional4 t0 S) ~! c9 x
// `randomizeHeatbugUpdateOrder' Boolean flag we can+ ~! {! y1 i' |# w0 _& I( n
// randomize the order in which the bugs actually run
! E! k. s* C% f* H' _: [. H // their step rule. This has the effect of removing any
( y" |& ]! Y2 K7 U4 W& j3 \ // systematic bias in the iteration throught the heatbug
2 O; f4 r' u4 L // list from timestep to timestep& q/ f3 c9 |6 j- m, g. ?, }) y n3 N
; u( H# H7 ~6 t2 L2 L% V4 d. } // By default, all `createActionForEach' modelActions have
. G9 R4 j7 t2 d // a default order of `Sequential', which means that the# P" c- q' r4 x1 U' V, k3 S
// order of iteration through the `heatbugList' will be
7 b7 g1 R0 w2 x! X! y+ F7 N // identical (assuming the list order is not changed
! O9 E8 t P1 E# ] // indirectly by some other process).9 d5 ] V& {7 O- D
: ]+ \9 g0 M% h8 l+ Z
modelActions = new ActionGroupImpl (getZone ());
' Q6 k8 I+ x/ p$ Z5 t. o4 w8 n% Y8 P
- ]0 b# v/ Q; p' [ try {
/ i& b, e6 W8 |- i2 o+ d modelActions.createActionTo$message
7 e" u- L9 Z' z$ \5 |- r- F (heat, new Selector (heat.getClass (), "stepRule", false));
6 d$ U9 F) y8 ?# i } catch (Exception e) {
2 E9 ]% g6 F/ C; A/ W System.err.println ("Exception stepRule: " + e.getMessage ());
" Z; `' y! j# V: C8 i! K }
# _& f" l, x7 T' n6 F9 ^/ k) X, q; u& B O! U% H
try {
; M) f1 n% D9 O; U3 u. A9 Q Heatbug proto = (Heatbug) heatbugList.get (0);
9 Q/ ^. B5 h4 o; l4 W% l- x: ?9 z Selector sel = ' @' D, d' x, Q2 D! G( {4 y
new Selector (proto.getClass (), "heatbugStep", false);6 K1 U: i; N/ z
actionForEach =6 y" i1 j5 {0 [) F- q3 U1 {- G
modelActions.createFActionForEachHomogeneous$call
$ f6 F) H8 B9 d (heatbugList,
( h( d2 T/ J3 Z6 T new FCallImpl (this, proto, sel,
9 s/ I( X6 c3 r4 a& e new FArgumentsImpl (this, sel)));& G: n4 ?, q# f0 ?/ C, d! P
} catch (Exception e) {8 @* y" G2 |- j: s
e.printStackTrace (System.err);
& ], \8 g0 Y2 V8 r, {4 B }) P8 ~6 E5 ^& a7 \; `9 C6 l6 c
- i$ V+ j* \# H0 I
syncUpdateOrder ();
H7 e# k$ c9 g# \; @! |; f- V: n+ o2 Y
try {
0 h, m9 q4 b; a# u" p modelActions.createActionTo$message
1 u9 c! ]: s# u (heat, new Selector (heat.getClass (), "updateLattice", false));
; N. G7 o0 P4 M% R. Y* I } catch (Exception e) {
( N$ j, D0 ]* \% F5 a# @ System.err.println("Exception updateLattice: " + e.getMessage ());
, G( L8 C: r6 D( Z }% ]! S+ }, }/ e+ Q
2 F( l6 M* Q# W7 V* [ // Then we create a schedule that executes the
& l# }1 f. `/ A4 _6 l2 }8 u | // modelActions. modelActions is an ActionGroup, by itself it
: E: ~- d% Q% V0 r. z9 [1 B8 Z l // has no notion of time. In order to have it executed in
# T, o5 m6 _" X0 k+ M // time, we create a Schedule that says to use the* l; E, `7 g$ @1 F( L
// modelActions ActionGroup at particular times. This
# a. Z! d! e+ A; ^4 _ // schedule has a repeat interval of 1, it will loop every
9 m8 r1 J3 h0 k9 b // time step. The action is executed at time 0 relative to
2 U3 n3 X* S' U' e" J // the beginning of the loop.
# s/ A( \+ i! j0 [) ] X9 i" z$ L6 T3 p: F) Z
// This is a simple schedule, with only one action that is
5 E6 {, P7 D( q, r // just repeated every time. See jmousetrap for more
& L1 U/ L% G! k+ F // complicated schedules.
! f$ W, a1 R. ~ 4 S# g. b$ R0 V# R
modelSchedule = new ScheduleImpl (getZone (), 1);* s- s0 ^; i# t0 S0 Y4 l5 V5 f
modelSchedule.at$createAction (0, modelActions);8 U" d1 E' L0 l0 `
; A6 {9 I5 \1 C7 o return this;$ J' `8 Y5 B# m$ s( r% f
} |