HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; @& V; l+ p \$ [
z0 H2 f1 Y4 D! |/ @ public Object buildActions () {4 p2 m A* r. g
super.buildActions();
# T, x s& ^4 @: D; h/ T
1 x$ r) K' d9 f h' Z // Create the list of simulation actions. We put these in5 R& ~. ~/ \+ T) h! p% [
// an action group, because we want these actions to be
$ i1 @" Z# q9 I& w+ K) d" Q // executed in a specific order, but these steps should0 q8 Z* x |+ Q$ s) m$ {; O
// take no (simulated) time. The M(foo) means "The message
: }" Q) J0 S% ~1 j3 Y4 ? // called <foo>". You can send a message To a particular% n0 w, a1 C: K
// object, or ForEach object in a collection.; f0 t& \, r# G" D
9 F/ n- q G4 ]8 d3 Z // Note we update the heatspace in two phases: first run1 }1 w! t$ l' b. Q
// diffusion, then run "updateWorld" to actually enact the! j! l' k! O' w, o
// changes the heatbugs have made. The ordering here is8 Q; Q8 g3 \# V J5 @5 ~0 L
// significant!; S0 F( D& C) I K
+ K1 q9 s8 \% u( c4 Z // Note also, that with the additional
" L1 `' T: ?3 y // `randomizeHeatbugUpdateOrder' Boolean flag we can9 k( `3 Q, p' X" ^) V! \! G
// randomize the order in which the bugs actually run
3 M7 i& A- o2 ?; [ // their step rule. This has the effect of removing any
( \- h" v9 d1 G6 u$ n8 i // systematic bias in the iteration throught the heatbug
R; Q, p/ J2 `9 F4 K: K% l) L6 u // list from timestep to timestep% p$ |* x, i$ R* s
1 R6 w4 h' Y. {" O7 w
// By default, all `createActionForEach' modelActions have
2 e/ w" H' B+ t) x // a default order of `Sequential', which means that the+ ~/ U# D1 |% ] a3 p. L
// order of iteration through the `heatbugList' will be
, @0 u9 V& |7 \1 q4 f! m+ m9 f // identical (assuming the list order is not changed
. b. ?" o( m; U: A5 `& ] // indirectly by some other process).' W# n6 Q% {% R D; ]) |5 L
1 `/ d7 @$ a. X# T2 m* i0 Y
modelActions = new ActionGroupImpl (getZone ());
* ^, i( |8 F% T5 \# c+ o. O5 o' K$ [4 q q/ J! S H: Q
try {
$ V7 M% S- X- |5 R0 f modelActions.createActionTo$message! A* k9 r. B. X' U9 I
(heat, new Selector (heat.getClass (), "stepRule", false));6 S/ P k, f" }7 K5 w
} catch (Exception e) {
8 P) R9 W$ l: k+ D" G* g5 ?5 _ System.err.println ("Exception stepRule: " + e.getMessage ());
8 b+ B2 U! l' f) G2 B( F! B" A* Q, p }/ s# w# o9 U$ C8 D& O( N t' W
# u8 w9 [: b; l) R# k2 D: r
try {
" H& z9 |% }- I Heatbug proto = (Heatbug) heatbugList.get (0);
, v1 l5 ^: q/ z+ L8 _ Selector sel =
" |- v$ A1 q" U, }9 z' B" M& Z1 i new Selector (proto.getClass (), "heatbugStep", false);
r4 \. d5 i$ ?. Q! S, e2 [ actionForEach =
3 Z% \: x* V0 N9 n! [: _ modelActions.createFActionForEachHomogeneous$call
) W1 a) E! K8 |" @- m, { (heatbugList,
* K2 O! ^9 O) y4 T7 B9 \ new FCallImpl (this, proto, sel,
: k6 |$ W& z/ [0 U3 v new FArgumentsImpl (this, sel)));
" ]' J5 w. O3 ?# e- Y% N# C' k } catch (Exception e) {, E& ?, w' `5 b% J0 D0 N. `, _
e.printStackTrace (System.err);
" Z R( G9 a; v6 P }) _4 X& e* J1 A, L+ D1 u; B' P
& |9 ]3 X+ W' }. ^% n0 D, C syncUpdateOrder ();
2 Y/ |9 `* |6 o! L5 l
& G2 B( l6 F6 v) |; h! D try {8 b( t+ U4 q) g# N
modelActions.createActionTo$message
7 H$ x9 ~! |7 z& U (heat, new Selector (heat.getClass (), "updateLattice", false));
) I2 }6 R+ y' i1 E- T5 N9 S5 P- ` } catch (Exception e) {* M& `( D! W2 s- K0 q
System.err.println("Exception updateLattice: " + e.getMessage ());
, ~) [ G" r2 E t }9 ] N' G V4 K a0 ~6 l
/ M% G/ S; Z# |; J# y! G6 O3 k* i% l
// Then we create a schedule that executes the6 J- S( R. Q' j& }' ?. Y! z+ X K
// modelActions. modelActions is an ActionGroup, by itself it
0 T% l& ^/ C! s8 U. O% `4 P% X* U // has no notion of time. In order to have it executed in9 F) y7 \6 v+ X# @3 I% {
// time, we create a Schedule that says to use the( n4 n' X4 s7 h' l3 N9 s7 A8 Z
// modelActions ActionGroup at particular times. This& n; ~* t( u) l
// schedule has a repeat interval of 1, it will loop every* q) B( K6 L0 R
// time step. The action is executed at time 0 relative to
; U* F* ^# H/ y" c // the beginning of the loop.
* o) R% B$ N! ]1 I+ l# D* F, Y, d$ Q8 j8 G ?
// This is a simple schedule, with only one action that is; y k) z+ {: Q: S
// just repeated every time. See jmousetrap for more
2 u4 C1 ~4 u, p+ W9 q // complicated schedules.$ ^) M% F/ N# Q! |9 m2 \
% s+ P+ H2 O# f/ p4 D* g& J modelSchedule = new ScheduleImpl (getZone (), 1);
4 w8 |4 T( l& _2 H) Q3 i- ? modelSchedule.at$createAction (0, modelActions);
n! ^! p5 p& h8 `$ ?
$ h; E" N' {/ G/ J& [# Q return this;5 N# I- W8 i2 [" {, d1 W. ~& M& i
} |