HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: `1 m: u" E2 ~ C8 |. C" O0 l; | L8 c) ~! p- e3 y
public Object buildActions () {
8 @+ Y2 Y1 m5 ~5 }5 V- t1 S4 n super.buildActions();
) z" ^' C- e/ q) r3 i* D
; y, y; A9 o _, n( a // Create the list of simulation actions. We put these in
' f. a8 |% n0 K+ U+ t# u // an action group, because we want these actions to be( ~. P: ~8 j4 t6 E0 r7 n F
// executed in a specific order, but these steps should$ o. E4 x) m$ F$ z) w
// take no (simulated) time. The M(foo) means "The message
' V6 k9 A' E: b3 n: B( N // called <foo>". You can send a message To a particular1 \. g4 z5 x0 _. {9 m: H" I- Y7 x
// object, or ForEach object in a collection.
7 G. }5 F. G# B: q+ p' M - W2 L% G0 C P5 R: x) S
// Note we update the heatspace in two phases: first run
; l7 U) q4 c2 w // diffusion, then run "updateWorld" to actually enact the
4 l; r1 U: ]5 m k4 O5 o, q // changes the heatbugs have made. The ordering here is
! F: s" m7 w3 {5 c+ A+ o* @% U // significant!4 e/ i) _" R: h. R
! B' c" h8 o# G5 z3 }9 ?& n# G
// Note also, that with the additional: f; ]. g2 e4 J6 J1 Y% `# `- h" o
// `randomizeHeatbugUpdateOrder' Boolean flag we can
7 n& S0 O J* T/ V5 L# I! L" C# z // randomize the order in which the bugs actually run$ b! ^3 `1 d' B
// their step rule. This has the effect of removing any$ j0 D( `- B+ p, \7 n: a
// systematic bias in the iteration throught the heatbug
2 [, \, q# O! F) K // list from timestep to timestep
8 D; j6 q5 U5 U0 `0 ^9 F
2 j/ c$ c* M1 J% Q9 z // By default, all `createActionForEach' modelActions have
# u; g2 R0 X, N // a default order of `Sequential', which means that the) p7 t# _. }4 e+ j7 ^
// order of iteration through the `heatbugList' will be
3 k4 b' y* a5 I$ j$ G& ` // identical (assuming the list order is not changed- h- F/ B3 q& G! x
// indirectly by some other process).. W7 K$ }) [6 E6 G) j
( a, U% `9 @ Q+ `7 H( O
modelActions = new ActionGroupImpl (getZone ());- B! y. u( @4 ?) C; r% f
8 r) ^: j' o8 V7 X, S
try {3 |$ U" j. r* V! [0 v
modelActions.createActionTo$message1 \! Y* X4 m2 L# e: B$ {, C
(heat, new Selector (heat.getClass (), "stepRule", false));
1 }8 `! F' s) C t0 i/ o } catch (Exception e) {
0 K* G, c# @- B; p: d7 ~ System.err.println ("Exception stepRule: " + e.getMessage ());
3 ~ X) q( w) ]% T8 w3 V- w }. u! h3 o+ s# U( F2 F' p
# R; q& l% G6 \! F7 k* h* [, \ try {( \4 g* t1 X8 k" _7 {: `0 ~9 c) V
Heatbug proto = (Heatbug) heatbugList.get (0);
3 R0 T+ C/ w& h Selector sel =
2 F& v" r. }: L) Y2 S, U1 a new Selector (proto.getClass (), "heatbugStep", false);
$ L) a! P. T y( A0 p1 u: _ actionForEach =0 w1 H0 b; U7 d1 A. V6 W' `
modelActions.createFActionForEachHomogeneous$call$ b9 f E4 }' w0 o* J& ]
(heatbugList,4 i# ~5 I1 U8 O' D' U( t2 Z8 l% I
new FCallImpl (this, proto, sel,; C. U: @$ `3 O9 ?7 C W! r; w U
new FArgumentsImpl (this, sel)));
9 V L4 u1 e" O0 k, k( K0 ]3 B/ a% C' j } catch (Exception e) {
8 z# ~1 {6 h v7 Y; p0 \. T( ] e.printStackTrace (System.err);
" v4 B+ g3 D7 m d8 i( ] }
/ X& U2 m- E+ a* x) |) F
& ~+ `" _8 C# i/ I syncUpdateOrder ();
) m- V1 {# `8 C; T% o: Z0 P! V- ?1 b; `( c4 e, C- c1 T
try {9 v) O$ ~; ^1 |8 Q w" S0 j- h6 \
modelActions.createActionTo$message
6 _9 _5 {1 f8 d) O& k0 T: j# @2 @ (heat, new Selector (heat.getClass (), "updateLattice", false));
8 s7 B+ U: s5 O' j% A: u } catch (Exception e) {; ? `. p P9 P d% Q o7 s. B; ^# v
System.err.println("Exception updateLattice: " + e.getMessage ());% a' e, n; S2 {
}
f! s" Y8 k- w f 4 O6 W q$ `% {
// Then we create a schedule that executes the6 Z5 z/ S% f0 Y5 @+ Z
// modelActions. modelActions is an ActionGroup, by itself it( M) s4 ~ A1 Y8 w0 g
// has no notion of time. In order to have it executed in7 F5 u6 I2 J, w9 ?
// time, we create a Schedule that says to use the8 z! X& F, t+ g3 N" i
// modelActions ActionGroup at particular times. This
; h4 x. Y0 I1 X# W% l // schedule has a repeat interval of 1, it will loop every: \( J+ i6 c! ~( c
// time step. The action is executed at time 0 relative to
8 t) d4 m* Q" w; w0 j7 @ // the beginning of the loop.0 r/ g% y" b% D" y9 E2 c( j
5 V5 q: W2 }4 B2 A5 P
// This is a simple schedule, with only one action that is. V0 ~$ ^* t+ b8 W) } j1 f
// just repeated every time. See jmousetrap for more# H) w( ]: Q& {& z- r9 z
// complicated schedules.+ z8 \$ D/ V& T2 l- N# h
2 t3 u( z' u/ F+ J
modelSchedule = new ScheduleImpl (getZone (), 1);: M/ t3 b! ]# l, M
modelSchedule.at$createAction (0, modelActions);7 E/ Z: ?# v E. a5 B; M
+ O4 i3 s ~& C/ [0 z3 q
return this;
5 C+ f0 E4 U% S3 P- J' T# P } |