HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- T) x+ ^; |. t6 B1 w4 p" i
, X9 ]! W3 \; F0 Q public Object buildActions () {/ ~ V1 O* E% ?; N5 w
super.buildActions();
$ n2 B/ ~ B+ t 2 j9 H# f6 u% q2 D! x4 t
// Create the list of simulation actions. We put these in
( G5 f; I4 h( Y8 H // an action group, because we want these actions to be
5 B5 R: ^: j6 x+ H* N // executed in a specific order, but these steps should
9 y. B: i/ T. X$ m6 D$ C( V // take no (simulated) time. The M(foo) means "The message
# y# b# C4 U& x6 m' I) X. b // called <foo>". You can send a message To a particular) |" U0 h% G! A+ U6 S2 @
// object, or ForEach object in a collection.
3 D) G" a/ s) O& A' t 0 e3 U5 j/ _5 f& P# a# u$ `1 _5 I6 m ^2 G1 X
// Note we update the heatspace in two phases: first run( S" q( V9 @4 t
// diffusion, then run "updateWorld" to actually enact the* B( I' S0 X: @, P9 u- l
// changes the heatbugs have made. The ordering here is
% b8 `3 r y+ m; f) V! R% { // significant!) U' S+ a; r% c6 k" e1 J* G
" v9 @+ v B$ C( X3 ~5 n5 r
// Note also, that with the additional
) g$ g+ {$ S7 P6 [% H3 w // `randomizeHeatbugUpdateOrder' Boolean flag we can
1 w7 T' l! {/ T# w& j // randomize the order in which the bugs actually run2 }, \6 O; c) p
// their step rule. This has the effect of removing any# @8 n6 ^2 g+ z4 U9 B% T. p
// systematic bias in the iteration throught the heatbug9 W- V2 A9 R. i& Z6 x; N
// list from timestep to timestep7 b* S5 F1 W; o" f7 ~4 N9 I; y
& d5 o+ |' ^( x" ^9 ^ h
// By default, all `createActionForEach' modelActions have7 W* F' X$ T8 C& `
// a default order of `Sequential', which means that the
& d+ _. E! K% @4 j7 Y: M // order of iteration through the `heatbugList' will be# l, n: f, e- E+ t3 j" j
// identical (assuming the list order is not changed
7 D/ M7 {0 T% |+ K5 W // indirectly by some other process).7 N6 u$ t8 w! z+ p! z, v* }
+ r/ V% u7 Y! V6 u' C0 O- w% E modelActions = new ActionGroupImpl (getZone ());9 Z' e; t, U* F- d' k
+ A, T) s, m( q9 N9 g. f) M
try {8 J! o4 [8 Z8 U- {- P0 Q
modelActions.createActionTo$message8 \6 S- U1 x, f: {0 x1 u# u: j( T8 A
(heat, new Selector (heat.getClass (), "stepRule", false));
6 V1 [& n x5 c } catch (Exception e) {9 B5 U; Y! R5 o
System.err.println ("Exception stepRule: " + e.getMessage ());
) K3 W3 n3 G( h8 E' i" r }8 R& g R9 |1 ]0 f3 ^4 w5 }
0 a) V2 f% K, M) g' f try {* C0 }% }1 Z$ K* M# V
Heatbug proto = (Heatbug) heatbugList.get (0);
% {1 o% d8 Y6 y Selector sel =
% ?2 D* M# T5 ^) D new Selector (proto.getClass (), "heatbugStep", false);! M; j; O& z5 M$ ?) q
actionForEach =
@3 O2 V& J0 l' |# `% W) E7 f6 b modelActions.createFActionForEachHomogeneous$call
9 m0 ~2 A5 W% N1 b8 k (heatbugList,
2 o0 e u3 Q M$ Z. r7 @ new FCallImpl (this, proto, sel,
. `$ O: j. w* h f9 d new FArgumentsImpl (this, sel)));
: J- b9 N5 D+ E) f; _4 z+ g4 M } catch (Exception e) {; k5 @! k* T/ N; P5 J0 |
e.printStackTrace (System.err);
9 @0 y' e5 s/ _0 B' U& C8 h }
' I) n; \4 q# t4 U. r " ? |$ r/ F3 v. A9 I0 X+ e' U
syncUpdateOrder ();, }/ q5 k9 e; m4 m
8 [) S, @, P J a' @" Y1 P# _ try {
6 e6 F' w' k7 K6 L0 x& s modelActions.createActionTo$message A0 i( g% n0 L
(heat, new Selector (heat.getClass (), "updateLattice", false));- r! {" }* a) s, O9 r
} catch (Exception e) {
5 x( I7 E7 C, E' z; L System.err.println("Exception updateLattice: " + e.getMessage ());, a* Q( F) s) P/ W
}
& A0 P' ]/ c8 Y! | & O/ B- |/ Q. d9 @+ m
// Then we create a schedule that executes the
- f$ ?- `9 M( C // modelActions. modelActions is an ActionGroup, by itself it* j% ?, i3 C/ T5 w7 _
// has no notion of time. In order to have it executed in
/ g" P% @. |+ Y1 @ // time, we create a Schedule that says to use the. H& d- C" O/ H4 T' p2 F
// modelActions ActionGroup at particular times. This7 k5 I6 c! Q* G2 l+ I- u7 M- v
// schedule has a repeat interval of 1, it will loop every
. A: j1 @1 l" B" g( Q$ f4 M( K# }! X // time step. The action is executed at time 0 relative to" M: Y6 ?1 ^ S( c5 e2 ]) I
// the beginning of the loop.
9 f' ?) Z5 S C/ M' ^4 A; | I! @4 w3 [& A1 C
// This is a simple schedule, with only one action that is3 M* f5 r2 i- B- ~$ Y0 _; S
// just repeated every time. See jmousetrap for more) b9 g) m& @1 z& i. Z. p
// complicated schedules.
( Z/ v7 N6 E/ _) ^8 w
+ [ V( A, I- n9 t1 C; [- c modelSchedule = new ScheduleImpl (getZone (), 1);
' ~% e, ]) i3 Z; D6 E3 I modelSchedule.at$createAction (0, modelActions);# U, k2 d9 h4 z# y5 P
4 l% g( @1 e( V% n6 k1 r$ o2 t
return this;
3 F" R1 Q6 P5 g1 d' a8 |0 C } |