HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! C) {7 |" V: q; h3 t; g T1 \. O" L, `% J. l0 p2 `' k2 S
public Object buildActions () {
9 J# A9 _" o6 m }6 {5 `1 ` super.buildActions();
8 q3 Y% r7 V+ u & x/ d4 Y( v& D/ u
// Create the list of simulation actions. We put these in4 h0 s3 F ~+ a- B& c3 C! @
// an action group, because we want these actions to be
. L( i+ `$ _% t! _9 T( `( t4 T. L // executed in a specific order, but these steps should+ h& X6 s2 x* r9 m
// take no (simulated) time. The M(foo) means "The message: O, _1 a+ u! v4 O5 ]5 ^% h w
// called <foo>". You can send a message To a particular; x* o& ?# H k# }/ ]. s& o
// object, or ForEach object in a collection.' [( ` T# r# \7 a" D* I
- h( p8 i8 D: V9 l8 j0 i // Note we update the heatspace in two phases: first run. o4 ]( T' R4 f8 q5 n, S
// diffusion, then run "updateWorld" to actually enact the; ?! C6 Y/ E$ P) \' x
// changes the heatbugs have made. The ordering here is
: Y$ D' k3 c( m" u; v: N7 K // significant!
- l/ l$ w2 ~ y4 C
% @! ~+ z: l$ M! s1 r // Note also, that with the additional
2 k1 X' y. m. j7 @6 d6 q8 e. o) u // `randomizeHeatbugUpdateOrder' Boolean flag we can
7 [5 v) H5 z% A R8 g4 k0 S) V // randomize the order in which the bugs actually run9 J/ ]! P; h: ~& K+ n* O2 v ]
// their step rule. This has the effect of removing any
- q* |4 G3 @ J) z$ o. ]7 S5 P // systematic bias in the iteration throught the heatbug1 m+ B( l+ y+ k$ _0 w& X6 ~2 c" K* i' r
// list from timestep to timestep
7 I Y' v& c8 E. E+ R, Z # U/ K6 r) i# X& x/ W; R
// By default, all `createActionForEach' modelActions have* D6 c6 J/ l [2 l4 b
// a default order of `Sequential', which means that the$ V4 ^1 W: | M# Q' m: l9 F
// order of iteration through the `heatbugList' will be
) f) L4 M; U1 a. g // identical (assuming the list order is not changed
( N4 A8 X& E, R // indirectly by some other process).0 O2 H/ w) q0 C# V7 ~6 \: W, C
7 N; z& s; w5 a- Z2 Y; ?
modelActions = new ActionGroupImpl (getZone ());
' |( K- F' U% n# [/ O, |
2 ~$ |+ B7 r% u7 c( x try {) \* l! B7 [0 y+ h) `
modelActions.createActionTo$message) H1 j" H4 G" L) p6 C
(heat, new Selector (heat.getClass (), "stepRule", false));
8 ]+ |8 ?$ r5 q/ L* H } catch (Exception e) {
: f7 |1 W. ?* a3 {" K- N System.err.println ("Exception stepRule: " + e.getMessage ());
7 M* v# O- |- ^, D% q( G" n) s }* ^! y- _5 L6 r: z. J
* q& h2 E. u R) h* u, q- Z
try {
5 D8 f% Y7 ]1 [( @# W Heatbug proto = (Heatbug) heatbugList.get (0);
& f- K/ f, e8 @+ b6 v Selector sel = % Q3 S; m' ]4 r" t
new Selector (proto.getClass (), "heatbugStep", false);, s/ h0 P# b/ Z% V: [
actionForEach =% O0 [( O4 W& b- C! X) r' l
modelActions.createFActionForEachHomogeneous$call. c/ n3 Y i4 a$ J5 O- c) J4 C
(heatbugList,
) U" J' T6 Y8 y% k$ Y new FCallImpl (this, proto, sel,
2 t9 h% [( T# v" Z% }, N W5 U new FArgumentsImpl (this, sel)));
/ Z" b9 @; k2 l1 U9 w/ x5 @# N+ ? } catch (Exception e) {$ P; h% X2 b5 g% [4 S
e.printStackTrace (System.err);
2 ^* T# C1 g `. O' r" t, B7 R }
0 p9 d5 t' O+ L0 b6 b P' T + ?! |: h& v$ }5 k
syncUpdateOrder ();7 M# n* Y! G1 x7 q0 t4 V$ D! e9 e" @
9 W s' Q: X9 u. Z
try {5 Q& ]7 b3 `: A2 r0 b
modelActions.createActionTo$message
3 y2 w5 r: V# H0 j- O (heat, new Selector (heat.getClass (), "updateLattice", false));' s2 L p. x E- c. a. j; \
} catch (Exception e) {
9 w: E8 y& B* d" i" W: g: R System.err.println("Exception updateLattice: " + e.getMessage ());
6 `# _! ?5 l2 H- S u; m# | }
1 V! M% I2 n0 r4 N 6 w4 b9 w7 Q; t7 `0 E
// Then we create a schedule that executes the8 i3 C9 B3 ~- ^3 w" u
// modelActions. modelActions is an ActionGroup, by itself it; A7 n" [9 `% M1 I! Q- B0 |$ N
// has no notion of time. In order to have it executed in
7 K8 q. z& n" X9 \/ A7 z2 F) t // time, we create a Schedule that says to use the
2 w4 x) G, `. L // modelActions ActionGroup at particular times. This0 v8 H- G1 G/ z. N
// schedule has a repeat interval of 1, it will loop every+ k# _; N" x$ Z* z5 [
// time step. The action is executed at time 0 relative to
. `) _* u, [, d6 G7 ~2 B" _3 y; M // the beginning of the loop.
' C8 I9 L: l3 O4 |4 p2 q6 {- `: y {" x/ I3 N7 b3 z
// This is a simple schedule, with only one action that is
) y" | b2 E ^& y+ V& D- i2 f // just repeated every time. See jmousetrap for more4 x3 _ M; G, q c( d8 c2 k
// complicated schedules.6 L3 ?* b& ~! l
# W9 b% c# q5 \+ K% I* U0 R6 k
modelSchedule = new ScheduleImpl (getZone (), 1);% k; ], ~: K0 ~/ T8 v2 a4 ]* M
modelSchedule.at$createAction (0, modelActions);! U# R8 y% M6 i+ h- X
& {" B. h3 }1 D( ^' C4 f6 b# o
return this;3 h) y3 j# E+ |( _/ m
} |