HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
2 S0 y+ E# l; P- V* C
' |& e1 y/ e. o! A public Object buildActions () {9 F: w0 p/ N8 ~4 v2 g* K. T
super.buildActions();2 R8 p" S4 l( A( a4 s5 C
1 J& d& d1 u; q9 J, ? // Create the list of simulation actions. We put these in z" ^+ H1 l( I
// an action group, because we want these actions to be
' f: l( Q7 [1 _# W% O/ W // executed in a specific order, but these steps should x+ }6 \6 o$ R
// take no (simulated) time. The M(foo) means "The message
9 K& |. S3 p! N# Y) L5 q/ t // called <foo>". You can send a message To a particular5 W/ H- x2 n2 s2 N t8 S
// object, or ForEach object in a collection.
5 G% q; p& u4 ?
# a5 A) y: q$ C U: Y3 S- r // Note we update the heatspace in two phases: first run- T5 r1 g( t @3 {9 |* F* {
// diffusion, then run "updateWorld" to actually enact the
: p3 c9 f3 |4 c6 D) l3 r& z // changes the heatbugs have made. The ordering here is
6 U% v, X* a( l // significant! Q; F$ }' x3 ]( i
* ~- s7 ^9 c) i* p
// Note also, that with the additional, o+ \/ I% X+ o) i2 e* {6 L. ?
// `randomizeHeatbugUpdateOrder' Boolean flag we can
t8 I( R( B& f2 D9 H3 }# m( D // randomize the order in which the bugs actually run
1 j' D, d8 I+ X // their step rule. This has the effect of removing any
" Q' [. e) V+ S // systematic bias in the iteration throught the heatbug
: V) d9 v0 a$ h, E8 b // list from timestep to timestep" P3 Z$ }- y& y: p8 V
) R* _7 V$ Q/ b' L; s" s2 X( _ // By default, all `createActionForEach' modelActions have
# H8 O- l+ e, u$ o // a default order of `Sequential', which means that the
4 d) v N: I0 [ // order of iteration through the `heatbugList' will be
0 c$ M) D. x& S0 {0 D, { // identical (assuming the list order is not changed1 [; ?5 I$ Z) o2 D3 }, f
// indirectly by some other process).) C( y& |' |$ s
4 ?, }0 l% s- g, N) I* R modelActions = new ActionGroupImpl (getZone ());
: \2 ~/ C% ]+ T. E2 K" B( M0 c* V0 g4 D
try {8 E* F. \! q% K; P
modelActions.createActionTo$message
# D4 @) [; T3 U, n+ J3 A* O (heat, new Selector (heat.getClass (), "stepRule", false));
2 U$ n% o3 Y& C& A( ~: t' d } catch (Exception e) {( w' _$ Q5 C5 G% n: i
System.err.println ("Exception stepRule: " + e.getMessage ());% i7 {) b1 ] n V. V! ~
}% R6 b3 M3 C/ I) \ N9 f
0 c4 \6 p6 ~" e; G) J! V try {$ w6 ]8 v5 b0 u/ ^! |
Heatbug proto = (Heatbug) heatbugList.get (0);
1 T; }6 I% r1 Z$ G" D% }/ L Selector sel = ; ~6 b4 y8 |, u8 R: n
new Selector (proto.getClass (), "heatbugStep", false);4 v' K t! N! v( p
actionForEach =
h! A y* r. n# d, I9 ` modelActions.createFActionForEachHomogeneous$call
3 `0 T! f* C1 F3 m1 F j (heatbugList,' Y3 d% C# I ?1 K
new FCallImpl (this, proto, sel," h/ `1 t4 F* m8 n
new FArgumentsImpl (this, sel)));
% |+ E. f- U" w } catch (Exception e) {/ ]" @9 ]5 v# y% h$ n
e.printStackTrace (System.err);1 y8 c6 S4 |+ G2 v7 y( M
}; a8 K4 A0 W J6 _
9 } G5 E i6 }8 `- H
syncUpdateOrder ();
, j) z! Z- `) f; ]: N" ?& f. o4 A5 f. i" Q0 w" H
try {
, j( @- }2 a( k5 r modelActions.createActionTo$message
! G4 K4 U6 s: D3 h1 e (heat, new Selector (heat.getClass (), "updateLattice", false));
$ l& u! r. n; X8 M } catch (Exception e) {
& @- R }2 O) p8 a* i System.err.println("Exception updateLattice: " + e.getMessage ());
5 f( t. `! x+ V; I5 | }- g; z5 s3 k# j: R' b- x" J; K
3 f+ W$ N6 f! r- \ // Then we create a schedule that executes the
, \* Z1 S9 l% Z5 Q; n; w. K; I // modelActions. modelActions is an ActionGroup, by itself it+ }) E6 ?- t( G, K2 ]3 G1 j
// has no notion of time. In order to have it executed in! i& Y% C9 ]4 b% Z
// time, we create a Schedule that says to use the
- U$ |' e; `' O. Y9 k+ S( F& F" \$ \ // modelActions ActionGroup at particular times. This
4 c6 n' p$ f9 d& o, } // schedule has a repeat interval of 1, it will loop every9 \& x3 s, B% h! l
// time step. The action is executed at time 0 relative to
9 f7 p! c6 U8 u, g3 Y0 p // the beginning of the loop., Q' O, j$ R& Q7 ^
6 w, V. y6 _6 Z( S! O0 R: I9 S
// This is a simple schedule, with only one action that is0 |3 \+ Z" Y, V" z& v
// just repeated every time. See jmousetrap for more
/ s1 O1 b* d) g6 m" J8 i& o0 m // complicated schedules.
) f* E' F* x3 j- K# z! h$ [
# ~ S+ [5 l" B$ Y/ v) l& l4 w* Y( ^ modelSchedule = new ScheduleImpl (getZone (), 1);
9 f v& a, f; K# ^4 Y* M modelSchedule.at$createAction (0, modelActions);' x8 t* K- l' c* A& q" u
8 { V: J1 }% z3 Z5 n: r6 d: P' v! }
return this;7 R0 ?0 @4 T. p* G7 ]
} |