HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# |# H/ k0 f3 D" i# ?
$ R7 ?% s, i1 c4 F4 D& t+ R; { public Object buildActions () {
& q/ z% `3 V2 m1 w super.buildActions();- \# m% `$ p, f, A' e
) ~, S5 Z* ]) z$ _! M8 V8 ]
// Create the list of simulation actions. We put these in
) f' v/ p, y# K0 c0 ?* g: W" e // an action group, because we want these actions to be% \/ ~/ m' I3 V# O
// executed in a specific order, but these steps should6 v) \* w8 }# {4 P% q! t9 Y
// take no (simulated) time. The M(foo) means "The message, M/ R9 q4 _7 h& Y* w0 @" Z
// called <foo>". You can send a message To a particular
# F4 }& [2 z* o) X" i // object, or ForEach object in a collection. M' e+ l6 g8 F' w! r8 A5 x
3 n# H7 |6 t U% y* s6 J // Note we update the heatspace in two phases: first run! u" h+ |5 V+ x/ L
// diffusion, then run "updateWorld" to actually enact the
/ Y2 ?8 u/ p/ o7 I // changes the heatbugs have made. The ordering here is
0 R4 O9 c0 o% [; G; w. h; E // significant!. Y$ A5 F$ o) {, t8 k! L l
/ M8 W% q! R3 I) P: S! b/ I
// Note also, that with the additional2 ~. j( `9 P+ P' s
// `randomizeHeatbugUpdateOrder' Boolean flag we can p9 B7 s& p: f P& R2 a
// randomize the order in which the bugs actually run
: \6 f1 Q. \* k0 s( }9 I$ w% K% f // their step rule. This has the effect of removing any
0 M' ?! V# I* u( l( m // systematic bias in the iteration throught the heatbug) m, _' e0 f Q! @) V7 J c
// list from timestep to timestep
; v$ p. R& p$ y* Y2 {; T
. i; _( Y y1 K" c2 R3 S& n // By default, all `createActionForEach' modelActions have
* e! G9 F8 B# h$ m // a default order of `Sequential', which means that the
( ?5 t) s" }9 [) J3 k w8 S // order of iteration through the `heatbugList' will be4 o0 a, \) S$ i; J/ i1 M0 i& x
// identical (assuming the list order is not changed
8 X+ w- F9 u4 o, }/ F // indirectly by some other process).
$ w: h6 m* r1 t* a + ~+ |' I5 [2 Q6 A+ y& u1 o7 ~' Q$ N
modelActions = new ActionGroupImpl (getZone ());( s5 v, N8 J: C; T% ~+ W" N
; J4 L: Y3 `+ D try {
6 ~! F7 g% P9 t9 j# ?4 Y modelActions.createActionTo$message
0 ~4 w# j% U4 A% B4 x: P9 n (heat, new Selector (heat.getClass (), "stepRule", false));
8 h. z. F) W6 K( { } catch (Exception e) {0 |3 q8 ?$ R+ `1 k$ J5 |3 Y
System.err.println ("Exception stepRule: " + e.getMessage ());
, M Y+ ?1 G/ _7 M( j+ I }3 g$ _ w/ y _# [& d# k
. V# G! l; w' ]' J
try {
, l! C3 k+ i4 w$ ~, r Heatbug proto = (Heatbug) heatbugList.get (0);
( s+ g" u' d" @- g& v1 S Selector sel =
. r# ?: [# E/ h3 Y: V) G( O: O new Selector (proto.getClass (), "heatbugStep", false);& E5 N4 S! J( ^1 C' m1 Q4 F
actionForEach =
- x# s( u4 B2 p L" o6 m& f# V modelActions.createFActionForEachHomogeneous$call
2 ^- y9 |5 a. P/ @5 Z$ D" ? (heatbugList,: y. e+ E$ g. R8 ~- K
new FCallImpl (this, proto, sel,
+ d8 E0 S8 v: w0 Q" L1 @% P new FArgumentsImpl (this, sel)));
! Q9 m; n% z% Z) Q# k } catch (Exception e) {2 O9 X6 J0 f2 a1 d$ f" D; p
e.printStackTrace (System.err);
% I# f7 v0 F5 Y0 x }
; Q& \; s- E" R
( w4 f0 L6 M0 F- _6 o7 q/ n syncUpdateOrder ();
X2 H7 o/ o- d
0 R; f. A! x E$ i" d3 e7 a try {. O, Y; O: q( \0 g
modelActions.createActionTo$message
/ R; u; A& f4 F8 h (heat, new Selector (heat.getClass (), "updateLattice", false));* ]" ^; p2 h7 ?) U1 x: M- [
} catch (Exception e) {( P9 }0 r! x1 _0 [; y% ]* I8 D
System.err.println("Exception updateLattice: " + e.getMessage ()); y, ^- n9 }, D8 c1 q
}
7 H3 W; M( M. S: [- C* P j
) `7 ?, h! d9 N, h# y // Then we create a schedule that executes the
9 Q) F% C I1 B ~& B // modelActions. modelActions is an ActionGroup, by itself it2 h/ \) ^3 @% A3 `1 V" \
// has no notion of time. In order to have it executed in
* @- ` h) o$ n' R, j // time, we create a Schedule that says to use the3 p: f; C" M3 S" O% ?& M* E2 f
// modelActions ActionGroup at particular times. This) D% L/ `% p! b& z& P0 I1 N! ?
// schedule has a repeat interval of 1, it will loop every
1 n3 X c$ Y! A7 O# j0 m- v // time step. The action is executed at time 0 relative to6 n7 K; S6 `- J
// the beginning of the loop.
5 |4 w3 U+ j m4 u" p0 Y4 c- x z
, ^2 `2 M9 t' }8 F" a) P // This is a simple schedule, with only one action that is
4 X. |+ _+ B% q5 \2 Z) [ // just repeated every time. See jmousetrap for more1 o$ y) _4 J* M0 z' q; W7 L* l
// complicated schedules.' n& ?. S8 `2 V8 A
# ~( K5 r7 t- k9 `( G. Q
modelSchedule = new ScheduleImpl (getZone (), 1);
8 A5 o! B0 A( x' z- e modelSchedule.at$createAction (0, modelActions);
# S6 `3 K( e+ g! Z4 N0 T% H4 t
: S" j( @1 Q5 b' ]' c0 l' | return this;
8 E7 |+ e7 r; T6 L$ G" s } |