HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! c. B7 d3 s6 \% i, c* m% X9 m4 b7 ~1 X
public Object buildActions () {
. \( t4 ~7 \- i, `5 ^ super.buildActions();
1 G3 m+ X& \ ?. V. N, ` % G' a9 k* X" p [8 _
// Create the list of simulation actions. We put these in
. F6 H: c- m( A- A! C8 W' r: Q // an action group, because we want these actions to be4 \* {2 m2 N' E
// executed in a specific order, but these steps should$ J- Z5 H8 V A- j
// take no (simulated) time. The M(foo) means "The message( g- ^ r4 J: l8 q5 |: }
// called <foo>". You can send a message To a particular; W. L9 g* k2 q4 [3 d2 k m
// object, or ForEach object in a collection.
. ~& j M, g5 c6 O2 j
: x' u3 e( L2 y // Note we update the heatspace in two phases: first run
6 s" q5 l; {: g `. K // diffusion, then run "updateWorld" to actually enact the7 s) z- m2 c; T
// changes the heatbugs have made. The ordering here is8 q8 L$ \& e9 I' L2 l* t
// significant!: L: [% D! M+ [9 Z
2 k$ q) _& \2 b( D! m- T0 J& x6 Z9 x8 d
// Note also, that with the additional
9 b. U( H/ w- W // `randomizeHeatbugUpdateOrder' Boolean flag we can$ b7 V0 ^4 D2 e1 e9 \, a
// randomize the order in which the bugs actually run' f' H1 B# N9 a0 z1 W/ s" Q: p% e
// their step rule. This has the effect of removing any
6 u' Q6 @ _+ m" \ // systematic bias in the iteration throught the heatbug
3 W: f5 I$ r V; ]" }6 F // list from timestep to timestep1 x5 h3 k) F6 i! u9 e0 v4 g
0 ^$ N' \5 Z: b: G& ]4 p // By default, all `createActionForEach' modelActions have6 z7 _6 _8 W( O
// a default order of `Sequential', which means that the7 p' q1 H% g# U3 @ @
// order of iteration through the `heatbugList' will be* C/ u8 \5 ^+ s; i
// identical (assuming the list order is not changed: p4 ~& s9 J0 k) }
// indirectly by some other process).6 K7 M/ B$ {1 m8 D# m
6 c0 M! w8 p$ o/ d0 e& c+ R- j
modelActions = new ActionGroupImpl (getZone ());
2 _+ F4 ]7 V% d+ z8 `7 [2 U! B
) Q: J: m! `' \5 z3 D% K# A1 ?3 }8 s try {
8 `9 K- m5 ?$ L modelActions.createActionTo$message
/ D, P' p$ z' K' d' Y3 w- u (heat, new Selector (heat.getClass (), "stepRule", false));3 I1 r; A, c% p( ^/ ?, f, V' I
} catch (Exception e) {8 Z( J, W- f( W3 k$ ^
System.err.println ("Exception stepRule: " + e.getMessage ());2 j- i; ^) I: z; I2 F
}, q3 g& }; ~# ]) `/ I+ J& j7 `
" t! E! t( r8 o& s try {
2 B5 D) {$ M3 x# S. C% ~& h% w3 Z Heatbug proto = (Heatbug) heatbugList.get (0);2 N, [/ n: q( p G( H6 J
Selector sel =
; W, T/ o7 ~" S new Selector (proto.getClass (), "heatbugStep", false);# D6 q6 }5 a3 W' V" x" v
actionForEach =
7 r2 Y0 M) A1 J, u9 @ modelActions.createFActionForEachHomogeneous$call/ ^& i# p1 `% y( M
(heatbugList,# Q' S% |# R/ r9 K2 U4 J Q
new FCallImpl (this, proto, sel,
0 t: o( f; a* a new FArgumentsImpl (this, sel)));
! |- M' v$ m1 j } catch (Exception e) {, J& \; ^+ J' i! o
e.printStackTrace (System.err);
+ c( N6 Z, T n, U$ G4 I }
9 J( d" _4 b9 k6 e * n- ^/ T9 x6 m" u2 @' R
syncUpdateOrder ();0 ], ?8 E& C2 \" j* s5 ~+ [
* V- c, b( P4 U( y# q$ l% D9 {% k4 | try {( j1 ^* _1 w- C* H: s. u6 P
modelActions.createActionTo$message . x# y7 ^" o$ Q5 g2 q- x% k, I
(heat, new Selector (heat.getClass (), "updateLattice", false));
) u) v" e) w5 v3 y } catch (Exception e) {
) [! _. w2 M4 h/ d6 m' P( I System.err.println("Exception updateLattice: " + e.getMessage ());/ _; ]4 x' |; {3 R
}
/ n0 A/ H F; {: r- K' G7 u
! i7 @9 [, g, c% z: c$ c7 D // Then we create a schedule that executes the" i1 c. j, g, G% I. @3 x
// modelActions. modelActions is an ActionGroup, by itself it* @/ q8 Q. ?5 n# j; \" F( h0 u
// has no notion of time. In order to have it executed in V& {, Z- s3 W$ f4 L: R% C
// time, we create a Schedule that says to use the0 H7 n# F. h2 ~
// modelActions ActionGroup at particular times. This" \; q7 X' R+ g6 Q
// schedule has a repeat interval of 1, it will loop every
" g( A) m$ x- u) O // time step. The action is executed at time 0 relative to8 g) Z, O$ g) d
// the beginning of the loop.
/ r- C6 Z+ l, n2 V
* n# k* ^; U% L0 R# Z( ` // This is a simple schedule, with only one action that is
7 w; a9 w9 k/ [ // just repeated every time. See jmousetrap for more
4 u8 }# C: k* w& y7 W3 T // complicated schedules., c7 l0 b; p/ s! x3 c/ W$ v
( N) W# {, V6 b5 w, _* S
modelSchedule = new ScheduleImpl (getZone (), 1);* |, p! n2 x6 G' A# W% i
modelSchedule.at$createAction (0, modelActions);) I6 o8 ?# b6 H3 I# J4 c% B
% b* D6 O: c2 X6 P# P, M return this;
( H R' Z3 ^. a' F+ V } |