HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" V! {: j8 t, `# M, _. D% i2 X7 ]
( {0 I9 R. Y2 `5 f* y; X( U9 N8 u public Object buildActions () {1 N) h2 f% z2 |" {9 k$ R: y% Y
super.buildActions();* s# D2 C1 r, J3 H
: N+ d7 c7 `# q/ e7 G- h4 u: ] // Create the list of simulation actions. We put these in
/ T' T8 `. g9 X! o: a) b // an action group, because we want these actions to be
; s( ]7 w: ~" T/ H/ [, ?5 q: C5 { // executed in a specific order, but these steps should y; b, w1 s! [) x2 T
// take no (simulated) time. The M(foo) means "The message b+ O# g6 M7 \- e0 C
// called <foo>". You can send a message To a particular
$ [4 M6 t8 t ]; p$ Z, J* B, ~ // object, or ForEach object in a collection.
! m4 M& V5 W5 E6 f2 f 6 a+ A& d7 N Z+ d X
// Note we update the heatspace in two phases: first run
, w$ g5 a0 Q* d& G3 S$ } // diffusion, then run "updateWorld" to actually enact the# J4 v1 F+ k$ d9 h
// changes the heatbugs have made. The ordering here is: \6 l8 {/ i$ F# ~
// significant!
7 D5 ~ R5 K7 |8 w
& S* Q9 W0 w: g) \ // Note also, that with the additional+ `- `) J2 [# S0 h* ^8 z
// `randomizeHeatbugUpdateOrder' Boolean flag we can0 K8 l9 R- I, t
// randomize the order in which the bugs actually run
9 E* r9 ?; ]% Z/ y4 q1 ~ // their step rule. This has the effect of removing any
/ V, e0 m0 W, @7 _ // systematic bias in the iteration throught the heatbug9 U) y. U; H5 ]+ ]+ ?, V) O
// list from timestep to timestep2 \( B0 F4 {6 @: V" I6 b
: n$ p$ H9 j/ Q4 }* W
// By default, all `createActionForEach' modelActions have
4 m. O' T' J& v/ h4 O7 K5 i! g // a default order of `Sequential', which means that the
! }! y5 W; h/ m. r, l" b // order of iteration through the `heatbugList' will be
# ]9 k- G- }: x* ^* _- I! f' b/ P // identical (assuming the list order is not changed
! Q) s; {- B8 e. _2 K // indirectly by some other process).1 z) O. Q t- x& n- U2 {, d
2 z7 Y* y% L- N8 T ]% j8 Y modelActions = new ActionGroupImpl (getZone ());
9 p7 b5 I5 a4 x4 U, E4 g+ _7 Y8 _0 W% h a
try {
1 n f' d1 \* y/ I' l6 B1 S modelActions.createActionTo$message
% q/ Y9 O: Z, Z$ ] (heat, new Selector (heat.getClass (), "stepRule", false));' C9 s4 b& F% B9 D' J) X$ a
} catch (Exception e) {0 z9 p' E1 u: e" W/ k
System.err.println ("Exception stepRule: " + e.getMessage ());) d6 g* l0 N( t4 a
}9 [- J5 z: S: I. B, U
$ a: @9 U' ?* X1 }( a try {
- t# K+ w9 Q7 C O Heatbug proto = (Heatbug) heatbugList.get (0);
o7 z1 t- z/ i$ I7 [( P Selector sel =
9 d7 Q. N4 t- _# K! D( k new Selector (proto.getClass (), "heatbugStep", false);
* L5 I- A# k2 ^2 q5 v actionForEach =+ ?# [* X+ n7 ]7 z8 P
modelActions.createFActionForEachHomogeneous$call
* [" E8 e. _6 l) Y4 Z$ f( m (heatbugList,
- b5 ^- _$ _6 U2 Z5 b new FCallImpl (this, proto, sel," u3 s% u2 c* Z. ?8 n
new FArgumentsImpl (this, sel)));
* }: S$ Y U+ T/ g0 a } catch (Exception e) {0 ?# e: b0 F, H% C2 A/ r
e.printStackTrace (System.err);
7 r6 N) P" r8 M( u4 q9 ` }/ G$ i6 p2 y( Y
, O/ u8 v9 Z; G6 q4 o @# r
syncUpdateOrder ();
) c5 j9 V3 g/ d- f. y( C' x& M0 ?2 `3 U9 j8 A( u
try {
7 w) Q# D# ]: N' [( e/ n' \2 B' E% | modelActions.createActionTo$message ! s) C# \( T- |. J! {1 S
(heat, new Selector (heat.getClass (), "updateLattice", false));' o; Q/ ?' l" t& K& f
} catch (Exception e) {
8 S2 ^& d- q2 ` System.err.println("Exception updateLattice: " + e.getMessage ());
, s4 _% L* |' a- t5 D9 _ }
( n9 ?$ [# G% p& t1 u 8 G: C; U* l$ h# m- ~- e
// Then we create a schedule that executes the3 h. v* x& m6 a' a' J* D) B
// modelActions. modelActions is an ActionGroup, by itself it
& C8 f! ^2 _5 ~: k // has no notion of time. In order to have it executed in
+ B. Q6 C ^: B- l0 A // time, we create a Schedule that says to use the& T0 P) h# N/ N' U
// modelActions ActionGroup at particular times. This
) C+ Y5 s5 X$ ], m // schedule has a repeat interval of 1, it will loop every
3 j; `# m3 f, h // time step. The action is executed at time 0 relative to
; n/ _3 b0 d+ F, p4 F2 {5 D // the beginning of the loop.
7 A7 T3 {' E( M6 f. _' g1 x; F5 F0 T1 s: w" X% D
// This is a simple schedule, with only one action that is
( n; { ^2 [0 `% s/ C8 F // just repeated every time. See jmousetrap for more( l; W* }' p8 i- m: c/ P
// complicated schedules.
! k& O8 m6 O4 }& o* K+ U
0 u# y+ b7 a2 R5 @$ D3 G modelSchedule = new ScheduleImpl (getZone (), 1);
- j: ~, t' _/ w7 M0 m modelSchedule.at$createAction (0, modelActions);% \8 N; U. U/ ~
. r# o* H6 K. i/ W
return this;
0 f3 a; t2 F6 e$ T% w, D& c" | } |