HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
* X+ {6 K h1 F* I
# B3 e- A6 y; n; Z, Y$ _2 ^ public Object buildActions () {
9 L* @# I- n; g9 ~+ u/ d% o* } super.buildActions();2 l; X) x) F. S" L
' a0 e; G' `3 n& M // Create the list of simulation actions. We put these in
4 ^3 o# ^: ~7 n/ Q" D // an action group, because we want these actions to be
/ t3 Y: h" a' Q' y // executed in a specific order, but these steps should
: h3 S/ f1 R, g* v; O // take no (simulated) time. The M(foo) means "The message3 o- ]( }4 D1 d! c
// called <foo>". You can send a message To a particular
+ f% {. s6 C, n# X9 O% R. [ // object, or ForEach object in a collection.2 K) [6 Y( O5 |! I) e6 S
! V0 L+ y. K. Y2 }, P2 I, v; i
// Note we update the heatspace in two phases: first run
P& V; b6 F8 } // diffusion, then run "updateWorld" to actually enact the% k9 R4 H4 p8 w- E7 |
// changes the heatbugs have made. The ordering here is8 X8 b0 o( m3 B) [( n/ H; ?
// significant!
( ]4 { V* e9 B: ] 7 ]% o7 m2 k( X7 n' o
// Note also, that with the additional
% V1 ^7 m7 Y4 w // `randomizeHeatbugUpdateOrder' Boolean flag we can, N- J8 P) B) `9 f; `$ Y* O2 a
// randomize the order in which the bugs actually run
8 @& h! z, K4 U6 W, s# j) U // their step rule. This has the effect of removing any
$ B5 ?) q) f- H& O7 E // systematic bias in the iteration throught the heatbug) t1 o% }# V! [8 ~* L4 a. i
// list from timestep to timestep
5 x4 s* d- T5 E. p; e
9 s3 n( T# R6 I7 w' ~; {3 ` // By default, all `createActionForEach' modelActions have
3 {& t; K5 Z" F0 T$ H' G" ]( t // a default order of `Sequential', which means that the
- Y4 B9 J" W g4 ?7 z9 a* ` T // order of iteration through the `heatbugList' will be% P( w0 q( B( D0 c4 V
// identical (assuming the list order is not changed
O' q! E) t$ d: a. K, z3 a // indirectly by some other process).
! l: L/ B6 X8 Z6 N9 c
& y5 p. O( q9 o3 u( ] modelActions = new ActionGroupImpl (getZone ()); _0 U! p( s/ d4 w8 p8 S' m/ v+ l
9 c" K6 b, p' e3 d1 O2 Z c try {; S( x9 L2 h1 w) W- U+ B5 a; Y; k
modelActions.createActionTo$message
0 g" j! `9 W6 N5 h (heat, new Selector (heat.getClass (), "stepRule", false));
. E; D0 `5 {* M- C8 l8 ]8 L } catch (Exception e) {2 W" i, w# H1 a4 }! F/ c
System.err.println ("Exception stepRule: " + e.getMessage ());: X/ N8 T% p! [/ X0 ]+ W9 \1 c
}: S+ p# k/ }% `3 e/ |
2 z+ j! e$ e# F% v" E2 i5 D4 U) i
try {
4 X: k5 L9 b/ C( O Heatbug proto = (Heatbug) heatbugList.get (0);
" Z: P$ R4 L9 H) x3 L8 e2 i Selector sel = ! j/ |* D+ d! k; r
new Selector (proto.getClass (), "heatbugStep", false);
1 V. }4 _, m: A d0 P; q" K actionForEach =
4 ~+ }3 S6 u2 R! `) B modelActions.createFActionForEachHomogeneous$call0 R7 i7 y+ e' g" U7 B: ]. z
(heatbugList,2 B% U; W- E, I9 w/ t
new FCallImpl (this, proto, sel,
$ @# p3 v" }( s; P& a' h* F new FArgumentsImpl (this, sel)));- a# v5 N0 }& ]! E& D( I) g; N. D3 T& Z# X& R
} catch (Exception e) {
6 r' u; _% q0 L8 h! T- D K e.printStackTrace (System.err);
9 \9 w3 o# d& a+ {9 n) c8 ?4 W4 J1 p6 h }
" ~& x, R2 ?; P1 A% S 8 @( Z' @; h& V/ w: H& r& M% Y
syncUpdateOrder ();, [. \% u6 }( M8 A! j4 W' Q" \
* [& R- n) W( k try {
" U3 D X O. a! y9 S) t modelActions.createActionTo$message # e$ i& A, v* p# ]' d
(heat, new Selector (heat.getClass (), "updateLattice", false));% v9 A4 o! S0 Y3 ^' ?! N2 X9 p/ U6 d
} catch (Exception e) {9 i5 m9 X2 `) T$ \9 }% Y& e
System.err.println("Exception updateLattice: " + e.getMessage ());
% B9 H+ |& x: E! u' f+ L3 w; ^ }0 k( j; J4 U. b9 r8 S- a
. r' D, X2 o4 J$ q3 _
// Then we create a schedule that executes the
' i% D& P) E# U; w% [ // modelActions. modelActions is an ActionGroup, by itself it; s. }$ o7 l+ s% |& R+ T+ X
// has no notion of time. In order to have it executed in
3 z. S! m2 ^( I0 r+ D4 G // time, we create a Schedule that says to use the3 k6 e' C, `# F5 d8 a+ L
// modelActions ActionGroup at particular times. This2 z/ X8 o5 ?2 I& L: ~! U! s
// schedule has a repeat interval of 1, it will loop every$ g5 T' a4 `0 Z4 I
// time step. The action is executed at time 0 relative to
" H& }7 F" v f9 a; y+ J0 z/ X( _( w // the beginning of the loop.
( n: o! S7 O% e6 @2 l
! W4 [/ h1 M7 @ // This is a simple schedule, with only one action that is
3 O$ C3 O% D6 V9 f0 _9 k' a // just repeated every time. See jmousetrap for more
, v: W6 N) ^( a4 H! g0 v- G // complicated schedules.
9 c! }# {* e8 B % G% s+ R, @% q5 R% v
modelSchedule = new ScheduleImpl (getZone (), 1);* r) U( X6 X/ e( b# G
modelSchedule.at$createAction (0, modelActions);
( C. y, ~+ t$ F$ H0 M9 V ) T' c; j7 }; d9 |2 g' c
return this;" ? ?" I& ?. o V) p% V9 r
} |