HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
' Z, e- U- q) G* ?1 O4 M
+ b$ F, N* w( o& u8 S6 \" E+ M public Object buildActions () {! J9 X) o9 M* M* Y7 T
super.buildActions();8 q9 K( \% k" Y. `$ q
& |: S1 p* N5 i' g2 ? // Create the list of simulation actions. We put these in
2 ^9 s) W6 ?7 m7 L5 D9 U // an action group, because we want these actions to be. x) c# v8 n2 g2 K' S
// executed in a specific order, but these steps should
$ n d6 U# g) L5 I // take no (simulated) time. The M(foo) means "The message
& ^& _: e4 E; b4 b // called <foo>". You can send a message To a particular
% l( t @/ t, t4 y% M // object, or ForEach object in a collection.) N4 @9 `( m; H/ t
! Y5 W4 X* m3 B( V1 b' n" b
// Note we update the heatspace in two phases: first run
8 a" z8 u' M/ U: Q3 @# S* Y // diffusion, then run "updateWorld" to actually enact the' ^" ~+ [( t' }, k) B
// changes the heatbugs have made. The ordering here is" ^/ }" t1 ?" K; i% x- b* ?# t
// significant!
3 H+ u' D, W0 i; U+ h ( [0 Y! Z* D" A% p
// Note also, that with the additional5 S2 D9 [8 j2 A% P7 q
// `randomizeHeatbugUpdateOrder' Boolean flag we can) D) P: Q- e, {, R1 F
// randomize the order in which the bugs actually run
$ v+ A; _0 B2 o% z6 ? j+ E0 k // their step rule. This has the effect of removing any8 l3 F$ w% ], I* X2 ~" C% H
// systematic bias in the iteration throught the heatbug
T$ {2 Z% G5 W3 g // list from timestep to timestep
6 X, M/ u$ c# x4 o1 Y , T% h. c; ^) k9 \& F6 p% k Q
// By default, all `createActionForEach' modelActions have
0 o/ Z; {$ A G& R- Q2 ?9 M // a default order of `Sequential', which means that the
! H% I$ `8 _, D8 B% i, g/ n // order of iteration through the `heatbugList' will be) n' W5 t: O& p- G" [6 o
// identical (assuming the list order is not changed
" d% S% r5 I8 T: f // indirectly by some other process).0 W% X1 Q$ a, K
* m1 N+ d( y6 O9 X. ^! _* `
modelActions = new ActionGroupImpl (getZone ());
! M6 U& O/ z3 v0 W, R- A$ t; c5 c4 E. K. C' ]
try {
. _; ~, M/ I! }' J' X modelActions.createActionTo$message
( ~' K2 Q" w* C' N( j, c9 L' u (heat, new Selector (heat.getClass (), "stepRule", false));) n3 |- C; Q; B: n4 a, N
} catch (Exception e) {
, _2 A1 J: t( m" F System.err.println ("Exception stepRule: " + e.getMessage ());
, F" }/ r" K1 e7 p& u }/ ^: O W3 V7 D) |. I, q
( B3 R' e0 ]) z. _4 D' L
try {8 N- y( M9 `# G$ j' o% y8 s6 y
Heatbug proto = (Heatbug) heatbugList.get (0);8 M/ b3 h# e% V$ l7 m, D* h
Selector sel =
& _, ~ c* Y0 o) c+ E; E new Selector (proto.getClass (), "heatbugStep", false);: C3 v2 I; z/ l7 J
actionForEach =
2 w" W1 _" J4 \% E" y2 `: d! P modelActions.createFActionForEachHomogeneous$call
! n* {4 O3 a/ \( p" `" R0 w (heatbugList,8 p+ R: R2 c$ O6 p, G% `7 y
new FCallImpl (this, proto, sel,+ f, S1 h# R' k) Q, R3 g/ ~
new FArgumentsImpl (this, sel)));
8 p6 K( E* l R+ j& ~ W! o } catch (Exception e) {
) M+ z6 Y! o$ a( P) B8 T+ H e.printStackTrace (System.err);. {/ Z) H5 `9 a3 c7 U* V# _
}: n6 q/ u; x( C
' Y4 g7 ^% V% P5 g/ U% c5 J
syncUpdateOrder ();& [$ D8 ]. L. K* {
4 m7 f' f" j: q+ ^; K
try {# k+ t/ R- O+ M
modelActions.createActionTo$message
6 G( l- ~* H. | (heat, new Selector (heat.getClass (), "updateLattice", false));
8 M. o& ?( O5 v( ~% Y: t } catch (Exception e) {
' C/ J$ `$ {! Z1 t System.err.println("Exception updateLattice: " + e.getMessage ());: g! ^& G0 n" D' j1 d1 l/ Z% U- m
}
: @% K! E9 t& ?0 Y4 y/ b) P + Z; w1 _; @& ~* E4 ~+ i% M
// Then we create a schedule that executes the
5 _" N& m7 p6 V! Q M0 p, O7 v3 r // modelActions. modelActions is an ActionGroup, by itself it
' f% i1 h/ }- G9 d0 |( p. W // has no notion of time. In order to have it executed in
9 o" g5 Z# F: e, \' A3 ^0 k8 m // time, we create a Schedule that says to use the
Z* I9 j& v% Q/ u3 l6 P. a. V // modelActions ActionGroup at particular times. This
2 E9 A/ {: t7 V$ g' k // schedule has a repeat interval of 1, it will loop every/ L9 ~$ k; v$ L1 G
// time step. The action is executed at time 0 relative to; D# p0 ]4 D# `1 h2 M3 j
// the beginning of the loop.2 N. D, ?0 b# f; A9 `
4 Q) K( I$ K6 I B5 U& g( J
// This is a simple schedule, with only one action that is
) }* p2 U; F( y // just repeated every time. See jmousetrap for more7 k# A, v) u" `7 R
// complicated schedules.6 h( C- v0 ?8 e- _- q
) W! q& p3 E- z* Q modelSchedule = new ScheduleImpl (getZone (), 1);9 T4 p! V4 _- [; {
modelSchedule.at$createAction (0, modelActions);
8 W8 }- }% c7 a1 f$ I) k
; ?1 V6 ~$ a6 O h! s return this;3 d4 z9 [- _; S& U; @
} |