HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ v( _: p6 X' G$ v8 B/ }3 P7 L; C% d
public Object buildActions () {
5 k# ]/ U& d! |$ L% Y super.buildActions();
. ^! [5 Z5 F" @ : x0 b9 Q: a W8 J& N
// Create the list of simulation actions. We put these in
( b- y) M# _6 X1 X# p# Y: i M8 h) z // an action group, because we want these actions to be) e4 J1 y( m+ S* N* K
// executed in a specific order, but these steps should; O) _9 z5 ~* C; [
// take no (simulated) time. The M(foo) means "The message6 b3 @$ p$ d2 ~
// called <foo>". You can send a message To a particular
$ a; t( s# ^5 q. t4 t // object, or ForEach object in a collection.
! ` L$ R8 o( L) x6 q $ B9 L% F, {: p$ v$ }9 s$ q
// Note we update the heatspace in two phases: first run
, ~' Z. o3 c" H& x' j // diffusion, then run "updateWorld" to actually enact the0 w1 Z, k7 L/ x+ \) |+ {
// changes the heatbugs have made. The ordering here is
, h5 T! w5 X+ I1 w' f+ u' t2 w0 D // significant!+ w8 d( M9 b. ~4 D; \" n
0 [. ?; X0 z: a$ F+ z
// Note also, that with the additional
4 l5 V( K- }# m; h8 L& u // `randomizeHeatbugUpdateOrder' Boolean flag we can- }' p! y- Z2 u, p$ O/ B: p
// randomize the order in which the bugs actually run7 _& n' d) M3 v4 z3 y6 Z0 {
// their step rule. This has the effect of removing any2 P2 _, E% f( J6 p$ o
// systematic bias in the iteration throught the heatbug8 g; y0 d' k4 b( p+ M9 R I
// list from timestep to timestep1 ~% x1 h1 O" L% D( r# U
j R! ]$ V8 M9 Y) Y0 O
// By default, all `createActionForEach' modelActions have
# B2 J/ n; k) E! d# W- l J8 p // a default order of `Sequential', which means that the5 h. D& @9 H/ K9 R/ I& R) |
// order of iteration through the `heatbugList' will be' A4 h: x/ E) u9 l) _, [: P5 ~
// identical (assuming the list order is not changed# @& \/ K! Q/ B
// indirectly by some other process).
, K: j l R0 _& r0 ?$ u % c# Y7 P7 e- u- K; `! t' d
modelActions = new ActionGroupImpl (getZone ());
0 P; Y* c4 j) F- b
; r% [. o5 m T+ x# `, K try {5 G& P8 U7 e- V) B0 r7 i$ d( l
modelActions.createActionTo$message
2 z& A: p3 v7 i/ c7 f (heat, new Selector (heat.getClass (), "stepRule", false));
9 {. q% o$ M. R2 C } catch (Exception e) {& [# Z* q7 Z5 l: M
System.err.println ("Exception stepRule: " + e.getMessage ());
1 g2 @- @$ ~* {0 O( p. O }+ t/ n4 R3 h6 S0 h0 J5 S0 {
6 W/ t5 ?; i5 U, B
try {( F( d' R, Y" F+ K- Q: ?
Heatbug proto = (Heatbug) heatbugList.get (0);
7 _ p& G/ G1 I6 [! T Selector sel =
( h" f8 X! h9 t% a new Selector (proto.getClass (), "heatbugStep", false);, ]' y- l9 ~- L$ J& l
actionForEach =. L' v' X9 {. w1 u* L4 {5 X# o1 _
modelActions.createFActionForEachHomogeneous$call; a0 G9 e, q! e+ G, j, s
(heatbugList,! H0 i2 b( n u% y! P- ^
new FCallImpl (this, proto, sel,' [2 ?* Q( M: N) C d, B0 z
new FArgumentsImpl (this, sel)));
9 b4 R% z; I" W' F: {9 s6 g5 [ } catch (Exception e) {( l6 c% r' S: V+ H& L1 M ]8 r: V. X
e.printStackTrace (System.err);
s9 A6 n! C7 y8 a }6 e! c* S) Y! i
4 g& |2 l' m3 _& C" c& k
syncUpdateOrder ();" j% D" R s8 Z
& C; l4 P: c; x' P9 z7 c try {' F. P) _! h0 K# R
modelActions.createActionTo$message
+ P3 X0 ^. `. W; I+ _8 K% h (heat, new Selector (heat.getClass (), "updateLattice", false));# u2 u6 R+ r7 i! U9 h' i* E1 V
} catch (Exception e) {
! N1 R) m# T! ~ System.err.println("Exception updateLattice: " + e.getMessage ());
+ I1 D0 j; M4 n% `( _; g' i }
; ]+ Y) P3 z3 Z$ F$ o6 V2 P
- L$ {" n( w$ ~+ Q // Then we create a schedule that executes the
b" l# i0 l" P" ~+ B- ? // modelActions. modelActions is an ActionGroup, by itself it0 m' [) h1 D- P$ i1 h8 v/ Z
// has no notion of time. In order to have it executed in
' O' W o k6 ]5 } // time, we create a Schedule that says to use the4 s; J9 X' S( x
// modelActions ActionGroup at particular times. This) H- L9 x. @& R2 G9 n
// schedule has a repeat interval of 1, it will loop every
0 A- J/ q, `0 l0 A. o' V0 j // time step. The action is executed at time 0 relative to
. d3 \. c0 d. L4 K4 k // the beginning of the loop.
' J! P! X8 m1 }, i2 Q! T0 Z+ I0 A2 ^/ w6 y
// This is a simple schedule, with only one action that is
1 {5 T0 p& P' T3 m% b9 W' G& P0 ^ // just repeated every time. See jmousetrap for more Z+ R+ u2 R: k2 {; o
// complicated schedules.8 a h/ _. g$ ]9 ^
2 [) B- ^7 F8 R0 h' }/ s# e9 H modelSchedule = new ScheduleImpl (getZone (), 1);
- U q J$ `* B3 u; J: g modelSchedule.at$createAction (0, modelActions);, }5 t) O# \, L" [3 q: z, L4 S8 U0 ?
8 }- M ?, e# c' O return this;/ T' b: I9 |( @8 M. O' g
} |