HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# n' a0 F! s9 e5 `
/ a# g4 M2 m y public Object buildActions () {: b" X& x) X; N3 {4 H
super.buildActions();
8 F ^3 m& ~+ k$ z% |0 T + P' T' |/ b% k4 F6 B# w
// Create the list of simulation actions. We put these in
! j( x5 D; U" A E // an action group, because we want these actions to be
4 f3 N# z' \6 q; t$ V2 ? // executed in a specific order, but these steps should ~( Z+ C& R. o8 u9 v
// take no (simulated) time. The M(foo) means "The message
) N W$ D+ C, g2 R! |0 S // called <foo>". You can send a message To a particular
8 r; D v% N1 \0 @8 b // object, or ForEach object in a collection.
! q9 t: u6 V9 a D2 F: ^ . H6 q# q3 w0 G9 J: d2 ?* T% O/ W
// Note we update the heatspace in two phases: first run
5 t. }" J7 D* Z // diffusion, then run "updateWorld" to actually enact the
" F3 b6 O) [$ h! A+ k9 ]5 [ // changes the heatbugs have made. The ordering here is
9 ]" V7 T$ e- K5 F) s3 _* A. F // significant!* u: P6 E, c+ C2 G3 M3 U) y1 O3 X
" t% [% P) r# B( U3 b2 s* K% T3 L // Note also, that with the additional
/ }% B \/ B' C. P! W: j% | // `randomizeHeatbugUpdateOrder' Boolean flag we can, ]/ ]* @( k5 G G* w0 p
// randomize the order in which the bugs actually run) t8 B) j( Z9 U" Q c8 R0 D
// their step rule. This has the effect of removing any
0 d- d' \4 ?4 I8 b4 Q+ j+ g // systematic bias in the iteration throught the heatbug
$ v1 P( f9 q7 M# r' h5 Q! d. s2 O( b1 U3 S // list from timestep to timestep A, \ _8 h( \, n7 L& L b
8 ~' J, G' P( z // By default, all `createActionForEach' modelActions have
: A' ~. K2 T$ H. [ // a default order of `Sequential', which means that the! P, X# g3 l' g6 Z `
// order of iteration through the `heatbugList' will be% p$ F& }( Y: a) |3 _# C' Z
// identical (assuming the list order is not changed
- f. X2 y: [! l7 X6 M- f // indirectly by some other process).
% }% k* \' p' x$ w: ~ 1 P+ e- I5 e7 P& I, o$ m7 ?8 N S
modelActions = new ActionGroupImpl (getZone ());5 E' \6 f7 Z+ w
5 o7 |! c7 \# U4 B9 Z, o& O7 L
try {
6 O: g* Q+ G# I modelActions.createActionTo$message
8 ], {* q3 j! k. U1 L/ P (heat, new Selector (heat.getClass (), "stepRule", false));
( h$ N5 [9 t5 h. |5 c } catch (Exception e) {
7 ?# L, g. a; p# h' M1 {4 \! T( e8 v1 ] System.err.println ("Exception stepRule: " + e.getMessage ());( u6 c0 M9 J: e, f, N
}
- _/ t, t3 y. t/ P5 y+ ]! c) `- I5 {7 H3 X- I [! @2 C
try {4 x3 k h6 Q2 \4 A( \
Heatbug proto = (Heatbug) heatbugList.get (0);
5 @ k$ \" _4 W( w \1 B Selector sel = . I' c4 S8 h2 v
new Selector (proto.getClass (), "heatbugStep", false);
1 z2 Y7 q l3 F/ K actionForEach =
$ r2 N' Q! o1 s# t modelActions.createFActionForEachHomogeneous$call% v( a1 c( p% p, }. S
(heatbugList,7 g% M; u K$ v) O! M
new FCallImpl (this, proto, sel,# \ T" H. P9 B% d7 ^9 F; Z
new FArgumentsImpl (this, sel)));
+ @5 [* g$ u l; x3 L q/ @: R3 ^ } catch (Exception e) {4 P. M% J! M4 @0 h! }* o: O9 }
e.printStackTrace (System.err);
) x9 `- h; o1 x; B; v, T2 b }" G; _2 M9 O% V8 Y
' m) {/ C) _1 p- h5 N syncUpdateOrder ();
' }; ^$ _& ]4 m3 U$ ~
, p' a* p$ u* f* H& @$ o. f try {
/ g* u9 {4 w# ~, r$ l7 [* y% P1 K modelActions.createActionTo$message
8 J7 ]; b* m& j3 `: s4 h7 o; i (heat, new Selector (heat.getClass (), "updateLattice", false));1 o0 c& W& t1 ^: Z% c: u' v( r9 P8 x
} catch (Exception e) {
+ M4 M N' K3 d; i System.err.println("Exception updateLattice: " + e.getMessage ());' S; S/ |1 p: A, m1 f
}
F1 v6 P k" Q2 z
+ p( }2 l5 j" [) ? // Then we create a schedule that executes the: \: P& |: Y$ ? q7 s0 W7 E7 G
// modelActions. modelActions is an ActionGroup, by itself it, u& {, o1 P1 D: u# O |% m
// has no notion of time. In order to have it executed in
, q. W0 W# S3 U5 s# T9 c. g // time, we create a Schedule that says to use the! N2 C$ Q2 s0 @* Y* ~
// modelActions ActionGroup at particular times. This1 z/ J* H; R B7 I i9 ?" j7 [& t
// schedule has a repeat interval of 1, it will loop every
- \& B& p8 x) e5 D8 l // time step. The action is executed at time 0 relative to
( H9 c2 |5 v" Q: J5 J) H& y3 Y // the beginning of the loop.
- C# ~6 V/ `1 n1 [) H o* i" }" n- e" H. |0 W [# Q" o
// This is a simple schedule, with only one action that is5 `+ k4 O8 `2 i3 M6 F6 \
// just repeated every time. See jmousetrap for more* j, M- P" l% Y% v
// complicated schedules.
5 H( {% g. O* L+ p9 e
3 M7 x9 `" Z6 a! ^- N modelSchedule = new ScheduleImpl (getZone (), 1);
) W& X& ~! c% A0 k( [ P' a modelSchedule.at$createAction (0, modelActions);) I( u7 B' ]! H5 b% x. k5 Z' [ k
, b5 {, }! P1 n6 z) i* ^
return this;, {/ o8 \3 N' J0 y! f. r, ~
} |