HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 @" a. s4 }' o3 O+ D8 R
- E8 G# L, F6 ~0 D* ]$ l public Object buildActions () {
- n3 u. B8 ~+ Z) i7 E- l super.buildActions();
9 y% j) B/ n+ }
+ C% Q9 u: J$ n. s8 {; j: `: V* ~& S P // Create the list of simulation actions. We put these in& }* b) }" @& y5 U* F+ _" ^- K; z
// an action group, because we want these actions to be' r; |9 N9 P0 I# g# J
// executed in a specific order, but these steps should
# I7 e6 N. O# @* A // take no (simulated) time. The M(foo) means "The message
9 F6 U ^" z0 ?$ [ // called <foo>". You can send a message To a particular
' c$ }6 w# x% T5 {7 L // object, or ForEach object in a collection.- `( Z: z4 Y( U, k4 v4 @ y
: m6 z/ ` l4 L& v; l8 ~- V
// Note we update the heatspace in two phases: first run
' v; ]+ W' h* ~# r4 E7 Q C$ Q // diffusion, then run "updateWorld" to actually enact the ?, g! Y9 K7 |% @
// changes the heatbugs have made. The ordering here is2 n! a( X8 t) w9 \7 m: x( d8 C
// significant!
8 s- Y7 s( }5 q7 j
( h( q4 D% p- x O5 H // Note also, that with the additional, }) O3 U$ h* C7 i( ]% E
// `randomizeHeatbugUpdateOrder' Boolean flag we can; D' j6 |% u2 Z. c
// randomize the order in which the bugs actually run1 K: r' u* q. f1 b
// their step rule. This has the effect of removing any
- h5 ^$ r* w: W+ h% h: Z3 k // systematic bias in the iteration throught the heatbug
0 D' ?7 S* U: k // list from timestep to timestep7 j7 h. e O+ E4 p4 |* w
( e( D8 N6 x0 e: \: o // By default, all `createActionForEach' modelActions have, S0 B! t9 [$ S2 y1 r
// a default order of `Sequential', which means that the- I: d# L$ y9 U
// order of iteration through the `heatbugList' will be) C7 O& v& V2 k+ o6 x
// identical (assuming the list order is not changed$ h0 M( `2 f5 y% H& d
// indirectly by some other process).4 ^; e: G: G# V- f
0 ]( \% P9 G9 c" D7 s modelActions = new ActionGroupImpl (getZone ());' y& L# l Y/ S" Z
& K( Y. K. x' s' G1 `
try {
1 \$ S+ t; _0 O* S# d modelActions.createActionTo$message
0 Y; b' T. s2 w( D4 A (heat, new Selector (heat.getClass (), "stepRule", false));7 ]7 }; x. C) v4 |+ @) Q
} catch (Exception e) {
9 r u( u' b$ w7 T" j! t2 I8 b( q System.err.println ("Exception stepRule: " + e.getMessage ());$ A1 D1 i0 B9 v
}2 D c V2 z7 f; P6 C6 y+ R
7 E/ B# O4 l" g( a5 ? J
try {
& t m* \4 x! M7 q/ p) P6 E Heatbug proto = (Heatbug) heatbugList.get (0);
( Y6 t- a& C1 p# g: |* Y5 E }1 V Selector sel =
. T7 v5 U0 w! l4 V" i( S new Selector (proto.getClass (), "heatbugStep", false);% O" a7 ~. Y6 K4 O
actionForEach =
J* N6 \3 j7 e modelActions.createFActionForEachHomogeneous$call
. h' @% i% z$ \$ }% u+ V (heatbugList,
! e0 ?( Z& X0 l' [5 | new FCallImpl (this, proto, sel,
! M. j# K* X# n0 z; ^& [ new FArgumentsImpl (this, sel)));
2 W% h4 v' j4 r S) q } catch (Exception e) {
* R0 X2 ]5 k7 B e.printStackTrace (System.err);
- \1 s8 L/ W7 p% C& q0 f4 {1 r8 D }: u, G* J2 |$ t: P3 _4 h) Y
& K: E4 ~. E4 M7 P/ B
syncUpdateOrder ();8 p1 h- \/ ?* x+ U4 V( F
9 y/ R( N; p: y, q4 h( c try {
1 m# l0 n' L; E, B% l modelActions.createActionTo$message , [4 z; C9 |# F- H- `9 {0 V7 P
(heat, new Selector (heat.getClass (), "updateLattice", false));( a9 p9 ^) I& [7 B" T
} catch (Exception e) {; F$ B1 J3 Z; y5 l
System.err.println("Exception updateLattice: " + e.getMessage ());# w1 ]; q2 t! m" `) W. |5 G
}& x- |# d. D }) |8 D0 `
9 f ?6 C$ y; G( D) ?; q // Then we create a schedule that executes the% Q3 z, i% p# X% n. n
// modelActions. modelActions is an ActionGroup, by itself it9 e+ D o4 i* r0 b5 j
// has no notion of time. In order to have it executed in0 |% {; s0 k7 c3 E/ v9 J% h# y% e. F
// time, we create a Schedule that says to use the
4 t7 S2 s7 e5 f, [& s) D4 Q8 Y w // modelActions ActionGroup at particular times. This+ U. X0 R9 n9 P6 t
// schedule has a repeat interval of 1, it will loop every
2 {: p8 r5 i+ |: o* y9 G& h7 W: q3 r // time step. The action is executed at time 0 relative to) H; S. j( N1 r8 J$ j% i
// the beginning of the loop./ u$ T8 A* ?2 K% {: L, |- W
! V( T$ `6 j( c r! ^1 n // This is a simple schedule, with only one action that is, j+ V" W9 ^( q) j) ?
// just repeated every time. See jmousetrap for more: a4 s. q; ?3 R' M
// complicated schedules.: W9 x2 Q" g4 |2 g. w5 U
: z5 Z6 m2 j$ r i3 i7 _4 @/ Q modelSchedule = new ScheduleImpl (getZone (), 1);
2 b8 p0 A- n( g0 x h modelSchedule.at$createAction (0, modelActions);
+ z6 x/ x" n- {0 [7 d ~
# ]. u$ c. [/ f( u: U return this;2 V+ B/ H. @2 p$ e% p
} |