HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
2 u& ?6 P* _. a, ]: d) N; ]+ Q# _9 a' t! K; s# E3 Q5 G, {; X2 w
public Object buildActions () {
9 Q. V8 p# O N( p( D super.buildActions(); P7 R* w! N) @! \
7 k- K1 I5 }. d$ L/ a" r
// Create the list of simulation actions. We put these in
+ W u- v' X* y2 l+ _- A0 ] // an action group, because we want these actions to be/ v" _+ g Y8 N9 z1 {# \
// executed in a specific order, but these steps should! _0 X# C+ v4 `' O8 u1 Q
// take no (simulated) time. The M(foo) means "The message4 r$ i% D0 j6 R, Y
// called <foo>". You can send a message To a particular: V% ^2 l5 k" N t2 q: s( S6 m% M) r
// object, or ForEach object in a collection.0 A9 w$ q, H: q( Z$ |
! v$ r3 j; b) e9 z // Note we update the heatspace in two phases: first run
, u# R n. S/ h+ c$ M8 o$ ] // diffusion, then run "updateWorld" to actually enact the
: F0 ?# R8 ^. B9 a) M/ b0 D2 X // changes the heatbugs have made. The ordering here is/ \3 y4 d f" ~
// significant!
# i2 @. l% a8 e6 o! ?/ W! a5 P " {9 ^5 }' J( _+ T5 u! F
// Note also, that with the additional
+ v! n$ {3 ~5 c // `randomizeHeatbugUpdateOrder' Boolean flag we can- L2 `' R- v! t9 F+ L- W
// randomize the order in which the bugs actually run
" x& D( g0 b8 g; F8 Y# l- ` // their step rule. This has the effect of removing any
9 k/ C0 Y" S) T8 R9 l, ]) ` // systematic bias in the iteration throught the heatbug
$ V3 @2 `9 A! W% J q; h5 {& w // list from timestep to timestep/ R8 T# b+ Z7 m7 T2 {6 ]
) b% c3 H- t2 I- P. B // By default, all `createActionForEach' modelActions have/ P2 [; L" n; H) r; B( o
// a default order of `Sequential', which means that the
) K( d2 s5 u0 R/ w4 m" d4 D // order of iteration through the `heatbugList' will be
& T& ?4 L. l0 e$ N( x% _- k9 ] // identical (assuming the list order is not changed
6 n2 F N( @9 l& [# ` // indirectly by some other process).
" J- w5 r5 N2 V. i - X. [7 q& X P- N) k+ Y$ g& U
modelActions = new ActionGroupImpl (getZone ());% d8 l5 w4 ]9 F F1 x1 d
# x$ e7 Q& v9 B( g; l, S
try {
# W6 `+ W9 ]# |1 `" u5 Q- z) C: P modelActions.createActionTo$message
7 E: @: [& P" ?% n: }: |$ Q0 p (heat, new Selector (heat.getClass (), "stepRule", false));
# `+ z5 l3 ]# r2 t" N% Z } catch (Exception e) {: T4 Y9 I7 l; S2 V
System.err.println ("Exception stepRule: " + e.getMessage ());. N q! Z# X( n, ~) @0 X
}
3 p c8 i- y6 n. E! x1 `' `6 p. y, k9 m% g8 G& C" y
try {0 F" F9 G( B, b6 X
Heatbug proto = (Heatbug) heatbugList.get (0);* Y, \2 ]% E" F( u. ~6 C
Selector sel = a) @1 {, Q/ ]# T8 i* c
new Selector (proto.getClass (), "heatbugStep", false);
! L, T3 t* A: I) x _! l" O6 G actionForEach =
) S: ?. A& V- W; F$ [ modelActions.createFActionForEachHomogeneous$call
( x8 c' Y! C2 ]: U2 t8 B (heatbugList,
S6 p/ k x. [: a' k* y9 k new FCallImpl (this, proto, sel,% l4 M: p* h$ E6 \) z; k
new FArgumentsImpl (this, sel)));
$ q0 z' T" Q/ ~: v6 E+ L2 H } catch (Exception e) {" o& G; I: H1 o5 ?* [+ y/ l& Q
e.printStackTrace (System.err);
# m t2 ]. j! ?7 s, ]+ d }
6 w6 @- Y* d$ p( T0 u. c% }- h $ D9 u8 a! T# n+ y0 O2 F$ t8 b* ^
syncUpdateOrder ();
9 c! c) x$ J* R# J" S4 E2 U1 B5 W" ~+ z( j
try {8 R1 s4 g$ L2 F' b) L& b: ]
modelActions.createActionTo$message : b( t8 _( d) U
(heat, new Selector (heat.getClass (), "updateLattice", false));
! M" }# g2 L5 r4 `. F } catch (Exception e) {
: u8 J% e# [. l1 W- k9 W m4 P System.err.println("Exception updateLattice: " + e.getMessage ());
9 z/ ?% P% ?2 J } O$ S1 U' V( n
+ R* O8 q1 A5 R
// Then we create a schedule that executes the/ R+ C0 O0 q" X6 a2 g
// modelActions. modelActions is an ActionGroup, by itself it
3 L- j5 L$ H; F0 l6 L // has no notion of time. In order to have it executed in
+ X( F" P$ Q5 W7 D' e // time, we create a Schedule that says to use the( w/ P- S6 o% ?
// modelActions ActionGroup at particular times. This! Y( ^4 e$ Q4 w! S. P$ T+ M+ {
// schedule has a repeat interval of 1, it will loop every
8 ^3 ]2 k/ w' \0 Q, R2 q // time step. The action is executed at time 0 relative to$ }8 c8 J1 G+ i
// the beginning of the loop.
: R2 `, D4 G( O4 m5 `# Q* {1 n
6 ]2 P" N& ~( g* W // This is a simple schedule, with only one action that is
" y3 P% r* m1 r1 T // just repeated every time. See jmousetrap for more
, b R$ U; C3 J; o- i' j // complicated schedules.; Y* s/ ]. C& T6 A7 V8 p
! ^1 H- |. ]5 h }/ \ modelSchedule = new ScheduleImpl (getZone (), 1);
/ F% i' C7 I; B; U modelSchedule.at$createAction (0, modelActions);7 [( @$ ^/ _% m& q0 h% F8 P+ A4 c
6 |0 G6 u/ E( e8 d% r+ Y T! z return this;# f( n' v/ @9 U% z
} |