HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 o6 R7 N5 {: ]% D
8 g7 T9 } ~6 n6 L* C/ J E
public Object buildActions () {3 s$ ?( C9 ~. k" P5 u- q
super.buildActions();
( `+ G! g" m- B/ q$ U
7 G# A4 }' b0 r- I) M // Create the list of simulation actions. We put these in
( b( V. H# R" `$ b* B8 }5 T( Z3 y1 J // an action group, because we want these actions to be. H- c z3 W) g& \" ^) m
// executed in a specific order, but these steps should
/ p# {1 J. s3 M3 p, E# }- d# x // take no (simulated) time. The M(foo) means "The message! G( G) I$ [: a
// called <foo>". You can send a message To a particular
* e* G) Q2 @1 q+ }5 h, |/ j // object, or ForEach object in a collection.4 K1 J1 k2 o% u) x' Y0 {$ [& ?
% j) Y* ], E* M+ r- V# @ // Note we update the heatspace in two phases: first run
$ R; r/ F- ?) C2 e; _ // diffusion, then run "updateWorld" to actually enact the3 M8 ]" ]& K9 h4 _8 {2 v
// changes the heatbugs have made. The ordering here is
0 y+ N) Q1 I2 G5 @4 r8 u3 N z2 d // significant!' O, e6 P+ |6 Z" V! ^& x
/ l, X: [" p& r! o
// Note also, that with the additional
7 u, u) [" G0 s% r+ L4 i // `randomizeHeatbugUpdateOrder' Boolean flag we can; D3 B( o3 N+ S t, |- ?% A
// randomize the order in which the bugs actually run( Y3 U7 `' j7 _: [# U
// their step rule. This has the effect of removing any
% ]6 w9 `; ~0 J) g+ q5 r- s0 e // systematic bias in the iteration throught the heatbug
# ~- G5 Y2 J0 e' b& g7 M$ H. ] // list from timestep to timestep3 n$ s1 C* G7 K4 L
2 V5 }" ~- q( z& b // By default, all `createActionForEach' modelActions have* @7 b; a: b7 l
// a default order of `Sequential', which means that the/ I- ?. Y; a6 _, `4 e& L
// order of iteration through the `heatbugList' will be. B* _* }' j4 @5 k: ^+ y7 s
// identical (assuming the list order is not changed: {7 E; f5 `- K, K% X& a
// indirectly by some other process).
j0 H$ h) v6 G; Q6 v 3 Q* S. D4 b# X8 f& m: v
modelActions = new ActionGroupImpl (getZone ());: p1 o( h7 C: E$ Q
5 f. r: X. y+ x# `' z" M9 k try {
7 T! {6 \; n8 S5 b0 g$ I) Y modelActions.createActionTo$message& J4 V, V: @. f: J" I3 a8 w
(heat, new Selector (heat.getClass (), "stepRule", false));. ~* d* _& R( I5 A0 B
} catch (Exception e) {
3 `3 K# v' `" A$ n! M4 r) m. A System.err.println ("Exception stepRule: " + e.getMessage ());5 B$ S( [+ r/ S Z
}
. i) ]2 M0 Y' H$ r* d0 R+ D- @+ j" i# _# K2 o
try {
6 i# a8 B2 F7 E2 z8 H% g& h Heatbug proto = (Heatbug) heatbugList.get (0);
* f( p. G4 J) S Selector sel = " G' \7 N2 [- m$ }' v; B
new Selector (proto.getClass (), "heatbugStep", false);& x5 Y. z0 A+ q. J! R# T
actionForEach =
( y5 G5 @/ l4 Z modelActions.createFActionForEachHomogeneous$call. ]2 H$ W5 c8 e8 e8 {
(heatbugList,5 o9 k6 w4 d! D' y# `4 k# B
new FCallImpl (this, proto, sel,1 U9 E" F& W( W, w7 k
new FArgumentsImpl (this, sel)));( \3 {; i' }9 b; M* z i9 V
} catch (Exception e) {
+ U: b8 v9 @! _4 ?! u8 e0 R e.printStackTrace (System.err);
1 f ]) y& i( H% F8 x) \! t( z& { }
2 ?" {/ r* t% L$ {5 a ` 3 w2 G1 Y+ v) l% m" @ f0 q: [* I
syncUpdateOrder ();: ]# p2 f9 }) c2 j. S; X
* v4 T) k- y; G" h/ q4 l0 z$ X
try {' j) o2 P5 f+ x
modelActions.createActionTo$message 6 h. W, [' ` [3 ?$ ~
(heat, new Selector (heat.getClass (), "updateLattice", false));" h: q$ w x2 r k
} catch (Exception e) {
% f7 C) z/ Q, k System.err.println("Exception updateLattice: " + e.getMessage ());6 B8 ]* u: @# S7 a3 t0 N. B
}6 V' a( i% X( d
# ~( f4 P: Z# q4 o! H
// Then we create a schedule that executes the
( S: U% O( e+ {. i0 q5 Z // modelActions. modelActions is an ActionGroup, by itself it
( q+ [7 l8 g" y4 [' E- _. i // has no notion of time. In order to have it executed in
) |* F' v3 q) v4 K; t) v& J // time, we create a Schedule that says to use the
/ ]3 }; F- T, e. {: _: | // modelActions ActionGroup at particular times. This
) c% S0 z4 O0 i1 I. G' q // schedule has a repeat interval of 1, it will loop every
5 g( \: `, w9 C7 d8 ^$ e3 M- C // time step. The action is executed at time 0 relative to3 U) n& O" S: \* \& f
// the beginning of the loop.
4 e5 F+ I P/ ?' M/ e4 A' c* @4 V! B: ^3 L# Y, O9 E2 k2 ?
// This is a simple schedule, with only one action that is
) F( P4 x1 q5 m8 Q* ^ // just repeated every time. See jmousetrap for more
) ?2 G+ e% ?* B6 W% Q // complicated schedules.( b' R4 C- v- T8 d$ c6 Y
. `1 R! k- H- d' Y
modelSchedule = new ScheduleImpl (getZone (), 1);
. a; @! n. { P+ \2 r1 b$ Q: h! @/ m modelSchedule.at$createAction (0, modelActions);4 D" `( T7 Q/ ?' L5 q
% k! {. F9 |# b# c, K- g8 t return this;
/ G% ^) y) s! k! Q3 ?6 V+ a } |