HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% Q' F* q$ o% G* @. ]& o
0 Y: t/ j, l+ v" @3 \5 ~
public Object buildActions () {
- H1 v. H& \$ P# h- \/ o super.buildActions();
9 W+ f* A0 N% v
# A9 j6 R' ?; K // Create the list of simulation actions. We put these in' j& Y9 q) j9 b) I
// an action group, because we want these actions to be
; p1 q' E3 Q1 \: D. U // executed in a specific order, but these steps should
9 |. K( B$ s1 O- [# b // take no (simulated) time. The M(foo) means "The message6 f* j# V% c* M/ Z! I% G* `& }
// called <foo>". You can send a message To a particular, z% Q7 n0 @* V* p! c
// object, or ForEach object in a collection.
- n+ A; ?) v7 X) V, c 0 u3 S( `6 T' o& p3 Z6 U) V X2 c7 n
// Note we update the heatspace in two phases: first run4 N' w3 P! K' y1 G7 q
// diffusion, then run "updateWorld" to actually enact the
7 V4 T4 K5 {! v6 O3 Q1 A // changes the heatbugs have made. The ordering here is) f( X. T2 c# P/ d( x, k
// significant!3 l5 N+ u( n! Y
( t$ H+ l8 Q- m1 a7 \4 r2 O
// Note also, that with the additional
\( A! c d+ h. f/ |, |( D; ] // `randomizeHeatbugUpdateOrder' Boolean flag we can
/ D3 n6 l) A, I* E // randomize the order in which the bugs actually run, u8 F2 P A8 V& u( t* U1 y. ^
// their step rule. This has the effect of removing any
0 n0 a6 S1 V8 w/ G, ]( b // systematic bias in the iteration throught the heatbug0 k# ~. Z7 h w( U- e# J
// list from timestep to timestep" L0 T- Y% U2 \
: n9 _. l c u( x; N) F o
// By default, all `createActionForEach' modelActions have
% e" M; u/ T, k5 @/ w% R2 ~ // a default order of `Sequential', which means that the, N3 k# q* K' d+ O3 o
// order of iteration through the `heatbugList' will be& R! d1 D, B# e7 S: u4 Y2 w2 C" E( Z
// identical (assuming the list order is not changed
$ y3 P' B5 B3 ~: ?4 i) h // indirectly by some other process). { U# x2 L9 [1 p* Y' @
n% `+ Y P0 \: H modelActions = new ActionGroupImpl (getZone ());
3 u3 c1 }, a3 ^* P" H
) \8 A. n: w( V$ v try {; J4 J8 h/ ^) L9 L+ J1 w
modelActions.createActionTo$message
1 _ r/ |- m4 |/ s (heat, new Selector (heat.getClass (), "stepRule", false));) U J; k$ s6 _+ H
} catch (Exception e) {
. j: h" s( D1 ]! o" v3 q& _% z- d6 i5 P System.err.println ("Exception stepRule: " + e.getMessage ()); D& n% v2 Z5 W0 s" z" X% p2 d
}5 C/ X ~7 w) W4 J8 T3 j
; O7 \1 m+ }8 E+ D/ B9 R, R/ e
try {
) h2 G% \ D. Y! q- X7 K Heatbug proto = (Heatbug) heatbugList.get (0);
0 `6 I/ w) k: w5 O! h% h Selector sel =
8 P+ _" j: j# i! ~( c new Selector (proto.getClass (), "heatbugStep", false);' U) s! ^; u7 \5 G# `$ Z* h
actionForEach =
( U5 x9 ]2 s- l" z% L x9 y modelActions.createFActionForEachHomogeneous$call; ?* x2 X8 v- j% N& E
(heatbugList,6 I' T! C4 j. _+ u2 d$ v' s
new FCallImpl (this, proto, sel,! W5 [: @0 J9 j" e0 \: Q( }
new FArgumentsImpl (this, sel)));
( p. Q4 j+ c2 w I+ P/ |6 Z } catch (Exception e) {
9 ?8 t, }% _* S% U0 s e.printStackTrace (System.err);; Q' i% @: o) B5 D( e
}
: p/ Q6 D1 X/ U5 t% C9 K
$ i' W+ K/ ?7 d0 C; c( J4 z) ~ syncUpdateOrder ();) P$ D4 A% e7 P
1 c. B2 Z( F' ~& w8 ? try {& z7 L: T7 o* O) k4 K
modelActions.createActionTo$message # o+ J# a t+ V* r# ], q
(heat, new Selector (heat.getClass (), "updateLattice", false));
d8 U3 d) B( Y6 V7 W1 @. v* k } catch (Exception e) {
* u+ Z B% {) [ System.err.println("Exception updateLattice: " + e.getMessage ());9 h1 K' C# `+ k% J8 g
}
% f( q" [$ }8 f8 T
) ^: L; t/ j0 {* q* R/ _ // Then we create a schedule that executes the1 j7 J0 t, ~* V) J) \
// modelActions. modelActions is an ActionGroup, by itself it/ @9 ]( @4 b2 M/ H) B- Y
// has no notion of time. In order to have it executed in
, x2 i) @$ v- _- _) ?1 B // time, we create a Schedule that says to use the
; i5 I; G( P" j$ v) {( A // modelActions ActionGroup at particular times. This# r. M. ~( G2 p
// schedule has a repeat interval of 1, it will loop every7 ~# w# ^4 }' `3 g! R* G. N% ]! j
// time step. The action is executed at time 0 relative to% ?. r/ ^, g2 \6 Z+ b
// the beginning of the loop.3 N) Y) I O( Q& m" k/ J: Y6 F+ \
: T- P& w5 @; Q p, e' t$ c u // This is a simple schedule, with only one action that is
# a: _) s( Z+ J1 [2 ]& B2 w // just repeated every time. See jmousetrap for more
1 a# L% u9 U6 P1 |! S/ T // complicated schedules.
. u2 o1 Y6 m. @9 D1 @% t5 r
Q' m5 \* E. }4 L# R modelSchedule = new ScheduleImpl (getZone (), 1);
# _8 R h7 c+ F' } modelSchedule.at$createAction (0, modelActions);% V" p4 n0 {) y$ V- [8 V- t
5 S$ I0 a1 A9 z return this;
# b- w; X6 R5 ?. Q: P; t3 H3 A } |