HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ v6 U; x# r& {, y1 v
: R' j0 i% v9 F2 A public Object buildActions () {: ~. \' ]6 t0 h9 E$ R
super.buildActions();
+ V8 M) }. e1 ?0 E' K8 ]2 w# `
) d R" l# f0 L; X // Create the list of simulation actions. We put these in
8 P. F% U/ v% B2 O& _* D+ ^ // an action group, because we want these actions to be. E. C* L7 c8 r1 H
// executed in a specific order, but these steps should
5 C4 N8 h- b& L$ C, |( N // take no (simulated) time. The M(foo) means "The message) Q0 l$ k! N+ `( N
// called <foo>". You can send a message To a particular
' t3 `( V" J2 m // object, or ForEach object in a collection.# w8 \; A/ l9 s9 p
( E' V2 Q/ {8 m9 r' s // Note we update the heatspace in two phases: first run
; p( W3 n( F* X, p) K // diffusion, then run "updateWorld" to actually enact the
, q( Z+ i8 h- A0 o+ P, i' g+ E // changes the heatbugs have made. The ordering here is
6 F+ g: {9 r# z5 B, ` // significant!
, E$ `, N( r( F9 }/ u ) L) ~, S' R& A( n7 ~3 L. C5 I
// Note also, that with the additional) |! k9 x6 S4 H* \8 D7 r' k
// `randomizeHeatbugUpdateOrder' Boolean flag we can
' `, L Y) |& B+ _! r0 b // randomize the order in which the bugs actually run
- H6 Y9 Y' g6 D! o7 B9 q+ `5 T // their step rule. This has the effect of removing any0 G+ m7 @6 s) L/ T
// systematic bias in the iteration throught the heatbug- ]" x# A( j6 N3 g' L
// list from timestep to timestep; q3 T0 O" P5 f, v
7 j) N- q6 N2 H, _( n5 D: T // By default, all `createActionForEach' modelActions have
+ {$ F( M7 p( u0 J8 f // a default order of `Sequential', which means that the
# T! R- w0 [: w. S // order of iteration through the `heatbugList' will be+ M/ _# D. l4 T7 i# s
// identical (assuming the list order is not changed1 u# L/ q' U4 c( R# L9 q* e8 Z
// indirectly by some other process).5 @, i4 g2 O7 h# I; ]
( h, }$ x8 _% ^/ f
modelActions = new ActionGroupImpl (getZone ());, X# z+ I8 d. F( z9 ?. E( t
3 `; ~7 U+ _6 k- l# B try {
, ^; t! e: S7 E2 ~$ \# V( Z modelActions.createActionTo$message8 ]. Q: @, {3 R. Z8 b
(heat, new Selector (heat.getClass (), "stepRule", false));
8 x1 \% L k1 H8 P$ { } catch (Exception e) {* X$ Q* H8 `& o7 @! b
System.err.println ("Exception stepRule: " + e.getMessage ());/ I& e& A* r0 ?6 u4 L' ^
}" V/ R7 Q5 L: G1 C3 G2 w: I
8 u, K" V5 p) D# a% L3 N
try {
8 @- W3 n% }2 J* \1 z Heatbug proto = (Heatbug) heatbugList.get (0); a2 z; L. B* I( u6 z3 i" m% I
Selector sel =
9 T5 N& X, g0 [. N* c new Selector (proto.getClass (), "heatbugStep", false);
+ x. w& S0 f4 F actionForEach =" z& I4 Q: p' ]. D! K
modelActions.createFActionForEachHomogeneous$call# ?* X: D1 I% d8 R- u
(heatbugList,9 ~$ N m/ L# U4 l3 E
new FCallImpl (this, proto, sel,
# ~0 l+ `1 x# O% y( S new FArgumentsImpl (this, sel)));7 Z Q) I6 d, m7 {9 W9 n
} catch (Exception e) {
" m" i* v: A, Q* m) k e.printStackTrace (System.err);
0 h, r: S4 n" `) a, P$ Y( g }
7 B0 T! O& N1 z7 s ; Z) l# |0 ^4 W5 Z
syncUpdateOrder ();
+ z( l) H! s, O% T5 u2 b* H( Y9 s
# U6 J$ F# S4 i try {+ |4 \; k/ e4 u, b
modelActions.createActionTo$message
% s I8 h8 F! | (heat, new Selector (heat.getClass (), "updateLattice", false));
$ ^5 s' R. k1 t# F9 h) ]( m } catch (Exception e) {/ Q! ]3 |* ~* D! |* g
System.err.println("Exception updateLattice: " + e.getMessage ());
# ]- n2 J+ W# P6 } }, J# M8 ?' Q2 J: q: f2 A
/ z9 h( x5 _9 ]8 s+ v4 N5 ?) e
// Then we create a schedule that executes the" `+ [) q [2 Z4 U# d, B" o
// modelActions. modelActions is an ActionGroup, by itself it
( a; k3 d' P" o2 [ // has no notion of time. In order to have it executed in* y! ?; W$ }9 D5 v; `% J% F! r
// time, we create a Schedule that says to use the
% h; l( }6 i- C& Z! a // modelActions ActionGroup at particular times. This( T# X9 g9 \7 k8 R. K
// schedule has a repeat interval of 1, it will loop every4 i" R6 n( S1 m# p
// time step. The action is executed at time 0 relative to$ i8 h8 J U; L4 e
// the beginning of the loop.
/ C% n: E4 ^/ Y5 z7 W- ~
' R# _ z) P. G8 X) k" \, q! j // This is a simple schedule, with only one action that is3 E# D J% C0 M. i# C; n
// just repeated every time. See jmousetrap for more
) C. n0 M0 h1 f, O5 a2 C1 T // complicated schedules.0 Y! @* ~& y7 z7 { `/ K
0 F4 b- i, j4 r# F7 u& e
modelSchedule = new ScheduleImpl (getZone (), 1);
' ]7 }: R$ H6 \* ~7 c7 H6 E! R modelSchedule.at$createAction (0, modelActions);
$ H, ~' M9 Z2 H# B 3 L( b8 v; I8 n, @0 q
return this;2 p$ v8 W0 b' k, }' w6 g
} |