HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 ?6 u8 M0 j& y) x6 G3 Q$ p2 y
# C6 ~3 u2 t5 O3 _5 u public Object buildActions () {# M: Z. Y, m3 h+ r/ \* r2 E2 @
super.buildActions();
4 [0 G* J# C# R$ J1 ]$ B6 e
" K: H9 t% i5 j+ y, v5 H // Create the list of simulation actions. We put these in
9 z$ U' U! ^2 q6 O$ e // an action group, because we want these actions to be
# V" V0 ?4 I3 `, Q( ? // executed in a specific order, but these steps should2 |9 n& E1 G% ]" E0 ], X' b
// take no (simulated) time. The M(foo) means "The message9 s& D8 T; ?. Y ~+ j# |( y
// called <foo>". You can send a message To a particular
( r8 j1 \" o3 D7 e/ R2 s // object, or ForEach object in a collection.8 H q7 ~1 ?: c% M* M
/ O( y- Y1 @. j& \8 {. k
// Note we update the heatspace in two phases: first run
9 A' Q! y) Y. c1 i* o- a1 ^" l // diffusion, then run "updateWorld" to actually enact the
9 M. ~! x8 a0 O // changes the heatbugs have made. The ordering here is/ j3 t9 j3 M( y, e! U5 a( Q
// significant!* @% F% z- e: A+ A/ j; ^% W. R
2 \1 r: a2 F& q. s6 A/ O3 y
// Note also, that with the additional
' [2 u' a X6 L% R% B3 W2 C# z // `randomizeHeatbugUpdateOrder' Boolean flag we can
9 e. ?: J& j& T) L' x" }3 H4 k. D // randomize the order in which the bugs actually run3 p( i' O( P0 ~
// their step rule. This has the effect of removing any
5 J$ v. Q' R+ E/ L" ~ // systematic bias in the iteration throught the heatbug0 Y5 W; e, |, O, X) C0 o8 ^% Z' s- X
// list from timestep to timestep
9 X* b; l# |7 G3 S: w# J& \ 2 s( k* n- n' o- b; E
// By default, all `createActionForEach' modelActions have2 s1 Y# ?2 H6 Y! g, p
// a default order of `Sequential', which means that the. C! }7 J9 t; g- v
// order of iteration through the `heatbugList' will be3 b0 b- _( w9 w2 g: B5 O9 e9 d
// identical (assuming the list order is not changed
4 p" V! k4 a+ P7 P8 J9 {7 L // indirectly by some other process).% }; t% x: G6 B1 H" ?& @: t
/ ^ |7 t, X$ K; U modelActions = new ActionGroupImpl (getZone ());
1 O: s0 w- i; g0 j- R
* |/ R L! p) Q; {& s try {
, _( V" k6 m# |/ t# s% N, n. I- O9 [ modelActions.createActionTo$message
- g ^$ V6 V* ~# ^; ~ (heat, new Selector (heat.getClass (), "stepRule", false));
( v1 w0 ^& c) |! E" u6 S } catch (Exception e) {
5 ^+ q( M. [+ s5 E: Q, K0 E System.err.println ("Exception stepRule: " + e.getMessage ());
, R( ~8 q6 Z( J1 S) g# o5 A9 @ }
5 }0 p( f8 k J8 z- a( m
1 m j7 d- v1 x6 ^( m try {
. s9 e- d) a& c6 t! y Heatbug proto = (Heatbug) heatbugList.get (0);+ Z' K4 L& F$ T' [8 M/ i
Selector sel =
: B6 n. E! r9 D: M2 N' g; \ new Selector (proto.getClass (), "heatbugStep", false);* C. F: X$ M3 t' Z% ]% {& S( U
actionForEach =
& s! L ^7 {- b0 G: o$ j5 b modelActions.createFActionForEachHomogeneous$call
1 e9 m. \: N/ k* X3 s (heatbugList,4 v" L( |% I& }! i
new FCallImpl (this, proto, sel,
) b# ]- E# f8 F% T( s: y new FArgumentsImpl (this, sel)));0 s) s) U5 \8 t7 Q3 c, h+ p
} catch (Exception e) {
5 j- b& }) _, ]) _* v7 x e.printStackTrace (System.err);( v3 L* n2 G# m; w! h0 K
}
v ^& G1 ], k" t8 I
% L7 m4 [# A0 a4 b2 J syncUpdateOrder ();
r: ]4 W- ~ E+ o% b, d- f. w0 g4 {1 D" r5 W
try {0 `1 @) i, X+ J7 k3 J9 ~
modelActions.createActionTo$message
( |6 {2 i' K6 T (heat, new Selector (heat.getClass (), "updateLattice", false));+ C1 I2 c: Y% N5 K3 b) i9 J
} catch (Exception e) {; j! z; k# O/ E/ s) b
System.err.println("Exception updateLattice: " + e.getMessage ());
6 S: y7 T5 |5 }/ q: g+ V }
; S, j6 H9 s' K: O2 V " a# v1 [4 v( N7 A$ w
// Then we create a schedule that executes the/ X3 Z9 c9 |# `% X1 n3 s3 p8 U
// modelActions. modelActions is an ActionGroup, by itself it- p8 ~9 ^0 f# F6 c: o& U4 d
// has no notion of time. In order to have it executed in+ W) m) C/ L5 @5 |/ ]
// time, we create a Schedule that says to use the& J [+ f R) T9 X. C9 G
// modelActions ActionGroup at particular times. This
; |# r' i! |$ E% v f% k8 F" ?0 u R // schedule has a repeat interval of 1, it will loop every
1 o3 n. S1 Q9 s2 ^, N' X2 C4 j // time step. The action is executed at time 0 relative to
. |. l% o' j( a4 {" ]) O2 _ // the beginning of the loop.
/ N" [4 i5 u# Y% i0 d0 K" F1 q( Y$ z% K" w
// This is a simple schedule, with only one action that is
/ ^9 n1 ^- b& K* O) f9 { // just repeated every time. See jmousetrap for more
8 t0 l u q9 ^ // complicated schedules.
s/ Z3 v3 h* l8 F! Z / @" I) ]9 z; b% j5 r1 K6 e
modelSchedule = new ScheduleImpl (getZone (), 1);6 y2 \9 d1 A& {9 G( f+ x6 v
modelSchedule.at$createAction (0, modelActions);& L" }+ @1 o3 ]: d x
t" F! p3 {: y% _
return this;
6 k9 \& C6 e% f5 A) Z8 a( d } |