HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ Q" K9 C. e+ C O* w8 [. ^1 w& X: y' ?1 f, p; M! _
public Object buildActions () {9 x1 i' U/ V+ r/ ]: M- h2 e
super.buildActions();) ?3 z- ^9 z: a. j
) ~) c: h3 q1 `1 W // Create the list of simulation actions. We put these in
2 G- ^9 F$ ~2 ^7 ~" m& r // an action group, because we want these actions to be
' z& p: Y9 W8 o; x) ^8 l" r // executed in a specific order, but these steps should
# l" J& u$ l/ Z // take no (simulated) time. The M(foo) means "The message
& ~* V2 s# U5 E- T* l( ^# @* k // called <foo>". You can send a message To a particular/ o& j i+ z+ u- C; {- p2 @
// object, or ForEach object in a collection.% M L6 k. s, y+ S( |
" A' h% `7 H' t% k o
// Note we update the heatspace in two phases: first run: ~, u0 z' c. C3 T& O
// diffusion, then run "updateWorld" to actually enact the* J4 p1 U! f: z
// changes the heatbugs have made. The ordering here is/ ]4 i0 ? {- O# X! H$ y5 U4 u
// significant!+ i; ^5 A# W3 z- w5 q4 S7 J' X
! v, }4 |1 g: s
// Note also, that with the additional) W7 u5 J( k8 z+ e
// `randomizeHeatbugUpdateOrder' Boolean flag we can
& b- s! q+ Y8 {: f" S' ?" O // randomize the order in which the bugs actually run) f" Q: p, F5 b0 S
// their step rule. This has the effect of removing any, ?& W- o1 b- ]( a5 R" D4 A
// systematic bias in the iteration throught the heatbug. @# @6 k% v0 K7 {9 {
// list from timestep to timestep4 I3 a6 b) D- t+ Q* K* |% L5 T1 I
, ^- E" R- s6 ?* j" q; ~) ]6 h% C // By default, all `createActionForEach' modelActions have7 e" z* h3 ~9 F- `! b$ M0 ~
// a default order of `Sequential', which means that the$ Q9 C4 R) [7 [4 g" X& V* I
// order of iteration through the `heatbugList' will be! j! u* n" _! X. g9 {
// identical (assuming the list order is not changed7 r# i0 @9 z$ \) T# c
// indirectly by some other process).6 K$ k- T/ m* H5 }! T# E2 K: }
4 S/ l" I5 Y7 i9 N# u/ t modelActions = new ActionGroupImpl (getZone ());
# {% Z! _, E/ U* v7 ?
Q! t. p% b4 Y2 k try {
. b7 M* z/ ~, M$ `) K( b' F, Q modelActions.createActionTo$message
& L; u9 B& N. j' ^% A D) E$ N8 Q9 @, C (heat, new Selector (heat.getClass (), "stepRule", false));
2 [1 l! `" x1 m( k. s$ T2 Q } catch (Exception e) {
* J3 O, J2 B5 A. a System.err.println ("Exception stepRule: " + e.getMessage ());3 h- V q I: q2 D
}* R+ l2 x9 F; h& G( B4 G1 }! _
, }, F( a8 l S) R4 h
try {
+ X: o% W0 \5 Q Heatbug proto = (Heatbug) heatbugList.get (0);
* F9 w/ g: X q4 X) d% O9 o Selector sel = 5 Y B" p# i7 O1 G8 }, y, Z
new Selector (proto.getClass (), "heatbugStep", false);' u% ^2 G% u: \) f$ N; ?5 Z: g
actionForEach =; U" Q$ |1 h9 d# }' V7 @, V7 c
modelActions.createFActionForEachHomogeneous$call
8 g# ?" Z$ J3 G0 s (heatbugList,) U! w. C: M0 e. R' m5 l5 g
new FCallImpl (this, proto, sel,2 T$ [" p/ s' u- R+ ^/ K! {; P
new FArgumentsImpl (this, sel)));6 }+ F4 E6 S: Z( g
} catch (Exception e) {
9 X0 |( x- Z4 ^0 d! | t# ^3 s e.printStackTrace (System.err);; \2 p( w1 G1 n' v
}, e6 Y/ m' w9 W' f: ?( b4 Y
' F6 C; W1 l; G3 j f syncUpdateOrder ();8 I9 m$ K( w; ` E( f
2 l$ }% ~; b z7 L6 d+ K try {
8 d1 A! P# l5 @1 ~# N( N modelActions.createActionTo$message
; I R* ~* [7 v# ]6 j. ]+ Y (heat, new Selector (heat.getClass (), "updateLattice", false));' Z; {* t j+ g: [2 {
} catch (Exception e) {
* r% k2 B. L7 Y5 D4 n' C System.err.println("Exception updateLattice: " + e.getMessage ());; A4 Q8 p( V8 [
}
2 ^* S6 v2 Q; }/ t, p& F) W ; `8 F9 g; W5 q; ?$ r
// Then we create a schedule that executes the
7 m" l, W- A! R- D! n5 L' [0 n // modelActions. modelActions is an ActionGroup, by itself it
& K, z g( R; B; w" R- j // has no notion of time. In order to have it executed in* z5 ~9 F9 L+ V% c
// time, we create a Schedule that says to use the/ j7 L' l {6 Q2 r+ q
// modelActions ActionGroup at particular times. This
- E7 d$ s* r' { // schedule has a repeat interval of 1, it will loop every7 r p1 e0 E* m8 L5 A
// time step. The action is executed at time 0 relative to
2 X* O: S5 S% A# |0 y% S // the beginning of the loop.
, D$ ]( f! q5 F& ^0 j3 f/ H& C# Q5 m
// This is a simple schedule, with only one action that is
1 ~8 O( s# f$ S7 V // just repeated every time. See jmousetrap for more
3 a" n3 h7 B) N7 z2 p4 N // complicated schedules.
( T6 \% S- b3 v
( }& [- N% m" n6 Q; o" h modelSchedule = new ScheduleImpl (getZone (), 1);9 m U6 R+ m; |4 ~! M. |: q
modelSchedule.at$createAction (0, modelActions);
0 s- C9 a5 X4 O& i
) S9 L% K6 J) H m( r1 G/ | return this;# q; @% \$ F# }' {4 L9 ~
} |