HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; s# U0 {8 ~7 y6 u
/ D. K/ U( m3 s6 E public Object buildActions () {
) o: \2 q2 i ?% O' y2 [" P7 B super.buildActions();9 Y/ x( n! e* _# v, Z* w# b7 u
. U+ T+ i5 }4 U7 m9 b; c0 Q // Create the list of simulation actions. We put these in
) t8 o+ [7 k5 y8 N4 V2 c // an action group, because we want these actions to be
# F3 P! z( x6 b* U, K5 k; p // executed in a specific order, but these steps should
5 S8 G7 h A3 B6 A. k7 t6 f/ w // take no (simulated) time. The M(foo) means "The message
" {9 R: ~# r0 J# q% W! ^( C3 F // called <foo>". You can send a message To a particular9 x- |6 y' O: A
// object, or ForEach object in a collection.
; K0 }$ x2 c1 L9 f 1 ~' p7 b, r% [/ W4 |* I' j9 w' z. |
// Note we update the heatspace in two phases: first run8 p0 h0 P4 f2 X6 v
// diffusion, then run "updateWorld" to actually enact the, w; E# L; }5 Q D6 }" g v' e
// changes the heatbugs have made. The ordering here is
1 S! H* W0 `6 x // significant! w: d& a8 G5 M" _$ b5 \
& y( }, ?, P1 t! V // Note also, that with the additional
) y7 i0 f$ E9 O) w7 v j" r // `randomizeHeatbugUpdateOrder' Boolean flag we can
6 b8 I/ P6 y/ d2 y& _ // randomize the order in which the bugs actually run
( E2 i$ e2 A* u: T# n& V3 t* ` // their step rule. This has the effect of removing any
( V8 M% h4 P7 _9 U // systematic bias in the iteration throught the heatbug- G3 h) ~ ^- ?" B# F
// list from timestep to timestep
9 d& g# I! R9 B* G+ P: U F/ D" X ; j* n* h, b% |+ b
// By default, all `createActionForEach' modelActions have
8 w. _6 o. Z; N( B // a default order of `Sequential', which means that the
# g. J: d; s: i+ l! K // order of iteration through the `heatbugList' will be
) r& W: `2 v( @3 s( s5 h" U // identical (assuming the list order is not changed
6 M3 @) `2 T; y! @# r7 `% ~6 h. I/ G; ] // indirectly by some other process).
/ Z. [( D4 G" ]" v, X e2 [8 r ' Z7 p# h$ X( d; f
modelActions = new ActionGroupImpl (getZone ());
9 H$ ^( l6 \) s# y9 m m. ~# l# `; Q/ b2 Q5 v
try {* ?$ R; N) S3 j9 d4 J5 Q
modelActions.createActionTo$message; z7 w. f I- a3 E
(heat, new Selector (heat.getClass (), "stepRule", false));5 C. F+ ?8 w/ \. u( \. D
} catch (Exception e) {2 q& ]/ e; t! [2 }% i7 k
System.err.println ("Exception stepRule: " + e.getMessage ());
# c' _: q. {8 N* u g/ I }% O% M' Q# ?; r
7 ]8 P6 Y5 B' u$ U. g- f4 \# _
try {
, u/ z3 f$ }) ], y6 | Heatbug proto = (Heatbug) heatbugList.get (0);
2 q) d: P! @6 _6 H, g Selector sel =
# V, |# O2 J- h9 ]( l+ @* w! f1 G new Selector (proto.getClass (), "heatbugStep", false);: F8 W: E1 u- q# g6 T7 I& O C
actionForEach =$ } a8 o- a' H3 ^; o' z0 V" a
modelActions.createFActionForEachHomogeneous$call/ l+ @+ d( x) n; ^* e+ u6 D7 `
(heatbugList,2 j0 p: ]; h- I1 U- Z- q
new FCallImpl (this, proto, sel,
9 ?9 {. T4 }- d* C1 B new FArgumentsImpl (this, sel)));1 Y* m7 K4 Q$ N, n6 Y& m: m
} catch (Exception e) {
3 R3 e) x, a, P e.printStackTrace (System.err);
% R6 I7 J7 @# R: k: r' n }
6 O0 u- M- g) Q% l5 b1 Z0 N8 ?* b : O2 b, s' f9 a) j2 X1 ~* D
syncUpdateOrder ();6 d2 n: |4 y* q- X
' U, _5 Y9 }4 _& i4 ]$ o, B7 d
try {) Z4 Z$ M6 A- H' Q5 F; F
modelActions.createActionTo$message
+ |# { I6 f' x (heat, new Selector (heat.getClass (), "updateLattice", false));
! i R! U' v2 P0 { I } catch (Exception e) {6 x. b6 U$ W8 ^' O4 d
System.err.println("Exception updateLattice: " + e.getMessage ());7 f8 b, R) F, Y
}
. @" @. G @. b) h2 h$ x8 D( f" _: r) n
$ U/ q& \+ D6 C; `, ~3 L // Then we create a schedule that executes the
4 R1 H: |' y: @$ D' V k3 N // modelActions. modelActions is an ActionGroup, by itself it
( X6 G( s2 g- k8 w // has no notion of time. In order to have it executed in. s+ v+ s+ e/ f& W0 {
// time, we create a Schedule that says to use the
9 Z$ F& A, ~6 S; @1 D // modelActions ActionGroup at particular times. This9 G, j7 s1 T5 g" @1 |2 j) ~
// schedule has a repeat interval of 1, it will loop every
( n2 }$ _# N5 v // time step. The action is executed at time 0 relative to2 A$ b R/ Z9 n* E. n! H$ O) w
// the beginning of the loop.+ b3 B. Y9 U+ j n- w7 K! H
4 l7 e% [- M9 f" R% F // This is a simple schedule, with only one action that is
% |3 k+ w& o0 e) I }9 a // just repeated every time. See jmousetrap for more
: f6 u. F8 e+ t% f6 J // complicated schedules.
1 O& g# g. _) C: J B9 {4 u9 D7 |' D
3 i. H8 h6 R. v modelSchedule = new ScheduleImpl (getZone (), 1);
6 }2 C5 @- V( P: w5 D; l* @ modelSchedule.at$createAction (0, modelActions);
* c7 d4 ]) c% x0 ]9 ]3 t$ L' ^% r 1 t. ]) M1 h4 C4 j9 ~7 [
return this;
8 A' R ^! C7 c/ h) _ } |