HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- `3 ~( I% H' r9 D7 k/ ^) O% R. U# b5 H9 D! U) b1 b- H
public Object buildActions () {4 ]% i+ |& Z" \- Z
super.buildActions();
" q$ h% o; Y9 ]
4 s! A4 A2 {; C* @6 h" d# u% s // Create the list of simulation actions. We put these in, k$ z) C$ X+ ?# |
// an action group, because we want these actions to be
1 Q4 @( v7 ^# ~! b, B+ y9 M! v // executed in a specific order, but these steps should3 j7 k2 T5 Q6 X( i; Q( o
// take no (simulated) time. The M(foo) means "The message
( P; D8 n6 o7 t0 d" Y* P* m // called <foo>". You can send a message To a particular
+ s5 B0 B- Z9 ], B5 a // object, or ForEach object in a collection.
, j, |$ b' ^" F( c, W9 c* Q " @' V4 t1 B+ V( S: Y: W( J
// Note we update the heatspace in two phases: first run) P* W- c$ y: j; c7 g9 N, W
// diffusion, then run "updateWorld" to actually enact the3 O5 n. R, e+ K& a$ g* K1 x: D
// changes the heatbugs have made. The ordering here is
& h/ s2 z# r$ B N // significant!# P9 g7 Y3 O6 ^$ u
1 H B! n' Q0 q' F. } n& U% t
// Note also, that with the additional" ?8 ?! K: V: d+ F' w* v* S
// `randomizeHeatbugUpdateOrder' Boolean flag we can
0 L% M0 W7 ~7 v" ^6 E // randomize the order in which the bugs actually run6 F( H+ q' I3 ^) i' Q3 q: P3 |1 P
// their step rule. This has the effect of removing any; F0 O6 E* }* c
// systematic bias in the iteration throught the heatbug
& b7 B; X C- n // list from timestep to timestep1 G' J {6 @5 F D5 S$ I( Q7 [
4 M4 V, R6 b* G6 J& F
// By default, all `createActionForEach' modelActions have
$ Q% I1 T/ L; a5 B4 q9 A4 d // a default order of `Sequential', which means that the
) |/ @! b0 M4 f6 p // order of iteration through the `heatbugList' will be
' u& O, Y. h( } t4 T$ n: p+ U% p // identical (assuming the list order is not changed
5 L' R% z# h4 X" f, d& `: d0 p // indirectly by some other process).1 z; b/ l: F3 p+ k! J; j
8 F0 F) X9 z0 c6 M
modelActions = new ActionGroupImpl (getZone ());
f6 D! Q/ [ `3 E) x' k& E# m& P3 x" V% c5 R% f
try {. t0 Z9 g" @# f0 k5 `! Z: }+ v
modelActions.createActionTo$message
- @2 u" {! k7 y c, ^( { I( c (heat, new Selector (heat.getClass (), "stepRule", false));$ M! F3 u% J0 u7 l9 x! S
} catch (Exception e) {
7 j% ]6 A3 ^9 k System.err.println ("Exception stepRule: " + e.getMessage ());
* F# C* X. {! T$ U7 b8 M- f7 i }
# ~% E5 f9 M0 f$ }9 r6 N0 I9 B0 v4 T+ r
try {: z, s/ O! J+ z: \4 i2 `9 C- S
Heatbug proto = (Heatbug) heatbugList.get (0);2 n, M$ E: `5 Q( ^0 n# M. s
Selector sel =
5 V7 s! E) \0 r& S new Selector (proto.getClass (), "heatbugStep", false);) G" t4 U* W8 d0 c5 x
actionForEach =
* l( r3 a8 b$ Z. s* W modelActions.createFActionForEachHomogeneous$call
( c% U1 C8 a2 @5 Q, N2 e (heatbugList,' X" p# x5 p- V1 I' y3 x
new FCallImpl (this, proto, sel,6 ?9 a z: t3 x7 V0 c i+ t; [; h8 y
new FArgumentsImpl (this, sel)));
- N G7 ?! ^; Q( h: n } catch (Exception e) {8 A1 r( X A+ y) h8 z' I% B8 o0 u
e.printStackTrace (System.err);
# R5 L5 p% Q% p, @. ]8 P }
4 V0 j8 r/ l9 U3 N * b- A# X2 V( j4 @1 e/ z' o
syncUpdateOrder ();
5 D7 K% l2 a+ x3 W( c0 p- v5 j1 M' Q d7 h8 w6 ^. v7 P+ l
try {
3 v8 C/ @6 _% \( y) w( K( f4 r* } modelActions.createActionTo$message
0 H* h; D7 R F, O. t y (heat, new Selector (heat.getClass (), "updateLattice", false));6 h6 L: x! z2 U ?+ a
} catch (Exception e) {1 ?7 u* C4 h2 E
System.err.println("Exception updateLattice: " + e.getMessage ());
' M' m+ |, e8 d3 M$ V/ a }
! Z3 i) s/ c) a* P 9 z+ y4 l6 `% |: R. k% Q! b, w6 g
// Then we create a schedule that executes the2 p9 V E4 q3 o: v" c6 R3 u
// modelActions. modelActions is an ActionGroup, by itself it
- n4 J. A8 _, n // has no notion of time. In order to have it executed in" R5 D' g, s4 e7 `
// time, we create a Schedule that says to use the
T# m0 ~* B: |( H // modelActions ActionGroup at particular times. This$ ?4 ^, O) B3 ]( K8 z/ E5 `8 P+ c
// schedule has a repeat interval of 1, it will loop every
/ Y% B+ T% }9 q9 _: s // time step. The action is executed at time 0 relative to
8 g" G) B6 o9 u // the beginning of the loop. S9 I/ N2 k9 F$ `. Z0 ^
. A) i; V; s7 q' ^2 d5 g( D1 m
// This is a simple schedule, with only one action that is( G1 t3 {# S& N9 E. Z" h" L& ?9 S
// just repeated every time. See jmousetrap for more
* R, `/ m, P. Y$ K' p7 h // complicated schedules.
6 g* I$ N, h7 l# T) F ) g( L, g- n! N7 C) f
modelSchedule = new ScheduleImpl (getZone (), 1);% c% K+ W/ q3 l/ _# M
modelSchedule.at$createAction (0, modelActions);# n/ G' P/ { w% U
7 {0 h; P6 y3 d. z4 B. M! h" G
return this;, M$ P: K$ A) a$ N, E$ H; W
} |