HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ D4 N: v0 M+ v5 p
7 g5 _- ?8 Z7 A9 P7 b public Object buildActions () {
2 U! y1 x; m+ F7 o: ~! C9 M+ D0 [% g super.buildActions();
; E' I- b7 M) b8 x0 x + o5 M8 E; }5 i9 X9 G7 @6 o
// Create the list of simulation actions. We put these in( S& R0 [$ h# ~" N* U9 `7 L
// an action group, because we want these actions to be
) T' Q2 }6 o- R // executed in a specific order, but these steps should
( C( s0 b9 n: i# C5 e# k$ b" K // take no (simulated) time. The M(foo) means "The message8 e$ m" C) Y" T B) h
// called <foo>". You can send a message To a particular5 @8 m; T0 ?/ R4 S; X, l
// object, or ForEach object in a collection.- i% D6 r; r/ J6 ?5 w2 |
1 N2 O* M4 P( D5 Q* `0 Q // Note we update the heatspace in two phases: first run# K6 t; g0 _: D. h; ?$ h& X
// diffusion, then run "updateWorld" to actually enact the8 Q v* c" i: W( J- g
// changes the heatbugs have made. The ordering here is' _& h$ w. b- G- s9 j! _9 P9 R
// significant!+ y* H0 N1 v. _! p; T: n5 q
* D1 n7 A5 h& r8 Y- N
// Note also, that with the additional
S2 n8 Y2 }$ j3 k/ k0 c // `randomizeHeatbugUpdateOrder' Boolean flag we can
, a% Z+ t/ n" J& D; r- n) C // randomize the order in which the bugs actually run0 C O) h' J- B3 H- B U4 _$ N
// their step rule. This has the effect of removing any! r' T& m1 ~! R2 N5 \4 G) Q
// systematic bias in the iteration throught the heatbug
" X1 ~ V. j6 x4 n# }7 a8 } S$ I // list from timestep to timestep+ A( R4 N* r, m( ]: m+ G+ p8 l
) J# U- \: S0 T5 r1 Z3 Y z
// By default, all `createActionForEach' modelActions have
* D! o4 M5 g5 V% {3 u. g: z // a default order of `Sequential', which means that the$ o& c2 Q3 X) x0 T9 K: w7 ~4 m' H
// order of iteration through the `heatbugList' will be
8 R4 b1 D& u ^; F+ v5 I$ [+ e9 c // identical (assuming the list order is not changed
3 O! Q$ J5 H7 |/ S2 k" {% _ // indirectly by some other process).
% X( B4 x) {7 R& m1 l $ |7 x+ W1 v+ h
modelActions = new ActionGroupImpl (getZone ());
* V1 M6 K6 G( J8 A' Z4 m# x2 l; c8 J- @& _
try {, r! w: j6 k$ M1 m/ b
modelActions.createActionTo$message' @' Z% n0 w4 P9 t/ ~, b
(heat, new Selector (heat.getClass (), "stepRule", false));; t1 j' e5 l5 h$ m
} catch (Exception e) {
6 s; n: w; f" @$ t3 H# l% g/ x System.err.println ("Exception stepRule: " + e.getMessage ());
- i$ x3 @$ t* } }
' o( _; k; r7 z7 k% F6 X# ]$ x
2 [% K1 Q8 h9 H8 R* I. j m try {* o6 ^4 f$ L- H6 Z/ Y% M
Heatbug proto = (Heatbug) heatbugList.get (0); r6 K8 h) Q" ], [4 Q! W7 t
Selector sel = 2 `1 Q) E, J9 L! N( @
new Selector (proto.getClass (), "heatbugStep", false);
- ]' |; \2 `7 `, S& }& S actionForEach =
* z3 J9 j' T/ t3 t' s- } modelActions.createFActionForEachHomogeneous$call
- e7 l# @" r$ w, t5 p, } (heatbugList,
' O+ r& x. O0 A7 r9 ?8 R* }0 q2 B; c new FCallImpl (this, proto, sel,7 H4 {7 d' w" D) M- \& D
new FArgumentsImpl (this, sel)));8 U0 X6 h! J! ?0 r& y" W
} catch (Exception e) {
% }+ I+ E) y5 o! Z2 q6 b1 N e.printStackTrace (System.err);- w+ ^. _8 b7 m- C# q, V
}
3 ~& N2 A3 n' _' F
- |: {% |% g: E+ x+ g, {* V0 ? syncUpdateOrder ();; @. b- u1 l* i: S
3 M6 E8 O' b& p7 z# @( l4 W4 S try {
. A4 c1 I y' x* u; W1 l modelActions.createActionTo$message
r7 ^: f' [5 b2 p3 T1 V (heat, new Selector (heat.getClass (), "updateLattice", false));
9 j3 d# p$ j8 g2 C. ]; t, o } catch (Exception e) {! v9 {$ ]% U& n$ m% Y
System.err.println("Exception updateLattice: " + e.getMessage ());- w4 W4 ] k* o% Q
}
2 [6 K6 Y& K& X( \6 F- i# v/ } + ?' D, H8 b: p, O S5 e9 n% N
// Then we create a schedule that executes the* c1 K* L g8 d" J O; L
// modelActions. modelActions is an ActionGroup, by itself it
' }( o; \& M. G8 p2 j2 I3 y // has no notion of time. In order to have it executed in
. G6 R# M) \; F7 }3 b& K! n // time, we create a Schedule that says to use the7 l# K$ i0 D7 o3 d# p
// modelActions ActionGroup at particular times. This
! S1 M! D0 p9 Z/ _9 }! {! z // schedule has a repeat interval of 1, it will loop every
0 V" f, c! _3 W/ M // time step. The action is executed at time 0 relative to
, s0 l! z$ a5 Q6 G' w1 E$ J- D // the beginning of the loop. h" b2 M" E: I$ G+ Q8 y% S- O
$ B: N' w; Y9 G" J. A // This is a simple schedule, with only one action that is1 N8 ` F& [( T
// just repeated every time. See jmousetrap for more
* R, z+ H* g+ r7 D // complicated schedules.
. \# T5 V* |0 D# M; B- A 3 s; Y* H! K- Z8 ?
modelSchedule = new ScheduleImpl (getZone (), 1);$ D/ }7 g c- V, w2 f
modelSchedule.at$createAction (0, modelActions);7 D6 K# B p* N3 J
6 y; b6 U* _, t3 l return this;
$ c9 x; f0 V- \9 ^0 b8 C } |