HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
5 B* D% I5 R+ s f# `0 K
3 l' Q/ c$ p! k5 F F; v7 C3 E public Object buildActions () {. W3 k5 K9 g: q' j. H6 [1 h
super.buildActions();$ a, H! U. ]8 T) `" h6 U$ ~; R& U. C
" P- u( W6 {# o6 X7 `" B7 l // Create the list of simulation actions. We put these in
0 o }* j" E) @$ C" ~' Q! h: S0 x // an action group, because we want these actions to be
" l" u u/ H' N* {# m; l // executed in a specific order, but these steps should& l$ V- E9 h) [' [1 x
// take no (simulated) time. The M(foo) means "The message& t- q! V& H0 m3 r0 Y" [) |
// called <foo>". You can send a message To a particular7 V9 m7 U/ z1 b0 W. T9 |/ E P
// object, or ForEach object in a collection.
# A8 O* c1 ?# q; D
' T3 D8 X1 B. a9 C* e, U9 [7 R // Note we update the heatspace in two phases: first run
' ?8 y% m, M% S# _; @% @ // diffusion, then run "updateWorld" to actually enact the7 T/ [' Z4 y& @, z, W
// changes the heatbugs have made. The ordering here is) V$ {- ]' k' z7 w9 v I! i
// significant!2 i L' n( I' e$ \" Y, i
9 e" y! C6 X5 Q; v* q
// Note also, that with the additional
3 l2 M3 g' ~; m( r% X5 M$ B$ o // `randomizeHeatbugUpdateOrder' Boolean flag we can) Z* I, x3 S/ N( g$ m7 S! [
// randomize the order in which the bugs actually run: V/ N2 E4 M; u9 K
// their step rule. This has the effect of removing any
; Q/ h$ R6 p& F0 x" g. o: Z // systematic bias in the iteration throught the heatbug* r, `1 h6 R9 b5 m; e
// list from timestep to timestep$ `' ~. X* r5 e+ j7 Q: z
8 [3 X7 q* ?4 K+ s0 T. C; y
// By default, all `createActionForEach' modelActions have4 d1 n8 v7 W+ {: |
// a default order of `Sequential', which means that the$ a7 u5 |( \" n2 c% O
// order of iteration through the `heatbugList' will be) {' ~' q5 L- n
// identical (assuming the list order is not changed _6 f- A/ g. ^8 v; Y
// indirectly by some other process).
! e* Y6 O; l; S! t
) L2 p! ]6 R" `. N% L modelActions = new ActionGroupImpl (getZone ());
1 d+ M O) w9 A' r, o8 z5 D. j( \0 S
try {
) i, @: V! D8 J0 w5 l; s! o modelActions.createActionTo$message, \8 e% u0 J& k- p1 i. X7 w
(heat, new Selector (heat.getClass (), "stepRule", false));
: M( `! E, |6 d5 e } catch (Exception e) {+ b$ R0 I2 I* N# j, I$ W
System.err.println ("Exception stepRule: " + e.getMessage ());
' r$ k R! @! Y% A& E( i. v }
5 ]7 S: X0 D2 o3 `/ q6 C3 ^( a& w5 U
try {
0 s, R! {# ]3 W* z- W0 |' q Heatbug proto = (Heatbug) heatbugList.get (0);- y' `# ~2 E4 G/ f* q
Selector sel = - _( X1 k. K; k& V0 ~+ z
new Selector (proto.getClass (), "heatbugStep", false);
! U1 C5 M( c1 T5 y0 I actionForEach =9 x1 o& U a% z- f' \, b& o) L
modelActions.createFActionForEachHomogeneous$call
: k+ L8 g1 u5 w) Z4 X0 z (heatbugList,
( V2 }% h' p/ _) h* ~) E new FCallImpl (this, proto, sel,% ?8 X1 U* o" C/ T, y
new FArgumentsImpl (this, sel)));
4 j* I- }9 H, _. I& o, r. h3 f } catch (Exception e) {+ d# ~: S9 a @# I1 @' |, Y
e.printStackTrace (System.err);
3 n3 y: q+ ~# \* x }
3 r( U; v& s) m4 g . Z1 W# d! v% {% Z* l
syncUpdateOrder ();
& C6 h$ X5 P) F) H; k! K1 x1 k! s; v3 x3 W
try {
9 R3 b* U/ B- P modelActions.createActionTo$message 4 K1 ]* g% [( O6 i
(heat, new Selector (heat.getClass (), "updateLattice", false));: @3 j/ \! E9 K Z ~
} catch (Exception e) {( W0 j* e9 T9 y6 J
System.err.println("Exception updateLattice: " + e.getMessage ());1 y5 ~5 @8 _9 O# q4 n
}
, }# ^$ F3 F2 i4 g) X9 n
! z Q, m2 _" R1 ]+ v1 T) j/ W // Then we create a schedule that executes the/ k P+ f, R1 T0 G
// modelActions. modelActions is an ActionGroup, by itself it t9 _6 N8 g2 q! o! A6 f
// has no notion of time. In order to have it executed in
' d- i- O' R+ @( p7 T9 ]$ [; V1 l // time, we create a Schedule that says to use the
3 a. U6 J9 X7 |7 j) @! P# q // modelActions ActionGroup at particular times. This3 w) z7 ]& m5 T4 @
// schedule has a repeat interval of 1, it will loop every, E/ F) W* i8 y7 M8 Q" k
// time step. The action is executed at time 0 relative to* C% a9 y. F2 y! `
// the beginning of the loop., M* ?4 C+ Z$ x! \, e
g. d6 p( @ X // This is a simple schedule, with only one action that is7 ]/ j6 O2 Y+ T e' {+ P' \
// just repeated every time. See jmousetrap for more
4 O# c( x# o1 Q8 ]1 C/ @ // complicated schedules.
, `4 r3 v, z( x. J) O ( D. U# E+ F# R' m0 o# x7 v
modelSchedule = new ScheduleImpl (getZone (), 1);
6 E1 Y5 U* c/ v8 I modelSchedule.at$createAction (0, modelActions);
, W W _) f) G5 l1 n$ _ , A( p% c' F; z2 ~' x5 j5 D
return this;
- q! S( W# [" R( m; L& c# V } |