HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& |# o( O, b, c2 ^+ z+ Y
! Z' x- Q- i; v: ?5 P: C1 W: c3 f public Object buildActions () {) e1 g: ^- n; l% }! Q
super.buildActions();% I. O" |9 a& Y
( ]; g3 i" e) S8 L
// Create the list of simulation actions. We put these in4 ]0 @$ ?( T% F
// an action group, because we want these actions to be9 U+ O! G. n1 ?; v
// executed in a specific order, but these steps should& k q$ q/ {0 O, M7 H7 v& {* K C
// take no (simulated) time. The M(foo) means "The message M P) C3 x' G1 T% A) ?
// called <foo>". You can send a message To a particular
0 S* k% O0 c) l/ @( D // object, or ForEach object in a collection.
/ ^2 U2 c8 p4 P& g0 L( H- u5 f
3 X' C( e' B9 C! T7 b // Note we update the heatspace in two phases: first run0 q5 \+ E/ Z; ]# P: e# N3 ^
// diffusion, then run "updateWorld" to actually enact the# X: }0 t& a) P6 |5 J# I5 G
// changes the heatbugs have made. The ordering here is; P" A$ I* A( ~3 U+ N
// significant!7 x3 O5 }3 E m# N
& e7 w. ], ~( P5 ^ // Note also, that with the additional
9 c) w5 Q6 I$ l. z7 { // `randomizeHeatbugUpdateOrder' Boolean flag we can `# @! a( I1 p) `: X6 w: k* l
// randomize the order in which the bugs actually run! s% O; P" w& E* b4 v7 d
// their step rule. This has the effect of removing any
/ Y' n; L2 s7 h) \: d // systematic bias in the iteration throught the heatbug u+ A d& L& F# w' m: r3 g& M
// list from timestep to timestep
1 m; [# u- |7 G$ {8 y1 B
- ?1 H/ X( d8 s3 g* D, `' ` // By default, all `createActionForEach' modelActions have
" R1 v; f3 X9 X2 c$ I! P ~# v // a default order of `Sequential', which means that the1 Q& P, {9 U% k( m7 Y
// order of iteration through the `heatbugList' will be' F* J( b7 a( U
// identical (assuming the list order is not changed
) r' s3 V$ A- b5 \% [9 k // indirectly by some other process).
- T1 b1 a1 b3 j- c
1 C3 j% y# b9 `# ~% M o0 f/ l5 N modelActions = new ActionGroupImpl (getZone ());. t0 t0 K0 }- B
) o2 J* F( `6 j* T, [# W* O$ ]! e$ G try {, }+ {5 w9 N6 Z) l
modelActions.createActionTo$message- ~( b f1 S4 J: Y" M- o
(heat, new Selector (heat.getClass (), "stepRule", false));
+ L, `# G+ w# z } catch (Exception e) {
: c4 `/ ?1 \! o) ]% i8 {' | System.err.println ("Exception stepRule: " + e.getMessage ());
7 J+ h. @0 m Y% C. D }
- d8 y7 X# Y, I1 E6 u
8 M) B. p0 ^' p" m4 i$ F try {
' N0 N( b3 P0 E Heatbug proto = (Heatbug) heatbugList.get (0);! T7 q1 I( W) L7 r9 f& i
Selector sel = 1 H- K) \& n. M
new Selector (proto.getClass (), "heatbugStep", false);% z: f5 A0 c% j ?( c) `
actionForEach =
+ F" Q3 @2 q. D# [$ j0 U! s modelActions.createFActionForEachHomogeneous$call$ z* @7 \9 p. E8 e# j
(heatbugList,
' v* D! Q- f) R$ Z0 m new FCallImpl (this, proto, sel,7 p t5 W" }# _3 j9 n. F
new FArgumentsImpl (this, sel)));
3 z+ P! g" i" q' } } catch (Exception e) {0 D; N0 q, ^6 w# H
e.printStackTrace (System.err);4 F: r( z2 `: N( X& r
}
. k9 W3 ?4 I. @, R. G
N7 V- U4 m; ]$ G/ b* R syncUpdateOrder ();
$ s$ u' Z4 m& N( T) Y1 F3 W% N. ~, a) z; t# a* x( l! D0 Y4 S( Z5 N
try {, z& f2 P7 V) v. y8 _+ `
modelActions.createActionTo$message # }: ^2 D z" \ d$ E
(heat, new Selector (heat.getClass (), "updateLattice", false));
+ z. ?7 R+ d' p* o- r5 y4 p } catch (Exception e) { Z# Q- T i# E5 L
System.err.println("Exception updateLattice: " + e.getMessage ());
' E/ V E8 M' j9 L& z }; C( s. i0 U+ [
+ D+ f. \" N# Z" n+ \ // Then we create a schedule that executes the6 `2 F: U! e' h) x3 b* Y
// modelActions. modelActions is an ActionGroup, by itself it, r# x/ C# |* `$ Y
// has no notion of time. In order to have it executed in b7 e& ?7 `1 t5 G4 _! P
// time, we create a Schedule that says to use the. u0 y# j- v4 u G0 U' {2 s- t$ [
// modelActions ActionGroup at particular times. This( t' H1 w6 g i
// schedule has a repeat interval of 1, it will loop every
3 [( J N, |& O // time step. The action is executed at time 0 relative to
R: i2 l- D7 z' f // the beginning of the loop.
* N+ j$ T3 |% t1 Y' c* z( b1 y# M0 w. |/ L" V- A( t( v
// This is a simple schedule, with only one action that is
4 P! f! }& W5 ^2 d. A // just repeated every time. See jmousetrap for more
# n. |% t" `9 Z+ m, D; N // complicated schedules.2 B4 Z0 n7 l3 c! _
% G6 H, w0 W) m& Y$ a% q ` modelSchedule = new ScheduleImpl (getZone (), 1);
! ^8 g; X( B" \8 n4 I0 e4 u1 ~ modelSchedule.at$createAction (0, modelActions);
1 g" Z$ A& x4 X# t* u; N) F6 a! R8 L 8 U2 F4 Z$ D. ~" R9 _
return this;
1 g# F9 P/ Z5 ]4 G5 F } |