HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% ~# ]! \% w8 W. |
5 T, s d' b! ?
public Object buildActions () {
. [7 z, H- R4 ^ super.buildActions();- N: F% O: H/ m4 D' I5 M& d
* s( o8 I2 Z* ^
// Create the list of simulation actions. We put these in
$ I# @8 L; X* b8 ^ // an action group, because we want these actions to be5 \8 b$ C8 Y, h+ U
// executed in a specific order, but these steps should1 a0 A7 _) ]. K, [( }& }. t
// take no (simulated) time. The M(foo) means "The message2 i x0 N8 i9 {1 r
// called <foo>". You can send a message To a particular& _0 {2 c& ]( `* E2 d
// object, or ForEach object in a collection.
$ C9 T4 {# _/ O7 g- ?% L - R' b4 e6 F( K! `" e* @9 d" H# V
// Note we update the heatspace in two phases: first run& Q' d+ E, ~9 Q' n$ J( F5 l
// diffusion, then run "updateWorld" to actually enact the
: x; I h V+ {8 h, ` // changes the heatbugs have made. The ordering here is; g" `9 j8 {9 E) o! h1 |* G" A
// significant!
" ]4 |( e/ X2 d* a
( v' l- Q) F$ v/ Z // Note also, that with the additional
8 p, X! s8 X- u // `randomizeHeatbugUpdateOrder' Boolean flag we can2 m5 g1 D8 b3 p4 @& h8 r: m7 A
// randomize the order in which the bugs actually run$ [% ]( q3 [$ J
// their step rule. This has the effect of removing any
9 N% M5 b$ n6 b5 G6 ^ // systematic bias in the iteration throught the heatbug
) k0 a# K$ g; y8 I1 y // list from timestep to timestep
4 i* p" k$ E8 Z) w [; C1 ^7 N 8 ~' [% m' A6 r6 }$ B% g
// By default, all `createActionForEach' modelActions have
. r; \/ q" `/ ?, C5 f6 | // a default order of `Sequential', which means that the7 @: G+ Z7 u5 ?, g* u
// order of iteration through the `heatbugList' will be
. M- r3 o- N- ]$ |# v9 U // identical (assuming the list order is not changed5 m6 h' U8 X. G6 t; v6 _- v3 m% u6 N" C
// indirectly by some other process).1 D1 |& ]3 t: f5 m F# A# [4 U
6 v) j% J4 }5 |! Z$ G4 K! `% |& C
modelActions = new ActionGroupImpl (getZone ());
0 g5 m O3 P4 w% [" \
; M' d4 a- ?4 P) A6 b/ g try {8 V5 ]: a1 ]! M1 |+ `
modelActions.createActionTo$message
0 g6 ?5 ]0 a. N; J2 d (heat, new Selector (heat.getClass (), "stepRule", false));
+ y$ s9 o( ]/ V: B4 A( `5 Z" b3 I! v } catch (Exception e) {& X! L# D: e9 P" @& q
System.err.println ("Exception stepRule: " + e.getMessage ());* n+ \+ C3 Y( X& O* |2 n
}5 u( o/ i; O8 O- B0 i
- |7 a5 h+ z* {$ o& g, m try {# Q+ c$ K- V1 r7 z4 o+ C
Heatbug proto = (Heatbug) heatbugList.get (0);) _ \3 `; _) M9 `
Selector sel = ; [1 v3 q! N+ z, q% b
new Selector (proto.getClass (), "heatbugStep", false);
* Y8 L! \' Y6 O. h1 f7 y5 a' w. B& B actionForEach =
: D# }: q6 i5 ` L( \ modelActions.createFActionForEachHomogeneous$call
3 g* w5 `; E, n8 J3 b7 P; l- u (heatbugList,- T$ ?5 g3 U) _3 }9 J" Y# T
new FCallImpl (this, proto, sel,
# k3 H& S" L x; p* e# }2 `6 O new FArgumentsImpl (this, sel)));
' v, ?$ _! o: Z4 Z, ] } catch (Exception e) {0 I$ O* w8 \& J
e.printStackTrace (System.err);8 k; z8 R% o+ N+ \
}4 E7 \% u* S1 ?$ ?1 I
1 D$ ?/ h$ S3 K' b" N E
syncUpdateOrder ();. k7 ^, V; o* F# A
5 e( c) T6 a7 d: z try {. Z9 M+ f9 M" l9 U, r$ _" G& A
modelActions.createActionTo$message 0 o: U4 N: A+ j) Q/ K3 q
(heat, new Selector (heat.getClass (), "updateLattice", false));* Q# R. q, a5 l! t" |, C1 Y. }" \
} catch (Exception e) {* X# l8 ^/ E/ ]7 @
System.err.println("Exception updateLattice: " + e.getMessage ());
. r2 L% K+ m6 j1 i% w# N }
4 e) u' H4 b t9 z. H; c C. G/ { $ }: Z) Z' d, a6 L- z. `' i+ A
// Then we create a schedule that executes the9 i6 D. N/ X7 F: q( I& U! t& k0 K
// modelActions. modelActions is an ActionGroup, by itself it7 N& E! O6 s6 }5 a- b/ l
// has no notion of time. In order to have it executed in
7 E" t& i: j, h2 N0 V6 C // time, we create a Schedule that says to use the: l9 W1 o+ ]1 ]" f
// modelActions ActionGroup at particular times. This7 d7 l. w" X# R; I! j: O; x7 f; U
// schedule has a repeat interval of 1, it will loop every4 ~& R, \) z; J4 j/ C
// time step. The action is executed at time 0 relative to
& L- o$ Q8 e. f4 @/ W j // the beginning of the loop.
0 p8 ^/ V; p5 [. Y' o( F c- I! o3 P9 E I' J3 [
// This is a simple schedule, with only one action that is
; [! z* w9 r: I* B8 _8 H // just repeated every time. See jmousetrap for more, I8 f7 k2 c7 K. A" D
// complicated schedules.& D7 B% @9 y8 c* D' A" f$ d R# F
6 C2 C! y; z: x N) B. n
modelSchedule = new ScheduleImpl (getZone (), 1);; E, v! w* ?. y( _3 T1 A
modelSchedule.at$createAction (0, modelActions);
! c Z# _# E6 E
( N/ ?' A8 B# ^6 J return this;, i1 k7 W" Z+ y1 h3 v; q" U# m
} |