HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% {( M; y# t' u2 S/ U3 T* W+ @3 L% V/ v% A' V* `
public Object buildActions () {1 ~8 E% Z6 u& z# b
super.buildActions();6 G7 U( p+ u+ O$ L* V4 a
& m/ g8 M/ ]+ A8 P: [3 O+ a
// Create the list of simulation actions. We put these in
0 L. q5 h- t" \) d. s! ] // an action group, because we want these actions to be" R) w. g t) H- v. P: g
// executed in a specific order, but these steps should. ~; G1 [/ d5 f* y1 M
// take no (simulated) time. The M(foo) means "The message
3 _) j6 w4 L: S; ?% g# k/ h# b2 Y // called <foo>". You can send a message To a particular
l/ Q& ]. f/ m6 n( { // object, or ForEach object in a collection.
. X& V% E1 Q% n- w7 y$ `
4 g4 ]9 ?: O6 P: F; J' c# b // Note we update the heatspace in two phases: first run# t4 g1 i5 Z/ J# E c# Q+ a ?
// diffusion, then run "updateWorld" to actually enact the
3 i& [# Z8 U6 K' f5 Y) w- {- W // changes the heatbugs have made. The ordering here is' {5 H5 [1 d% P2 d4 v0 f
// significant!9 M9 N: g! x* N4 i" x$ O4 C6 D6 Y
; K) y$ ~& r( |$ S4 V6 E // Note also, that with the additional3 s, V- v, c4 K) N4 |3 F: g# k/ u5 z
// `randomizeHeatbugUpdateOrder' Boolean flag we can
3 d! h; G) X; d2 m' i% B // randomize the order in which the bugs actually run
6 H3 _1 g0 u& K' w9 o& C; u // their step rule. This has the effect of removing any4 b: _) O x5 R& K# e t" r$ u8 x
// systematic bias in the iteration throught the heatbug' W8 H# X: M% Q& p) B8 t$ Y6 N
// list from timestep to timestep- D' T6 p4 S: l8 A
" Q7 c' M+ [8 ]' `2 Q8 S. Y
// By default, all `createActionForEach' modelActions have
% F7 ?# a6 n1 n // a default order of `Sequential', which means that the
+ c5 V* R( p- |! P" y. z // order of iteration through the `heatbugList' will be
) H0 B$ l/ f2 G% J! _% ~ // identical (assuming the list order is not changed. b u9 D% o; A3 b' k& Q, Z
// indirectly by some other process).* S, ~8 @! ~% |) J% j' m+ u1 N2 Y
, x; [ k' w1 W6 ]
modelActions = new ActionGroupImpl (getZone ());
9 N. Y' x" C* Q" N" F b$ ^) h& Q# M3 b; T' z
try {. o0 c* b4 T) N9 ?; S: K; d: y
modelActions.createActionTo$message
7 n: b( g2 P" z! e- O8 U1 U (heat, new Selector (heat.getClass (), "stepRule", false));
8 x4 G8 O9 {) n: e3 m } catch (Exception e) {
7 g$ O: F' |. y( n6 Y ~0 x' L System.err.println ("Exception stepRule: " + e.getMessage ());0 G# R- X6 ^- E/ s
}
' f# w0 Z, I% {( c
; n2 m8 f- p+ @; k7 e try {
; X! v- X/ q9 p Heatbug proto = (Heatbug) heatbugList.get (0);2 N6 O4 a2 G4 A: M+ m
Selector sel = ; S% {' i) [% a) g* h N1 y1 x
new Selector (proto.getClass (), "heatbugStep", false);
& C, W: t0 \- e) H5 i e" J8 B actionForEach =
+ y9 l" n* D6 t3 p modelActions.createFActionForEachHomogeneous$call5 P% R& P0 e. M+ l
(heatbugList,; s# Y0 o1 N1 z8 ?
new FCallImpl (this, proto, sel,- y% u& D9 J* x% U+ i: }. n
new FArgumentsImpl (this, sel)));
5 B. I) R9 N3 x5 z. J* J. {/ a' N } catch (Exception e) {
' C9 R& K$ `; g3 Q* i* l e.printStackTrace (System.err);
! }) t- X* }/ S4 v }! F0 _2 S' x, C( G5 g; z8 t5 ?
$ A2 k/ ]1 @3 c$ F; p
syncUpdateOrder ();
( ]) y' F+ h! S3 C3 v2 w8 ]( }1 n) s: Q) w) r3 H r2 R% x
try {
- G0 Z8 U r1 ~( } `) ]! ~' V/ u modelActions.createActionTo$message
5 J7 e+ }! K9 G2 } P+ g! o" ` (heat, new Selector (heat.getClass (), "updateLattice", false));
$ A% p6 L) g. h, A' Y4 ]) m } catch (Exception e) {
* U9 u2 A: V0 n& q q; X System.err.println("Exception updateLattice: " + e.getMessage ());
- J( C& V+ ?; |, w t4 ^% k. G$ I }
7 ] t2 x- f4 \9 n0 n# T* X 4 `5 e, Y- O/ A3 A4 b- ~
// Then we create a schedule that executes the9 a2 T& N$ r1 `2 \7 w* E* N+ X
// modelActions. modelActions is an ActionGroup, by itself it
" e' |- ~3 x$ O5 P0 ^! t! Q // has no notion of time. In order to have it executed in4 B( B/ t) x% k3 e0 n! Y
// time, we create a Schedule that says to use the" `& f f% E& q5 H; n
// modelActions ActionGroup at particular times. This
) v# U( ^% N$ o9 W% s // schedule has a repeat interval of 1, it will loop every
' Z- v! j) g A // time step. The action is executed at time 0 relative to
( X& g* F$ O) z( s% M; `; o // the beginning of the loop.
Q1 Q0 D" t- d% z6 `1 {6 c) b1 |8 `
// This is a simple schedule, with only one action that is
- }! y6 j$ d* d- `& Q8 Q // just repeated every time. See jmousetrap for more
0 C0 c" |/ F2 ^& o( d // complicated schedules.' {0 d( y. K* r& _) c
' o5 o/ |; C7 o8 ]- I) H modelSchedule = new ScheduleImpl (getZone (), 1);+ G2 R7 t _$ g. t# k$ F
modelSchedule.at$createAction (0, modelActions);% I4 G* R" C- O F: }
; A+ p. G& H: t; | m return this;
- a3 v) y9 P6 b% k' A2 u } |