HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 X+ h! \- I( f
9 ]& b# h2 E2 S' ~8 m Y4 w# T
public Object buildActions () {
3 s$ \/ q/ {/ y8 j: E6 h* r super.buildActions();+ Z. X$ y, m1 ?- Y" j
1 U& M: `# ~% ]6 _5 o; g$ t
// Create the list of simulation actions. We put these in
, R2 w( ^ I U( Y7 @0 Q // an action group, because we want these actions to be9 j, C5 j, U2 W" u7 d
// executed in a specific order, but these steps should6 ]) n0 ?, }2 h1 F/ f3 S3 r
// take no (simulated) time. The M(foo) means "The message8 ]4 H4 L, v: S. a: a: K% q& P2 s
// called <foo>". You can send a message To a particular
, q, ^1 A% F5 ]# k% M# `; k+ N, \, ] // object, or ForEach object in a collection.5 O& d. ?% u$ E3 ?/ N9 ?
4 B" J% [% c: s6 \$ K# M8 C1 j // Note we update the heatspace in two phases: first run
7 E# }- x" |+ B- T // diffusion, then run "updateWorld" to actually enact the
1 j9 f, R4 y& ]+ z- {: w- q5 w) H // changes the heatbugs have made. The ordering here is
. f2 b" ]0 c! I# P3 F // significant!
# x6 \8 U! \9 F0 A# \ + c3 ?7 @. C' n4 E3 _
// Note also, that with the additional
( S* g7 U; h+ y* k // `randomizeHeatbugUpdateOrder' Boolean flag we can0 x9 `/ [: S, n: N# u' t) z
// randomize the order in which the bugs actually run
( C1 c1 X; H0 m4 F2 x, u* s // their step rule. This has the effect of removing any
. X5 Q3 r' ?( L6 X" K& S // systematic bias in the iteration throught the heatbug) L) p M& y- H8 Q4 s
// list from timestep to timestep2 @% f1 |1 o" @9 J+ \& l8 F" `
6 S0 l, P; o. z- ~- T3 ^5 b8 t // By default, all `createActionForEach' modelActions have
0 D3 F5 `; r, p# s3 `+ h+ d // a default order of `Sequential', which means that the2 a/ K0 ~, s) [+ S7 U2 P
// order of iteration through the `heatbugList' will be
9 W% T# ]# [, H8 h, V% E // identical (assuming the list order is not changed
3 }, N; U* t' d# k+ v, w+ ~ // indirectly by some other process).
& |: [7 _# i; w! X" A7 J0 G
( x( f* b3 L1 @ modelActions = new ActionGroupImpl (getZone ());) y6 m6 h( A7 E, m3 u3 j- k1 z3 M
9 w4 m1 W s2 x( E8 c
try { d7 R4 S% ?3 b( ?# w E8 `7 s
modelActions.createActionTo$message
. j) _$ P0 k$ p5 W" O& w8 T (heat, new Selector (heat.getClass (), "stepRule", false));
. m# t3 Y' z$ J+ z } catch (Exception e) {
. y, f& e3 t; n* ~% p System.err.println ("Exception stepRule: " + e.getMessage ());
# ?: y# y9 I5 D) `5 m }
" I" l2 u: m. D+ ~; i6 L( |7 y: X) ]) g" v& G* e& x
try {! Z9 d# B: H' l+ ~0 q# y: K' z6 W
Heatbug proto = (Heatbug) heatbugList.get (0);' y2 P8 A; n7 \$ U* L, M4 y( b4 \
Selector sel =
7 Y+ H9 q2 Z( B1 V6 R) m: d/ G new Selector (proto.getClass (), "heatbugStep", false);8 Q; e; w' f' P& O8 D
actionForEach =+ I7 J) [2 Z2 R( t8 j7 j
modelActions.createFActionForEachHomogeneous$call( Z5 |4 G5 U; c2 F4 W! }
(heatbugList,
; N# P4 Q& T. l( j* c new FCallImpl (this, proto, sel,
. a% @5 y0 m( j- @2 C9 r% \4 Y new FArgumentsImpl (this, sel)));
3 x$ O; b& I7 s4 F0 t9 T } catch (Exception e) {9 b# W" g5 B. B
e.printStackTrace (System.err);# m4 Q Q8 A4 _* u& V' `
}' d- V0 H/ O- ~
. q O# [& E5 Q4 z syncUpdateOrder ();
$ T! _5 t' Y Y0 ?8 z( W( G; ?
; ?. W6 k% q+ ^! I3 f7 H try {
( h- ]6 [& _, R modelActions.createActionTo$message
3 p: E3 D2 o* O& p. X$ w1 q (heat, new Selector (heat.getClass (), "updateLattice", false));
: B$ b1 b& p- V$ r( Y1 T6 D! M } catch (Exception e) {: ^. V5 b7 s; l# a9 u) @& [& ?
System.err.println("Exception updateLattice: " + e.getMessage ());$ [% e/ q0 g( K; t
}$ ~1 l- d0 X1 v. O. ~3 ^1 ^
6 b/ i9 {- k5 \, ^6 j
// Then we create a schedule that executes the
- i* S1 s1 M& O' g6 ?" S5 [9 t* g // modelActions. modelActions is an ActionGroup, by itself it
( M7 D' v% C: e+ r // has no notion of time. In order to have it executed in
* M8 B6 o% B' f1 L/ e: Y; G // time, we create a Schedule that says to use the2 N. Z8 w2 O6 U5 d
// modelActions ActionGroup at particular times. This+ d) O8 n* t, C! B1 _7 F6 `, Z% H# E
// schedule has a repeat interval of 1, it will loop every9 k- ^$ D- G2 r
// time step. The action is executed at time 0 relative to
D$ u% A' b' ? `7 l3 M6 o# X // the beginning of the loop.
5 |# s6 `4 B4 m# [% }
: n: A, h/ ^7 m$ Q" R# D // This is a simple schedule, with only one action that is
5 H- ?# Y' \# q& ?8 X1 p" i7 U // just repeated every time. See jmousetrap for more, W1 M8 O/ u8 f2 z; X& L. l
// complicated schedules.
0 `3 K# N7 {% M. H9 b& {6 Y! S: h
) ^9 w* |/ g3 E modelSchedule = new ScheduleImpl (getZone (), 1);8 F% e$ b' P. n' r/ Y8 |( t
modelSchedule.at$createAction (0, modelActions);
; V6 A* i% T1 j/ L* e) B
! p y6 o+ ^/ b9 g" r return this;
) ^6 `8 x& R4 i) c T1 b( r& X6 c1 X } |