HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 Y6 d, O& t+ }) y) K. t$ }5 M6 m% f7 P
public Object buildActions () {
6 b- O% l* w9 {' W8 U0 A super.buildActions();; Y: @6 s: }5 Y! d& P7 J9 W9 x
& K& ~! W0 S! w2 G6 U* c // Create the list of simulation actions. We put these in
% a7 G7 A: E; _% W) j0 f // an action group, because we want these actions to be
& E, z: [ m$ F& }/ b6 x // executed in a specific order, but these steps should
+ r% z. g0 P* s // take no (simulated) time. The M(foo) means "The message: P7 k) \8 y5 \2 S* z; M: C5 R
// called <foo>". You can send a message To a particular$ h4 m* V# d5 @/ B
// object, or ForEach object in a collection.) r0 B4 G# ]6 {+ h( [
) J* N, k- V H5 Y! V+ F
// Note we update the heatspace in two phases: first run
$ J- l' U+ o+ P' @, i. v( g. v5 v- J // diffusion, then run "updateWorld" to actually enact the0 y i' Y( c" Z4 k8 ?) v7 b
// changes the heatbugs have made. The ordering here is5 g4 v0 x6 y- N3 q! d" o0 G2 f' B
// significant!
3 P( X) ~3 V x" W3 j- | 3 e# N7 o( j2 G* x2 G
// Note also, that with the additional
5 ~- u5 [ Z5 _& Q // `randomizeHeatbugUpdateOrder' Boolean flag we can
4 B; S9 ?# [$ a+ r3 z2 v6 B // randomize the order in which the bugs actually run
, a7 z' v$ z7 D7 U k // their step rule. This has the effect of removing any
8 F b/ ?1 B! }# j# R, D // systematic bias in the iteration throught the heatbug
( b' f- s- I5 B' [1 d& B8 H // list from timestep to timestep
. ]+ X9 B+ z% q+ [( l" ] 6 r8 f- T4 }" a4 Y- w; r4 e% ^
// By default, all `createActionForEach' modelActions have( c0 q: I6 d- B8 \$ }' o9 Y
// a default order of `Sequential', which means that the
8 U4 Z6 d9 d1 x5 R/ l& u // order of iteration through the `heatbugList' will be7 m6 X0 |/ X0 M
// identical (assuming the list order is not changed
3 _$ d7 K* |. M7 s# s // indirectly by some other process).( I% r, Q6 ]& n. e
. n( ]* g e" ?" R! r8 s
modelActions = new ActionGroupImpl (getZone ());
0 r# H; X4 Q, j
& {7 I! d4 Z- F# A% n try {
& L. _% D+ ~! j( {( r modelActions.createActionTo$message
7 c2 _8 q9 [" Y& e& { (heat, new Selector (heat.getClass (), "stepRule", false));
" u% T) B' x3 \6 B9 A } catch (Exception e) {( {: ~* I- c8 U/ J, y
System.err.println ("Exception stepRule: " + e.getMessage ());5 _7 C4 N. `& l. ~
}
2 ~, E& {' Y/ U7 j& `4 i Z1 H& X2 I6 p
try {
' C2 u. A, v1 g E8 p6 U Heatbug proto = (Heatbug) heatbugList.get (0);
! b8 z8 b% n$ N9 C s& c, x Selector sel =
4 B1 y9 T8 {: v1 s new Selector (proto.getClass (), "heatbugStep", false);
9 @5 G1 c( O- } P- a actionForEach =8 N# H5 z' u) c! o/ j% q
modelActions.createFActionForEachHomogeneous$call# q) u8 C' l4 b7 r4 R3 a
(heatbugList,
- Q! F+ ^2 ~, i U% N; Y' y new FCallImpl (this, proto, sel,
1 I2 i1 P. ?$ P1 v2 l new FArgumentsImpl (this, sel)));/ ?8 Y1 `& A; C, a i H9 _
} catch (Exception e) {
" [: L% }# u5 k6 ?& u e.printStackTrace (System.err);" T% k9 h: S" P2 W2 j
}% s, j0 o9 g9 A7 ?& m
8 i6 p& n ]6 s6 ]: \ V& G
syncUpdateOrder ();
3 r- p7 ^" T' l$ X) m$ {: h v. d y/ h7 C3 P+ r. K3 \4 Z, q
try {
3 p! R# q5 p/ T. I% s3 B modelActions.createActionTo$message
: W L+ [' }& W' ^; K) j. B (heat, new Selector (heat.getClass (), "updateLattice", false));
) U' j; ~& r! G } catch (Exception e) {
5 _0 W( q5 @$ U System.err.println("Exception updateLattice: " + e.getMessage ());8 C* w- A/ x* v( `, R
}9 n% s8 O5 l8 ^
! ?; R$ O! @! A // Then we create a schedule that executes the. ~) q$ b& Y, K
// modelActions. modelActions is an ActionGroup, by itself it
# P# d+ y3 ?4 `3 m // has no notion of time. In order to have it executed in" a2 y; k; o* q2 A7 o0 C! g
// time, we create a Schedule that says to use the5 ~1 {# b" {; t$ y
// modelActions ActionGroup at particular times. This5 f1 s. Q, K/ j& f4 z. r
// schedule has a repeat interval of 1, it will loop every: {6 G0 Z4 i. k1 T' l4 {. Y
// time step. The action is executed at time 0 relative to% y6 K: Y5 l. W) B( T# e
// the beginning of the loop.! Y; {% p: [5 g! O9 T* C
7 f2 n* x; E9 U) _6 t3 y8 d" c // This is a simple schedule, with only one action that is
9 w& a' m$ A6 [$ W2 S // just repeated every time. See jmousetrap for more" o" I, p6 m% M2 L
// complicated schedules.% j* U5 X% ^- ^- N. S5 l2 o
9 I" Q4 Y! b! @6 Q1 j) j# I
modelSchedule = new ScheduleImpl (getZone (), 1);, b+ h% M8 c% D/ b( O6 w0 } @ Z
modelSchedule.at$createAction (0, modelActions);
0 V' I# _4 d6 p5 l; ^9 [4 f2 q1 r
- S4 _8 b. m6 E7 r1 g) [% A* Z" S* y return this;
; b8 o( U4 n9 q& t1 ^4 N } |