HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. M" @& y# l; s7 B' s
; ~+ ~6 h% u; s: y& b: i public Object buildActions () {
: ^! `: m. P( f, }9 {, u6 s/ G8 B$ ^ super.buildActions();; ^% j# u, Z+ |0 n; F
3 F. [. G: e$ i) o% [" Y // Create the list of simulation actions. We put these in
. P$ I2 D! B0 t" y: l+ t( L7 E% W // an action group, because we want these actions to be* \% C3 M Z. U( J2 b
// executed in a specific order, but these steps should* ~% b( d& J2 b( b1 O8 L
// take no (simulated) time. The M(foo) means "The message) M6 L6 H& [# l; m
// called <foo>". You can send a message To a particular
" k7 f5 m3 u1 B) T // object, or ForEach object in a collection.
^% h- @1 W _ , X# Y. r8 g7 t: m5 x4 F
// Note we update the heatspace in two phases: first run
% D' P! T9 F+ X& ]! S* |% Q // diffusion, then run "updateWorld" to actually enact the6 x7 Q. f5 t# U) ^3 Z8 M- ^. k
// changes the heatbugs have made. The ordering here is
9 h4 P% j! y# _, u3 H" ^# K, Q // significant!
7 `" S9 b9 s7 g2 w1 U & U& D E3 k I
// Note also, that with the additional: G$ d5 \, K6 \" F7 V1 M
// `randomizeHeatbugUpdateOrder' Boolean flag we can
- c4 I' K6 n5 l // randomize the order in which the bugs actually run
, k9 i. I4 e4 M, Y // their step rule. This has the effect of removing any
; N! k6 b; I8 z* u: J* _1 F // systematic bias in the iteration throught the heatbug
/ y; `3 X `5 B9 T6 d! n$ I: q% E // list from timestep to timestep% }; E* ~& J1 R4 t
6 h% _% K A% B1 `/ b5 x4 t1 y // By default, all `createActionForEach' modelActions have4 r$ _: {' O- _' @
// a default order of `Sequential', which means that the
% }# ?3 M1 h! i' ~; n // order of iteration through the `heatbugList' will be
7 [9 w2 f( C5 n* g6 p8 ] // identical (assuming the list order is not changed
) ]3 p) q8 h# i7 k S4 I8 ? // indirectly by some other process).
& f+ G/ K- U6 {# k
" o. |3 h+ h* n( H) a; y; Y9 l modelActions = new ActionGroupImpl (getZone ());
5 E# u6 ?( x' [; G4 U
/ `9 f' n1 @' S' J' [ try {
) v q+ L# A. v/ }6 m modelActions.createActionTo$message
& r- J0 d7 s: A- q (heat, new Selector (heat.getClass (), "stepRule", false));
4 ~" P) @+ P |2 ] } catch (Exception e) {2 M, L9 y# A" O' [; ~
System.err.println ("Exception stepRule: " + e.getMessage ());
' o: y. e5 \0 P+ A7 T6 [% I }
7 M1 H0 e8 g4 l4 Y0 Q' ^5 g; O/ X1 {1 U6 F' e$ N# k, m# ], V
try {
5 }2 `4 O8 o/ Z$ E! W$ g Heatbug proto = (Heatbug) heatbugList.get (0);/ [$ R% f4 l* Q0 g# a, L
Selector sel = + H1 u5 U, W @# f, g; M6 c& C
new Selector (proto.getClass (), "heatbugStep", false);
- z0 a: Q: |1 ?# j* |$ I' _; f actionForEach =, e; P5 x9 ~4 X N# U& b. n
modelActions.createFActionForEachHomogeneous$call
5 `/ A f* l: d1 w% b G (heatbugList,
5 p9 E/ N ^7 \+ d0 n5 Q new FCallImpl (this, proto, sel,9 ^' K) l! {7 T! F; m6 ?
new FArgumentsImpl (this, sel))); p- s9 F# }9 N+ n. Q
} catch (Exception e) {
8 f! @- l: x! W; }, A9 W e.printStackTrace (System.err);
" a7 g @% x, |0 w& G# l }. w p! u, b* F7 ]& R
% {9 x7 N* l3 D7 o2 M; D) a syncUpdateOrder ();
, W4 `) p& i7 Z% t% p9 R+ n: G* M
+ v2 D1 T8 o9 B/ H# m5 H try {
+ |9 c, F0 i! S( [" }) Z3 C modelActions.createActionTo$message % }- O" W2 L$ D+ @1 R9 ~" g
(heat, new Selector (heat.getClass (), "updateLattice", false));
) R& I( \* g7 b- \4 Z- B } catch (Exception e) {
$ z- Q$ Z! k" ? System.err.println("Exception updateLattice: " + e.getMessage ());
! G5 T5 H. |! [( ^8 y8 D }# E$ U/ p8 V. u# H+ w- R# q r
+ W* U- b H: h% h // Then we create a schedule that executes the
- x' j m1 B9 I // modelActions. modelActions is an ActionGroup, by itself it
7 ]% T3 ~5 A2 I3 W; H/ m // has no notion of time. In order to have it executed in
6 K2 J; T# `0 Q. C$ _$ M // time, we create a Schedule that says to use the7 p- c/ \% }( s# A& U) }7 I) x
// modelActions ActionGroup at particular times. This3 B# e0 a, i1 @1 r
// schedule has a repeat interval of 1, it will loop every
' i; o" }' O& F6 C& Z7 F // time step. The action is executed at time 0 relative to
5 u3 t0 z X; R1 y( W. _% ~- d // the beginning of the loop.1 A. I4 \/ S# V8 }; s- ]
$ w8 w! }0 r+ v( C% b
// This is a simple schedule, with only one action that is- P" U3 D$ x, M: c' ]! ^- z
// just repeated every time. See jmousetrap for more
0 W+ X4 y$ Q* g$ \' b4 k // complicated schedules.
8 \. ~6 ?0 o( t) z4 Q, C
4 I) U' n4 }9 u ~0 ? modelSchedule = new ScheduleImpl (getZone (), 1);& b' r/ [/ B3 P- |$ X3 j* ~
modelSchedule.at$createAction (0, modelActions);
. y) _! x+ \! u! [% m8 R2 \ - ?* a% g7 @) U" H; t: u( g x4 w
return this;+ o" k# L$ f( N2 v1 M0 i+ x
} |