HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ K7 Y9 C) w9 H) q
* d: r8 ^1 |( B2 H7 N$ j public Object buildActions () {
( A9 j% ]( H7 L0 V6 z+ O super.buildActions();
7 V. ?9 J: N. F* S
# S/ y0 |2 h8 `3 A // Create the list of simulation actions. We put these in9 a0 E0 j/ `0 _% L/ v
// an action group, because we want these actions to be
+ C, O1 I$ [$ X% o1 [- r5 H // executed in a specific order, but these steps should
2 P1 y" T/ S8 ?' j1 {4 ^2 ~! Q // take no (simulated) time. The M(foo) means "The message
: A5 |$ d2 }' `& f$ [7 g // called <foo>". You can send a message To a particular
0 U9 |1 _7 z p$ u n // object, or ForEach object in a collection.
. u( Q- l8 q: W6 N1 ^" Z( B9 A. q , X E7 o6 s0 T5 e
// Note we update the heatspace in two phases: first run
" S# L2 G# Y6 Q // diffusion, then run "updateWorld" to actually enact the
1 W/ S8 `. I9 Y y1 G // changes the heatbugs have made. The ordering here is
# b4 z( i/ _1 V( `1 T4 H // significant!/ ^4 r- q0 | ~" D3 K/ K# T# a/ |1 Y
# S! `; X* c/ s, U" J* j/ i3 Y" h0 m // Note also, that with the additional
' u5 C7 [. l, e, v% l# e* E // `randomizeHeatbugUpdateOrder' Boolean flag we can, E2 D0 }3 D9 }% J
// randomize the order in which the bugs actually run
; A& ?: s. F( q // their step rule. This has the effect of removing any9 `# ~; i/ _, P6 [9 W' j% N
// systematic bias in the iteration throught the heatbug
% v. W- R8 }$ A; L1 M0 \+ ^% j7 R // list from timestep to timestep: t2 P$ e; R" K# t1 ?9 @% c! v! G! `
+ N' K( U% K" M0 {2 Z // By default, all `createActionForEach' modelActions have, ]$ R. s, W0 q/ d# H1 B! `
// a default order of `Sequential', which means that the
- X- k' Q+ F; d$ e // order of iteration through the `heatbugList' will be
( c) k( h7 x( ]! m8 P // identical (assuming the list order is not changed. k1 ]' E1 V! U" t& o( v$ r
// indirectly by some other process).7 W8 U. b: _ o& S2 v* v* L# I
$ p" S! Y! ]3 s modelActions = new ActionGroupImpl (getZone ());
, ?3 C. L( W1 g
6 Z, h2 N* Q( V3 e try {0 f; \7 d1 ~$ Z8 w" y' a8 O8 |9 O
modelActions.createActionTo$message
& y% |' N0 b$ _7 [# k0 [ (heat, new Selector (heat.getClass (), "stepRule", false));( e1 a& b; W2 N; f7 Y
} catch (Exception e) {% e$ j( z- R3 P' @1 e
System.err.println ("Exception stepRule: " + e.getMessage ()); [2 |9 Z- H1 H9 j# r
}
2 m# P0 Z0 Y- z% ^" M
4 {7 @5 C/ m0 M8 Z9 k5 C try {% F1 |* X* o% \2 P
Heatbug proto = (Heatbug) heatbugList.get (0);
' o4 y7 b7 Q) X; U8 }/ u! Q* d5 k5 Q Selector sel =
, V: J! T# t( A! K; N7 [, @ new Selector (proto.getClass (), "heatbugStep", false);( u* v8 q# S4 A2 b
actionForEach =/ u. M! J# v+ r. N
modelActions.createFActionForEachHomogeneous$call
; v5 P+ a& r; @2 Z! g7 q. I (heatbugList,! j- h; _4 @' j; O2 x. Y$ j
new FCallImpl (this, proto, sel,
1 ]3 @9 K: Y: U% l4 r _; C( x new FArgumentsImpl (this, sel)));! X7 y6 {+ v" O+ g
} catch (Exception e) {
' y& p( ? \3 Q# n& ?7 }# q/ j e.printStackTrace (System.err);/ L# f' N! M5 X4 t4 k# c" ?
}( a, L6 |0 q% L) n! Y( N1 s s
7 |2 z/ e6 Z: D
syncUpdateOrder ();% F- F: @% z- J0 A; Q# R* u
1 u4 e# F+ k3 f# i try {
& \+ Z) G+ m& L+ Z) U7 J. ]( m% H/ X modelActions.createActionTo$message & f% q' U/ |2 z% I' e$ c9 c' {. ^
(heat, new Selector (heat.getClass (), "updateLattice", false));9 O# M% A% b( ~1 v
} catch (Exception e) {
: Y" G* Q! `' I0 e8 C/ A) S System.err.println("Exception updateLattice: " + e.getMessage ());
4 m4 D% B4 I3 l5 C6 ]+ z }
4 |5 p4 }3 ^" G+ q3 m
1 M$ o* X* m( B# B: m( Q // Then we create a schedule that executes the2 I0 z+ b# a4 W8 V. q2 l
// modelActions. modelActions is an ActionGroup, by itself it
z8 O6 h" g ^ g // has no notion of time. In order to have it executed in/ ~1 `% m- |* ?, q: G/ J1 X( ?
// time, we create a Schedule that says to use the
1 ~. Q. B! B& K0 t) a6 H; I // modelActions ActionGroup at particular times. This
( T f7 I; b& P) F% y( } // schedule has a repeat interval of 1, it will loop every
- v% L+ Z0 } |, ~) P // time step. The action is executed at time 0 relative to
8 Y0 { C/ Q, G // the beginning of the loop.
7 M, y9 W2 r+ [5 ~0 ~! B4 Q6 Y# L
. a1 x( d1 W% c // This is a simple schedule, with only one action that is
2 B" B( I- P: H. ?2 r1 G // just repeated every time. See jmousetrap for more
# g& n% u, `2 c! Q+ Y- X* `/ C. t // complicated schedules.
, R1 W, b* A6 ]* t u8 o5 _/ }9 h6 d ; A/ H0 z: r! y; a
modelSchedule = new ScheduleImpl (getZone (), 1);5 Y7 N5 c2 a5 P
modelSchedule.at$createAction (0, modelActions);
1 d/ V+ Z0 J' ]( G7 i
" X8 Z0 @5 Z: d) r- w+ \ c return this;
# m; {1 Q; o) I, z- g- x. V; Z% [ } |