HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, K Q% Q3 a* P _$ }2 ^/ I4 p2 }8 |+ y
public Object buildActions () {
' H1 ]; Z0 o2 ^ super.buildActions();
3 I- O/ F) r& Y" N$ l1 G/ O
4 O( E- A4 F& u3 j' z$ k! u // Create the list of simulation actions. We put these in' z9 e/ Q& f% m0 W1 t
// an action group, because we want these actions to be: }, _* t$ t! c- t1 d
// executed in a specific order, but these steps should
. A# m: `* v# w2 _8 s // take no (simulated) time. The M(foo) means "The message
. i: [3 g- `1 Y( X0 g // called <foo>". You can send a message To a particular
* c9 e& r8 Z$ C! E7 @6 h! k // object, or ForEach object in a collection.4 p- Z- W% Z' C+ |4 T: @
: {+ X8 ]- @; c+ X6 u
// Note we update the heatspace in two phases: first run. D9 p0 R3 T3 @) k' [- J
// diffusion, then run "updateWorld" to actually enact the: v% u: z1 r: P1 f/ s* O. Z
// changes the heatbugs have made. The ordering here is2 w$ b. Y3 Q# L1 C6 _6 [
// significant!" X- {1 Q+ }( s" F
4 P: Y/ Z6 R! q; K" j% x
// Note also, that with the additional" J4 E$ E p5 ~. r0 X$ s
// `randomizeHeatbugUpdateOrder' Boolean flag we can
( z6 {7 ~9 ]9 i4 y3 Z6 [6 x // randomize the order in which the bugs actually run! d- f! }7 a+ G+ R2 t2 H% h
// their step rule. This has the effect of removing any
7 {+ j) H' V1 Z5 a // systematic bias in the iteration throught the heatbug
# W$ j1 P) F/ ^- R; }8 G // list from timestep to timestep2 L# w5 m/ |' L; i9 t3 u$ S3 ]) m- T% T' b
3 y) Z$ O/ N' K2 }8 h& ]4 d" U, B
// By default, all `createActionForEach' modelActions have
* k9 |! P$ l. G/ t3 [: _9 [ // a default order of `Sequential', which means that the0 { {4 O2 o: q
// order of iteration through the `heatbugList' will be
$ Y1 @6 _ m, C+ ^& C& n/ Z' Z // identical (assuming the list order is not changed: `4 G, G9 I. i" X% F+ L
// indirectly by some other process). t, D4 [$ T$ R2 e Q2 }
! w. ^3 B: h8 @# \ modelActions = new ActionGroupImpl (getZone ());
. F9 a0 }/ U' ?7 W% v3 \
1 X- {( N* ^" }, z! G* ~" p try {
& o; D8 c( g3 z! g modelActions.createActionTo$message$ N3 l" b. [" X9 `* k# M
(heat, new Selector (heat.getClass (), "stepRule", false));
2 z# a$ |: ~- t, i9 A } catch (Exception e) {
8 L/ W3 Y% E4 E' z; M% ~- z System.err.println ("Exception stepRule: " + e.getMessage ());! ?3 p# k3 w+ ?) h: m
}9 Y% F3 ?& V! l) E. Z& K
- C9 Q& M7 y# D, N( u6 x& C try {
' q7 i' e" ?$ k# v* g: j8 M; W3 A Heatbug proto = (Heatbug) heatbugList.get (0);' ]- h0 r8 l0 b1 ~4 w, y
Selector sel =
7 W, G" r6 K+ i: ^; R( U2 z M new Selector (proto.getClass (), "heatbugStep", false);
2 q$ _9 X2 M' C; u* i actionForEach =$ G0 M, N6 I5 f Z D
modelActions.createFActionForEachHomogeneous$call' ?7 K6 v( }% k* i& O
(heatbugList,
5 \! w3 v/ B3 h new FCallImpl (this, proto, sel,
* s/ I$ S0 x7 Y: e new FArgumentsImpl (this, sel)));
# k) Z/ k/ j ^* [ } catch (Exception e) {$ i+ m1 ]! D( E, C. g a8 V: ]
e.printStackTrace (System.err);' L2 H& |( `7 ~8 u0 X+ z1 i
}
' Z# V) O% h: M& ]+ q: G / w/ Q+ ^" p/ g) ~
syncUpdateOrder ();( H& c" Q) _/ Q. C7 F
- k' f6 j: l1 {5 y: N6 d try {
; v' K5 t' j4 l; T+ B6 D8 E# d0 e* @ modelActions.createActionTo$message
' E% H4 M: s T0 z, R (heat, new Selector (heat.getClass (), "updateLattice", false));! t: Q. `+ f: x
} catch (Exception e) {8 o. }& }8 ~7 X; p8 d& F
System.err.println("Exception updateLattice: " + e.getMessage ());
1 @ g# S+ \3 F# |' R% J }
- N" f5 {% g8 ?+ ~# E! T
' {+ |/ O7 ^' Z1 L0 J' E6 m5 q! N! p // Then we create a schedule that executes the: s; l( J6 P# k) } Y' C
// modelActions. modelActions is an ActionGroup, by itself it/ y& D1 U; y$ _2 F
// has no notion of time. In order to have it executed in
$ O" ?* P- C7 F2 D // time, we create a Schedule that says to use the: G/ M8 b0 X7 T0 G
// modelActions ActionGroup at particular times. This
: @7 X2 V- s8 Q9 Z! M5 X( f // schedule has a repeat interval of 1, it will loop every( }4 f' A8 e' Y9 m/ Q
// time step. The action is executed at time 0 relative to! B: C& _$ p5 e- J
// the beginning of the loop.0 J! |( l5 J3 F7 _. y7 ^0 L
' ~0 i3 j4 H$ R& n# H
// This is a simple schedule, with only one action that is
" `* m; G8 a, B1 p+ \/ o( _0 F // just repeated every time. See jmousetrap for more
% i! t4 q+ ^( S) ], I. G5 f // complicated schedules." L5 W( r' H6 n, T6 D
7 Q" t- v7 N0 i6 I1 P6 _$ k
modelSchedule = new ScheduleImpl (getZone (), 1);
, S7 P% N* T$ S4 | modelSchedule.at$createAction (0, modelActions);/ O! Y9 k0 }& E) `4 E w6 ?
8 L! i7 m" Y" u; i' `9 y
return this;
5 w# N" A. Y% z0 V8 U! v0 a9 Y } |