HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ k6 f; U2 Y) ~4 q' x( Q
4 w1 v. `5 g7 L9 M- d
public Object buildActions () {
) q/ f# g( g& i1 E super.buildActions();* @4 `- u. y1 Z" b+ ]* u4 h
# G1 H( b ^5 K/ C // Create the list of simulation actions. We put these in1 D5 N" ]: @1 t( `
// an action group, because we want these actions to be
4 J" H" b, v1 Z( ?3 P- u // executed in a specific order, but these steps should4 M( v/ H5 a8 w8 }: R
// take no (simulated) time. The M(foo) means "The message, E1 {: z/ W9 a+ i+ F
// called <foo>". You can send a message To a particular4 t& S9 c( ?* y( N
// object, or ForEach object in a collection.5 f$ e) Z; [1 B6 e: k$ a
1 P8 C/ T& R* X/ c/ I* d
// Note we update the heatspace in two phases: first run/ z7 ^, X; S4 E' n, K
// diffusion, then run "updateWorld" to actually enact the
; H& I; X, X. I y // changes the heatbugs have made. The ordering here is) a" |; {6 b, N, {; `% x
// significant!
3 k; c* d$ A, X M & X+ f; B" U- I
// Note also, that with the additional2 V9 k# c1 ]7 A; d2 Q: Z- u
// `randomizeHeatbugUpdateOrder' Boolean flag we can; D* m8 o$ i+ b, x. v
// randomize the order in which the bugs actually run7 |% Q$ J- R5 J! r* [% B
// their step rule. This has the effect of removing any8 x0 X5 ?/ z* i1 z/ s& x6 ?
// systematic bias in the iteration throught the heatbug
# L& t [8 S0 i2 _ f5 h // list from timestep to timestep7 c" y- v0 A" b
% [' ~' Y/ m- S7 z9 K/ r9 s" d
// By default, all `createActionForEach' modelActions have
7 r4 `1 G+ A. o/ L // a default order of `Sequential', which means that the
. l) @9 G, H" F+ `9 |: D5 h // order of iteration through the `heatbugList' will be
2 K- q" I5 i; _5 K, W2 r- u // identical (assuming the list order is not changed7 F9 w1 l3 W) h9 l/ V2 w
// indirectly by some other process).: w% Z. m! H# D/ a8 [2 ~
* E7 `( O+ k; P& ]) P+ `% {
modelActions = new ActionGroupImpl (getZone ());
& _2 p! b% c* r: b2 S! P% J$ ?5 o6 g% f O. Q. `
try {
1 m( Q" d% `+ [1 R" n+ b& C modelActions.createActionTo$message
, |: q: s1 ~6 b) Z3 I' @ (heat, new Selector (heat.getClass (), "stepRule", false));5 S8 C2 ?1 r, z) {9 t7 d" A" ^; b
} catch (Exception e) {
6 ^/ u: n" N4 r* l3 G. [: M System.err.println ("Exception stepRule: " + e.getMessage ());
' x- |: v1 b3 e }
! f& w5 z" L% M3 J% ^0 m" [3 Q; i2 T, I; j
try {
9 m4 v$ G7 ^% n. a Heatbug proto = (Heatbug) heatbugList.get (0);
% ~! O4 k8 n' C, I$ C2 l1 K& R Selector sel = ! N z9 u7 L* q: I' A
new Selector (proto.getClass (), "heatbugStep", false);; g5 d# W* [6 b# l, W; [& \ q
actionForEach =) ^4 k- P: E3 M9 _- I% ?7 T9 y/ b
modelActions.createFActionForEachHomogeneous$call
# L' F2 m' J1 S; ] (heatbugList,7 @2 r. g+ i/ i9 i4 |8 p% Q: D) H
new FCallImpl (this, proto, sel,$ T6 P8 I5 y' A- o, a3 O. t( o% R
new FArgumentsImpl (this, sel)));
6 v$ R2 p$ C# v! g R! B+ ~ } catch (Exception e) {2 {6 j1 I2 ^8 ]/ C8 _( r
e.printStackTrace (System.err); ~" q/ W# R6 P6 J4 ]1 u, E
}8 D* i+ C0 u4 e
5 r C% I7 U, [& s: s/ F. p syncUpdateOrder ();* g; v# c5 @$ r V* B3 \% ~& G; p
4 X, h4 e0 ^+ `8 _- \3 { try {; r$ b+ [( V% Y
modelActions.createActionTo$message
6 p; o4 v- x' k2 j/ o (heat, new Selector (heat.getClass (), "updateLattice", false));/ H( }6 b/ j3 L& |
} catch (Exception e) {
& P, S: r$ I) {' R/ y1 @ System.err.println("Exception updateLattice: " + e.getMessage ());
1 R# P+ d' m" { }
# @* i, G1 B+ X/ |# u+ }" k2 V * g. ?2 _5 i' K' ^& {/ V/ ]
// Then we create a schedule that executes the+ N* s, k7 u5 f+ i
// modelActions. modelActions is an ActionGroup, by itself it7 u' R! J% d# }1 i8 w% g3 \' r; f
// has no notion of time. In order to have it executed in
" ?, j! x4 n" b* O! C" g // time, we create a Schedule that says to use the; O6 W+ y+ Y0 m1 a ^
// modelActions ActionGroup at particular times. This5 p7 [. z. ~0 B0 H& O' Z% c
// schedule has a repeat interval of 1, it will loop every
' m, ?, H1 @3 H8 T- Y // time step. The action is executed at time 0 relative to' t8 J/ X9 @% w& [7 @
// the beginning of the loop.
$ \4 O {( }" q/ x
8 E, K! ?3 G3 v/ Y/ k* G // This is a simple schedule, with only one action that is$ I' t5 w1 k3 M& m5 P( v& U
// just repeated every time. See jmousetrap for more
& P( R/ C P, s/ T: m/ X$ m, { // complicated schedules.
8 ?1 z m4 p9 |$ U
: j! F' @! n4 D v" c$ X modelSchedule = new ScheduleImpl (getZone (), 1);
. V9 ^6 W/ c$ G# d+ {7 F; f modelSchedule.at$createAction (0, modelActions);
% |& n, w/ |$ e3 F5 j + t# R. H2 X9 P& y6 e/ q# L
return this;
0 T* @2 M1 r) c" t5 f: W) L' D: c } |