HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- f' ?- F! n3 a( Q2 b& v
' u" @7 Y6 y, k) W1 G public Object buildActions () {/ b# f/ }- n* m0 t3 [
super.buildActions();( V& j/ W6 f. D4 D) M3 N
1 D# y5 D! F' P // Create the list of simulation actions. We put these in8 p6 c$ _. d, p' B+ Q& ?
// an action group, because we want these actions to be
1 x e1 F1 k3 q+ ^0 C4 I2 W- |- T // executed in a specific order, but these steps should
# p4 `7 {* a, K% Y // take no (simulated) time. The M(foo) means "The message4 D/ ]* ]: |$ w# u$ Z! J- \' c
// called <foo>". You can send a message To a particular2 ?$ b+ ~4 p/ w+ p
// object, or ForEach object in a collection.: o8 m0 v, i9 _& Z
9 R3 S; A) ~! B+ R2 V* N // Note we update the heatspace in two phases: first run+ p {( V2 B, O
// diffusion, then run "updateWorld" to actually enact the. u) m( f$ a( @- I8 T" b! A! z( |6 Y0 o' m
// changes the heatbugs have made. The ordering here is( g* b/ |& @) D* Z: {( O
// significant!
' I; |9 _. y1 C8 N% B
4 n# U9 [: t* {4 R // Note also, that with the additional
! [ t$ `4 |1 w$ V; Q" f( X: J // `randomizeHeatbugUpdateOrder' Boolean flag we can
8 x- h$ e6 \# J: f: v // randomize the order in which the bugs actually run
% O5 H% L4 {* I' R! H1 S+ y // their step rule. This has the effect of removing any; S" @7 r* F6 N: p% h F' b1 J
// systematic bias in the iteration throught the heatbug( `+ s3 V" e C( E1 K& w( m" E
// list from timestep to timestep
: W9 N) w! [$ {8 Q+ _( m0 a! ?
+ d) P4 J) Q( s6 `% i; `# T, k // By default, all `createActionForEach' modelActions have
. l! S0 a, ]$ z // a default order of `Sequential', which means that the0 J2 I, {3 i3 t m7 l8 V
// order of iteration through the `heatbugList' will be
) R, t8 E) B7 V: Y7 Y+ h" D // identical (assuming the list order is not changed/ J4 u7 ?' }* O0 D: n: s
// indirectly by some other process).
% `3 k; }) |; [" a3 K; y & j' e* i8 O6 W: i
modelActions = new ActionGroupImpl (getZone ());% {2 J- I7 K! T' A
4 p9 h& G: K7 {+ R* {9 @ try {/ S9 q1 c2 N: B. d
modelActions.createActionTo$message/ M/ Y7 q7 P7 H
(heat, new Selector (heat.getClass (), "stepRule", false));
3 M; I6 _7 K. g } catch (Exception e) {
7 z0 z- h6 X V1 J) |$ `# g System.err.println ("Exception stepRule: " + e.getMessage ());9 f' e8 L. U4 l" @! A3 T
}- @+ A u" N" v! ]# n: W
; r0 O6 @7 C7 X- J3 h, a try {9 d6 P- |3 M. z8 H) z; x ^7 n
Heatbug proto = (Heatbug) heatbugList.get (0);7 {$ t+ y2 H" U ]4 ]6 t
Selector sel =
" G0 A7 _* {4 Q; g3 V3 K0 K new Selector (proto.getClass (), "heatbugStep", false);
; S0 A& I9 f( G2 r! z+ l$ h actionForEach =. a- X6 L5 P* Y3 B \' `3 d
modelActions.createFActionForEachHomogeneous$call! k1 ~; _ c7 X& j2 p8 ^
(heatbugList,
$ V$ a$ U" g7 W F new FCallImpl (this, proto, sel,
, E3 c8 H8 r, E, o) v new FArgumentsImpl (this, sel)));3 Y9 N, w$ x c& } d
} catch (Exception e) {
' L& V0 y L& T8 ~: A9 B' g- U e.printStackTrace (System.err);% B( Q4 j* F& W0 s
}
8 f$ t. @' Q i# `( L1 X ! A8 r2 |5 @8 {
syncUpdateOrder ();& q* W7 b: {8 \6 }$ U% n
2 I; z/ e% ~. [* G0 F9 Y
try {6 y# {; C% S k5 a1 S: H
modelActions.createActionTo$message 6 ]' o2 b6 f, {
(heat, new Selector (heat.getClass (), "updateLattice", false));
' \' k+ {0 h, o% d! e2 Y } catch (Exception e) {
5 m9 _$ X# ?( j+ q: k, R. C. O System.err.println("Exception updateLattice: " + e.getMessage ());
: _! n# d0 v5 r6 A7 C$ S e1 ] }
9 w2 Q" Y, z' K
8 x/ K, a3 ] e& X; f" z) ~- v // Then we create a schedule that executes the
( G7 r3 h {( q: R0 H& s. t+ R // modelActions. modelActions is an ActionGroup, by itself it0 T! ?: j" f' t- ?/ `$ [
// has no notion of time. In order to have it executed in
5 m& J: N& Z& d+ d // time, we create a Schedule that says to use the9 Y* o4 ^& j1 D, Q' \3 [4 V
// modelActions ActionGroup at particular times. This. \& I; p4 s) q6 j! J8 C$ e
// schedule has a repeat interval of 1, it will loop every
& z; f5 h7 \$ u% c8 c2 | // time step. The action is executed at time 0 relative to# E3 p j( }( j7 O! {5 L
// the beginning of the loop.
+ V. r8 L+ L6 s2 v
! y) O% R) F- R8 {( S" C# E2 {1 } // This is a simple schedule, with only one action that is
2 e F7 o% D4 b5 Y5 L // just repeated every time. See jmousetrap for more
- i6 j5 ^$ M7 q+ g // complicated schedules.
3 ^% a9 D0 T2 h! P: X. } ! E- K$ @% L# m
modelSchedule = new ScheduleImpl (getZone (), 1);1 h- J& Y6 U7 Z0 E3 S4 ~/ V% i
modelSchedule.at$createAction (0, modelActions);- ~8 c/ D' t9 M/ j) [+ w: h/ q7 {5 f
" t7 T+ W$ c$ g' } return this;8 U2 X2 O u/ K5 S% S- D& t
} |