HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! a4 s& T- E5 t
! P) m* t" ?+ I3 H/ y+ Z0 r public Object buildActions () {
7 R7 A; [. W% Z4 f0 z! Q6 G super.buildActions();# F2 `4 |5 t( R
$ L6 f7 v% x/ C& U2 w- B // Create the list of simulation actions. We put these in
' d) c! `5 D; p3 u+ Q7 ^; u$ C0 h- H // an action group, because we want these actions to be" j7 v, q- D h- [
// executed in a specific order, but these steps should
1 j! M& {* a ?+ f' T // take no (simulated) time. The M(foo) means "The message
1 B( c @1 T2 g$ S8 B6 @8 A' D( { // called <foo>". You can send a message To a particular
M5 K" o y! Q% m // object, or ForEach object in a collection.
8 H, f8 h5 y# w2 Z0 Y; `9 Q1 k ! |* y% S1 i/ K8 |+ Y; h4 E
// Note we update the heatspace in two phases: first run/ q" G4 {1 n: ^$ _5 c; M) d, d
// diffusion, then run "updateWorld" to actually enact the- \) |0 G0 z* V$ j$ }$ @! b
// changes the heatbugs have made. The ordering here is' h/ @* U" l! g$ L( Q+ {
// significant!
0 P- a6 Z4 r! _$ g ) P' z% m" M; n" w
// Note also, that with the additional
( g0 Q# K1 p7 z# I // `randomizeHeatbugUpdateOrder' Boolean flag we can. @+ f( I2 g. n: G+ f* o: o Z$ M
// randomize the order in which the bugs actually run/ H9 w3 [3 B# x2 E) @7 Y6 t
// their step rule. This has the effect of removing any" r; L( g p% @% B: c6 K6 j
// systematic bias in the iteration throught the heatbug* W* X/ q8 @* ^" J( n, T4 S
// list from timestep to timestep, V1 G1 w B7 I
, a4 |- T2 E5 p9 Q( |
// By default, all `createActionForEach' modelActions have* K" A* K6 d' q2 n6 |; p
// a default order of `Sequential', which means that the+ D9 C6 F# W. P
// order of iteration through the `heatbugList' will be4 r5 f. r; ]7 K" _' |3 z. N9 l# K
// identical (assuming the list order is not changed: e6 O& a) {3 Q- @
// indirectly by some other process).
9 r3 D, F- h/ {$ C% r8 z5 m
/ D- C5 U. H5 _+ C. ^; o$ {% p modelActions = new ActionGroupImpl (getZone ());6 x* o% z w! E1 h+ `% i4 j* ^
# f$ p5 f# m( n0 ]0 F3 G7 Q
try {8 f2 B$ `& i6 x, K( [$ ^0 B
modelActions.createActionTo$message
6 s8 c+ S- T9 G( T; ~+ ]! M7 Z x (heat, new Selector (heat.getClass (), "stepRule", false));* G: N. K" M" {( F; ?/ C
} catch (Exception e) {8 p9 x. r5 a5 n8 C, ], G+ t4 ~0 @$ {
System.err.println ("Exception stepRule: " + e.getMessage ());5 s# a, ?5 |( |' `& s9 T" Z! ~
}+ f% F5 `9 ^% M% W9 `2 _# c" g) }1 X
1 ~( W N2 n' |# Y+ K. Y
try { Y! D# ~& c% ?4 U p
Heatbug proto = (Heatbug) heatbugList.get (0);, y3 |) _/ J. |" l
Selector sel = , }7 b, e& R9 ~& s
new Selector (proto.getClass (), "heatbugStep", false);7 B: [6 p! U; | w) i; q9 W
actionForEach =
! C9 d5 [* \9 \ modelActions.createFActionForEachHomogeneous$call
( o" u- }- J, x3 H! w2 p (heatbugList,
/ u% n: ]! t' @* }2 R- {# t& q6 b new FCallImpl (this, proto, sel,
) M/ p* Z7 N$ X. |+ w1 y6 w/ n& T9 e new FArgumentsImpl (this, sel)));
s4 k! r) k6 a4 m9 ^ } catch (Exception e) {
- g; ~6 u$ d! ]' l4 n% w( j( s e.printStackTrace (System.err); S s$ R" \# E
}
) [( O1 p1 e% k+ M. @" ] # ~ z- j- }- L; N) l; m! s; @
syncUpdateOrder ();$ y4 k$ {/ S/ W8 ^- l
/ ~ t: g/ H8 K& y% T" J# v
try {. U5 e$ ^8 j, y! v
modelActions.createActionTo$message 7 i/ n1 v0 n1 M: j
(heat, new Selector (heat.getClass (), "updateLattice", false));' k- c# J6 ^- n3 X' s
} catch (Exception e) {
- ~7 V3 v3 Z' d% a9 o System.err.println("Exception updateLattice: " + e.getMessage ());( } M3 d6 A$ p/ ~' F) P5 ?
}2 c6 x5 e( o* ~
5 Q; c ]9 n1 j2 M) r, C. p# z
// Then we create a schedule that executes the$ P1 T$ P0 W) _
// modelActions. modelActions is an ActionGroup, by itself it% q9 d9 k$ T+ L' h( ^3 {% j
// has no notion of time. In order to have it executed in6 Y! P( C8 m. s5 a5 k# `2 x
// time, we create a Schedule that says to use the
. R2 h- {3 N; E // modelActions ActionGroup at particular times. This- ~9 N8 W2 p: e$ b. q# _( F
// schedule has a repeat interval of 1, it will loop every: k" W6 `/ w8 O: ^1 F( R2 C
// time step. The action is executed at time 0 relative to6 g4 r. q, H5 A, H* t8 g( |
// the beginning of the loop.
0 Q K# L( A2 N. q! [$ T3 @5 S- ^0 X g4 v( t, x' V( e2 ~
// This is a simple schedule, with only one action that is! c/ E5 n* o0 @5 I. T
// just repeated every time. See jmousetrap for more+ e7 i# \8 H4 G1 y, n8 `: x2 Y" T
// complicated schedules.
( d2 l- z9 a6 U . l! o( k5 \7 p8 b3 l1 n
modelSchedule = new ScheduleImpl (getZone (), 1);
. @, a% H+ q" b modelSchedule.at$createAction (0, modelActions);: a5 f* E: P4 K5 _. c9 _
# U6 a. E# i1 L) N& d0 {- W1 i) W4 X return this;
8 x7 g/ |" M& Y8 q |+ S5 ~8 F } |