HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
5 X7 x: @$ e+ S* _2 ~. a, i4 e" m
9 n4 O; q L* _2 I& o public Object buildActions () {
$ I& J8 V# Q, n. L1 c& G super.buildActions();- [0 I+ W9 r' n0 b$ V# F' W( A
8 S c& x2 c. v% M# r // Create the list of simulation actions. We put these in X0 z: j' f1 I4 E: @
// an action group, because we want these actions to be
0 H# g) ~5 e; b( A8 } ^ // executed in a specific order, but these steps should- h$ c# k/ f, t$ {8 @
// take no (simulated) time. The M(foo) means "The message
) i/ X, r% J1 \7 u; v // called <foo>". You can send a message To a particular
7 d* Q9 s- c. G // object, or ForEach object in a collection.
0 M$ k# I6 d$ x/ |0 o 1 |7 S2 s! I+ ^8 N6 }
// Note we update the heatspace in two phases: first run
( i9 p2 ?# c* x% |' G" k4 X a // diffusion, then run "updateWorld" to actually enact the
! W* n8 j# c* y; m n2 M- K // changes the heatbugs have made. The ordering here is4 [' e$ ?) Z9 G, s6 t
// significant!
9 e) d0 C: B* v3 { " w, ]1 `7 O( f. D
// Note also, that with the additional) e1 P, D& }5 `# ?9 Z
// `randomizeHeatbugUpdateOrder' Boolean flag we can
" {! k- O, G" p# d7 r1 X0 ~8 K // randomize the order in which the bugs actually run
/ ?& Y( M) H' Z# d* }# J8 K# a // their step rule. This has the effect of removing any
, P& d& W! R% e# w( k( `; g // systematic bias in the iteration throught the heatbug: R! E4 o- W% e. t t r5 k
// list from timestep to timestep
9 @* t7 }& O4 \6 c# p& c S0 H0 `& s5 [9 P& r/ e; p
// By default, all `createActionForEach' modelActions have- f6 C2 {- T5 j2 t
// a default order of `Sequential', which means that the" U$ K7 Z5 w) P1 [8 p1 B8 ]
// order of iteration through the `heatbugList' will be7 d. g0 ^1 h7 }4 c9 ^/ ?: V+ O8 t
// identical (assuming the list order is not changed
' P$ ]3 R7 \3 @5 i* w) E R // indirectly by some other process). O: Z# o1 T& j: ]
2 Q# X7 {; e. z
modelActions = new ActionGroupImpl (getZone ());/ S$ l, F# D' J3 Q6 `% ]- G
7 n& G6 J( d5 [9 {% i( ]
try {
% `* {$ ? v: [9 x3 O modelActions.createActionTo$message( |; i" ?1 r' ~1 }+ M7 O
(heat, new Selector (heat.getClass (), "stepRule", false));6 a0 O2 U3 a' o9 g1 N
} catch (Exception e) {
$ k% [- Y% `/ o$ f System.err.println ("Exception stepRule: " + e.getMessage ());: Q! n5 y0 ?' ]9 W
}, I& j4 X: A0 J) Y) {
& D% @5 L) ?$ h3 G4 U
try {
$ a3 Y- ?" a( _7 H6 u c Heatbug proto = (Heatbug) heatbugList.get (0);. t& r* Z/ l5 \% L: k5 i P
Selector sel =
- L) o+ W7 b0 F1 P0 I new Selector (proto.getClass (), "heatbugStep", false);% q9 I7 ]. B' w4 k. D0 {0 V# M
actionForEach =* p5 _ g/ D- V
modelActions.createFActionForEachHomogeneous$call
1 b6 g6 I4 q5 K0 z+ ~. i D( z, d8 E (heatbugList,, v/ u9 D2 O, _" I( d
new FCallImpl (this, proto, sel,. W) H3 E2 E5 R$ m
new FArgumentsImpl (this, sel)));: ^5 I, r% @* m7 i
} catch (Exception e) {
. c9 D$ N* q! ]8 \+ S- _ e.printStackTrace (System.err);
2 ]1 E# C- W% O5 d }
4 q8 X3 S9 a: i
+ }: T0 B$ n' E# W5 i syncUpdateOrder ();$ N# A, }, }7 k9 R' ^
0 L5 L* k6 F3 _6 s& q3 b% u
try {
. }' [' [+ y# M7 X; F modelActions.createActionTo$message . ^/ m' t1 ?8 i4 ^) `6 n
(heat, new Selector (heat.getClass (), "updateLattice", false));$ a7 o; y" ~9 v4 n$ g
} catch (Exception e) {
7 ?. v& y, b) f9 U" x$ g$ E, l2 A System.err.println("Exception updateLattice: " + e.getMessage ());
4 @0 e8 C' B! \/ S0 P4 w5 B }" c# _# c5 d) e: I. M/ X. ~
0 N- r2 }3 S# M4 K7 a
// Then we create a schedule that executes the
9 D5 U) M1 a$ E* e // modelActions. modelActions is an ActionGroup, by itself it% F% [. }0 L2 T# M/ [
// has no notion of time. In order to have it executed in
* q2 ?8 i8 F2 U( Y ] // time, we create a Schedule that says to use the
1 _# a. A W7 b* S // modelActions ActionGroup at particular times. This
6 R7 [& o- e6 W/ A+ K7 W3 Q // schedule has a repeat interval of 1, it will loop every
8 |. \' W3 Y' }3 p // time step. The action is executed at time 0 relative to
+ s+ [0 E2 o$ k- O // the beginning of the loop.% l+ a' a8 f1 r! a \
$ h" C; y& v& Q2 s$ U
// This is a simple schedule, with only one action that is6 G; f& ?9 d4 |/ x' ~. E
// just repeated every time. See jmousetrap for more( R9 o- G4 Y# s V
// complicated schedules.
5 O X) O& h. i" f' ~! P
4 w& ^# P0 z- O1 ]8 M% |+ w modelSchedule = new ScheduleImpl (getZone (), 1);+ j: R+ ?+ D8 }* `4 B4 c
modelSchedule.at$createAction (0, modelActions);+ |: _$ M. p5 Z! V& z
% _. H; \) u- K4 ^6 d5 M4 t
return this;
9 L, w( w" X5 L: j } |