HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
5 Q0 c1 @4 q. y0 Q1 s
5 m5 B F. x+ @; @6 K) N+ r- R) b public Object buildActions () {7 y. t9 }; x7 K4 [/ q# S- E
super.buildActions();' h0 ?! x. a" P- G
7 O$ e' v' K: G! B // Create the list of simulation actions. We put these in
( I: F* _" L& `5 B: ~ // an action group, because we want these actions to be/ h, T) Y8 z H2 P
// executed in a specific order, but these steps should# F1 {. O2 l& w. Q4 z
// take no (simulated) time. The M(foo) means "The message
# n6 D0 E1 k4 z( y3 x& b // called <foo>". You can send a message To a particular
- Y' C3 {% B) w$ v9 A/ o // object, or ForEach object in a collection.' {( u) H9 V/ k2 g$ r6 S
8 H0 F" P4 t1 v0 N7 X+ ? // Note we update the heatspace in two phases: first run- \' U- ?5 t6 G' ]/ V
// diffusion, then run "updateWorld" to actually enact the
2 _0 a" u: E' u$ e // changes the heatbugs have made. The ordering here is& M% a8 A! y* n
// significant!
# T+ @4 v2 u5 [$ V3 c' Y
0 T# I7 D& E, z5 @ // Note also, that with the additional
7 j& \' `3 W$ `* X$ `0 x: |( A // `randomizeHeatbugUpdateOrder' Boolean flag we can
4 ~8 _1 L" c" F7 e/ h1 f2 g/ k // randomize the order in which the bugs actually run+ B E1 X" t, k, t
// their step rule. This has the effect of removing any
' |5 L3 z1 E) t$ Q: r // systematic bias in the iteration throught the heatbug; r) M5 m4 [* q7 t' F" z- ~3 M
// list from timestep to timestep
+ b, ~% g% T( y# C+ O2 g: x 3 j6 p) h+ M( L8 ?
// By default, all `createActionForEach' modelActions have
: x" _- b% p: J, W3 {& k // a default order of `Sequential', which means that the
% h5 T! |. ], V9 a" E7 r2 l // order of iteration through the `heatbugList' will be; @; N" M. T7 }- ~
// identical (assuming the list order is not changed
]/ }7 _% Y8 ^1 J // indirectly by some other process).+ k( H1 m" @7 l; f# c. \
, b' L' V$ @) Q0 d; d
modelActions = new ActionGroupImpl (getZone ());. D- z+ ]; Y% h( Y6 U" ~& J& O
- H0 M6 |5 a# v# B# e
try {
- M$ S5 @2 N6 S modelActions.createActionTo$message9 T* m" F9 S8 ^: M Y% a
(heat, new Selector (heat.getClass (), "stepRule", false));* ` o. p6 y, f2 W3 F H
} catch (Exception e) {
. J# w1 E. G8 \* f% y2 | System.err.println ("Exception stepRule: " + e.getMessage ());( r$ ?8 l0 m8 n$ l/ S( z% N+ P7 N
}- `# N* u; |/ f u
% v# l% p& T, `! _3 T
try {
" i& }0 }9 R4 N! {* o7 P Heatbug proto = (Heatbug) heatbugList.get (0);
; o! ~0 e4 P7 m" _# x i Selector sel = 3 L+ M" ^0 x6 g9 T4 R0 Q: r5 @' P
new Selector (proto.getClass (), "heatbugStep", false);
! T; _- S9 ?; H7 B7 K1 k, C actionForEach =
6 V2 q' L6 U$ y. b. C1 Z modelActions.createFActionForEachHomogeneous$call
# X1 ^; N* ]( v5 m2 h* _- T (heatbugList,6 |1 o- W& C8 @6 B
new FCallImpl (this, proto, sel,: E8 O0 X: i; ^
new FArgumentsImpl (this, sel)));
. d5 b+ ?) ?1 O } catch (Exception e) {3 g+ p, C l0 D$ o
e.printStackTrace (System.err);0 ~; d7 h# [ d5 U: ?
}
4 x) P0 p* q: |3 D; j/ D7 C
+ a) T3 t" s, |6 n1 Y syncUpdateOrder ();
/ z( `; g7 b/ [7 A1 S6 h
: \2 h ?9 d; Z; p+ F) n1 y try {5 Y7 a& ~) J3 R5 f4 z
modelActions.createActionTo$message
! W, u! {$ ^+ O. t8 m, P. B (heat, new Selector (heat.getClass (), "updateLattice", false));% V& G+ l& N+ t- k4 d8 q) `
} catch (Exception e) {) ]( K; @$ x6 \8 n( T
System.err.println("Exception updateLattice: " + e.getMessage ());
- m1 h' l |" o# n4 O) ] }
) _2 H- X9 h0 m4 [, `( W& K
3 N3 E1 S) n5 g0 v9 O // Then we create a schedule that executes the7 ^3 F. ]8 i+ n2 L
// modelActions. modelActions is an ActionGroup, by itself it% b. u6 q9 `4 k2 K
// has no notion of time. In order to have it executed in
0 D: C$ E! v7 b; A- L8 L9 S: A // time, we create a Schedule that says to use the2 c6 i# Z5 F3 K2 X
// modelActions ActionGroup at particular times. This$ @/ ]6 r2 f9 K/ }: G" j! A) a
// schedule has a repeat interval of 1, it will loop every R/ p. Q7 }5 h9 @* F
// time step. The action is executed at time 0 relative to5 J) x7 a4 P! ^! s7 s5 P
// the beginning of the loop.- S& `; I( T5 X$ s
/ \3 [: n% @* |, g8 i2 d) b
// This is a simple schedule, with only one action that is
/ \- S) D q) u5 k // just repeated every time. See jmousetrap for more' x. N; o# r, }# c0 I: i4 j
// complicated schedules.- S+ z; D5 U) \# R% ^- c' t( z0 i
( J- o- D u# V7 _8 z modelSchedule = new ScheduleImpl (getZone (), 1);3 I- I$ [1 g" ^/ S, }: J+ \
modelSchedule.at$createAction (0, modelActions);
8 O7 U' R, |4 D: I3 @
* a. S6 [+ I {+ ^+ G% F return this;
/ ?% }9 s- C. y } |