HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& |% \* `, Z: W/ T0 m& g
" {! A! L7 K& Q/ o- l0 |/ E' R. I( ~; i public Object buildActions () {) q+ T& l! ]' W4 Z; a! R1 C# F
super.buildActions();
3 P+ }% c" _8 y8 r t, M/ K3 Q# { : n" b: q$ {$ Q% s1 N
// Create the list of simulation actions. We put these in
& n* ]$ ?1 \/ m/ P2 w8 _- X // an action group, because we want these actions to be
/ B9 x& ^) l' r, p // executed in a specific order, but these steps should( U4 \3 y( E" z% o/ g& p
// take no (simulated) time. The M(foo) means "The message% e5 N' d* S+ S9 O* O
// called <foo>". You can send a message To a particular
( a; ?4 J( {; F6 J // object, or ForEach object in a collection.
- z: J9 x) \6 _" c; q" R
9 B4 {. w6 E- M2 N' u) e6 z7 h; b // Note we update the heatspace in two phases: first run
' J3 T$ Q3 \" H# n; s. c // diffusion, then run "updateWorld" to actually enact the5 B2 }3 W: I) b" e* G0 x& A
// changes the heatbugs have made. The ordering here is# @: B' x( s0 T a1 q+ `1 @0 \
// significant!
! S0 N ?1 Q, w) _; r
# j! L. o Y2 Z // Note also, that with the additional4 k( _% V" S) _, P V5 |
// `randomizeHeatbugUpdateOrder' Boolean flag we can! s. G: A# V& J8 s( f* B
// randomize the order in which the bugs actually run$ k, A+ U7 s9 y' d# q% Y; ?
// their step rule. This has the effect of removing any
" c# ^0 ~7 e( K* Y // systematic bias in the iteration throught the heatbug
( t" h' \/ J* H& u8 B5 g0 J8 L/ e5 s // list from timestep to timestep
( v# u1 j9 ]" H* V" U$ m3 `$ M : w2 M' F9 M" B( h' o1 D5 o
// By default, all `createActionForEach' modelActions have; J- |' G8 c9 K
// a default order of `Sequential', which means that the8 i" p3 U% b# `" w+ j6 d
// order of iteration through the `heatbugList' will be
. E" P2 x: I. q. `+ m* b! d" ?8 u, ] // identical (assuming the list order is not changed2 `9 f8 D2 Q5 b+ o+ f2 A
// indirectly by some other process)., m+ y0 H1 L' R0 y% U5 x
* H+ f3 a0 m$ f# k
modelActions = new ActionGroupImpl (getZone ());7 U. x, |+ M- x* q& U/ p* o' ^
: ?8 k6 x% Z. A, p9 A3 R try {
8 \. }7 b' V- `4 e) v modelActions.createActionTo$message
" Y; q) Q0 C; J& }0 i. z( m( J (heat, new Selector (heat.getClass (), "stepRule", false));+ p4 _/ T" ]; K9 U4 G2 F
} catch (Exception e) {
1 i4 F4 h: M E$ ` System.err.println ("Exception stepRule: " + e.getMessage ());3 i! C* U9 O2 M4 w E: X
}
, P1 k1 x. m7 ] H2 Q, l
! M& F( Z: ]1 @4 ^8 \6 M5 v$ V' z try {
% n5 `0 n& N: _9 H! ^% H: {- E Heatbug proto = (Heatbug) heatbugList.get (0);: v4 c2 e2 S0 }' y
Selector sel =
) E1 @$ q2 I _) p6 w, ?: G7 c new Selector (proto.getClass (), "heatbugStep", false);$ u$ [. j7 O: B7 h+ k! e
actionForEach =
2 W- P" B1 e) k7 B e modelActions.createFActionForEachHomogeneous$call; h2 V( `8 |2 ]; T, k. q
(heatbugList,3 v" U5 x) r, I- `% E
new FCallImpl (this, proto, sel,, c! _ Z# w1 t6 |' d
new FArgumentsImpl (this, sel)));( Y$ k# p) G" y% J
} catch (Exception e) {3 o; \6 b6 m ^& A% D$ V0 `: W$ C4 F
e.printStackTrace (System.err);2 N" X8 ~3 |" j8 g+ }
}
9 Y4 w% Q1 \0 |- H5 T8 P- \ , ^3 L# C* y# w8 W+ Q
syncUpdateOrder ();& B, P- c7 e! A2 ^$ k, X
% n. d. k Q9 i' b" p; e try {! S, J* P0 C! L, j. ^0 \
modelActions.createActionTo$message
+ {2 z, h8 J/ t, C$ @) Y (heat, new Selector (heat.getClass (), "updateLattice", false));& I/ Z. J4 P4 `! z2 W H9 {: B
} catch (Exception e) {
# a" ?( O, O0 U% b a System.err.println("Exception updateLattice: " + e.getMessage ());
, u& }! k! p5 v% U0 G9 | }, J4 i; ^- L/ h$ t n: F' p) m
' s9 P& _. ]& o# {/ [; h // Then we create a schedule that executes the
S7 j0 J) Y0 }; E- g& a( I, d // modelActions. modelActions is an ActionGroup, by itself it
3 K' ?; ~( m5 G/ c3 B) @' d1 Y$ H // has no notion of time. In order to have it executed in& L8 l: Z) |. h0 h. ]
// time, we create a Schedule that says to use the; j) q/ m# O) \/ d- G
// modelActions ActionGroup at particular times. This
2 w0 L; D$ z" c- D4 `- s // schedule has a repeat interval of 1, it will loop every
& l$ S7 ^1 G$ u$ r- y& a! I // time step. The action is executed at time 0 relative to/ s Y) T: m8 U) w0 i. Y8 m1 x
// the beginning of the loop.( h9 U% Y; |0 ]) j6 P% R- L$ ]
% O# D. i& k o5 |5 y x6 |
// This is a simple schedule, with only one action that is, V# z1 `. r( R3 e4 E
// just repeated every time. See jmousetrap for more9 O7 K/ s. H* {4 y$ F
// complicated schedules.
( R& c" ~8 j: |& S4 J / ~( _2 h% s8 S( o0 J
modelSchedule = new ScheduleImpl (getZone (), 1);# f# t# s5 e5 R
modelSchedule.at$createAction (0, modelActions);
+ }* S0 {" G2 ]+ M# h9 X
3 t: ^$ p6 h$ I" g T) g# r return this;
( l* K+ q+ ~, n } |