HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- l" m% C3 [+ [
1 m5 ^6 d) X( Y4 s( E$ J Q public Object buildActions () {
+ d: P l2 d" D- H0 n+ Y$ x super.buildActions();3 g1 r3 m; _4 J" P, W% z3 M
) o# d4 v$ t6 j5 t' y% k // Create the list of simulation actions. We put these in
, C, Y1 }9 O7 k' k // an action group, because we want these actions to be2 C/ U" Z# N# {
// executed in a specific order, but these steps should* Y0 V3 j3 u8 o4 B" W
// take no (simulated) time. The M(foo) means "The message+ B; f/ {1 L+ Z; B
// called <foo>". You can send a message To a particular
) q0 W0 w9 U. h8 b" w7 G4 Z // object, or ForEach object in a collection.! k+ A9 e v% K2 V$ t. K) [
$ P& Y0 N" H' b6 Z6 k
// Note we update the heatspace in two phases: first run
6 _ {$ [9 R8 `- h; q, n // diffusion, then run "updateWorld" to actually enact the
8 ?8 k- ^# S8 G2 J // changes the heatbugs have made. The ordering here is
+ ]' w, N! A0 S/ O& \ // significant!
/ c2 ~6 q( f9 c2 T' \
+ m9 s$ Y# e( t8 O // Note also, that with the additional
/ }2 K' e c& f0 b# v // `randomizeHeatbugUpdateOrder' Boolean flag we can
2 L$ O0 t1 t* G4 g1 b' N' s // randomize the order in which the bugs actually run
: D0 y, N' W' ^* E // their step rule. This has the effect of removing any
7 {* M3 w# T& a7 X4 Q // systematic bias in the iteration throught the heatbug
% |# b9 q6 q# L9 o3 O* t, s // list from timestep to timestep* b' b# I/ H; d) a7 x0 b
1 u* e1 y, v4 m N% |
// By default, all `createActionForEach' modelActions have; D7 @' z6 u8 I1 A/ x( U
// a default order of `Sequential', which means that the" B' @; x6 h9 l7 @
// order of iteration through the `heatbugList' will be
' i! W! h$ I& O: m( h% e, {$ b% W // identical (assuming the list order is not changed
7 H8 ^6 i. A* P2 e' _( u, u7 J // indirectly by some other process).$ k% C/ T9 R/ N! d5 J1 {
& _8 i- f e. s( j3 H
modelActions = new ActionGroupImpl (getZone ());
+ |; j9 y1 a/ w' w4 i" Z+ q# q! H2 y# C5 `7 d6 k
try {
2 |6 V# f" C3 U4 n f N modelActions.createActionTo$message3 Z! j" r1 {3 c+ B+ {
(heat, new Selector (heat.getClass (), "stepRule", false));
( Q; V/ i& @. V; s } catch (Exception e) {
- K& y2 z: f9 Z( v' I& V9 X( a System.err.println ("Exception stepRule: " + e.getMessage ());
" S! C l' d8 Q! o& h+ @5 C, Y }
1 _) |/ E3 @0 i. Z1 J5 O# i# F6 J+ P" c1 ~3 G* |; n
try {
( J1 a; @, i; F, N! L0 e! h Heatbug proto = (Heatbug) heatbugList.get (0);% `- p* }/ t* w
Selector sel = 7 }2 L& {' c( L
new Selector (proto.getClass (), "heatbugStep", false);
7 w" \& \% W: O W9 ? actionForEach =$ \# R) } W/ p/ ?, y, q+ Q
modelActions.createFActionForEachHomogeneous$call
" S6 m! E* c9 x (heatbugList,0 T8 D9 B3 T0 ?3 G
new FCallImpl (this, proto, sel,# v; B+ i) t5 F( E: r" z
new FArgumentsImpl (this, sel)));$ T3 B0 E$ J4 T! w2 K% }9 I/ P
} catch (Exception e) {
5 q/ A9 T' ?) S5 ]2 B- g v2 e; s. D e.printStackTrace (System.err);
5 U6 x! |& t* e( I' \% @! P/ _ }2 f$ v3 D- h/ _, Z9 v% N3 x
' e# _) Q8 G h8 F syncUpdateOrder ();+ K5 q7 ?& n& v3 B- Z( H3 R7 |
+ _4 W. I) K" h7 p! z1 ~ try {$ E t8 L' n: i7 K
modelActions.createActionTo$message 2 Z5 Y8 x' H. b. t5 F' h; F# B
(heat, new Selector (heat.getClass (), "updateLattice", false));
" i B7 K; w$ t: B: ?. n } catch (Exception e) {
: I: v C, A5 a System.err.println("Exception updateLattice: " + e.getMessage ());
1 @- u/ H& C/ }% q8 S }
% m" G5 I0 I6 [, ~; B1 F4 Y3 t . a# Z; ]! B' m' D! c
// Then we create a schedule that executes the& T; ~$ b+ {: I
// modelActions. modelActions is an ActionGroup, by itself it
( @2 K3 d$ E, J$ q& R // has no notion of time. In order to have it executed in
2 v0 n( g$ C* J( T' ?( y // time, we create a Schedule that says to use the& K1 _( J& |0 [: h; W
// modelActions ActionGroup at particular times. This! u6 s! i6 v; U. L% C7 n' K5 J+ e6 d
// schedule has a repeat interval of 1, it will loop every1 B, @" i9 D& U4 u& z
// time step. The action is executed at time 0 relative to0 j6 ?( C4 V @7 c& z
// the beginning of the loop.4 t- `/ ~* A7 h0 T( N
0 N I! z, n& |" F // This is a simple schedule, with only one action that is
( l: b1 Y# J2 [' X% a7 s6 P( k9 ?5 o5 \ // just repeated every time. See jmousetrap for more
5 f2 l0 x3 L5 N4 { // complicated schedules.
$ X9 P4 S1 w' f+ T' C/ X
* u9 I2 o" \# b, z" S6 s& q& Y modelSchedule = new ScheduleImpl (getZone (), 1);. |1 a$ G% V$ Y4 ^+ P; D. C/ T
modelSchedule.at$createAction (0, modelActions);
( M1 t( u. e8 h4 \ Y
: G' t$ Q; P6 g8 t7 B return this;) G8 O5 V* k+ g2 H3 \+ N' I- g; d5 x
} |