HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" b) |0 O6 P* H5 W3 ~& l# |) D
$ A4 y0 Q# |3 G5 q# i$ Z$ n public Object buildActions () {7 T/ v; C ^4 Y; D+ l
super.buildActions();. c" y. f( A1 C$ X% Y' P
( S% S+ H0 a4 J9 b0 Q2 D // Create the list of simulation actions. We put these in
/ M/ V/ ^+ H. e# f" o // an action group, because we want these actions to be
0 N# M0 T1 U* w9 E- w // executed in a specific order, but these steps should1 i" V; @& |5 _; J; A d
// take no (simulated) time. The M(foo) means "The message$ k/ k/ i- f7 m: M7 Z
// called <foo>". You can send a message To a particular4 F0 ?" n% p& w, @
// object, or ForEach object in a collection.5 ^ C Q) a9 G9 c$ ]" B9 K, m- h
, f( s" r3 J) a" H+ }! T6 l4 ^/ p
// Note we update the heatspace in two phases: first run( ^0 B* i" i7 u+ Y6 B# R V
// diffusion, then run "updateWorld" to actually enact the
$ J8 m6 k1 t* r) u8 E- K // changes the heatbugs have made. The ordering here is
W% r5 R( ~7 }& l% b // significant!2 O! S$ W& N6 U1 T- {% a$ F
( R, A: J& e- a- [, I // Note also, that with the additional7 Y! `2 W H6 p0 f" x0 v
// `randomizeHeatbugUpdateOrder' Boolean flag we can
1 u9 a, j; h+ r! {1 S // randomize the order in which the bugs actually run) w1 }- O4 C: F
// their step rule. This has the effect of removing any7 ?/ O% C3 G2 ~
// systematic bias in the iteration throught the heatbug2 y0 V" h; d9 W+ B; z
// list from timestep to timestep' N5 m) ~( ^! ^
0 \( e$ R6 _/ N // By default, all `createActionForEach' modelActions have
: t$ `' B8 u, h* [" k // a default order of `Sequential', which means that the
7 J# A" k$ o5 W% i7 h! n! R) [ // order of iteration through the `heatbugList' will be. f% |5 ]+ }& _* }% S1 ^8 v- K
// identical (assuming the list order is not changed( ~" g+ f! t4 ~) f
// indirectly by some other process).
% D" m0 ~0 K6 k
' F) A# C) X# U" e7 F1 o modelActions = new ActionGroupImpl (getZone ());
2 M) l2 ]- N: G' C2 e3 a0 M
# I/ I% c( t* z try {- m' w6 ]' B- e e! `+ T4 g, @' _1 t
modelActions.createActionTo$message
8 S1 P6 {6 g5 J" O4 S# o7 I (heat, new Selector (heat.getClass (), "stepRule", false));' F, m: p3 O( K4 ]; z
} catch (Exception e) {
8 Z& C% `) z$ ` System.err.println ("Exception stepRule: " + e.getMessage ());
" ?' C' W e2 s' [ }+ ~, q% L: L1 h+ K0 H2 @/ b$ M+ e4 k
2 p H+ ^% u4 q+ m* g try {
7 s, Q' u! k M+ j" f Heatbug proto = (Heatbug) heatbugList.get (0);
( p" n: o- r, k% X# I4 f Selector sel = 2 o- D, n. a: H6 W& Q5 k0 \
new Selector (proto.getClass (), "heatbugStep", false);9 o" t" p* H+ ]9 u6 v$ s
actionForEach =
3 E9 i( e3 c$ F6 }( U# A modelActions.createFActionForEachHomogeneous$call$ O) w1 N9 N9 A1 t
(heatbugList,$ [- B1 Z2 x. S) i
new FCallImpl (this, proto, sel,9 u6 i; _' q: W" z% Y) V" e( y
new FArgumentsImpl (this, sel)));& n f" B# L; n6 _
} catch (Exception e) {/ r9 L9 y0 H& o# `1 ~6 v3 s
e.printStackTrace (System.err);
+ u! z% @$ Y$ R7 C2 d }
& x1 d! _+ x7 v& n
2 c4 O# h6 y* h. i syncUpdateOrder ();- Y% f) H, X6 a9 j3 I# u
; c# @2 \ V8 f6 S+ b9 m, I% W7 \ try {* R- }2 b/ }8 t3 e# d- o) h
modelActions.createActionTo$message ! j" P7 A; z( W [
(heat, new Selector (heat.getClass (), "updateLattice", false));& Q5 z0 m, u2 p n+ r+ Y9 W0 U5 M
} catch (Exception e) {
& s. O; P- }6 Z5 m0 A System.err.println("Exception updateLattice: " + e.getMessage ());
0 m" G! X6 B n% a }
- D6 G! ?+ e, y- z2 D# ]+ @
: C; [9 C. y+ ? // Then we create a schedule that executes the
& i$ Z+ b2 X0 a8 d6 N' ]( N0 V7 k // modelActions. modelActions is an ActionGroup, by itself it4 k) `" e5 h, O7 k4 q& ^* n
// has no notion of time. In order to have it executed in4 u5 S/ h" `& U# B# C! w- T
// time, we create a Schedule that says to use the
9 q' L' M+ C" y // modelActions ActionGroup at particular times. This4 F$ t Y# m ~5 f/ `
// schedule has a repeat interval of 1, it will loop every
' G* p7 I$ Q( }. c. Y/ Z; R // time step. The action is executed at time 0 relative to
/ c$ H) v# u3 D" E& U // the beginning of the loop.
4 ]. }. L. [0 n
* h! ], A5 F2 h9 d! }4 t' s+ c- d, a0 v // This is a simple schedule, with only one action that is
7 R# ?4 s8 L3 ?+ K0 l // just repeated every time. See jmousetrap for more+ A9 P! \" j1 k. b
// complicated schedules.- r& t4 Z8 i! G- A7 p7 C* {( A
7 s' _8 u0 b* Q: S- c, Q
modelSchedule = new ScheduleImpl (getZone (), 1);
, |0 C; a$ L0 f' v modelSchedule.at$createAction (0, modelActions);! O! o- a }5 O4 l+ @
. Y% b% v( M2 y& ]7 f* Y, j
return this;$ M9 Z: N9 f2 t& r. H6 c) O+ C1 L
} |