HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" [, ~/ K/ I3 c# }
0 k) V9 w3 r# O public Object buildActions () {
' X% V7 Z, W" a1 t) d1 w super.buildActions();! R/ |( |6 D2 ^- U# X' q8 G+ |. F @' b
( V% B/ i% L* ~/ E% J! ], ~5 K
// Create the list of simulation actions. We put these in
, ^" c2 u7 `& u/ k; T! o2 Q // an action group, because we want these actions to be
& d! g: T7 ?, j, ]1 Q. c% L6 T // executed in a specific order, but these steps should
# Z# S' c: g+ ?7 J4 A // take no (simulated) time. The M(foo) means "The message
: ^/ v6 R' ?) v' B: K+ M // called <foo>". You can send a message To a particular* ^& ` X0 l; z! J6 _
// object, or ForEach object in a collection.
8 l* U2 \* ^/ G& E( W% F$ q/ |/ A
. ?7 y1 E8 G6 ]' l9 t% E+ [. H // Note we update the heatspace in two phases: first run- }5 u/ G: d+ r9 D9 C5 a7 ~& N9 l
// diffusion, then run "updateWorld" to actually enact the
1 a8 B; C8 R9 }; E // changes the heatbugs have made. The ordering here is0 v( X4 P# ~9 |6 g, S6 x
// significant!6 ^* Z4 H% j% c
' m1 o0 E! e3 O1 s2 h! H
// Note also, that with the additional3 M2 O; a* h+ a2 {
// `randomizeHeatbugUpdateOrder' Boolean flag we can
9 r' U! H& h3 q6 g6 q8 `3 }; v // randomize the order in which the bugs actually run
! ~, c6 k z( o7 `4 _ // their step rule. This has the effect of removing any+ i- {; ~1 B2 ?, v1 K$ }
// systematic bias in the iteration throught the heatbug
$ n$ k0 w$ M- @. I // list from timestep to timestep' o" X, \9 I3 b% o8 v, k+ [ C8 v2 y. o
6 n" j R6 ^2 [5 r# V
// By default, all `createActionForEach' modelActions have
7 E! M, X. a7 E9 e // a default order of `Sequential', which means that the2 \/ F% S( M S+ h) S g5 P
// order of iteration through the `heatbugList' will be
' j. @8 S& ]2 G+ A1 U7 J // identical (assuming the list order is not changed
* M% e: q% k2 ]& y3 |; i // indirectly by some other process).
0 u2 |8 U! T8 d# L0 z' ^ : i- C( P2 Q" f# i+ i7 A
modelActions = new ActionGroupImpl (getZone ());
. W+ o8 B, f1 @' _' H) a) L% ]
try {
2 V/ y5 T9 o" v% ]0 q5 i modelActions.createActionTo$message* b( O9 S+ ^. `0 l0 r3 p% s h) P
(heat, new Selector (heat.getClass (), "stepRule", false));
; T" M4 x9 l9 Y7 G7 p A } catch (Exception e) {
2 m7 j- b" w" t0 _ System.err.println ("Exception stepRule: " + e.getMessage ());1 D0 G$ a8 |, K- G: n3 h% D
}
* d' q4 O5 p* y
+ m5 Y \, h. {) Y2 J. K. j5 C try {7 y2 W, s6 q' m: F1 U2 G
Heatbug proto = (Heatbug) heatbugList.get (0);
' ~% @) h. H! l Selector sel = , D- k- P8 A. g, _, H
new Selector (proto.getClass (), "heatbugStep", false);& U; _! _ z$ b4 [% f
actionForEach =! a6 N, Q* I) |
modelActions.createFActionForEachHomogeneous$call
. B; b8 ]0 Z) D% S: q1 P( r (heatbugList,
8 Z7 F( q: i g/ t new FCallImpl (this, proto, sel,9 W3 `' p0 F8 M1 z# ]
new FArgumentsImpl (this, sel)));+ ?. f; r. ~* z' _% A0 k
} catch (Exception e) {6 r# k; [. m; N. R3 [$ C
e.printStackTrace (System.err);+ X k) W5 e3 {6 X+ k3 K$ v
}
7 D+ u# [% M$ z! I
; ? U5 _' D# _3 P5 j, F5 d( O- ^ syncUpdateOrder ();7 h& h1 H0 F* Q' }) G4 {* p
. ?$ w6 y+ A0 [2 ^7 a% O C try {
, n5 V1 i0 b7 K2 p8 A2 Y modelActions.createActionTo$message
1 x# x/ N# y- |: R8 s) |* z" z (heat, new Selector (heat.getClass (), "updateLattice", false));
) H* u4 B- A# G% B0 Q& v' V } catch (Exception e) {
4 y& r$ C" X6 f7 f- K, w System.err.println("Exception updateLattice: " + e.getMessage ());9 G/ P& r" w, C+ H, v1 W: g4 V! Z
}# L6 b5 B7 ]- l) {7 b5 i
2 F$ q( ?% U: x8 T3 ?2 F; k
// Then we create a schedule that executes the
# y+ ?" }2 l) K' M // modelActions. modelActions is an ActionGroup, by itself it
; \1 B. T& j; e4 G+ C // has no notion of time. In order to have it executed in
4 H( O" b" w0 W$ E% N // time, we create a Schedule that says to use the
: C9 ?8 c+ q) B0 _! D" } // modelActions ActionGroup at particular times. This
, r3 H1 h% N7 ]8 w K+ {9 w // schedule has a repeat interval of 1, it will loop every9 q6 q$ D$ N+ r6 S4 }
// time step. The action is executed at time 0 relative to
# K9 C( r4 j" X1 N, R // the beginning of the loop.
) ?) _2 }* Q; Q- C, S
: W y8 n! {5 z! y3 t" @4 E // This is a simple schedule, with only one action that is7 Q0 V; y% D8 `8 A; N
// just repeated every time. See jmousetrap for more
8 f+ s# U7 `, z$ X1 H: s // complicated schedules.! V k( z3 S; o/ y Q
- `: e ^6 o- P# z+ q modelSchedule = new ScheduleImpl (getZone (), 1);
1 E% ^' k/ k6 h- f" B0 ^6 ?9 L modelSchedule.at$createAction (0, modelActions);
1 X8 a/ t& Y, b' z9 {
5 c( R% y0 y3 C- |0 W: Q2 R' D6 W return this; {( E9 V8 F8 Z0 Y: L
} |