HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
) \2 K0 M7 Q) w! b" R6 G( W9 e6 `( i9 w2 h
public Object buildActions () {5 J3 K8 x5 f+ C4 S% ~
super.buildActions();
3 ?/ f: I i6 M. w$ R & X3 Q$ F* R+ q
// Create the list of simulation actions. We put these in5 D- ?" W9 N0 l* S: H! B* e- \
// an action group, because we want these actions to be
+ ]+ j9 Z9 X7 y6 g- u6 A0 Y# e$ ^ // executed in a specific order, but these steps should
' L! }, q- F( {- F8 r% A // take no (simulated) time. The M(foo) means "The message% m' S2 i2 V: W5 m9 S- T+ p8 v
// called <foo>". You can send a message To a particular# N2 b( w% e& r( V6 i' i
// object, or ForEach object in a collection.( L& v* Y' \( a/ D
+ y7 c- U! H6 ]2 G/ Z // Note we update the heatspace in two phases: first run
" v; X: F8 h% [ // diffusion, then run "updateWorld" to actually enact the c5 p! c7 e) J5 O6 o
// changes the heatbugs have made. The ordering here is
2 k# c' [& j2 ^, I3 W8 V# ? // significant!
& q% }6 t7 H* i3 e# F7 B
' w) _7 @* z# b1 r q // Note also, that with the additional9 N6 B% r$ u7 y& B! i j2 t; w
// `randomizeHeatbugUpdateOrder' Boolean flag we can% Q* V" H- r# T$ k5 {
// randomize the order in which the bugs actually run
7 K6 u" ?* r4 c% Q // their step rule. This has the effect of removing any' c9 @$ v* J* l* A4 e
// systematic bias in the iteration throught the heatbug% U! ?( S; i) g$ j9 p- f
// list from timestep to timestep$ m$ C, C7 O- ^) F
$ z3 W1 n* S* C4 h, T; O
// By default, all `createActionForEach' modelActions have8 F5 {# F) z% y) Q4 c* m2 P9 [& L& s
// a default order of `Sequential', which means that the' ?; r/ `7 p7 _
// order of iteration through the `heatbugList' will be" q" Z' ?# D+ a. Y5 x, @
// identical (assuming the list order is not changed
# e, r2 O- w5 J' ^ // indirectly by some other process).
5 x4 a6 m: a! g! m6 j4 |8 f& ?
1 U, S: V/ i }; r/ d% b! k: P modelActions = new ActionGroupImpl (getZone ());3 ~. z& F p- m& f
! G6 L. H* i9 C3 c try {
% C! y0 o) a+ _% u modelActions.createActionTo$message! Q$ ~ ~- K: |( f7 T/ E6 T
(heat, new Selector (heat.getClass (), "stepRule", false));
" ?: P/ I1 ]- m$ ]5 ]$ } } catch (Exception e) {
) ]+ G+ ~! V3 p1 [3 G- L System.err.println ("Exception stepRule: " + e.getMessage ());
- m* G3 o0 A# a- m }( q2 ] A3 l; V; U4 ?
+ P1 r& }% [$ I6 B$ a: V2 `2 J7 x try {0 z6 u/ ?8 p# y
Heatbug proto = (Heatbug) heatbugList.get (0); z7 Q3 N" r5 R7 o7 j
Selector sel =
. U: ~8 b9 w8 X* N2 X( l new Selector (proto.getClass (), "heatbugStep", false);
0 s$ N: g$ M% I+ l, F8 p actionForEach =
! |$ S+ ^6 r- V H H+ h modelActions.createFActionForEachHomogeneous$call" _' c% b/ y6 \8 p, E4 o+ E2 @
(heatbugList,: ?6 i4 l& R& S
new FCallImpl (this, proto, sel," X/ }+ z" J- b5 `! v6 U
new FArgumentsImpl (this, sel)));
6 H( h. D+ f7 D4 X8 k/ u L } catch (Exception e) {
0 ^* M/ q6 y7 U' z e.printStackTrace (System.err);) N+ i5 w! @! T/ h. \
}
1 x4 @- f7 ~& ~4 ~+ K0 y
% g9 q5 f) b2 S. n0 y! `9 Q2 V syncUpdateOrder ();: ]+ z* e7 x3 z
; ^% [1 R! t) d0 V1 K. d, O0 A D
try {
( y: n Y6 ?; P( u. ?* J modelActions.createActionTo$message M6 V( F$ }$ f' s, K
(heat, new Selector (heat.getClass (), "updateLattice", false));0 P8 T, v7 @2 H+ A
} catch (Exception e) {( w6 @8 N' l6 \8 j0 X# A6 H: R- ~
System.err.println("Exception updateLattice: " + e.getMessage ());; ~! R1 `. _9 Z& N8 H$ J7 n0 A8 B
}0 W# a# U5 b. W7 y9 j
, ?* h; K* Z) i* d // Then we create a schedule that executes the
1 U- R* J, R7 @0 u, l: T! }# I // modelActions. modelActions is an ActionGroup, by itself it
. t. P a% J+ D0 Q! V/ M& v$ M) } // has no notion of time. In order to have it executed in
3 O! K. l7 S0 B m! \0 h/ A // time, we create a Schedule that says to use the& J) w2 |0 p. U
// modelActions ActionGroup at particular times. This
4 u4 }: U9 t; f6 P+ \) o // schedule has a repeat interval of 1, it will loop every
* o1 i6 e. U- c. j# q8 b' T // time step. The action is executed at time 0 relative to
% j0 k& q; @/ W! Y; a // the beginning of the loop. x& V- e, {1 o1 o5 Y" Q
2 n% l! Q4 b2 P) g // This is a simple schedule, with only one action that is9 V. v% [' q. R8 v
// just repeated every time. See jmousetrap for more
2 E8 F( l$ t0 H/ M8 y9 e6 ]% u7 _ // complicated schedules.
+ Y5 M" R7 B' D0 n1 ^1 a; |7 G4 | + S( J4 i& {$ h9 `( }
modelSchedule = new ScheduleImpl (getZone (), 1);
]: a4 t' D m! u& @5 u4 Z modelSchedule.at$createAction (0, modelActions);5 B1 G6 b+ o$ i
% K* L5 l2 F+ | S9 [- \* u return this;
6 r8 W7 E' T" B) s; ^% R' _+ ?) o } |