HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 L8 n, @5 L- w7 D8 R( N
: p5 N: \7 g, y/ l3 \6 Z+ X, m
public Object buildActions () {- c4 L2 G. M) \9 w/ y
super.buildActions();4 J7 L* t) {3 d; x; v3 [2 R
5 n; M& e! V5 d" ^ // Create the list of simulation actions. We put these in
- K+ y: E8 |4 j4 b1 }. l, b' @ // an action group, because we want these actions to be
, H$ B3 I9 Z2 m, I5 y0 d( V, v4 D/ g // executed in a specific order, but these steps should# D) d! W4 P7 C, b0 Y% V1 m5 q
// take no (simulated) time. The M(foo) means "The message! \) W. I* ?+ D
// called <foo>". You can send a message To a particular
0 k0 N& C' T' Z // object, or ForEach object in a collection.
& m, k/ G8 A$ s: e- m7 z ( s7 ?; d8 j0 `8 f2 |. T) G4 Z
// Note we update the heatspace in two phases: first run
( E, A" y. R8 l+ f: w8 d2 V+ \# \ // diffusion, then run "updateWorld" to actually enact the7 [3 t8 L! K$ ^
// changes the heatbugs have made. The ordering here is* ?9 _% Q& R0 [7 C/ a
// significant!5 d; t2 R: Q& C( Q+ { [
" Y# g; ^3 w& h; ^
// Note also, that with the additional
* ~' [& t* K1 m( n+ t: A$ i // `randomizeHeatbugUpdateOrder' Boolean flag we can
' s+ G' V% V+ L$ V- X // randomize the order in which the bugs actually run) `/ l F, [8 E9 H5 Q' ~
// their step rule. This has the effect of removing any* l; z' |& m. M0 c+ q
// systematic bias in the iteration throught the heatbug
3 }; C7 J( Y- s! a4 A // list from timestep to timestep
; { i% R V6 L3 p4 F# C
* O; e {# i( {) l9 f // By default, all `createActionForEach' modelActions have
; R6 \* J9 N: R0 t // a default order of `Sequential', which means that the0 k( i5 o8 D$ y# X0 @; b/ H
// order of iteration through the `heatbugList' will be
# i4 m' L/ p+ \& E' j! ^ l // identical (assuming the list order is not changed* ?: ^ r# ~' w' n
// indirectly by some other process).
& _9 O5 e1 Q- Y 7 S5 c3 Q+ g, x0 W$ |
modelActions = new ActionGroupImpl (getZone ());
4 \& `. W7 ^3 z6 W! F ^
7 N0 }/ q9 r& g try {4 {' G6 l# t9 [! ^2 q0 }; W
modelActions.createActionTo$message, `* w- d% i5 j/ V$ }" ]5 q6 {
(heat, new Selector (heat.getClass (), "stepRule", false));) y3 c+ F, `6 B1 I, Q/ t5 K2 q
} catch (Exception e) {8 K+ }# Z# H" `, v+ Z
System.err.println ("Exception stepRule: " + e.getMessage ());/ X v' X6 Q/ Z4 O2 n- r$ D+ |
}
! q5 m. b5 }3 [" y6 N) n" s2 d+ i1 H1 y( o; i* h4 f
try {8 y$ c+ ]& U* @* H7 e
Heatbug proto = (Heatbug) heatbugList.get (0);
) \0 w& k& J3 {% N7 a( S Selector sel = # Q+ p: ]" s2 @
new Selector (proto.getClass (), "heatbugStep", false);
$ f5 D+ A; T5 n' f actionForEach =
# h/ ]/ j2 H' U1 {6 W6 ~6 S modelActions.createFActionForEachHomogeneous$call
1 v# S) b+ J1 q- I6 |. C (heatbugList,
9 a# V+ G y7 P1 X- I new FCallImpl (this, proto, sel,
* Q+ L; O4 Z0 G" s7 t' [' Z2 W new FArgumentsImpl (this, sel)));' A0 V( a0 K0 C& K9 Z
} catch (Exception e) {
! t; x- i% M( N e.printStackTrace (System.err);4 s/ k; g8 M# s- U* a3 _1 g+ M
}" v( s( k& C- ?$ c9 L6 o+ R+ }5 E
- q2 X& R. ^" Q2 R" k2 L8 ^ syncUpdateOrder (); h$ r4 L& I/ D
& a+ K3 C0 K, }6 M2 x6 i; R5 ]
try {/ s( {6 s3 {) r2 t b5 k, |
modelActions.createActionTo$message
5 k* w8 M- x* D7 y& W (heat, new Selector (heat.getClass (), "updateLattice", false));, w- V+ N9 P8 L, s4 U
} catch (Exception e) {
9 J+ Q; {9 D- F1 T" Y5 z System.err.println("Exception updateLattice: " + e.getMessage ());7 }' l( F( r* z) Y9 S, t' v( \
}0 t3 @4 y0 H1 V$ M9 X$ U
2 P# q7 G: y, r0 n5 P$ D // Then we create a schedule that executes the! _0 z' c) ]" H2 B
// modelActions. modelActions is an ActionGroup, by itself it" C" Y& q; v5 e1 q' u+ @* |, U2 X
// has no notion of time. In order to have it executed in. r4 ~/ d- W0 U) ^( ?% B0 k8 f! h/ p
// time, we create a Schedule that says to use the
8 p I! ~+ W' Q4 O // modelActions ActionGroup at particular times. This
* }! w4 D3 H7 {( d* c; L // schedule has a repeat interval of 1, it will loop every
3 f, U5 ~# m% Z/ W5 p, R // time step. The action is executed at time 0 relative to
9 M! H' X# I; P' F // the beginning of the loop.& c, U) r& R4 c5 x: b
5 C; ]# i( o+ c2 r( `# O
// This is a simple schedule, with only one action that is
, i) {9 ^5 k4 E1 F' G1 ]# U% ^ // just repeated every time. See jmousetrap for more
: [9 e% ^+ l6 q // complicated schedules.
; Q6 ]! }$ e' \3 f
/ _5 G( [+ @& b. A modelSchedule = new ScheduleImpl (getZone (), 1);7 P+ P( C/ b2 j$ m% q9 J
modelSchedule.at$createAction (0, modelActions);
) k$ J _ {; Y! C: H: s
; h1 d- V7 ?% F! f4 ] return this;( Y* }3 o* c8 W" z" ^% f; F
} |