HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:' M, Y( f5 ~$ Q3 ]" g
1 T* Y. x m" Q! ^ k' [( @ public Object buildActions () {* m8 ~" A( f8 G d V3 @ o/ i
super.buildActions();
9 @" e5 ^2 _7 J% c$ E- w# w : E- k2 _# |+ {5 N0 |) N- B5 f% N
// Create the list of simulation actions. We put these in4 N0 x1 C4 Z$ G/ l: C. g% A
// an action group, because we want these actions to be
9 {: A7 \, N; F: G4 a2 m // executed in a specific order, but these steps should
: |4 W9 W, o3 c. q% D // take no (simulated) time. The M(foo) means "The message
. }5 S7 p" v+ N4 j2 C // called <foo>". You can send a message To a particular3 q* D, ` o( N: _( Z
// object, or ForEach object in a collection.0 {/ K; o$ v9 r* E8 E# b2 S
# Z6 Z2 A/ w7 D+ ^: c // Note we update the heatspace in two phases: first run$ y A) x+ y1 Y- m2 W
// diffusion, then run "updateWorld" to actually enact the5 |8 m3 X4 l0 }
// changes the heatbugs have made. The ordering here is
7 U0 z$ D: v* B // significant!' l8 `. Q5 E! p# ~& E! [
" {+ C% E: w& Y1 l3 F0 m // Note also, that with the additional9 E" {1 J% B9 M ?# g% k
// `randomizeHeatbugUpdateOrder' Boolean flag we can1 ^; |% L9 C6 g; _3 K* M: t
// randomize the order in which the bugs actually run
: P) e- A; _* s9 X% v // their step rule. This has the effect of removing any
2 y }! @# f' t5 \/ O& P% T // systematic bias in the iteration throught the heatbug
& O( V8 R. }$ ? // list from timestep to timestep4 F9 |" [% a% h- [3 w, N
0 Y6 O* U- L: w6 O+ u9 D% h& ]1 o
// By default, all `createActionForEach' modelActions have
" }$ T$ j7 E& R$ Z // a default order of `Sequential', which means that the
: B! Y1 A- I2 [* p // order of iteration through the `heatbugList' will be1 {6 T) Z/ |. i- S) z) X
// identical (assuming the list order is not changed5 M. E5 j' u! K2 }9 r: t
// indirectly by some other process).
: t# f) t6 l0 y5 J 5 Z& `6 t7 o2 A/ ~/ |
modelActions = new ActionGroupImpl (getZone ());
5 ?& |& T! \. T$ H% X. a" D- L: `. ?: h. X F
try {
% g5 l2 Y+ n6 L9 t' [* c8 v modelActions.createActionTo$message6 j! W# ~& x( `( G( c
(heat, new Selector (heat.getClass (), "stepRule", false));
1 E h: I* X( N( E } catch (Exception e) {' ?" X ^8 ?5 J. y
System.err.println ("Exception stepRule: " + e.getMessage ());; g+ M1 N; X [: w1 M) U! d4 Y7 [
}
1 r3 X/ j C. o9 v2 {* b5 R9 d ?- G0 n* ^2 J, s
try {
# B+ E8 T* P0 B- h0 g: o+ B: E Heatbug proto = (Heatbug) heatbugList.get (0);
+ J5 e7 x& n5 y2 `) N3 A Selector sel = " O- d0 ?+ q o' Z: p8 r. g- Q- O
new Selector (proto.getClass (), "heatbugStep", false);
6 y" X3 [2 n; y4 S/ ?5 ~) \6 n actionForEach =
- ~2 t: o2 q5 w: q modelActions.createFActionForEachHomogeneous$call+ r2 F3 }) O: d+ h6 X: h$ w
(heatbugList,7 j! n' n3 O% f P2 M7 L
new FCallImpl (this, proto, sel,3 k9 F! V- w9 G6 p* ~1 B, s6 n
new FArgumentsImpl (this, sel)));" `& W# c+ P0 W
} catch (Exception e) {
$ C8 E" o* B' t6 j$ n( g9 r. W e.printStackTrace (System.err);
! j' m) _$ R1 p8 G; M5 j% @ }
% i! w0 l2 F9 f. C 8 o2 R4 r# T8 \% K
syncUpdateOrder ();
0 x. l+ @/ H- N. v6 p: r
( q) ~3 Y; c" |1 W" s try {
) I n) C# O# w8 {- k modelActions.createActionTo$message
5 O/ ]; F7 M0 ^/ I" p1 h7 q (heat, new Selector (heat.getClass (), "updateLattice", false));4 s8 j( h& [% x1 H
} catch (Exception e) {
6 Q1 P$ H1 N0 p' L& D. k System.err.println("Exception updateLattice: " + e.getMessage ());
9 z% k/ {; ]# I: c }! z" P, c9 n- p( B5 C! K& [ o
" h, V) k M4 k! o
// Then we create a schedule that executes the) T$ y( Y, E0 j, G7 e- ~
// modelActions. modelActions is an ActionGroup, by itself it7 }8 g- @0 B/ \7 j* J+ P" |
// has no notion of time. In order to have it executed in: n0 G# P( E0 G* m4 }0 g
// time, we create a Schedule that says to use the* j5 a1 I' G# F; q
// modelActions ActionGroup at particular times. This
8 t+ X4 C; M. u. v4 G1 n- q% f // schedule has a repeat interval of 1, it will loop every. Q, o+ e S" V& Q+ {/ X3 ?
// time step. The action is executed at time 0 relative to/ i% e4 p! ~9 O2 {5 i3 r1 O- p
// the beginning of the loop.
" O: \% x# I+ a! O: g
4 f- W. ?/ g0 ~2 I8 q+ h // This is a simple schedule, with only one action that is. |& C+ S( @, l0 j2 U; ^1 Q! j; l+ y
// just repeated every time. See jmousetrap for more: e* W0 t9 t: K; Q% S/ x; r* N
// complicated schedules.3 W) M- k- f6 M% ^( v* _
2 D, ]) ~3 q- ]7 v
modelSchedule = new ScheduleImpl (getZone (), 1);
+ S$ Y# s. s- r/ i/ n2 ^ modelSchedule.at$createAction (0, modelActions);& }6 r. u1 k# j( K$ k; m9 v. P; L" m
3 |( D; }+ }( k6 H return this;$ P5 O4 s1 p! y4 E+ d; k$ ~
} |