HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; p( Q/ J4 M7 w: Q
3 m7 e1 b- _- f* @* \2 N# s0 E8 W
public Object buildActions () {
. G3 S/ n! t; x2 a8 b2 T& ] super.buildActions();* B! N$ s/ q/ ]- L8 Z
' v0 ^! ?+ c2 E0 G' U // Create the list of simulation actions. We put these in3 Q+ U- C! b' s9 U3 y: N
// an action group, because we want these actions to be- f0 _" ?! T% k" B' ~# ?
// executed in a specific order, but these steps should4 d% v) l+ d* \# N; J7 y
// take no (simulated) time. The M(foo) means "The message
" I. ~+ `0 j7 {* D5 D // called <foo>". You can send a message To a particular+ k$ h8 \2 ]3 [# u Z
// object, or ForEach object in a collection.0 [7 c2 j, Y/ }" N& Z$ u
( F. j' Q4 w$ U // Note we update the heatspace in two phases: first run/ j7 n5 |8 f! y$ |+ `1 q% q2 C
// diffusion, then run "updateWorld" to actually enact the3 @/ F% p: g- E T8 p) n
// changes the heatbugs have made. The ordering here is
' a! L. b1 v/ J3 E5 f/ \ // significant!( D4 j8 N3 U0 S5 f6 D
# j! ]9 U# G" r2 X6 C9 B! o' g // Note also, that with the additional' k- n, [& _5 ~3 Q
// `randomizeHeatbugUpdateOrder' Boolean flag we can7 h( L% H" |0 `, U
// randomize the order in which the bugs actually run
" t8 n8 T: e% g9 J; ^ // their step rule. This has the effect of removing any
3 b" }4 x, B3 r% O$ e! H+ | // systematic bias in the iteration throught the heatbug
% i6 ?, Q r7 p$ X/ A // list from timestep to timestep: X/ @3 f3 Y$ [+ ^4 | c, S
! @8 l* z* M+ O* m // By default, all `createActionForEach' modelActions have
% m( z9 w9 h/ J* p0 |# q, L // a default order of `Sequential', which means that the" R; t2 j7 j) P0 N
// order of iteration through the `heatbugList' will be
; D: ?; ^; n% K2 v9 s // identical (assuming the list order is not changed
8 i% h) ~: t6 q h$ x! M( I9 C // indirectly by some other process).
. D9 |) v' \9 x ], M : M8 S! G2 p' e5 p& i7 A! p" K" Y
modelActions = new ActionGroupImpl (getZone ());' Q3 c" G' p1 K5 B' }. I: V" v
3 M& p' Y5 j1 O& V4 r7 d% c
try {$ P* {) p; J7 ], Y) m: O9 |( o# h
modelActions.createActionTo$message% }! [& T: Z$ b
(heat, new Selector (heat.getClass (), "stepRule", false));
- s9 D8 \1 z+ P% G } catch (Exception e) {" f" p; h- p V- I: o
System.err.println ("Exception stepRule: " + e.getMessage ());
( k5 e0 |3 ^7 Z" T; I* F }
( D2 i o$ W m7 g1 Z
0 S4 ^/ ^! h2 s h+ P. P try {9 T1 I! z w% [9 t' V/ z; P
Heatbug proto = (Heatbug) heatbugList.get (0);
, s; d; {( N0 V0 J4 e Selector sel = % n; X) C+ X1 P6 }# c2 W0 U# S
new Selector (proto.getClass (), "heatbugStep", false);
. b- q& M2 ^$ C5 I actionForEach =4 k2 I. a# I+ ^# z3 g
modelActions.createFActionForEachHomogeneous$call0 G' H9 Y: M( [+ g( f
(heatbugList,7 ~4 r9 G, R$ _1 t8 f
new FCallImpl (this, proto, sel,$ k6 Z9 [ F3 w8 c7 ?# j) U
new FArgumentsImpl (this, sel)));
& @4 ^& C: L" i J' C } catch (Exception e) {
8 E# N9 I, R5 C% F2 @ e.printStackTrace (System.err);
% y( T+ R8 b+ B8 n5 i7 I# e }$ X$ R2 c' O) B8 Y. |$ |
& b0 e7 I: U0 |. x0 ?
syncUpdateOrder ();; k) f w1 C" Y. M
- h( \' I+ O: U, A- n n try {2 i9 M D; U! V* i5 Y
modelActions.createActionTo$message 0 P7 P( ^- ?2 L9 j: I
(heat, new Selector (heat.getClass (), "updateLattice", false));
. A: n& c/ i4 O/ T1 p } catch (Exception e) {
3 I( F( H' b5 a# ]! u }5 J System.err.println("Exception updateLattice: " + e.getMessage ());
, S" ]% T; d$ ?8 X' o }& q* |5 _( B/ l- r# T6 s
- A9 u1 O7 t0 D ]; t/ O. z
// Then we create a schedule that executes the5 s5 n( C1 h! y/ {* S# B# o
// modelActions. modelActions is an ActionGroup, by itself it% }. L) k, ?' X6 e
// has no notion of time. In order to have it executed in
V- P, [$ m4 J6 Q6 d0 A+ N // time, we create a Schedule that says to use the
}! c f8 l C0 z // modelActions ActionGroup at particular times. This
* G6 l' `1 ]& s/ U3 N) I' r- e' U // schedule has a repeat interval of 1, it will loop every
* }7 V3 }/ g! x8 l: ?) f5 y // time step. The action is executed at time 0 relative to. V$ @! C/ y+ ^, ^ _
// the beginning of the loop.5 V* l4 |- j2 s% U6 ^
& v8 `8 ?- Z& i; m
// This is a simple schedule, with only one action that is
# ~' Y7 @6 j4 g+ `; d# h2 ` // just repeated every time. See jmousetrap for more
- U: _' d% N# ^5 x // complicated schedules.
: ]6 V* a- H8 ?. R3 X9 q; M ) r" A3 i5 g8 F t% Z
modelSchedule = new ScheduleImpl (getZone (), 1);: G F+ {' W$ F, y( W. E, j
modelSchedule.at$createAction (0, modelActions);( s% n. [( E$ E5 I- g/ s/ f4 e
" t- i. |' q+ x2 [- v M
return this;
- _0 H# a3 F+ P* E } |