HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! \0 _( t' K7 Y! C; a+ q5 F- T5 ~
3 D0 F3 j& Z D6 X2 x' ~ public Object buildActions () {
8 d( s' V4 T0 d# ^% W& R2 {7 c8 G super.buildActions();! o3 N1 f9 _+ k; L
5 d B/ _1 P' N' K4 F3 ~
// Create the list of simulation actions. We put these in
( S3 [3 r& ]% j0 ?- M // an action group, because we want these actions to be; U) m& n [: j( |! |# A1 C7 q9 }4 z% b
// executed in a specific order, but these steps should# B+ j" v0 f, f B9 E) e
// take no (simulated) time. The M(foo) means "The message
! T. n- S4 `' I m- R; Q // called <foo>". You can send a message To a particular
5 m" @% C( C1 I4 y8 U" u& ?% [0 Q // object, or ForEach object in a collection." M; Y' z# l3 `. |& J1 z
' {) @2 a3 f" V c/ c // Note we update the heatspace in two phases: first run& G g3 r0 T, q a! g6 F
// diffusion, then run "updateWorld" to actually enact the7 u7 n8 S2 U& b
// changes the heatbugs have made. The ordering here is& d- j5 N* ~7 v$ ~* @0 Q
// significant!( N/ ]9 a7 u: }7 F5 d$ z6 z3 A
3 a+ V, X& W: Z9 d // Note also, that with the additional
) y, P' N9 v3 x/ g9 B // `randomizeHeatbugUpdateOrder' Boolean flag we can
; D! w6 Y0 @$ j! @9 C _ // randomize the order in which the bugs actually run3 ~8 T" A) r; u$ S
// their step rule. This has the effect of removing any9 V" D5 ~ _$ ^+ v# S* s) o
// systematic bias in the iteration throught the heatbug" J, s7 R& r2 |9 p9 A2 t8 {2 @. D
// list from timestep to timestep. v6 \% m* `. ]/ ^) \0 \3 s
4 @5 ^/ c0 ?( [ // By default, all `createActionForEach' modelActions have
6 x i' N" S: n$ i+ V: Q // a default order of `Sequential', which means that the2 K& |+ t+ E6 y8 J
// order of iteration through the `heatbugList' will be$ ^% x4 `1 {/ Z8 r+ z
// identical (assuming the list order is not changed
" x6 T/ i8 J* }$ w/ _ // indirectly by some other process).
/ B+ n) s- B, ?: B3 z 9 \6 L0 {( [* V* Z0 ?: ~6 I8 [
modelActions = new ActionGroupImpl (getZone ());- N; M! w4 [/ u; s8 t
& g+ P2 J7 Y1 Z( A try {$ o" s# I( u; O; h
modelActions.createActionTo$message7 ]* t$ h/ x+ s
(heat, new Selector (heat.getClass (), "stepRule", false));
' ]" T, h8 @" c } catch (Exception e) {
: C% t0 C# n; M7 l, X. l% T; T System.err.println ("Exception stepRule: " + e.getMessage ());
# h, `; a/ T. Y) F7 K p6 A+ I }0 s9 s9 r8 @. [7 N
. q4 e: }1 a: d1 H
try {
0 j' i6 |, V; D" O Heatbug proto = (Heatbug) heatbugList.get (0);. b' s5 }) H0 q
Selector sel = / B* f: t) s2 ^# K! h
new Selector (proto.getClass (), "heatbugStep", false);
! y/ J4 }" H' I8 x0 I R# _6 ` actionForEach =
% w+ t6 `8 t, a' v4 D: F' |, u! l modelActions.createFActionForEachHomogeneous$call
, J$ C! }- J8 i+ V) ~' _' Y (heatbugList,
! A9 T, b9 Y' v& E new FCallImpl (this, proto, sel,
) b- D" n$ d7 u* u new FArgumentsImpl (this, sel)));% Q2 y. \% C3 n
} catch (Exception e) {9 z0 V7 Z: E# D* r5 m3 y
e.printStackTrace (System.err);
9 `/ o& j* @5 v9 {" R* L" m }, y! n j1 J4 s1 }; }! s8 c
( Y/ _; Z P! F+ R. a& |3 H
syncUpdateOrder ();
' Q$ M8 h8 I$ X, R: O6 l6 ~! W% x( b+ G) |9 U6 }; m# W% M5 `
try {" E# c. K; z1 `
modelActions.createActionTo$message
6 S0 a& T; `8 _$ Y2 I0 d (heat, new Selector (heat.getClass (), "updateLattice", false));
8 ^/ q2 g+ X0 K3 y } catch (Exception e) {3 u* E2 O. n% ^* ]
System.err.println("Exception updateLattice: " + e.getMessage ());/ p1 b6 e4 y- v: m
}7 G+ h: d/ ~/ d+ m: T
: \6 O+ L( ?- c: S/ \1 G4 E3 J // Then we create a schedule that executes the* u$ K% c/ L/ i4 l
// modelActions. modelActions is an ActionGroup, by itself it
/ |+ W* e8 {7 h3 w // has no notion of time. In order to have it executed in1 l$ `" N( |, `, {
// time, we create a Schedule that says to use the/ ~1 `3 B$ A9 S* n
// modelActions ActionGroup at particular times. This
5 U1 P9 v9 P" B8 p" k // schedule has a repeat interval of 1, it will loop every& w7 R2 G3 M# N' o! K8 N: K
// time step. The action is executed at time 0 relative to9 p6 |! x: y7 N' p1 @' G2 `
// the beginning of the loop.
* Q; g# E- Y1 L" j u/ G/ Q b& j. N! P+ y8 d7 b( Z4 h% z! r
// This is a simple schedule, with only one action that is
7 `" I5 N# j- Y // just repeated every time. See jmousetrap for more" g3 `7 t% {% Y: I. e; h/ _
// complicated schedules.
$ D& K5 S' |6 }: [# V 9 m2 y1 D! r$ B# T1 q: I. Z7 W( K
modelSchedule = new ScheduleImpl (getZone (), 1);5 q( p" v5 [. |
modelSchedule.at$createAction (0, modelActions);$ p; C' r; d! N0 Y
0 e: ]+ H# X0 _$ M% f7 r
return this;. {8 M- x9 b2 ^+ u" ^& e. o
} |