HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:8 r# [$ i+ a. [6 G( W
' U! s+ a/ ?9 ?, u- s6 v public Object buildActions () {
; h9 g; ^. z9 U super.buildActions();
2 R* }- x. I; P( U; W% H
% X7 h8 ]3 s3 X6 R // Create the list of simulation actions. We put these in& L% I6 }; v6 Y- n$ F+ c
// an action group, because we want these actions to be
8 d e& n( ^4 P // executed in a specific order, but these steps should
9 g X7 K" [( R- s5 ` // take no (simulated) time. The M(foo) means "The message
7 i/ [( O' |' |6 U1 t' J // called <foo>". You can send a message To a particular
- n; t& v0 q# @) k // object, or ForEach object in a collection.
7 n! m9 l* z9 q+ J ; h& x) a; {; K
// Note we update the heatspace in two phases: first run% ~6 T' }' R7 H( J
// diffusion, then run "updateWorld" to actually enact the
. q# L5 K6 [: g // changes the heatbugs have made. The ordering here is9 ~/ i- |* V) h# r
// significant!
l( @, b B# [% [* N . c, z2 _( Y3 p2 y& v. y1 M( M
// Note also, that with the additional
" p; W; h: e6 @' R/ T // `randomizeHeatbugUpdateOrder' Boolean flag we can
2 J! o& n5 h0 w // randomize the order in which the bugs actually run
# _! q$ A" ?# L+ t0 s+ d* p E0 I // their step rule. This has the effect of removing any W+ o4 m/ q* I2 h' M3 Z8 w
// systematic bias in the iteration throught the heatbug b. B7 U& U- i& j" Y2 e
// list from timestep to timestep( G' {7 j. U6 @. |( L9 k
( k2 U# U* _ e9 \ // By default, all `createActionForEach' modelActions have7 M& R# r0 C( c/ {2 [
// a default order of `Sequential', which means that the, A& g* e5 f6 M8 \2 T; g
// order of iteration through the `heatbugList' will be
7 t. A6 x2 q' n6 R: R // identical (assuming the list order is not changed
" L4 T- O# p1 Y9 ^ x // indirectly by some other process).+ G' X* l2 {/ W! {) b& w
, O2 _8 X& @* M: u: ?
modelActions = new ActionGroupImpl (getZone ());
5 u+ }) t& g* [7 F; a3 e. ?7 f& Z9 F3 i$ }- N) Q& \$ o2 i
try {
' C; C5 |9 R6 G! T1 x' X modelActions.createActionTo$message+ m- p2 T5 _! P6 I/ S- A+ J q: X3 [* a
(heat, new Selector (heat.getClass (), "stepRule", false));
0 n7 C3 O+ l& p+ |/ ]& X } catch (Exception e) { R. d+ p) d+ M4 {8 @. ^' j& ~
System.err.println ("Exception stepRule: " + e.getMessage ());0 D' I+ n0 |5 X, G
}
. s6 Y+ F% ]/ }
' |4 @' S; z0 a% P9 v4 b) ^. E try {
# \" M* e5 }7 m& u# g G) x Heatbug proto = (Heatbug) heatbugList.get (0);
9 f4 w) t' q" ^- p3 ? Selector sel =
: O& J5 K& ~- m' A5 M3 `! \. h- P new Selector (proto.getClass (), "heatbugStep", false);' X* b/ w0 `( w; E( J- L9 r
actionForEach =8 M# ]. b* p8 C; a5 R
modelActions.createFActionForEachHomogeneous$call( }* X- l- t' O8 g" K
(heatbugList,7 a" z% {+ q+ a& S% H% M7 q' g# F
new FCallImpl (this, proto, sel,
! a5 H; N+ [1 m& W- T+ C. [ new FArgumentsImpl (this, sel)));6 E5 ?. i3 Z8 ^ s8 i/ s5 n ~% F- p
} catch (Exception e) {
: n5 v( P/ O7 x$ q e.printStackTrace (System.err);
0 b7 I& |, g3 x: Q) N( A }
) X! w6 D$ ]4 @( G6 I4 l " S9 l. J% g# h0 L& q0 g
syncUpdateOrder ();
. q" K) | ]* |, k
, v: j: _' }2 X" n: j try {
! M- y. y- A4 h! ^ modelActions.createActionTo$message E0 ?6 B$ B- C' V, b' A
(heat, new Selector (heat.getClass (), "updateLattice", false));
6 H) {& D+ {4 R! p } catch (Exception e) {
+ g' k6 h. \# W System.err.println("Exception updateLattice: " + e.getMessage ());
) N+ m+ i$ `/ X }
' P$ V9 o% T% @" v
! L' j2 c3 h( A8 A7 I) h5 P9 n8 ~6 w // Then we create a schedule that executes the
% Q. ]+ N( h( y9 X/ |0 v: g/ b) Q // modelActions. modelActions is an ActionGroup, by itself it9 G" D4 Y1 p, ^$ j- p! Z
// has no notion of time. In order to have it executed in1 r9 R+ h" Y" z' @; a; }
// time, we create a Schedule that says to use the
5 B% ]7 @) r2 I) T) ? Q // modelActions ActionGroup at particular times. This( ]7 D8 {" X5 d( `. p- P5 F5 r& F
// schedule has a repeat interval of 1, it will loop every
+ z$ B; z4 B) W0 v4 s& a7 \ // time step. The action is executed at time 0 relative to
4 f$ N. r: \* Z6 D% z. z // the beginning of the loop.
+ ~/ i+ t# T4 }$ N/ W0 e z* D- k$ e
& ~ J- K+ z- C% }- j) M I // This is a simple schedule, with only one action that is1 u' m% B/ k8 y
// just repeated every time. See jmousetrap for more
& F+ p2 Y. C1 m0 g9 s3 a1 w // complicated schedules.
1 Y8 }2 R x, T 0 j) U/ o" N2 y; H t" o
modelSchedule = new ScheduleImpl (getZone (), 1);9 n* D) [1 m3 F! c1 p5 r9 V, X
modelSchedule.at$createAction (0, modelActions);+ D$ f* p" P: X" ]: t0 t% _6 M2 d
( d$ `' Z4 a. A& w, N0 {. l return this;
7 e n: _, ^- f7 E& S2 K' p5 Z) V } |