HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, c1 K% D9 @9 r# O6 T# L9 k3 H1 F; U
public Object buildActions () {
$ e' Z/ Z& H* n3 \! W: m4 e super.buildActions();
6 `0 K6 Y: \+ e x/ D* ? & R+ z5 d; G* Z; w9 x; e1 C+ S& @" i6 L
// Create the list of simulation actions. We put these in
& }6 @: T0 v, A1 Z1 V // an action group, because we want these actions to be
% \, |* g5 @/ U9 u. F: [+ D // executed in a specific order, but these steps should
1 j- p* P a# B* H$ S* H // take no (simulated) time. The M(foo) means "The message
l7 y1 B3 _; b# p8 G // called <foo>". You can send a message To a particular# j; f. @! M3 E8 y: P
// object, or ForEach object in a collection.
3 X! _* p d2 A
) g! T" v5 P0 p. X // Note we update the heatspace in two phases: first run" V$ h* \* N' v" y$ l8 ~
// diffusion, then run "updateWorld" to actually enact the
* r; ~0 e1 q, T( Y1 v6 s3 G // changes the heatbugs have made. The ordering here is
0 y6 v6 J7 _$ m- }" P, N // significant!9 g" D2 u: d* ]
+ E" m/ a8 g& T% r) c
// Note also, that with the additional5 `$ _ ~1 K2 }+ x m# \
// `randomizeHeatbugUpdateOrder' Boolean flag we can
. C0 `1 f) N( v5 f9 W% K. f G+ b // randomize the order in which the bugs actually run
( [* E5 l/ V a$ a% r6 v4 y // their step rule. This has the effect of removing any& ?! u1 j! I2 x( j, l7 M
// systematic bias in the iteration throught the heatbug. o' m2 u1 l+ f2 M/ t
// list from timestep to timestep" J0 \ t0 v" A: c6 C+ `% P1 `5 B
: g+ h. @( }& t // By default, all `createActionForEach' modelActions have
/ A* Z/ g0 }8 e. ? P // a default order of `Sequential', which means that the1 s' H4 A( x' s1 \' F6 J% g
// order of iteration through the `heatbugList' will be
2 U+ d8 v+ O! A& n // identical (assuming the list order is not changed
2 r* }/ ~ ` \0 V% U& p // indirectly by some other process).
' [% {! u* g* h( k8 j, m 7 U4 @! `" B; @2 D
modelActions = new ActionGroupImpl (getZone ());" c) V( d' s. C+ S
& q9 c: Z' x/ }5 H( t; n) e try {& E, G7 _0 u0 D# k
modelActions.createActionTo$message
1 J, r$ U& s' @; ?* c (heat, new Selector (heat.getClass (), "stepRule", false));" v3 g+ j6 P* l H
} catch (Exception e) {4 v$ ?2 \7 c9 [# _% W( N
System.err.println ("Exception stepRule: " + e.getMessage ());: t+ B. s+ ]7 s4 [
}
* i" \- K* X# i$ l9 P
* S- ~/ z& X4 H2 H# e2 _ try {- [( C2 t2 j. B' @* W9 Z* D
Heatbug proto = (Heatbug) heatbugList.get (0);& q2 Y; v9 Z2 W0 K l! r
Selector sel = 7 U& g) Z/ N9 y7 ~
new Selector (proto.getClass (), "heatbugStep", false);2 d- _' |. c/ L5 {
actionForEach =
- ]& g2 J0 y+ v7 `) v$ P# T. Q. Z modelActions.createFActionForEachHomogeneous$call
0 C2 W. N4 r9 C0 m3 j$ a3 Q2 T (heatbugList,4 m2 H4 U2 K; ^& {- X( u
new FCallImpl (this, proto, sel,
. R( @+ v; z3 `. i& n new FArgumentsImpl (this, sel)));6 l3 f+ h" U% R4 v) \: C6 C
} catch (Exception e) {
( K1 R8 F. K Z. W! l e.printStackTrace (System.err);
9 p: x- ]* K: ?3 w E }
* P" l [ D3 h' C9 o
z% U3 [1 G+ ~& ]* Z syncUpdateOrder ();
2 C W% w1 Q6 [% c
" g* x5 D ]4 G7 e' F* y: \# m try {8 C8 p" n0 C7 A$ s& w& I0 M
modelActions.createActionTo$message , h2 d1 W7 X3 o$ M; H L4 C! `
(heat, new Selector (heat.getClass (), "updateLattice", false));
: T+ y; O; c+ z3 q } catch (Exception e) {
; @0 O! X/ c7 S+ J2 j& p8 X System.err.println("Exception updateLattice: " + e.getMessage ());" m. }$ \/ `5 e8 \* ?7 P4 `
}: Y8 W, A, e2 [: j5 w | l1 y
' L- P9 \$ {- J, ~- A+ z
// Then we create a schedule that executes the
9 q5 @9 w- k; `6 b // modelActions. modelActions is an ActionGroup, by itself it5 s$ P) f0 t( }; v" D
// has no notion of time. In order to have it executed in! }% \$ o5 J; U
// time, we create a Schedule that says to use the' U1 Z H$ m) H9 N6 A* @* s* K) _
// modelActions ActionGroup at particular times. This
! J5 m4 L, V: W+ a$ K // schedule has a repeat interval of 1, it will loop every: i/ X( e% t9 S. w) j
// time step. The action is executed at time 0 relative to
3 `1 [# ]2 t' | // the beginning of the loop.
+ W. Q; i3 N, H% V. B2 A- o' l$ c6 I8 g
// This is a simple schedule, with only one action that is/ |( F( q- y0 ?) A/ q5 |
// just repeated every time. See jmousetrap for more# a$ K8 {+ s( _3 u6 J$ j( _( [
// complicated schedules.) ~8 _7 R' o }( L
, m- P: O* Y5 T3 [0 {( S$ p# v
modelSchedule = new ScheduleImpl (getZone (), 1);
L; E( A8 l. s$ e B1 r) Q5 P modelSchedule.at$createAction (0, modelActions);% ]# R0 z4 l) q0 F+ q& Q
. I! x0 O4 a; f" S& ^! I8 H# W return this;
) v, Q6 Y! Y0 F9 M* A } |