HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:: {3 y: Q0 t! a
8 w r3 [# q, o" p2 d4 T7 W. Z, r public Object buildActions () {
5 b+ r& q; Z+ G4 ~, P, I super.buildActions();( ?/ l. U& ?9 v7 f
1 M- u( J' f9 v2 X# W
// Create the list of simulation actions. We put these in
3 v, _4 P1 ~) d/ O5 B* o# e // an action group, because we want these actions to be
# C5 h, n K, T6 J8 d // executed in a specific order, but these steps should
: D; E: \% p- r0 N5 _ // take no (simulated) time. The M(foo) means "The message
" U+ P5 s+ G1 e! E' k" d! W // called <foo>". You can send a message To a particular" ?0 f% d" ?2 ^. j
// object, or ForEach object in a collection." b& I! T( k8 c$ o s! _
# z' {* C/ l/ R z! |, w* G+ C // Note we update the heatspace in two phases: first run
4 ] p2 e$ t& c* z! M- U- E* G // diffusion, then run "updateWorld" to actually enact the
0 |, s D t# F+ w( ~ // changes the heatbugs have made. The ordering here is
! _7 f9 Q& H( J+ M" v. [ // significant!
8 E5 v0 \8 \& {" }3 T8 y+ q ( E/ h8 e" {; L( ?: {
// Note also, that with the additional
. O& ?8 H0 b1 h // `randomizeHeatbugUpdateOrder' Boolean flag we can0 R4 O# b! A/ B/ L+ R( _4 ^- g# ^
// randomize the order in which the bugs actually run0 D b! C8 ]0 t: T' k z m/ Y
// their step rule. This has the effect of removing any; ~7 R( Y! s* Q" `/ i
// systematic bias in the iteration throught the heatbug0 w6 T& @' n) g& W) v; l/ \
// list from timestep to timestep
+ c) q" K+ J. }8 ^$ G! b 0 h" D9 c- @( V( r+ ]' n
// By default, all `createActionForEach' modelActions have
7 c0 m+ b O! }: d4 `3 B2 P( n2 i // a default order of `Sequential', which means that the4 o+ a. ` D9 c# s; Z4 ~0 M$ C
// order of iteration through the `heatbugList' will be) p' V+ k8 ]- c& H0 P
// identical (assuming the list order is not changed9 z( W: V$ b2 [" Q" S) ~& j+ z
// indirectly by some other process).% a& t% ?/ a9 M" f. H3 e: f' b5 N
4 |2 j9 ?3 b2 I; P7 W8 _
modelActions = new ActionGroupImpl (getZone ());" A2 d7 h1 W; T- U8 ~
e; I7 A# N& a+ P- ` try {' f+ l2 ?6 W+ p% Y( f! ~1 U$ L
modelActions.createActionTo$message/ Y' S* h, J; K
(heat, new Selector (heat.getClass (), "stepRule", false));0 L+ d% ] C+ Q4 _/ n, |
} catch (Exception e) {7 v& Q+ R7 ?' v2 Z) A/ k
System.err.println ("Exception stepRule: " + e.getMessage ());
, Y. T& S0 E/ B% G! a# F }) t* J( X7 w- q( ]0 T; b
- d9 v$ S( T* w$ k) a
try {
7 A* @ b7 {3 M4 ]8 O. s. u2 U8 t Heatbug proto = (Heatbug) heatbugList.get (0);
& g1 z: |( J, Y5 @ Selector sel = ( E1 B+ d, p- r( @/ p$ @, B- k7 ^) j
new Selector (proto.getClass (), "heatbugStep", false);
; ~2 d+ x8 N: t8 F0 G2 D; m2 v! D" q actionForEach =8 l. U9 G4 |9 M( W3 s3 K7 f" q, U
modelActions.createFActionForEachHomogeneous$call
1 K) s( g" f( m/ e2 Y% ~! ^* } (heatbugList,5 _* e" U c, T* q4 t. p. n
new FCallImpl (this, proto, sel,. Q" T X D9 }- |8 k, Q
new FArgumentsImpl (this, sel)));1 r6 |& h; q9 M9 |% ~
} catch (Exception e) {
! e8 |3 C }5 n* a9 y, s: r e.printStackTrace (System.err);/ k. q+ ~6 v8 j4 g' A& X
}* f* Q7 r1 B: h& ?' b" w* [
5 l( O4 p" J1 F5 W6 e0 L3 [ syncUpdateOrder ();
8 u# o0 J0 n: `
: o6 n- `2 c. }5 } m try {% G9 m0 B" A1 j) \( B2 b5 x
modelActions.createActionTo$message ; F7 o& R- M/ T9 b' \# e6 e0 C
(heat, new Selector (heat.getClass (), "updateLattice", false));
& ~/ G$ s- z; X0 _ } catch (Exception e) {! E( `/ S! G, {. q" `6 ?5 L
System.err.println("Exception updateLattice: " + e.getMessage ());' r8 t8 h" B% S, M( u: u8 m& k4 Q1 g
}
0 j1 | d1 J# @. R% }
+ Z! y& Q% Z! W v, D7 v // Then we create a schedule that executes the' Y; D( m9 g' k% c) U! u
// modelActions. modelActions is an ActionGroup, by itself it
9 T/ _6 v/ [# \" d // has no notion of time. In order to have it executed in* |4 {/ I. J4 X+ S* n
// time, we create a Schedule that says to use the- U+ I; u) R' ]% \
// modelActions ActionGroup at particular times. This3 ?+ O3 }+ W1 v. |) d: X
// schedule has a repeat interval of 1, it will loop every* @' [& t: b: Q0 B0 w) _
// time step. The action is executed at time 0 relative to
1 u$ @( M2 a% V2 F // the beginning of the loop.! i' h8 ~; j$ K) X8 C, K, B
# z* Q# {" Z, e // This is a simple schedule, with only one action that is
0 U2 V" L3 P F0 ~$ O# `/ y // just repeated every time. See jmousetrap for more3 m5 u: j, s& W' ?
// complicated schedules.
2 M* `1 @9 f7 p7 Y& G3 e* ]
! y1 \' s# a1 d( b# q1 P0 N: E. B modelSchedule = new ScheduleImpl (getZone (), 1);
! e; y5 h( C$ Z( w5 O9 C% D% B+ y, V modelSchedule.at$createAction (0, modelActions);3 w$ `8 u' L. {8 ~$ j
3 [9 e3 {; I: q0 B7 ~ return this;7 I! ^: m$ p& e3 z0 Z }
} |