HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 [2 F. _8 ?6 d7 v' X
8 `8 F2 D; b+ r0 ]1 \6 @' g public Object buildActions () {
6 c, d# A( S; @% ? super.buildActions();, z0 D+ v$ c3 y V
5 n5 f" \0 @0 n, d! Q+ F
// Create the list of simulation actions. We put these in
, }' U, X4 a) i& D" s/ i2 q // an action group, because we want these actions to be
; h/ w" t) t7 E2 @! j, Q o q. E& j3 L // executed in a specific order, but these steps should
+ j7 u% ]$ C# Z$ H! c* k // take no (simulated) time. The M(foo) means "The message8 |5 y$ p- X- s
// called <foo>". You can send a message To a particular X4 ?) q0 H* _' [; K0 S( d1 X& C
// object, or ForEach object in a collection.
8 ^: Q8 ^* E( u8 S, I. L
% Z1 p* G+ o$ V( A$ Q, ?( Z // Note we update the heatspace in two phases: first run
$ q2 h0 p9 e9 b; q7 y# D& ^ // diffusion, then run "updateWorld" to actually enact the
' z+ A( l9 l5 g4 } `* G( H // changes the heatbugs have made. The ordering here is
* X6 y( {$ _1 J2 m1 W // significant!" {* L3 i; G& s8 u$ P
; L: ^1 A5 g4 g+ k$ ~5 W. m // Note also, that with the additional
, W' u3 A2 R; _( W9 o9 ]1 e // `randomizeHeatbugUpdateOrder' Boolean flag we can
) F+ F& N* R* |/ M8 |. }" { // randomize the order in which the bugs actually run. Y2 O5 e- L# p% g4 B
// their step rule. This has the effect of removing any, X* ~2 I8 k4 O' P0 l
// systematic bias in the iteration throught the heatbug4 A; U# R! m6 a7 b& a3 B
// list from timestep to timestep
( S% Q7 |" s# [4 s3 s; x
' J/ S- S2 g4 y // By default, all `createActionForEach' modelActions have- ? W. D' s% w8 H4 M
// a default order of `Sequential', which means that the
3 j# F+ m; D2 a // order of iteration through the `heatbugList' will be
, [. l0 J" D& I5 ]2 m& c4 P // identical (assuming the list order is not changed
) z0 x+ G2 Q; N1 }6 m& Z/ p // indirectly by some other process).3 X0 H; q2 A- z" `$ F6 O( L. l
% V$ B, S9 z' a3 B9 j, v
modelActions = new ActionGroupImpl (getZone ());+ y6 a0 n* U7 E* _8 F
" q: s5 \- P6 \& L8 u' t4 ^' f: C* ?/ e
try {
+ U! I& O; `; S' x, S$ E8 x modelActions.createActionTo$message
8 Q a; `% d5 H, T (heat, new Selector (heat.getClass (), "stepRule", false));2 S: j8 l1 L x" G" C
} catch (Exception e) {
3 G0 ~1 B7 x: V5 g8 s0 y1 D4 Q" W System.err.println ("Exception stepRule: " + e.getMessage ());8 ~$ |) g1 O% E) G! }3 O: W
}0 f$ E( a6 t+ O n! g
1 ?3 u& P/ W/ V/ z' E& n try {. @: C8 x7 u4 A9 N% [
Heatbug proto = (Heatbug) heatbugList.get (0);, O) c5 D5 v, K8 k3 @$ O7 Y K
Selector sel =
! R9 }! l3 z4 R: Y1 J4 n1 S5 C: C new Selector (proto.getClass (), "heatbugStep", false);# b/ n% ]5 n* ~1 a: G5 ~/ l
actionForEach = t. s) d, k: v+ w% p9 J0 b
modelActions.createFActionForEachHomogeneous$call, M5 A: z* @0 R% l
(heatbugList,
- a6 t6 d. j& g+ u! d1 m new FCallImpl (this, proto, sel,
- Q( p" y+ V' v9 j( a, \2 ]- `3 \4 } _ new FArgumentsImpl (this, sel)));
& Y- A; }+ Z( J3 t! G3 s } catch (Exception e) {' T" Q$ @$ x+ `4 A/ R+ K* M, C
e.printStackTrace (System.err);6 t2 @) U6 N# W* c' b& Q2 Z' ^; t
}
5 T. P/ z8 n1 N, `+ W
+ B& r% _3 G1 P& Y9 u9 e7 o7 R9 E& Y. e syncUpdateOrder ();- K' H) ]6 D8 T
/ L5 f+ ?' j- [; t P* m, ` try {5 J+ J7 m( u" a. g5 `/ }9 Q! ]4 F! \: `
modelActions.createActionTo$message ' h l7 o. A. b( z* S9 b8 S) k
(heat, new Selector (heat.getClass (), "updateLattice", false));
5 E& @7 E- l, j0 J } catch (Exception e) {
. J% i4 E x' m8 S( Z4 C8 u System.err.println("Exception updateLattice: " + e.getMessage ());
5 W4 M( w/ o# Q' [* t( ] }$ S- h9 j* g8 S# N
3 X9 L( X/ X/ m) ~0 {- i // Then we create a schedule that executes the
% M) a4 R! Q: i& E: z" ~ // modelActions. modelActions is an ActionGroup, by itself it
+ r6 P n% `6 ?/ C* q: ? x0 y% q // has no notion of time. In order to have it executed in
' e9 u( P0 [( W2 q; s // time, we create a Schedule that says to use the! x+ |: v" G' G; _
// modelActions ActionGroup at particular times. This
# A+ h" ]" ^$ G+ V // schedule has a repeat interval of 1, it will loop every8 C1 r7 c% ]* l- m
// time step. The action is executed at time 0 relative to
, `- m5 K6 [6 U" P/ o3 |( m1 {) ^ // the beginning of the loop.
; _" v8 ` V$ t8 a- @. K! P' y7 h
// This is a simple schedule, with only one action that is
0 d' [9 P" Y/ ~ \4 A // just repeated every time. See jmousetrap for more* {/ L8 [/ d. E, |; e! m
// complicated schedules.
2 ^# r! F; Y" g3 b, m2 ?! A ) ~3 d- D& E$ t' b
modelSchedule = new ScheduleImpl (getZone (), 1);' o6 l6 I1 g9 N" X2 e% V; j
modelSchedule.at$createAction (0, modelActions);
6 N: o6 g( B L6 F' y% C. x
# x7 S# y# f/ V* U7 K m return this;. ~2 c. {. G1 O/ Y
} |