HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: C7 N) i% g0 G8 V/ K2 K1 X
( T3 T, D$ A) J. `+ U public Object buildActions () {
7 r$ h$ H; `9 s; l4 |$ x# \+ `5 G# p super.buildActions();
- Z9 O; g) K# P% L: J" }+ | " G$ B$ ~, S* `! Q8 j! X7 q# z
// Create the list of simulation actions. We put these in
9 q2 d$ U" L) b) f- i // an action group, because we want these actions to be
' {4 ]0 o, J+ c: Z3 z5 O // executed in a specific order, but these steps should) V5 T% m1 l7 X: |1 E+ j
// take no (simulated) time. The M(foo) means "The message
+ O" C4 o# Y( Y( T" c // called <foo>". You can send a message To a particular5 d O$ K5 X) l5 A2 X2 q( `/ }
// object, or ForEach object in a collection.
1 K! W6 E9 A- g
; n: ~7 e, r2 @ // Note we update the heatspace in two phases: first run8 i$ P' \" ~& ]7 D% p% O
// diffusion, then run "updateWorld" to actually enact the
! r5 w5 s7 W8 J: J# P; }! p // changes the heatbugs have made. The ordering here is. T/ q, C1 b/ m5 E: U
// significant!9 I( C9 N" V T! b2 c8 Y
$ h2 y. R: l9 a1 o. ^4 f4 W: l k3 V
// Note also, that with the additional
$ y7 e! Q! v: |" D! Y; n // `randomizeHeatbugUpdateOrder' Boolean flag we can
: ]' u: _$ I( K" S0 L( v9 E0 q // randomize the order in which the bugs actually run
9 w% E7 M& b! v" ]/ G' H0 {! C // their step rule. This has the effect of removing any
) E, H( D0 A; H/ h4 n. ~* v2 w5 t // systematic bias in the iteration throught the heatbug
. f" c O9 b" A, A: s% N% ^ // list from timestep to timestep' Q/ r( I- _$ }' J6 m3 o- I
& }$ w2 M" o3 s
// By default, all `createActionForEach' modelActions have E$ h3 d7 Z: T+ V: k0 n* b
// a default order of `Sequential', which means that the- n% H* ]' j8 L2 r
// order of iteration through the `heatbugList' will be
9 g4 ~: e1 f8 t- L% B3 m/ }2 z. ~ // identical (assuming the list order is not changed& T# u, q$ V( i5 z) m
// indirectly by some other process).( J$ O) Z% |" z% A: G
+ f# k' _# s! c+ l4 X
modelActions = new ActionGroupImpl (getZone ());, h! z. R7 b" A- i, C' J9 o9 D6 A* z& Z
) X- H* p! F* I( {) k try {: e0 @1 i3 a. G6 ?$ T8 G; b7 B
modelActions.createActionTo$message
* {5 F' i4 A, \) C0 z" k2 l/ K+ n (heat, new Selector (heat.getClass (), "stepRule", false));
' y2 D! L9 [7 ^, o+ d } catch (Exception e) {
! @ Q O* r- T, J2 q4 B: Z System.err.println ("Exception stepRule: " + e.getMessage ());* Z: m6 {: V& B" E6 ^: n0 D: k
}
5 q {) o1 B% s: `4 V W% [: b0 p* Z2 j! G
try {9 j. R% b1 e# D! i2 |; ?; e
Heatbug proto = (Heatbug) heatbugList.get (0);
8 ~8 f' q" m9 I5 C/ Y, T Selector sel = . j# O& m" K# t8 C! b
new Selector (proto.getClass (), "heatbugStep", false);8 j' {" K r) @1 \
actionForEach =$ G. V: t# h" _# e6 w
modelActions.createFActionForEachHomogeneous$call
8 c. `0 h" p- a1 p (heatbugList,
$ H; F: D' e& p' Q new FCallImpl (this, proto, sel,
2 h1 d3 }' T6 s6 v. m# H new FArgumentsImpl (this, sel)));+ Z8 [; N$ k/ v$ y9 Q0 l/ P' B4 D
} catch (Exception e) {
; K0 s- P- s+ F% z W/ c e.printStackTrace (System.err);# `( ^& E) b. u; t& H
}, f& Z( }# ]' P; r
7 E( L! q; Z! ]
syncUpdateOrder ();9 E: B, F! q9 O q6 F
/ h# A% ~' M3 b% _ try {
3 m3 U6 I1 |; A: h6 n modelActions.createActionTo$message
, D6 S, y; `/ m9 K# c2 j# u5 w (heat, new Selector (heat.getClass (), "updateLattice", false));5 {" e2 b" `/ J2 c! ?
} catch (Exception e) {
5 n! T: P: [) o4 ]/ X6 `9 x System.err.println("Exception updateLattice: " + e.getMessage ());3 i- Y( m. s8 n) z" Y+ o7 u- m
}
7 Y+ M% F% `- L. S" M
* a% W2 \$ ~6 }9 A // Then we create a schedule that executes the1 Z' @4 |* F$ a' N2 e
// modelActions. modelActions is an ActionGroup, by itself it
2 L5 J6 _, |$ {+ U% | // has no notion of time. In order to have it executed in& N- t: a6 A5 F0 F3 M& J; K
// time, we create a Schedule that says to use the* X- @. U# D0 A3 x7 [& W3 P9 t- Q
// modelActions ActionGroup at particular times. This
. Y7 T0 E5 L% m, c2 g% v& f0 } // schedule has a repeat interval of 1, it will loop every
5 m$ g- x/ U; F2 k // time step. The action is executed at time 0 relative to. R' _0 l# |8 L
// the beginning of the loop.
9 s7 }6 u A" w1 ^) K F' k; z& c
// This is a simple schedule, with only one action that is/ W2 G7 o8 t1 ]3 N+ ]
// just repeated every time. See jmousetrap for more
! B- W. M' M# O' t: E3 l9 D5 k // complicated schedules.
. N2 v% H* o* L- i% A5 V# d& \ & Q- L- T5 x# f; U5 y
modelSchedule = new ScheduleImpl (getZone (), 1);. G0 g+ Y7 E" l6 K( X) ~4 e8 y
modelSchedule.at$createAction (0, modelActions);0 A2 Q5 i4 y( I, \' `
) r3 ~5 N& `2 h2 F& e: A3 m
return this;
7 w5 a7 U, E {- k } |