HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ S- A- y+ I2 }( h4 [
) {: J( L; B* y9 T5 @
public Object buildActions () {# x7 k6 V8 f4 y, g. J4 F- D
super.buildActions();6 ^$ a( M; N- j
3 K0 j( c% k- ]3 w$ B8 Z! W // Create the list of simulation actions. We put these in
# o/ ~; W8 c: X/ R // an action group, because we want these actions to be
$ h4 q- G7 S& s* [1 U& F // executed in a specific order, but these steps should
- ?! a2 @* d3 V$ j0 q8 z' C* M3 h% }6 a // take no (simulated) time. The M(foo) means "The message
. i2 z# F2 {+ P' N" u, P0 | // called <foo>". You can send a message To a particular
/ e* Q( U: X' P* p // object, or ForEach object in a collection.& G3 l. g* I8 _4 i4 ~' ~2 u
: | z6 H& q$ ~8 R! m
// Note we update the heatspace in two phases: first run
9 q# Q7 u( F& Y/ q- R // diffusion, then run "updateWorld" to actually enact the. H# k7 V n3 _0 {
// changes the heatbugs have made. The ordering here is7 O: T( ?/ Z$ e: \$ w! b1 W
// significant!
7 d: K3 ]' i' A+ r2 K. m
) h# K' X$ N& j7 U) W$ u0 ] // Note also, that with the additional- H7 p/ c, k1 _6 {7 Y4 {3 e
// `randomizeHeatbugUpdateOrder' Boolean flag we can
1 k# \+ ~& ]# H! o* l3 u // randomize the order in which the bugs actually run
% S' [" G, _+ U2 J; I6 a // their step rule. This has the effect of removing any
9 \* O% n. a# r* B$ T/ m // systematic bias in the iteration throught the heatbug8 c, X" ^# ?4 d' K) @" p+ @0 t
// list from timestep to timestep
# V, _+ G! G4 x$ N 4 J+ u' z) q z" y1 p
// By default, all `createActionForEach' modelActions have" ~6 n' s' s: u$ u+ y& B
// a default order of `Sequential', which means that the
7 G2 p5 H% J" q( s. T- @, H // order of iteration through the `heatbugList' will be+ Y0 L5 G; _6 [* z$ a
// identical (assuming the list order is not changed
, L1 S5 c# D6 l/ B- n // indirectly by some other process).* E* p6 O- F) I
5 s( E) e1 }( G# [ modelActions = new ActionGroupImpl (getZone ());
5 h4 j8 o3 B% f$ a* @3 P" d
1 A: z3 `- g( i" D+ f+ P" S+ H try {6 G3 Z. R. [# U% O6 G
modelActions.createActionTo$message" O; U; E3 ]" S, i6 P( F0 K
(heat, new Selector (heat.getClass (), "stepRule", false));4 M. I! p) ^! ?; R* I* C }
} catch (Exception e) {
6 x$ j" F7 t+ y7 n$ H$ O$ T System.err.println ("Exception stepRule: " + e.getMessage ());5 s0 ?( o" @* b+ w0 O
}
" q- d! ~1 g7 X. z- v* D W! r" n6 X4 Q1 ]' `
try {1 \' @( U0 a" e) Q/ j
Heatbug proto = (Heatbug) heatbugList.get (0);
* L8 |, I7 K" T, Q7 L9 ]; J+ S' { Selector sel =
; a5 e* o5 d6 i, z& w! d& i new Selector (proto.getClass (), "heatbugStep", false);
" G, \% O8 Z* @+ }' l6 G% ?1 } actionForEach =
; `2 c. `) x& v3 X: h- y. q modelActions.createFActionForEachHomogeneous$call
( P( ^$ h) f. K: b) g (heatbugList,
5 r% S3 r" v: P- `5 t new FCallImpl (this, proto, sel,
5 e( Q% s" e1 C* q+ p3 A new FArgumentsImpl (this, sel)));6 ^+ d7 _* M2 G# G& H- P0 R% ]
} catch (Exception e) {% w$ {& j4 z& r! ^ d9 O3 J! Z
e.printStackTrace (System.err);
, A) c' f+ d5 h k, F9 N }$ V6 h( S+ Y/ p7 p8 O( J
* K2 Y. e/ t0 H# p4 Z& M syncUpdateOrder ();6 P. O: V1 C4 R1 c$ p& C
: `4 I) b: M. g0 c7 T; J
try {
8 y- b0 O! C# t( E0 z) y modelActions.createActionTo$message
9 \/ Z( Q; h0 R y" z (heat, new Selector (heat.getClass (), "updateLattice", false));
8 ^: H* Z. g- b' m; g } catch (Exception e) {
; d$ A: L1 R, }) s) Z9 N1 O4 H$ S System.err.println("Exception updateLattice: " + e.getMessage ());# X- b( _( o* K
}
, O7 Y( G9 d% g" j ( O, {( ^: w0 v9 v
// Then we create a schedule that executes the
# k/ R6 {1 j* ^, E7 N* L // modelActions. modelActions is an ActionGroup, by itself it
5 l2 i. @+ P/ e // has no notion of time. In order to have it executed in$ n( p/ b2 p4 r4 H8 @4 |' x
// time, we create a Schedule that says to use the* O5 r. e& i( w# w! x
// modelActions ActionGroup at particular times. This: Z7 Q2 Y* n" Z9 T
// schedule has a repeat interval of 1, it will loop every- U6 s2 F! o4 k% M: S1 e5 O" T% L
// time step. The action is executed at time 0 relative to
1 o! S. M) t# J c // the beginning of the loop.
0 J+ E/ Y, {1 |3 g# Q3 S4 [$ p
7 w' L0 ~( ]: M& ]- V6 G# K9 q' S // This is a simple schedule, with only one action that is- ~+ Z5 k& r: }, Q6 [. O* q P
// just repeated every time. See jmousetrap for more
. ~1 p/ F m) i7 N/ s // complicated schedules./ [1 R3 I4 e+ t
9 O! Y" l; g/ l
modelSchedule = new ScheduleImpl (getZone (), 1);
; U0 a/ T' @4 a modelSchedule.at$createAction (0, modelActions);" Z, r$ S$ N4 ], B1 u
$ W: x- M7 M: D5 @6 b; c
return this;
4 J+ B8 ]0 {# _' l: _ Q9 M% [3 R } |