HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
) O" z6 k" c5 k6 H0 b% x! @* S1 u2 o: A: H
public Object buildActions () {+ w* |) Q" g z1 K
super.buildActions();1 T1 w6 L* b" I+ ]& H" N+ y$ @
, J" e% M' m9 g# a* Y: ^ // Create the list of simulation actions. We put these in
* @+ R5 n# g( @ // an action group, because we want these actions to be
$ t0 G7 K8 G; r2 Y // executed in a specific order, but these steps should3 O( S% y3 J5 ~0 D
// take no (simulated) time. The M(foo) means "The message8 Z! ]0 H7 R% e: O$ |* N
// called <foo>". You can send a message To a particular
3 F4 A1 J! P% h+ t) m; ~( t- l // object, or ForEach object in a collection.% }) `+ o" W o# H
; {3 J' a! c0 | U8 s$ r% c/ P/ i // Note we update the heatspace in two phases: first run% F, G7 ^$ Z7 s2 u
// diffusion, then run "updateWorld" to actually enact the2 s; R- K, V; b1 f+ M: C+ j# R' z
// changes the heatbugs have made. The ordering here is
% r _' O; q' Z I/ R // significant!
' a+ q4 ^2 N, {, D2 M5 Y/ w / [) v) J$ {5 F+ c* z1 U# V0 [
// Note also, that with the additional: h! Z3 `5 e. @5 x
// `randomizeHeatbugUpdateOrder' Boolean flag we can
5 p) E% ~$ x7 a // randomize the order in which the bugs actually run
9 I0 |$ q+ z4 W# e // their step rule. This has the effect of removing any
: a/ E# N. E7 r6 D' A- I // systematic bias in the iteration throught the heatbug
# I( F# X# G! F2 |" L. o // list from timestep to timestep
* q: u* c5 D5 e- T1 Y / p8 s! f ?' Q! ^# ?7 N
// By default, all `createActionForEach' modelActions have
$ s, \5 x# a) T. w! | // a default order of `Sequential', which means that the" F6 q% U8 l8 z1 S
// order of iteration through the `heatbugList' will be; {" C+ n% \1 [0 X5 \! }( U2 F
// identical (assuming the list order is not changed
/ k1 R# Y5 p& C0 X G. \ // indirectly by some other process).; G7 m6 r( W) \( A3 k
- t8 w+ Q. D6 e/ A. g modelActions = new ActionGroupImpl (getZone ());- u' w, G' }2 R& Y) @/ J# s2 ]
" U0 p# r& R4 z; [5 b; I6 ~2 f
try {
8 T5 V: e7 N& W modelActions.createActionTo$message
0 ]6 t2 Z1 g. g (heat, new Selector (heat.getClass (), "stepRule", false));
& W: B. K3 X3 X, b7 D; a } catch (Exception e) {) y) }8 C6 Q Z2 T( H
System.err.println ("Exception stepRule: " + e.getMessage ());
% H* q/ f9 c0 m7 B* X9 F' U }
3 i4 U1 z6 e% S l/ D5 V7 W( u9 C; ~
try {% [. z. h/ D3 R; \/ _
Heatbug proto = (Heatbug) heatbugList.get (0);/ G6 |9 W0 a9 `+ q
Selector sel =
; m; j0 O C7 l" _" s6 \- {. m, v' o new Selector (proto.getClass (), "heatbugStep", false);5 N0 c' ~% g0 y1 T% Z2 x8 @! k
actionForEach =2 Z$ }% y8 U+ ~8 y9 Z, c( \
modelActions.createFActionForEachHomogeneous$call
8 l! d/ v& S- ?0 G" Y3 v/ x0 J (heatbugList,
2 x6 c; |$ V( W% Q: Z6 g new FCallImpl (this, proto, sel,
3 r6 K, `' F* @, X" r# G3 L d new FArgumentsImpl (this, sel))); J9 @( G7 L, W k- m! }
} catch (Exception e) {
0 H- w+ ]* O+ I9 b% @: ]6 s; T+ Y e.printStackTrace (System.err);
; r' A9 p* Z6 M- w }
( ^% `7 e8 W: V0 ?" e' R
) v, J3 C' {, R' @! N& `: ] syncUpdateOrder ();
- Y7 q; R; `+ R \; E
7 x z: M2 H: z$ ^/ `, H try { T( I# ?; K# g( M8 X* d
modelActions.createActionTo$message
7 `/ ?$ w! _1 C& Z, A (heat, new Selector (heat.getClass (), "updateLattice", false));( j, a% a; w8 t$ w/ b! R6 |: J
} catch (Exception e) {; c( H6 n7 n8 q+ N' b; Q1 U) L
System.err.println("Exception updateLattice: " + e.getMessage ());2 U% w- @& ]) G- [+ p% O# S. J
}) @. `- c8 y# K& T- ]( Z
" o! K2 ?3 W) H2 O c3 X# {
// Then we create a schedule that executes the t' k$ T0 S- B, A
// modelActions. modelActions is an ActionGroup, by itself it) M I4 S2 ?0 o% c, ~7 W$ ^
// has no notion of time. In order to have it executed in
0 U7 R D3 g) r$ o2 j // time, we create a Schedule that says to use the
* e1 ?9 U+ ^+ ^. @! \7 n // modelActions ActionGroup at particular times. This
% T% c' O$ [0 E0 z) s6 j // schedule has a repeat interval of 1, it will loop every
! Y. E0 m7 V! V8 ^ // time step. The action is executed at time 0 relative to3 o; X/ \, s, W. J
// the beginning of the loop.8 O; h% d) H. a/ R' x8 `& }
7 f/ a7 b! h4 _5 t- c
// This is a simple schedule, with only one action that is+ ^7 t1 J2 o. b1 D) ^- L7 L
// just repeated every time. See jmousetrap for more
. ?) i# I ^0 O& n+ N4 U // complicated schedules.- l1 K$ t0 p C$ v
. e* [' s$ [+ l% i modelSchedule = new ScheduleImpl (getZone (), 1);
, r6 y b+ M, U( f) V5 \9 ? modelSchedule.at$createAction (0, modelActions);
% z/ v% ]; [3 P! I X4 m( h) {
* T% F! j. b u l& j1 }( r return this;
0 D' ^9 \1 j. o& s; _+ k8 R' B- g5 y } |