HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 D% U9 U# X4 n9 H1 |( h: p9 x) f5 l; Z2 Z* H1 Z
public Object buildActions () {0 b; Y3 c$ T7 }
super.buildActions();+ C7 x. k$ D3 C* N$ d* T
+ `5 e1 i; p3 f- W& L4 N4 b
// Create the list of simulation actions. We put these in
- J. D6 O* F4 } // an action group, because we want these actions to be) w! {+ q; n; U" l* D" k6 W
// executed in a specific order, but these steps should: H% t* M. ^! N. l6 T& R
// take no (simulated) time. The M(foo) means "The message
# C# |& e+ T) R. Z; }! d // called <foo>". You can send a message To a particular
; ^6 Y' v! z% Y) `$ _' v // object, or ForEach object in a collection.
1 L0 P% w- r. R% p4 ]6 c
8 t% g' a y9 C; H2 e! t // Note we update the heatspace in two phases: first run- H1 e: A8 r; m! h3 B$ `1 h
// diffusion, then run "updateWorld" to actually enact the' l' L. v. c9 x g1 }0 T7 O; F$ p5 H
// changes the heatbugs have made. The ordering here is
4 {4 m _, D ~( c2 E7 ?5 h( e // significant!- Q+ N C1 ^! p3 o7 Q1 M$ M k" p
7 I) H2 J/ }" W3 m
// Note also, that with the additional
7 C% ?6 | H, t/ `3 s // `randomizeHeatbugUpdateOrder' Boolean flag we can
* M& D' P# W5 J& m9 S) v // randomize the order in which the bugs actually run- A, e/ m4 J1 d) N
// their step rule. This has the effect of removing any
) Y/ i c% L* K // systematic bias in the iteration throught the heatbug: Z$ R+ u- i3 c2 _- n' p. _7 B0 o
// list from timestep to timestep
$ Q' m: Q: ^" {. ^2 z" s
) D& P% V. [) @/ Y: Q // By default, all `createActionForEach' modelActions have
# ^ M" \ X+ x- H4 t" T' @2 j // a default order of `Sequential', which means that the
) `0 c$ m$ x5 I1 |/ F // order of iteration through the `heatbugList' will be. @8 Q0 Q* s7 G& E# }% ~
// identical (assuming the list order is not changed
3 s7 W( D# C! u$ B // indirectly by some other process).
b& u' c& {; J$ X
: D5 n5 ^% }: ]. g* P modelActions = new ActionGroupImpl (getZone ());* ?; U1 _0 G6 D* z3 u
0 _4 S% e3 |* [& @% l try {' ~; x6 {# P: R! e! |' g$ k
modelActions.createActionTo$message
/ w7 m+ c$ l4 S: t) f- n7 P0 h (heat, new Selector (heat.getClass (), "stepRule", false));- ~4 L0 l. j' Q7 \
} catch (Exception e) {" D2 y2 n' G( x6 H: Z* E
System.err.println ("Exception stepRule: " + e.getMessage ());7 Y' B; q1 [% h( m. U. ^8 W
}. g9 P5 T1 s; Z# {7 P+ Y
# D% U% n4 g, d
try {8 |4 L& F1 J; p
Heatbug proto = (Heatbug) heatbugList.get (0);& d$ Q% r2 u% ^( I2 u
Selector sel = . @4 ?8 B0 P P: x; ^- @* A3 m
new Selector (proto.getClass (), "heatbugStep", false);# i/ t7 g0 D% g0 _% f
actionForEach =7 l! d/ R4 U+ _' B8 \6 u3 O
modelActions.createFActionForEachHomogeneous$call* i1 q. ?/ K. Z7 J" G
(heatbugList,
9 k3 I {2 T( N; ~( w z% c4 g& O new FCallImpl (this, proto, sel,
j7 }5 _( U3 Y4 v, | new FArgumentsImpl (this, sel)));
8 ]% I4 e& ?0 x+ d } catch (Exception e) {- n5 m! ]3 E3 r8 a0 e' ~" V( \
e.printStackTrace (System.err);
0 R5 u4 E9 N) a$ n' [! Y }
6 c. {. T; ?4 ^* T
' a: U0 y# [% C5 n2 b% I syncUpdateOrder ();
9 s. b0 H( x8 D5 V% B. e/ [
% w, T5 w: b. F try {+ k4 ^7 {- A( a! [* G4 [* u7 H
modelActions.createActionTo$message g8 ]* X5 B0 O2 p5 c+ @
(heat, new Selector (heat.getClass (), "updateLattice", false));
" ~# g2 {+ R) k" ?( D3 r } catch (Exception e) {( w- G h$ r! e( e" t
System.err.println("Exception updateLattice: " + e.getMessage ());) [8 ?. C( Q: g* O% R" P* k* l
}
1 L+ Z+ l) Q" Z; Y. |
* t; A# Z1 c! y0 _0 B# J4 P0 D) x // Then we create a schedule that executes the
+ N7 y# ~* k" ^4 v5 G3 y8 t: q& E // modelActions. modelActions is an ActionGroup, by itself it
( H' F p. W2 z! ]2 k3 I // has no notion of time. In order to have it executed in
0 ~7 O* Z! X( S6 d // time, we create a Schedule that says to use the
8 A1 x* x6 W" V9 \0 A( I" W/ V // modelActions ActionGroup at particular times. This& Y, }6 u% k7 H4 p: R
// schedule has a repeat interval of 1, it will loop every
3 g4 V& W# [+ e5 g( D' t4 } // time step. The action is executed at time 0 relative to
* F& D5 O0 W+ c // the beginning of the loop.
4 \- }8 @: M7 ?7 R3 n$ h9 U
# `) z5 q5 a f, } // This is a simple schedule, with only one action that is- @) U. w$ T# L" i. j5 k2 s; p
// just repeated every time. See jmousetrap for more" p# G$ R! w9 ~
// complicated schedules.) w8 G' {* Q7 x Q" x& B
) s, e* _! h( t& }. y
modelSchedule = new ScheduleImpl (getZone (), 1);
1 ^: \" i4 ^9 ~ modelSchedule.at$createAction (0, modelActions);
u: U! e2 d; n" W7 r3 J! I ( K7 J1 a+ T1 ]) k/ G7 q
return this;
( c9 I7 Z1 S4 Q! ?4 S' b. D' o8 e } |