HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 u' Q, k7 v/ {
( J# a+ |0 q3 Q/ \ public Object buildActions () {' ]+ l; H- @+ @5 I% D1 Z- F: X
super.buildActions();
8 L8 b3 d0 {: C7 I' p+ w 2 G) r, y* H4 p& H
// Create the list of simulation actions. We put these in7 a2 j; Y1 I0 v: d; D* z
// an action group, because we want these actions to be
5 t8 M- d' W- M j# X6 Y // executed in a specific order, but these steps should: n2 u: R$ y* G( x
// take no (simulated) time. The M(foo) means "The message/ V0 q7 a. B. x- T& d
// called <foo>". You can send a message To a particular
8 ~8 ?/ e% r+ j U4 A // object, or ForEach object in a collection.
/ g. {+ o+ M) k3 l. e
: Y2 ?$ Q* v/ |! L4 D! m# q // Note we update the heatspace in two phases: first run0 W; a1 W( U; Y1 d" I2 o
// diffusion, then run "updateWorld" to actually enact the
4 I6 {9 I1 U: j; r' x // changes the heatbugs have made. The ordering here is9 Q. } T" t9 ~5 s7 M
// significant!
; A5 G# I' J& g7 A
+ T, w+ ^* a7 v // Note also, that with the additional. v1 y! V( N4 }- n5 F0 m
// `randomizeHeatbugUpdateOrder' Boolean flag we can
( L! j( r7 k/ c5 X. L- Z // randomize the order in which the bugs actually run! D5 A" r1 @5 i3 C2 t
// their step rule. This has the effect of removing any8 J$ w0 w& a- K/ y+ ?, V, K
// systematic bias in the iteration throught the heatbug9 G+ @5 k# v: s: i( b/ I) V- K
// list from timestep to timestep
" K% J0 ^: [9 m! o' n# r
$ A' c9 j+ I$ z( a4 B4 a/ t) E // By default, all `createActionForEach' modelActions have) @; Q! o1 o6 z( k- k- j
// a default order of `Sequential', which means that the
% u1 W9 k$ x$ X0 ? // order of iteration through the `heatbugList' will be2 b& ]% {8 r2 I$ Z( s+ a
// identical (assuming the list order is not changed
) \4 E1 }/ F% h/ y& ?- y! `5 G // indirectly by some other process).
' ^6 Q0 _' h9 k4 o# L
- d4 q u9 C( R' x4 \ modelActions = new ActionGroupImpl (getZone ());, V4 |7 E+ C2 S# V; g* z% u
2 G, {6 i* f' ~( a- |. y) x try {( B% C7 y/ |& c5 d
modelActions.createActionTo$message0 n( C7 T1 {, U3 W/ T0 \2 ^. F3 }
(heat, new Selector (heat.getClass (), "stepRule", false));
, i$ h. @$ A" {. t8 n } catch (Exception e) {
. E0 `& x* _7 [ d System.err.println ("Exception stepRule: " + e.getMessage ());0 W9 H+ |- I# S% Y
}3 {4 B* g$ I4 J7 C7 W" t: J! [# ~
6 X. o& Y0 d& z0 n1 r5 l
try {% w1 O7 X2 S: C
Heatbug proto = (Heatbug) heatbugList.get (0);
3 E3 L. H/ C+ S B% m/ ~1 e+ ? Selector sel =
; M6 n9 ^+ f! v) G% `4 Z new Selector (proto.getClass (), "heatbugStep", false);! r$ \1 W# Q1 [2 a1 `( ?6 |
actionForEach =
0 T) U3 Z; T) f2 J& I2 D# n modelActions.createFActionForEachHomogeneous$call
1 i! ]: M& U- {4 B, }. I4 k& E( F (heatbugList,
6 q" P3 ], |) I( }. Z3 F) \ ] new FCallImpl (this, proto, sel,& b( P, I8 B7 p0 T
new FArgumentsImpl (this, sel)));
- c/ s8 S6 Y8 J- ~6 Y } catch (Exception e) {
& u9 _: x+ T/ }+ ^7 X8 J5 A e.printStackTrace (System.err);7 D# i- C% b2 ?
}) ~$ V A6 p- m8 V0 O6 U
! H$ [, d: t0 V3 R* j
syncUpdateOrder ();6 s4 s [( t% b6 i+ J
# [# `% J/ `$ n0 k: y
try {
+ ^ N: D: w1 m/ Q! E1 J modelActions.createActionTo$message * P# M" p1 X* w( z! Y: Q$ F
(heat, new Selector (heat.getClass (), "updateLattice", false));9 j1 W9 T3 J; D' G
} catch (Exception e) {
2 g1 U& y$ |2 z8 Z+ M System.err.println("Exception updateLattice: " + e.getMessage ());
7 [1 O1 m2 h6 L }
0 B0 l% Y$ I2 h* f" D+ K$ l
) g5 j; ~' v8 `( A9 \8 G" Z // Then we create a schedule that executes the9 d$ {* C% {8 F8 |4 I
// modelActions. modelActions is an ActionGroup, by itself it2 c: Z6 \2 x. a& S9 C
// has no notion of time. In order to have it executed in
- i- J. o1 y% F* I // time, we create a Schedule that says to use the
5 L* e1 C& U' T! Q) `+ N! y- E // modelActions ActionGroup at particular times. This
5 w" ~8 p/ u, Z0 o) i // schedule has a repeat interval of 1, it will loop every7 h1 J1 ^$ A( H% `6 H/ P: E
// time step. The action is executed at time 0 relative to% Q7 D" P; L6 w8 R& ~
// the beginning of the loop.& d0 M; v1 k9 W" a3 r4 o
: ]8 g3 ]* u! a$ l z" ` // This is a simple schedule, with only one action that is# c; m3 t+ ~0 b$ `5 N% C
// just repeated every time. See jmousetrap for more
8 I( n/ J Q g // complicated schedules.: Q% t8 W3 m; p! |: Y
) ~; s# b% \6 z; ]* l
modelSchedule = new ScheduleImpl (getZone (), 1);( \8 ?0 _' x& f. H
modelSchedule.at$createAction (0, modelActions);3 L& @" N1 n9 N% u
. ^$ C+ `1 _' z& m2 D% O4 Z return this;! Q" V) s* B; t" g
} |