HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 z4 D# e4 S( @/ j" h" P% E
6 x& n% f0 i* x" T0 [! p/ t9 S
public Object buildActions () {
: ~9 d3 T- {% k9 c super.buildActions();# X/ d L9 t* Q% \3 I
2 M- H" G% G, E$ U9 v
// Create the list of simulation actions. We put these in+ ?# }' x% \. a/ |$ h! `
// an action group, because we want these actions to be, I ]( _: z& O6 h+ q% M! z2 w
// executed in a specific order, but these steps should; @2 a8 N% z* o7 B0 J
// take no (simulated) time. The M(foo) means "The message9 n I/ ?1 C- d7 ?4 K! n
// called <foo>". You can send a message To a particular# K4 V' x7 l5 v Z0 l4 t8 H7 h
// object, or ForEach object in a collection.
& m0 g5 C+ ?+ d2 f: q
6 `1 s/ d8 V0 Y) n: \: G& _: H // Note we update the heatspace in two phases: first run
: q! L) U+ e) {$ |; {9 ^; \6 J# t // diffusion, then run "updateWorld" to actually enact the
8 D8 Q, s" r* t // changes the heatbugs have made. The ordering here is
0 S2 x7 s8 {- v& w9 J l // significant!7 Y+ L) f- z _& F; O
9 m! ~. S. i3 Y. p, Q# D/ U
// Note also, that with the additional
. P7 o$ Z5 T/ [! n0 I! W) N // `randomizeHeatbugUpdateOrder' Boolean flag we can0 e! h! w u5 c, x3 l
// randomize the order in which the bugs actually run+ j I1 p! X; }$ ~, s( o* j+ [
// their step rule. This has the effect of removing any
2 v( d* I1 N# X3 A // systematic bias in the iteration throught the heatbug
' l0 l2 L- |( [* F, C // list from timestep to timestep9 l) D: e# D ^8 K
& `6 [; R. Z' C9 i3 S# g$ ?' b/ g // By default, all `createActionForEach' modelActions have
) H( k( e' X3 {3 Y ]8 L( { // a default order of `Sequential', which means that the
) o A- {# L- p5 M // order of iteration through the `heatbugList' will be
8 o( { W4 G5 T9 p; ~/ Q" W // identical (assuming the list order is not changed
8 _% \6 O5 y! L9 q" B // indirectly by some other process).
1 M& W M5 @$ M& E
# P1 N, K4 ]4 S u modelActions = new ActionGroupImpl (getZone ());
8 [6 d8 b! u# ]" h- Z0 T4 f* S; |- W* }- `# e( T* ~+ y" h
try {
$ I9 u6 u9 r# k5 D. U$ D! [ modelActions.createActionTo$message" \( x) i# `* v" k+ z
(heat, new Selector (heat.getClass (), "stepRule", false));$ w6 x f9 _! j" f$ A2 @$ u
} catch (Exception e) {
" o( Z* s. ^; e9 C9 K! i System.err.println ("Exception stepRule: " + e.getMessage ());
$ `/ `' D- U' I9 n4 n }7 @( K: v& n- M6 I% _# k
) d; M" P" Y4 Y1 Y8 J try {$ g3 [+ { B& Z
Heatbug proto = (Heatbug) heatbugList.get (0);
# T N( w' T( J3 q) C Selector sel = 7 ]8 F9 J! @* h9 O# O) }
new Selector (proto.getClass (), "heatbugStep", false);
( J$ i2 x$ x! M actionForEach =0 y$ N% W6 B) b* b1 K& a
modelActions.createFActionForEachHomogeneous$call
; d. ~1 N B6 b2 i, q (heatbugList,
Q8 H+ h6 ?# |/ K2 X9 j new FCallImpl (this, proto, sel,
; I4 d% ]9 x9 R9 V* n' h new FArgumentsImpl (this, sel)));5 l, i2 q. `" e& }
} catch (Exception e) {! V9 ^* H! G6 l8 J: r
e.printStackTrace (System.err);" t+ q; Y1 j2 A
}
g L4 U2 l: ~% t! H4 t! M% `
* S( L) {5 }3 v/ _7 _+ G' | syncUpdateOrder ();$ C7 U+ x6 { n: }( {1 P' C
6 z4 @- i6 J7 H try {- g) z# G- m2 k. y4 e6 W0 c. a. S
modelActions.createActionTo$message 2 [( F' G8 o; b, B g4 X6 \- v
(heat, new Selector (heat.getClass (), "updateLattice", false));
" C9 { w2 o+ v& a8 V } catch (Exception e) {# S" d5 ^# p# p9 S' r& D- P) L
System.err.println("Exception updateLattice: " + e.getMessage ());9 U7 G: u/ A3 y/ J2 l
}
% [. t' D8 }; k4 S0 p+ ` 3 }$ T/ m; @: S
// Then we create a schedule that executes the
+ Q& Y0 ^6 F/ I* B( Q6 c // modelActions. modelActions is an ActionGroup, by itself it
2 A3 M3 t/ C5 v: ?$ ? // has no notion of time. In order to have it executed in
9 @# D/ a9 G; F6 F* J0 n // time, we create a Schedule that says to use the
7 I0 v! b7 P' J- G/ L // modelActions ActionGroup at particular times. This2 b" \) N# N9 k1 Z$ a
// schedule has a repeat interval of 1, it will loop every9 F7 @) ]3 ]8 c3 j/ q! Z3 \& T& W
// time step. The action is executed at time 0 relative to
1 u$ @) W# k/ |$ J3 e& r" C3 i2 a // the beginning of the loop.) J9 z) Y& C4 s
+ I8 m0 N; V5 H+ H6 \ // This is a simple schedule, with only one action that is
7 M* k7 w! h% }9 ~ // just repeated every time. See jmousetrap for more) H U) d. X; H; {: \8 R! Y$ e# R' B
// complicated schedules.; l0 P- L% N( s- i" `! v
" t* [, ]5 P) {
modelSchedule = new ScheduleImpl (getZone (), 1);$ x" G) M6 g3 y/ w5 k
modelSchedule.at$createAction (0, modelActions);
( o4 p' z, D: q$ H
. Y, `+ H) N6 ]* [ @ return this;
% a* S- W8 i+ h; [" M+ a4 D } |