HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" K9 @; e6 K3 i) r3 t
\+ B$ R" v# J% K1 c public Object buildActions () {- A0 Z, m" [. V. H1 Y
super.buildActions();8 X9 \9 H- U3 z8 w7 K: I- e
- @. L( n4 L9 @/ Y9 r0 c4 J
// Create the list of simulation actions. We put these in( b7 Y8 @! e5 Q, P Q6 x5 P+ y
// an action group, because we want these actions to be# M, W. U6 u K6 P! Y! T1 J
// executed in a specific order, but these steps should9 J2 Y' `& n, L8 O1 S0 H; i* _1 z$ I
// take no (simulated) time. The M(foo) means "The message
}" q# b, V, Y, [$ ]- T# m // called <foo>". You can send a message To a particular
- ?5 ~( S2 D" m( j$ ~! [; F2 D // object, or ForEach object in a collection.7 `9 I" \! W& g5 _
/ F6 ]1 b& p6 X" `+ X // Note we update the heatspace in two phases: first run
; }) p: b, }0 w8 g! d! T // diffusion, then run "updateWorld" to actually enact the" X4 w* e2 J- j s* c+ R
// changes the heatbugs have made. The ordering here is; {7 l* ^+ a2 B" A' R
// significant!
: j/ u6 o9 ]' r- H
2 \* [( z* @3 u% ?1 |: ?( m // Note also, that with the additional: I. m, h: v" R9 J) ^3 c7 L
// `randomizeHeatbugUpdateOrder' Boolean flag we can( H4 q1 F! } \, d5 s; {
// randomize the order in which the bugs actually run" X8 f5 W4 `# k4 i% N6 {. C g
// their step rule. This has the effect of removing any
9 o9 ^. T" H' Q4 d; f // systematic bias in the iteration throught the heatbug
, e# D/ h2 U7 ]7 o% } // list from timestep to timestep% B/ Y$ U* l6 R$ Y5 O/ W: e
3 X+ X+ | |5 F9 @: O) d. K
// By default, all `createActionForEach' modelActions have
" C6 ~ q8 G+ ]+ [' L2 H5 H // a default order of `Sequential', which means that the/ {% s% e, F: W
// order of iteration through the `heatbugList' will be
4 r! {- j/ p8 e- W" Y. x5 q: ` // identical (assuming the list order is not changed
$ j* ^$ J+ p0 t; [0 m8 U9 m // indirectly by some other process).3 W9 b$ h1 l# g) S' x
0 {. M: P% x3 I; \7 O/ M8 ^( B& t' q
modelActions = new ActionGroupImpl (getZone ());+ y! b0 J& u8 \
+ a2 P& F6 [* Q4 e6 q: w! E `9 W
try {
; g8 f* |0 m! J( Z modelActions.createActionTo$message
/ r0 X7 I- W4 A. Y* X0 { (heat, new Selector (heat.getClass (), "stepRule", false));
7 ]; {* Z0 K) `( \+ S2 Y* }1 q } catch (Exception e) {' Z, [' D+ F& W2 U) C& B1 [% n- \5 A9 T
System.err.println ("Exception stepRule: " + e.getMessage ());
6 q! }1 i, ~, Q9 O, V }/ w$ F: L) s2 L
4 I8 R: H9 g3 s* O) E3 B/ o try {/ `: E: L, x1 v: R9 Z2 V' j' V
Heatbug proto = (Heatbug) heatbugList.get (0);
) h5 Z* [9 C" v; ] Selector sel =
% p% p6 h' N! ~ new Selector (proto.getClass (), "heatbugStep", false);5 \) ]7 V% T, V0 J, m3 i% l* \
actionForEach =
% Q; G9 j& W2 W) l: n0 c5 `5 q modelActions.createFActionForEachHomogeneous$call
6 s4 z. N3 C# {# m! Z (heatbugList,
$ N+ T8 n( p t& c! z: P; P new FCallImpl (this, proto, sel,
" |4 u5 e1 y- l5 }1 e a new FArgumentsImpl (this, sel)));* M8 w. x; o8 n
} catch (Exception e) {# G' ~1 i4 n4 u3 b Z! D( c
e.printStackTrace (System.err);: Q( a/ _* |4 X) w* X# ~3 B
}
, _/ ^" L* I4 C! Q9 ` 0 v1 L8 c9 H) G
syncUpdateOrder ();! R- Q" y7 B. ~5 k. a
5 I' U& n5 M! L; d
try {
& y, C9 H2 b+ B9 F& B5 Z modelActions.createActionTo$message 8 F' q9 U/ h) r, n3 L
(heat, new Selector (heat.getClass (), "updateLattice", false));0 ~ t- c. |, G5 J3 [9 j; S
} catch (Exception e) {4 ^; M) v& U2 O! o0 x0 g4 @; ]: Q1 W$ @
System.err.println("Exception updateLattice: " + e.getMessage ());: z, T( }2 H! U! i- a3 r( S& F
}7 A0 |3 G% r( P) x
. T& p; \" \3 L+ x+ `; `* ^ // Then we create a schedule that executes the3 |# X( Y. K( ]7 B, ^2 S
// modelActions. modelActions is an ActionGroup, by itself it
; P( [, z. c5 \1 R0 k6 w) N- n // has no notion of time. In order to have it executed in
. c: E# O9 ] J C7 M$ X2 k5 V // time, we create a Schedule that says to use the3 R' s' [ n, R) j) ?
// modelActions ActionGroup at particular times. This
" H7 X! w4 ?7 R1 d# ^/ _ // schedule has a repeat interval of 1, it will loop every
/ F5 ?4 j% n! h; V // time step. The action is executed at time 0 relative to
6 w1 a; M; n% b& c% R8 ~9 Z // the beginning of the loop.9 j% f7 |" B6 I
3 v/ r/ l0 |. Q" A2 ~ // This is a simple schedule, with only one action that is
5 p! J9 l) L5 x" ^4 r9 |, ^ // just repeated every time. See jmousetrap for more. @4 E7 X: a2 v W/ @
// complicated schedules." \; C& ~/ ?8 o. s7 }
1 b! P) x) M( C
modelSchedule = new ScheduleImpl (getZone (), 1); ]8 }4 ~: S/ J
modelSchedule.at$createAction (0, modelActions);/ a. \; C2 V3 v V
" I& g" C- n1 {5 b9 A% k! B2 S5 M return this;
* Z. ?! t& o0 g% z; H } |