HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
; j5 v) ]+ j* b7 g' I8 h$ N! n4 ~2 U$ i
public Object buildActions () {( C" ?- u% \& z2 S& v0 P/ w8 I1 t
super.buildActions();
" d8 F3 `. y6 @9 R
; P% ~- ?% V" g7 M // Create the list of simulation actions. We put these in! x9 _" ^( ]% e. v6 H# V
// an action group, because we want these actions to be
3 p, g- Z; j y( o" U n9 } // executed in a specific order, but these steps should
7 A& |2 r( F% V1 B3 P& Q // take no (simulated) time. The M(foo) means "The message4 O* S: L# t& w
// called <foo>". You can send a message To a particular6 ]1 n$ D1 L. ?& d9 a
// object, or ForEach object in a collection.
4 C! t( b/ Q9 Q1 J- Q
5 Q2 k5 V" [( P* f! w // Note we update the heatspace in two phases: first run
& G/ }) ~% j" m$ d // diffusion, then run "updateWorld" to actually enact the! g4 v7 w2 H9 l
// changes the heatbugs have made. The ordering here is
* U1 Q# F( ~" Q W- S& t // significant!. |4 D2 s) ]" U1 Y' }& D" B& q
; s6 k# ` u6 R$ P6 d8 z) d // Note also, that with the additional
+ c; v+ d5 q1 e# g, [% O8 d // `randomizeHeatbugUpdateOrder' Boolean flag we can2 i9 I+ u) ~; W/ ]6 l. z
// randomize the order in which the bugs actually run; `4 S% ^0 h6 T6 Z3 B, Z/ R
// their step rule. This has the effect of removing any, S: H! Y& a# w3 ~$ G5 _
// systematic bias in the iteration throught the heatbug8 C! {# H) N: @1 S' U7 c
// list from timestep to timestep( n/ D- y F) S+ h
; x6 ^* Q" Y! @7 L$ M
// By default, all `createActionForEach' modelActions have7 Q2 d% f& F( X$ z& |
// a default order of `Sequential', which means that the# i* w$ v1 N& V1 S: M8 g4 o& \
// order of iteration through the `heatbugList' will be* H) v' o: _7 S% @
// identical (assuming the list order is not changed# m/ L7 P1 ~: x# s1 H/ O3 }+ R
// indirectly by some other process).
8 E z1 d+ v2 @' X/ {' g: m : }* A! l* Y4 f! J* W- y
modelActions = new ActionGroupImpl (getZone ());3 @: N8 L" u$ P8 J" {
# v& P* F: `, D2 _0 J% b1 t: x- l
try {" d0 }0 ?. W) {" B# W' U4 O
modelActions.createActionTo$message
3 s3 D+ U/ @% d1 y (heat, new Selector (heat.getClass (), "stepRule", false));3 T5 p! n. G3 G$ E/ p5 k* m" s. z
} catch (Exception e) {6 m6 g0 @* W8 S* N! D# V
System.err.println ("Exception stepRule: " + e.getMessage ());- u. g: m3 m/ [) h% ?
}
1 e6 ^! p0 ~; z/ {4 z% b( j" K
/ b7 C8 G2 U$ t( V4 m& C/ Z3 \ try {
$ ~! y8 w* o! G3 |- w, l Heatbug proto = (Heatbug) heatbugList.get (0);
4 m q6 H( B Q: W Selector sel = 5 x7 x( f: J& |8 b U! \6 `
new Selector (proto.getClass (), "heatbugStep", false);; K& [# j+ o) e. S9 {$ O
actionForEach =
. {0 m2 v- P2 d# l. H3 e modelActions.createFActionForEachHomogeneous$call; j/ }# e) T, M
(heatbugList,& S3 E4 B+ p/ Q+ f7 e* R
new FCallImpl (this, proto, sel,
7 t! c y+ s* d/ O K5 \, S3 R new FArgumentsImpl (this, sel)));
+ K, |1 W' ], h0 g( p } catch (Exception e) {# s6 @: Z, Y) e
e.printStackTrace (System.err);
* u5 L7 g- I; v. X4 T- Q }
( o; `" x, S4 l! C: s: f
8 x" f# `: X5 a# \1 n8 G9 \ syncUpdateOrder ();
A/ a9 h( K$ h* V
) j0 O" ~* E& p' P try {
4 x4 o5 t4 B2 o: F modelActions.createActionTo$message
6 {$ ^& ]" |$ d2 O$ j (heat, new Selector (heat.getClass (), "updateLattice", false));2 W, I9 N1 P2 z5 j2 N
} catch (Exception e) {/ c& K) U3 z' Q2 R
System.err.println("Exception updateLattice: " + e.getMessage ());/ @( k7 z- q0 n- Z+ n
}8 \5 ^4 t6 ^" @: l( M2 [
4 A8 {# L/ B" h/ G4 r4 G
// Then we create a schedule that executes the
$ m; ]4 ?3 P0 A1 L9 ] // modelActions. modelActions is an ActionGroup, by itself it0 i6 E( E; `, }4 ]4 t2 o
// has no notion of time. In order to have it executed in
/ e9 h3 P* a. A$ ] // time, we create a Schedule that says to use the$ X7 Y6 ~" A& i: C! U; j; U. |# J
// modelActions ActionGroup at particular times. This
* ~/ U3 |% D A4 i, U+ j* V // schedule has a repeat interval of 1, it will loop every
) [" ?/ I2 ~$ W1 Y // time step. The action is executed at time 0 relative to0 k2 z# _' B, U4 W e. n& [ z1 S
// the beginning of the loop.
+ ]1 X+ b& l# ~9 w9 O2 O: {( O! Q$ E4 S; l e% ~
// This is a simple schedule, with only one action that is
! w( e& U: y, a# ?+ }/ ` // just repeated every time. See jmousetrap for more$ Y+ @/ g2 y {# y) ~+ Y2 E
// complicated schedules.
) ]5 w; G* r. M- a: H . E# g0 C# Q* ?$ J
modelSchedule = new ScheduleImpl (getZone (), 1);3 ?( Q! ]0 P7 U, ^5 K8 U$ r
modelSchedule.at$createAction (0, modelActions);
& A7 N+ i0 X9 G4 N4 a ( K0 e) o6 t, e. B
return this;
1 m- B3 u8 E" K8 S8 v+ C# f6 B* u } |