HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: R4 H& q9 p+ |" y+ D
/ z$ Z, ^8 i. Z- [$ h public Object buildActions () {
7 p4 @/ Y# s+ ?3 | super.buildActions();
; @' ? ?$ F' k8 z- d 8 ]! {- E( k: w) w, p, O
// Create the list of simulation actions. We put these in9 V' ~- w. U; y" k$ t1 E
// an action group, because we want these actions to be3 R# n+ b4 p2 a' r4 O& c! X4 [( J; l
// executed in a specific order, but these steps should' [& O. g2 S: I8 H4 g
// take no (simulated) time. The M(foo) means "The message
/ {; S9 a$ p, C" l( t5 w4 x // called <foo>". You can send a message To a particular
4 D7 q; G9 p& Q. P& [) J // object, or ForEach object in a collection.+ W+ F# L5 @' Y. z4 o
b& o; H1 X" ]9 B# Y, P8 g // Note we update the heatspace in two phases: first run
( U5 x* ]. ]; ?% }3 A; G // diffusion, then run "updateWorld" to actually enact the1 N5 Q/ P0 M6 \7 s* t
// changes the heatbugs have made. The ordering here is
- M3 D' p, G1 h; A // significant!- r; H1 c! P2 D7 i# z
. y: D- Q3 q, i; a$ R4 J/ Q // Note also, that with the additional. z( v: D- D, X9 y6 i4 [7 c z
// `randomizeHeatbugUpdateOrder' Boolean flag we can
. C# z& r; U. Y( ~) t: {: Y$ l; q0 I // randomize the order in which the bugs actually run, i- r8 G, N3 Y' S' K& M' C) z
// their step rule. This has the effect of removing any* Z" L8 [ W6 f8 N
// systematic bias in the iteration throught the heatbug
4 k, Y% g0 ~: M // list from timestep to timestep9 \3 W& f( _8 a
( h ?, d. a2 H
// By default, all `createActionForEach' modelActions have
5 o, P/ l& c& j G) @7 w9 d // a default order of `Sequential', which means that the
! K- r: Q7 ~2 w1 r, t // order of iteration through the `heatbugList' will be
6 w$ |7 g; E! B& E% U9 w9 U% A // identical (assuming the list order is not changed
) k, t% g7 O9 |; N7 I3 G/ y // indirectly by some other process).
# p+ r. f! x3 h2 `/ C" F) V # V# d% Y* M% d4 A& S8 F1 e
modelActions = new ActionGroupImpl (getZone ());: S% I" L' x {/ @6 T6 C
1 s- D& F2 Q' m# u6 j6 Z1 a6 T; ?, r
try {
' S) }0 r2 ]9 v& N; Q! G modelActions.createActionTo$message
; Q2 b, j5 [7 _: G (heat, new Selector (heat.getClass (), "stepRule", false));% `* x0 t: {" |; U
} catch (Exception e) {
3 x# L2 P+ K; ]: y( i7 `% \" ? System.err.println ("Exception stepRule: " + e.getMessage ());
* A/ |! z/ ]8 H& d }
& M" e& \0 w# p. z
4 h6 K' |6 h% F0 B7 r) H2 R2 p* X5 u try {3 {7 g2 ^* h# z( p0 D
Heatbug proto = (Heatbug) heatbugList.get (0);: f+ U5 N- n7 e7 X2 X7 v
Selector sel =
5 Y. Q! A: l5 B- B+ J0 h2 [ new Selector (proto.getClass (), "heatbugStep", false);
: Z$ W! z5 }' M# h actionForEach =: ]: t# H8 }5 Z; Y" E% R" b& m
modelActions.createFActionForEachHomogeneous$call+ n7 m$ ^" b8 O$ {, i
(heatbugList,: D5 v- b1 A1 g- @! c; f2 L
new FCallImpl (this, proto, sel,
2 Z! _$ h* n, O4 b) P1 s new FArgumentsImpl (this, sel)));9 L0 _! {9 j7 a8 Y, n
} catch (Exception e) {
7 ~& _# M& V4 \8 C5 r: M e.printStackTrace (System.err);2 M& u7 l% h6 X: ~* {3 b( i5 i# I9 w
}: J4 c: e% T) h8 K
2 [/ |) _/ p* k! \. [. ~( R
syncUpdateOrder ();
2 R. V" a! S2 r% }7 n7 d& y
% | t, ~; a5 y try {
& G6 X1 z; n+ G) E modelActions.createActionTo$message
7 A" r7 Y4 T! \0 x; @8 } (heat, new Selector (heat.getClass (), "updateLattice", false));
1 V- Z6 j# _+ H% h/ ^8 w } catch (Exception e) {0 X; g7 s4 {/ R
System.err.println("Exception updateLattice: " + e.getMessage ());* c: J$ h% M( y3 A9 I
}
2 A' |) T. K5 w, L/ i* A( e1 R
6 p& p2 D+ q. y' _( c' _ z8 ?, B$ B // Then we create a schedule that executes the4 _1 d9 H, x) N( g0 g3 n
// modelActions. modelActions is an ActionGroup, by itself it! o2 N8 i* t. {
// has no notion of time. In order to have it executed in
1 E6 V2 J0 Z- E: E3 E // time, we create a Schedule that says to use the
; |8 Q5 D+ l2 ^; _9 f6 L0 X // modelActions ActionGroup at particular times. This
/ C9 C0 }! Y5 c* I // schedule has a repeat interval of 1, it will loop every
3 Z6 `9 ^ q' [% L+ b3 l( t // time step. The action is executed at time 0 relative to8 M) P( o2 V1 G9 V
// the beginning of the loop.0 e) I; [+ x1 Z* a' d
9 G) G5 B7 ]4 q5 [4 j // This is a simple schedule, with only one action that is
0 g- {( m) e; W7 N: S6 c n8 r // just repeated every time. See jmousetrap for more4 _- J+ Y- h2 R) R7 A, z: N
// complicated schedules.) k/ m, g. Z" y6 [' d
% u* t6 t2 X/ t
modelSchedule = new ScheduleImpl (getZone (), 1);7 J3 k7 Q; u# C/ L; i/ a6 ]% Q
modelSchedule.at$createAction (0, modelActions);7 b4 U( I0 L8 {6 M" f2 Q
R& G. Z o! V! A6 @" f0 I
return this;5 w/ k$ C G, \1 a/ F
} |