HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
; p9 I/ w! I. P9 S$ r
3 X$ y9 J, B, q3 L& J# j& M$ h5 |$ o public Object buildActions () {
) Q( T9 i1 i2 |* j super.buildActions();
% b {" ~' W H' C! c: ] 4 j* r4 E- z% Z H" H0 E) R3 y
// Create the list of simulation actions. We put these in
/ X8 j5 m' V# b) y/ o( i // an action group, because we want these actions to be
- A7 c: H* e0 o& V // executed in a specific order, but these steps should
7 h# B0 l; l. |. {; U // take no (simulated) time. The M(foo) means "The message
" x' [+ c6 N0 C // called <foo>". You can send a message To a particular$ ]9 v4 ^/ y6 u( l
// object, or ForEach object in a collection.& Y7 d- | ?5 e- c, F4 p
/ p2 G9 i/ |: r, j b4 C // Note we update the heatspace in two phases: first run" u' R6 B [0 ~* K+ @0 j0 f: b
// diffusion, then run "updateWorld" to actually enact the7 [( Q: p7 T2 X8 |( v* C1 J5 O0 H
// changes the heatbugs have made. The ordering here is
* i$ X# b; {' `% R // significant!8 M1 f( U0 F# Z$ ^& U% N2 i
* M" w/ x5 `+ W1 _+ J1 X. o
// Note also, that with the additional
' H0 w! U( f; W7 y# n9 h // `randomizeHeatbugUpdateOrder' Boolean flag we can
+ E3 ?: s, s& K6 @3 y // randomize the order in which the bugs actually run! g' ^. s& U" m/ x4 p; c
// their step rule. This has the effect of removing any
" J1 M1 i! Z# x) y+ o; h // systematic bias in the iteration throught the heatbug
5 q! T3 |% b0 X8 |$ I // list from timestep to timestep
# N& A: D! t, x% B, p
k _; V; {3 Y7 K, d6 t4 c' }/ A // By default, all `createActionForEach' modelActions have
/ S$ ?$ R/ e1 O+ [( Z; `7 t5 k" L // a default order of `Sequential', which means that the: U8 c* ?" _0 {* |; S# b/ L
// order of iteration through the `heatbugList' will be
* y7 G( j3 [1 `/ \ // identical (assuming the list order is not changed% q( m( t& L1 K5 B a
// indirectly by some other process).
# M. {$ u% n: }8 y# R
# ?4 ?7 U4 }4 f; j) { modelActions = new ActionGroupImpl (getZone ());
- H0 Z) p7 w. M
& |4 T6 C- s; M. l k try {% {% R4 [: o8 @8 x
modelActions.createActionTo$message
7 S4 d4 ^" e( x1 O6 T+ s# M (heat, new Selector (heat.getClass (), "stepRule", false));
! q- B. t/ k( f } catch (Exception e) {# ]. ~) D5 O* q Q8 }
System.err.println ("Exception stepRule: " + e.getMessage ());
- D* W g1 h. S6 R }
5 V6 P: [2 T1 L! [$ Y0 j8 e- |0 g/ r
' }5 E& j$ }3 k try {
1 P4 e- M2 u/ q Heatbug proto = (Heatbug) heatbugList.get (0);* Q$ |' d& l; ?! H2 Q
Selector sel = # A1 h6 Q' H- i8 C- l- V3 Y
new Selector (proto.getClass (), "heatbugStep", false);
# W4 Z5 Q2 q6 ^: ?0 O3 V actionForEach =8 S6 h# C7 Y/ @% q1 X
modelActions.createFActionForEachHomogeneous$call$ @7 {/ e( e% D6 N' t
(heatbugList,$ j( q7 J( {7 ?( W, a
new FCallImpl (this, proto, sel,+ @! C: x& j6 k+ T2 U0 z
new FArgumentsImpl (this, sel)));
: a& A! [# s& b3 T } catch (Exception e) {
, I+ Y3 ]3 B# a/ X* F# ] e.printStackTrace (System.err);; ]) Z) \, J) B2 u" k, v
}
( r2 D4 J3 ]0 y) f. J9 Q ' v0 @+ Q( N( X; ]
syncUpdateOrder ();- M+ _. | Z# Z* l
. |: Q; A. w4 q& h& b. Z& ` try {
1 l# N0 d1 `; b4 u3 I4 P3 P: A; r modelActions.createActionTo$message 0 e( }& q$ F/ n3 O3 h" R) P
(heat, new Selector (heat.getClass (), "updateLattice", false));
3 Q5 @( K `% g9 x- `: [ } catch (Exception e) {8 t( E! c# ~/ E1 x, u& A' [# h) E+ n f
System.err.println("Exception updateLattice: " + e.getMessage ());
! `3 s2 o" W& l# I! M% K }7 u, n9 b4 ^( g: [: C
7 @& \8 B, @" S% k // Then we create a schedule that executes the, H1 n- W/ x; Y, D1 C* j6 W; A7 s
// modelActions. modelActions is an ActionGroup, by itself it
- v" I( c* P0 g // has no notion of time. In order to have it executed in
! D S# _/ Q( D$ k+ ^6 I L // time, we create a Schedule that says to use the( n- g! A0 v0 i+ w1 l
// modelActions ActionGroup at particular times. This
" r/ [3 L0 x. o7 O' i, e% L5 I // schedule has a repeat interval of 1, it will loop every
' x3 {$ N, y9 d* D // time step. The action is executed at time 0 relative to
6 S+ `$ ]4 O/ r6 G // the beginning of the loop.
+ v4 u% T5 J/ X( x0 O& G
! O4 V7 W" [& D+ T // This is a simple schedule, with only one action that is7 x7 \1 s# X+ t
// just repeated every time. See jmousetrap for more0 F! [7 U+ E( }$ w+ ^; }8 v; b
// complicated schedules.+ J- N2 N5 Y! d9 C9 i% H( p6 D
4 O( r, S- `# h$ F modelSchedule = new ScheduleImpl (getZone (), 1);2 F% c/ R/ q9 W; \
modelSchedule.at$createAction (0, modelActions);2 ?. _7 q2 W# E* O6 C2 x0 c
& Z* Q; A: x3 ?2 w$ Z# j. w: X return this;, z' |, }7 L% J! ]6 c
} |