HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
M) @6 J& b1 X G; o
h% R- t% X" e$ F public Object buildActions () {
1 W* f4 m& l5 Q; A5 p super.buildActions();
9 U' u" _4 a* ^+ z9 ~# M& k 4 Z4 V# H. l& M2 e! C& y
// Create the list of simulation actions. We put these in
- o3 u0 t, g( P5 d8 P `5 N8 w1 O: V; U // an action group, because we want these actions to be2 v) i2 R; p% x. s
// executed in a specific order, but these steps should* }8 a9 b& I5 |( _$ l. C
// take no (simulated) time. The M(foo) means "The message
9 w% Q, O! W4 Q# _& u# f, n- u // called <foo>". You can send a message To a particular% \6 Y/ M+ A9 r
// object, or ForEach object in a collection.+ e7 M* I6 s- y% x* N( O7 b
4 Q. U. W/ v5 h N/ c1 {6 _/ q // Note we update the heatspace in two phases: first run. ]) f2 \ U* h; K
// diffusion, then run "updateWorld" to actually enact the
7 q; d3 l8 V/ |& B- h* Y // changes the heatbugs have made. The ordering here is
& w/ C. J: X2 z. ?0 m( F' i$ ^) k // significant!- l! y' c' h$ Q( [( i( g
! `9 `) _4 X' A, D" {
// Note also, that with the additional2 l: t# u* \4 `* {. v1 q2 M6 U( O
// `randomizeHeatbugUpdateOrder' Boolean flag we can7 H R2 d/ b: l
// randomize the order in which the bugs actually run9 f4 f3 H" @( f3 H) u7 t
// their step rule. This has the effect of removing any
" ]1 {# E$ D6 x" Z // systematic bias in the iteration throught the heatbug: x A3 k9 }' p8 W0 I! q
// list from timestep to timestep
5 d/ w: ]! J- E - s: a- s9 v( a6 A: D2 ]
// By default, all `createActionForEach' modelActions have' a9 }. j+ k. V r5 N
// a default order of `Sequential', which means that the
. ?: g1 m( j; W/ s4 q) b // order of iteration through the `heatbugList' will be
( S) b {% e& [0 b- S // identical (assuming the list order is not changed
3 T& A9 T. \3 [0 i/ g0 O // indirectly by some other process).+ R5 Y0 Z% c* R! o- p# x. `: u
* o6 {1 c" Q4 N
modelActions = new ActionGroupImpl (getZone ());
6 ]: b+ a. B! T: {! g, e R
5 u/ M( f! h$ N% \8 e) T2 b# c try {
r' n V; w1 X' c) M! }7 q modelActions.createActionTo$message \" }( l8 y4 k* \
(heat, new Selector (heat.getClass (), "stepRule", false));! C* T# N! f& _* [; q
} catch (Exception e) {) D3 k {% U( m- S6 o
System.err.println ("Exception stepRule: " + e.getMessage ());
V/ q( F8 E6 k: R$ a6 }; j }3 D, E3 c. ~' [$ b) ?
0 [# ]& o4 I% W5 y
try {
6 J& T7 o2 X! W! j Heatbug proto = (Heatbug) heatbugList.get (0);
( o4 H$ P$ R+ B7 g6 M4 n0 } Selector sel = * e2 o0 Y! A, C+ m+ \" E# `7 ~
new Selector (proto.getClass (), "heatbugStep", false);
) |1 Z" Z( M" N4 Q actionForEach =
& ]" Y; n& O9 _; {1 ~3 g modelActions.createFActionForEachHomogeneous$call
/ p- E; w) _1 ^& [9 u, M- j) F (heatbugList,
# D. u+ N" m) J0 r( x+ D new FCallImpl (this, proto, sel,
$ C s/ i/ a5 T- ^( J- X new FArgumentsImpl (this, sel)));
9 ^( T9 H" }0 ` } catch (Exception e) {# H5 Q: F/ z& O
e.printStackTrace (System.err);
- E& k0 |) ^" ?1 m8 P6 s# j+ h! r3 l }) l; Q/ o K4 D r' p7 n
1 R" W! E# M% A* T0 C% G! P syncUpdateOrder ();
- |& n) F- g7 j9 g% k9 d
: f# S y+ N: e. S try {
% j7 v. ^2 y4 F! N) O3 [8 P modelActions.createActionTo$message
( W# c, x3 d- s% ^ h& L2 n (heat, new Selector (heat.getClass (), "updateLattice", false));$ k8 Y7 |# z$ j4 z" e& Z4 J
} catch (Exception e) {8 t* C7 i$ M: E1 H
System.err.println("Exception updateLattice: " + e.getMessage ());4 D8 w3 h; s( ?' E% E/ W" `
}
' ?, ^5 V% ~2 H/ @4 G# D0 R' p# B + R1 W+ T) d6 W# v O
// Then we create a schedule that executes the+ r4 u7 e3 T: V+ W1 r, o/ k) o
// modelActions. modelActions is an ActionGroup, by itself it
I3 s; s4 x8 c: p // has no notion of time. In order to have it executed in
4 t3 @' Y( t$ W( P5 T // time, we create a Schedule that says to use the
" d5 D' e' J. J5 P: }0 N // modelActions ActionGroup at particular times. This
+ T. C- F; `! P2 y; E/ S" j6 Z0 @ // schedule has a repeat interval of 1, it will loop every$ O5 Y# D: O! t" o8 R+ K8 D
// time step. The action is executed at time 0 relative to
6 t, Q4 {! c9 _" B' H // the beginning of the loop.8 Y* H% h. y) _1 T% ]! P3 `
6 D6 T: V. R' I, A: m
// This is a simple schedule, with only one action that is# z) S5 ]0 A e( I; z
// just repeated every time. See jmousetrap for more. N* W( T0 t( K' f' ]/ [, C+ N
// complicated schedules.4 @# q: l* @# A" r6 F7 E- s
" b" p2 E# }% F. r8 ?$ I$ x modelSchedule = new ScheduleImpl (getZone (), 1);
& u% C$ P; M% m: N: T modelSchedule.at$createAction (0, modelActions);
+ N. \8 W8 ^; b# ]. h
8 g Q$ c/ c' L: h5 f& D return this;
- V) L0 l+ X0 [ @2 i& T } |