HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( {, r' u0 ]- _( Z3 j3 r
. j/ V' q9 n9 A/ M public Object buildActions () {' j( `0 E) h H1 `
super.buildActions();
! T% D* C6 D' s: [1 C0 p3 m
" w% p6 x( P( @/ S! S' w' b // Create the list of simulation actions. We put these in+ B! k5 T6 j, m/ k
// an action group, because we want these actions to be( g# y4 \- j( j; _8 z
// executed in a specific order, but these steps should t7 k4 m" l( q
// take no (simulated) time. The M(foo) means "The message
) A( {8 Q6 x2 e b1 \$ }& n D // called <foo>". You can send a message To a particular: b0 f# R- a0 g( e/ O( Q- {; B
// object, or ForEach object in a collection.1 @0 ^, \1 G8 y. z C7 O6 G# P
* h8 Z1 ~3 C+ S- ]
// Note we update the heatspace in two phases: first run
+ e$ i" `: } H: q // diffusion, then run "updateWorld" to actually enact the/ ~; Z) Z8 ?) X* [- m5 X# n
// changes the heatbugs have made. The ordering here is Y' x; U7 {$ f' l+ D7 b+ P4 d
// significant!* t; _( X0 e, O9 f
; X4 J. J7 Z2 x0 M
// Note also, that with the additional
! i6 Q- W2 L$ N // `randomizeHeatbugUpdateOrder' Boolean flag we can4 Q3 ?, P: L+ h
// randomize the order in which the bugs actually run3 Y, ~6 n- ?$ @: [' x% S( g0 B% O
// their step rule. This has the effect of removing any
+ A! k2 z9 v2 ]9 h3 z // systematic bias in the iteration throught the heatbug
1 t0 Q+ F7 [; x4 w0 P7 [8 | // list from timestep to timestep
. U) }1 x7 S; P
' p2 c( _. F; w' b // By default, all `createActionForEach' modelActions have
' H- r% f6 E) \0 n% K5 k4 B // a default order of `Sequential', which means that the. G$ e' i0 s' Q0 k* I. L
// order of iteration through the `heatbugList' will be" W/ O& u# @" i" _: g
// identical (assuming the list order is not changed+ y3 B6 _. H8 z
// indirectly by some other process).
8 b, N+ Y/ j0 B* B9 K6 I% b1 V" a* y ; N2 ~: V4 Y) j+ l9 I! K7 B
modelActions = new ActionGroupImpl (getZone ());
- w+ Q' }3 f! |. \: W+ q$ E4 n0 R3 `+ N% T" D% b
try {
6 W1 h; E G) B$ S. I% G: v- j; w modelActions.createActionTo$message
$ u$ k. k- w9 z: ]. R (heat, new Selector (heat.getClass (), "stepRule", false));$ _( w& b2 F7 z7 r8 U2 x1 T
} catch (Exception e) {% Y7 b$ T! {4 ` h
System.err.println ("Exception stepRule: " + e.getMessage ());+ H! ?' Y- i# D. Q$ G+ y
}7 C1 I5 t3 ?2 K: g
" m: B5 a) R. `
try {9 t8 Q1 Y: `/ e7 \/ c3 b! X
Heatbug proto = (Heatbug) heatbugList.get (0);& y/ _- z8 k1 X3 n1 c7 W
Selector sel =
0 m6 Y) R' `; `/ @9 R+ Y" g new Selector (proto.getClass (), "heatbugStep", false);
! _8 k# P. L( B6 B' @9 z actionForEach =
( O2 A' [" }/ m0 t; \- c0 g6 L5 ~ modelActions.createFActionForEachHomogeneous$call+ P8 _ b- M/ a! [+ e# r
(heatbugList,
, [( y! B- I1 v new FCallImpl (this, proto, sel,
, S, m& g" s& H9 N7 a A new FArgumentsImpl (this, sel)));
0 }1 H( X# ^2 z9 g } catch (Exception e) {
7 L$ b: n( H7 H1 v, { e.printStackTrace (System.err);5 e# v7 O7 q1 z! U. X# d; R+ B* L
}4 ]6 Q! a/ H5 }# Z, O* o
; j1 ]9 c" b$ x0 _2 Z4 K
syncUpdateOrder ();
0 r6 d6 m' T- l) _& M( B, F
; ?: L% ~' |5 K( q8 Z try {
e8 _. w W0 @/ l3 t$ h modelActions.createActionTo$message
% ]( {8 Z$ s& K8 _& q2 v; ? V (heat, new Selector (heat.getClass (), "updateLattice", false));
- Y, f( J3 H m1 i+ p' T } catch (Exception e) {
3 v! W1 C0 g7 W: l/ `# w System.err.println("Exception updateLattice: " + e.getMessage ());
& h" W% b8 W, a3 k }
, T7 B3 { C; H" |4 k) Z 7 R6 q) q1 I k+ T( J: i
// Then we create a schedule that executes the# j1 f4 z! O" v; @$ |/ p) @
// modelActions. modelActions is an ActionGroup, by itself it9 J( y5 _* T0 P
// has no notion of time. In order to have it executed in
- l7 @* A; g4 l // time, we create a Schedule that says to use the
! M; R% w: [# i6 s$ e // modelActions ActionGroup at particular times. This
- u* x. @( R0 w2 h$ }' ?. ] // schedule has a repeat interval of 1, it will loop every' X% w! f: F2 X- M: u- L
// time step. The action is executed at time 0 relative to6 @+ o& Q% T2 Q: a9 M/ L& q1 q
// the beginning of the loop.. m' Y/ P2 t6 N% E0 ^1 b5 @
1 R# l8 N% R% f% I$ o) y, v) W // This is a simple schedule, with only one action that is( m- v( Y7 }4 |( `& k; w ^
// just repeated every time. See jmousetrap for more
5 W- r8 N2 A% H5 s X7 j: t5 v // complicated schedules.3 ~! x2 U/ B% V; }2 b6 U
4 ^4 C3 Z' J# l' B+ m+ l
modelSchedule = new ScheduleImpl (getZone (), 1);
9 C' {2 @. q- S modelSchedule.at$createAction (0, modelActions);
' Y) c% d! g% f, M, ?5 \ # @! y& ^( r* l! l: ^+ ^, j! x7 ]
return this;
7 q2 i, D# K& n0 z# v& Q } |