HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
' C7 C; p7 l+ i
$ z+ C4 Q9 ~. J/ F6 g public Object buildActions () {
' D4 C! N* Y/ m) _8 C& S# @6 S, e: S super.buildActions();+ x+ m* W( k" C5 v
' U2 X; ^+ c( |& Q0 O# A // Create the list of simulation actions. We put these in4 k1 T' q+ [6 K4 D" O
// an action group, because we want these actions to be! M4 S6 ?9 U: g2 E
// executed in a specific order, but these steps should
% w' U K) l) x8 o7 N: ~0 U // take no (simulated) time. The M(foo) means "The message
9 \7 ^( }( B/ K3 G7 o [ // called <foo>". You can send a message To a particular
4 U2 b% w% R1 x // object, or ForEach object in a collection.# @+ k& ]% n' j3 `: d2 K% O
1 M" |4 b' t% N" k+ Y7 D% e, {6 a
// Note we update the heatspace in two phases: first run
7 ~# \, l' a' ^( j: Z // diffusion, then run "updateWorld" to actually enact the
7 F* h( k# [0 I# }: V; K // changes the heatbugs have made. The ordering here is
+ S, s9 v( L5 i+ H // significant!
2 c9 t1 M% C. H h" h( @
0 i; P9 F8 b+ y( g* k3 a; R4 k- s3 N // Note also, that with the additional
( Q$ x. g( h! Y) L l: c // `randomizeHeatbugUpdateOrder' Boolean flag we can* A: x* y" W% B3 h! y& Q/ e- c
// randomize the order in which the bugs actually run
9 C+ V3 p/ M8 h; N+ `$ C O2 | // their step rule. This has the effect of removing any
' \9 V6 J* q) F5 @7 T // systematic bias in the iteration throught the heatbug4 n+ }6 I7 d5 m
// list from timestep to timestep
2 ]; ]1 `4 a: @* f) R+ y( O4 Z
& d% \5 f7 t, Q( Q, ]) N // By default, all `createActionForEach' modelActions have5 S$ ?4 c, U8 V
// a default order of `Sequential', which means that the
& _# R$ \& j# x8 E2 W3 Z. a- s // order of iteration through the `heatbugList' will be' r& _; m) m7 w. S
// identical (assuming the list order is not changed, }4 l5 \! u: ]$ N
// indirectly by some other process).# @& u M0 X& }( R J) H
) U! f: y! E% |% }1 b* s$ { modelActions = new ActionGroupImpl (getZone ());
5 h5 ]* [1 P, Y* l
3 z: R/ G, H' {% P2 L4 a try {
& M& h) d D" O0 W1 V7 `2 F modelActions.createActionTo$message
8 ~" f# m1 {' N- x; M, } (heat, new Selector (heat.getClass (), "stepRule", false));( H5 V# c* m! y5 ^* ?
} catch (Exception e) {5 g* c6 I7 V( ^ w) N: w
System.err.println ("Exception stepRule: " + e.getMessage ());8 I7 v8 s4 m# b p# ]
}
. \8 m, k0 T8 S/ b! }" V1 C. ~1 a0 ~: d
try {3 @) ]5 G! f# x; P4 @0 W
Heatbug proto = (Heatbug) heatbugList.get (0);& |; c! M* ]9 @& F7 G0 V
Selector sel =
2 C$ e0 ~2 m8 k( X+ a4 I% ?* C" ^! n6 c+ L new Selector (proto.getClass (), "heatbugStep", false);$ {5 m" D- t. g8 J
actionForEach =
$ `: d+ Y0 k& s+ D I modelActions.createFActionForEachHomogeneous$call
9 ^; o' I6 y3 d% x (heatbugList,
; H- t% ~& s2 s- G8 S' {5 H new FCallImpl (this, proto, sel,
" `8 X, x/ R+ h4 y new FArgumentsImpl (this, sel)));, Q9 P) u: v" P, h/ l/ U
} catch (Exception e) {' D4 x( d c2 t. h
e.printStackTrace (System.err);+ I. I8 B! J" T* a
}
0 d/ @% d, i: Z, n: f 2 }0 S% M9 o( m$ q$ l# U
syncUpdateOrder ();
3 `1 M3 ?. h) c4 M# a) z) E+ m5 t, c
8 {% ]3 j! q+ e5 s& E- \& i& C try {+ y3 L: ?# g% v" v" k0 D
modelActions.createActionTo$message U' `( f+ S# i: G* P8 H+ W- y
(heat, new Selector (heat.getClass (), "updateLattice", false));) m- v* ?- J. z6 X3 {
} catch (Exception e) {5 Z# L; G5 h2 q" p6 S
System.err.println("Exception updateLattice: " + e.getMessage ());0 W( a3 d+ X$ C3 x% O
}
" p- x) _: M& n4 u: |
3 ]* |" Y3 V0 K' y$ D // Then we create a schedule that executes the4 \) x- ~$ ?, {/ t2 k
// modelActions. modelActions is an ActionGroup, by itself it; Z& h T- x1 ]2 U4 C" ` _
// has no notion of time. In order to have it executed in' R+ W' b, K# E' [
// time, we create a Schedule that says to use the
- L H& `/ @& F& q // modelActions ActionGroup at particular times. This! s2 F1 A0 s" p2 e- Y
// schedule has a repeat interval of 1, it will loop every- u0 j' H8 F. W. s/ `- p% P9 f
// time step. The action is executed at time 0 relative to
" R4 D: e/ w6 C2 e% p9 } // the beginning of the loop.* u y, ~: D( h- S
4 H& y: ]- ~( `6 ]9 `
// This is a simple schedule, with only one action that is
4 l8 ~- d! l% a8 C& d // just repeated every time. See jmousetrap for more' p9 [; x$ \$ B* Q' ]
// complicated schedules.( L J' u5 r/ s# J i
2 x. i3 v& U7 D modelSchedule = new ScheduleImpl (getZone (), 1);, R" v0 p/ S4 V1 |3 t& W
modelSchedule.at$createAction (0, modelActions);4 V/ @( A7 b( D( E: L$ l, Q1 I
: S3 U6 j \" `; O' [: B return this;
% f* ^: s$ X9 y } |