HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 |6 p. _ Q9 k: J! ^) H
; A* L) ~" w4 C9 w- B
public Object buildActions () {
, f5 j5 L9 b4 p' R super.buildActions();
a2 z! I1 }2 E- r
6 F6 X! t( g1 _: K- ]* @ // Create the list of simulation actions. We put these in
7 K. |. w3 d' x' d& s) Q2 ~- T // an action group, because we want these actions to be% P, u+ G" N8 f; `
// executed in a specific order, but these steps should
$ a9 W' a- S1 u; K // take no (simulated) time. The M(foo) means "The message
+ h, B# A. y4 ?+ F) d0 ] // called <foo>". You can send a message To a particular& a2 U. @( E! b, j; X
// object, or ForEach object in a collection.$ |4 T) _" m6 e9 Z2 m8 w
5 I/ p4 L. n: y: ^" @ // Note we update the heatspace in two phases: first run" Y, [# u' y2 J# J& ?
// diffusion, then run "updateWorld" to actually enact the
. g' Z! q7 e* x" {. {' Q/ m6 c // changes the heatbugs have made. The ordering here is
+ ~: s) o6 J/ Y- O3 d! K; Y/ U7 f // significant!5 e+ c U5 f. G/ K
t* S6 s: X K // Note also, that with the additional
. W& r9 I( B, } // `randomizeHeatbugUpdateOrder' Boolean flag we can
1 y- i9 o& |" Z: K) ?( j& a // randomize the order in which the bugs actually run
7 U* T* X) Y: p3 _2 ?; o$ ` // their step rule. This has the effect of removing any: p+ a1 O/ m# x( Q$ E! N
// systematic bias in the iteration throught the heatbug
0 J; O2 K, ]$ K; N // list from timestep to timestep
0 } I- F0 i. y4 t; E0 { a( q ( a$ K" P2 E' Q* w- j
// By default, all `createActionForEach' modelActions have
, A$ s3 T0 c& n/ A5 G$ a // a default order of `Sequential', which means that the0 G. u0 \! S: {! m
// order of iteration through the `heatbugList' will be
5 S$ k$ y# V1 R# f3 Q' [1 g // identical (assuming the list order is not changed5 h) B0 p5 \% K' j! w' B6 ]6 Q
// indirectly by some other process).
& d4 u4 Q3 H0 ]( [/ L! o1 Z( r
; S# s5 P" B5 ? modelActions = new ActionGroupImpl (getZone ());
6 |! W% V2 B# |1 X& s
& H. n/ E( Y) R9 W try {# v. X6 Z- ~0 J# j2 b+ _6 G
modelActions.createActionTo$message `/ p+ Q3 ^; k% S0 y5 q( E
(heat, new Selector (heat.getClass (), "stepRule", false));
$ H' R/ s! G$ {. l% K) i } catch (Exception e) {4 e; E7 D$ n3 D/ j; n
System.err.println ("Exception stepRule: " + e.getMessage ());. K# H) Q1 C8 n: n# U
}! }& |- Z& s7 b
5 P% b5 ], }8 ? \: U try {6 N8 n U$ y. h3 |5 C; `
Heatbug proto = (Heatbug) heatbugList.get (0);
8 b/ Q7 l5 I8 H) g Selector sel = 7 `+ f2 B! O: K& D. ^4 ~, _
new Selector (proto.getClass (), "heatbugStep", false); \. N2 z1 a9 k* V- y' N
actionForEach =6 u) }1 ^ m! o" R
modelActions.createFActionForEachHomogeneous$call3 @' a/ C0 q$ @% a1 K
(heatbugList,
& z& _3 b b2 S- j new FCallImpl (this, proto, sel, g1 X& D! _) D7 R; R' ^& s
new FArgumentsImpl (this, sel)));
/ O# O7 D" b7 N; v' ` } catch (Exception e) {( x8 @( [4 N* B1 c7 g# k* v0 P
e.printStackTrace (System.err);
8 j6 V/ `; T7 _$ B3 z1 J) V- k }, p% S$ \2 j) F& S" ?. Z; a) Y( _
0 |+ X4 o% A6 f2 \ l2 A8 w
syncUpdateOrder ();$ Z0 z" q; M, @9 h; q6 o/ h' H% f0 s9 I
2 j0 `, j( U& H6 T" o; ^
try {
. Z' b3 H# D( Y( I1 i' W modelActions.createActionTo$message
& F# _) Y0 {3 E4 ?( n& W# j. t (heat, new Selector (heat.getClass (), "updateLattice", false));
0 K. O- W& x, Q } catch (Exception e) {, e( B T p/ i2 Y2 M7 H
System.err.println("Exception updateLattice: " + e.getMessage ());0 V' M3 I9 v* y: k' D1 ?
}
6 o$ D& Y# f- u& \
0 m. T, Q; o. o+ c g; f // Then we create a schedule that executes the; F S4 U8 y9 G
// modelActions. modelActions is an ActionGroup, by itself it
) x7 ]0 t+ f% X! c) O // has no notion of time. In order to have it executed in7 m9 ~1 q8 N- n7 j* e4 P
// time, we create a Schedule that says to use the
% @, T. z% U" @5 e1 c4 C/ p2 Z7 J- K // modelActions ActionGroup at particular times. This
" {8 v; m8 o Z+ R$ N1 r // schedule has a repeat interval of 1, it will loop every
' i1 \0 m+ j2 ~: L' z7 P // time step. The action is executed at time 0 relative to
: R" N7 d6 R0 o: p* @ // the beginning of the loop.
$ c5 g7 _1 J: @9 A' \& [2 T! I3 G5 z) b9 o7 t
// This is a simple schedule, with only one action that is) |3 u: L! v4 r8 `$ I( @/ B2 v
// just repeated every time. See jmousetrap for more
% n& z; \7 [1 J# t I& y8 S // complicated schedules.
& j/ w+ r' B9 k7 r% N8 y! e" { ( M8 h8 {, s- B) G2 g* m6 @5 ~
modelSchedule = new ScheduleImpl (getZone (), 1);
1 z3 f: y9 \& c1 d9 p' ] modelSchedule.at$createAction (0, modelActions);
9 c* X* X9 F& t$ E0 V0 \" [ v4 z& R9 x; \
return this;
3 J1 Y. r% ?. v# v } |