HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
5 }" B9 W0 R; W5 |: {" N
9 c2 c. J! N4 T" x! L% Z public Object buildActions () {: X1 g! D% D7 p8 K5 {
super.buildActions();# M u3 }) W0 k7 N, Y2 ]% i
" Y/ S0 |7 f: a% T
// Create the list of simulation actions. We put these in7 m' A( N6 \: [0 |: \$ \
// an action group, because we want these actions to be9 s8 U: U8 ~+ [3 T6 ^6 A8 e, g
// executed in a specific order, but these steps should" ?! {2 D% y# }( r
// take no (simulated) time. The M(foo) means "The message7 i6 E" Q- d8 a: D& S. D; w3 q
// called <foo>". You can send a message To a particular3 r2 M$ w9 _. r" M1 |5 h9 ?
// object, or ForEach object in a collection.- y3 R1 o* I: R# h0 q
# `& Z7 Q, J! H // Note we update the heatspace in two phases: first run/ m& ~: S3 Q, A
// diffusion, then run "updateWorld" to actually enact the6 A$ i4 r" F, Y5 ]% p* F) C
// changes the heatbugs have made. The ordering here is6 _: a7 c* F4 j- Y1 X
// significant!
& }; |( I& f* Y" h% b
2 [1 b1 \# i. ~, o( b // Note also, that with the additional: Z6 U) K7 p; A& ]* q$ C
// `randomizeHeatbugUpdateOrder' Boolean flag we can% E7 j" c- _3 J, C" C1 W
// randomize the order in which the bugs actually run
% y$ e6 p. p6 P$ |9 ^ // their step rule. This has the effect of removing any
, a- m! \' z0 I) o4 C! _: q: K // systematic bias in the iteration throught the heatbug; _, U* {& k" G) z
// list from timestep to timestep1 X- t1 }) u. W& o" D* A
7 L" k. U% _9 |! A% O; M
// By default, all `createActionForEach' modelActions have
( V; I1 r% k4 y0 s" K2 w# ` // a default order of `Sequential', which means that the
4 s5 t5 X2 s. z0 d* e: f+ t // order of iteration through the `heatbugList' will be
' A' ?' K y) ] // identical (assuming the list order is not changed
8 o, J/ I; C. a& z( x- C& c7 ] // indirectly by some other process).
0 f& W) N" U) X' s8 h' p 3 g' {& w% y; p! ~+ j
modelActions = new ActionGroupImpl (getZone ());
5 n9 D+ m8 x* K2 w* Z6 r# M" _; P, |0 D, A7 I$ f) q% A
try {3 G* |8 ?% m' [+ V& }: l
modelActions.createActionTo$message/ D# p* i: H; y- b
(heat, new Selector (heat.getClass (), "stepRule", false));
" |) x& K5 [% n } catch (Exception e) {) ^) d/ q! ~5 [: i% S; A( g. |2 Q
System.err.println ("Exception stepRule: " + e.getMessage ());7 A1 ?1 O8 b- s. A
}
3 D- K' i' @8 T/ ?- v3 b
1 z( u9 Q0 s* v+ \, u& l+ `7 u# e try {
3 U" t/ S; D5 W. ^- {$ Z S- f Heatbug proto = (Heatbug) heatbugList.get (0);
5 w# @- ?5 W1 [8 `4 B' L9 }: R9 G! ] Selector sel =
$ A, a+ o6 c2 c' G: S new Selector (proto.getClass (), "heatbugStep", false);7 C. |8 k0 B+ F2 L5 V `
actionForEach =
' t s0 S* A$ T" h modelActions.createFActionForEachHomogeneous$call
+ j. K- o$ ]! X% Q' S3 C6 ~ (heatbugList,0 A, U9 Q8 e0 A# M
new FCallImpl (this, proto, sel,
0 A2 f4 r* [, Y: X; \$ J4 x new FArgumentsImpl (this, sel)));' m, b8 |* y. r8 {' H6 c# C i
} catch (Exception e) {2 d! ]' {% Q. h- s3 c1 y
e.printStackTrace (System.err);
8 |- x% i$ E& ?0 z0 [' r }
6 K* n4 d. O; [. ] : R( t, u {1 j& s
syncUpdateOrder ();
8 D) I$ T# N) U8 r! o! `7 K$ }3 L
try {( F: C; g1 Y# s# ]& g4 P- }
modelActions.createActionTo$message
$ G a `2 s- t, o7 m- @' y% j (heat, new Selector (heat.getClass (), "updateLattice", false));
0 _0 h% Y3 u" z7 z0 R } catch (Exception e) {
; b; j2 A/ \8 U# b& Z System.err.println("Exception updateLattice: " + e.getMessage ());
0 w; o }1 s" I9 K+ \1 q } R$ N2 J. d0 T; |' n2 A% Z
8 L! P- F& \: ?+ e3 ^& w6 p // Then we create a schedule that executes the
3 g' {% u5 b" g; n // modelActions. modelActions is an ActionGroup, by itself it
6 y/ ~! E' Z) i // has no notion of time. In order to have it executed in
3 }5 A, n! c0 G6 Z& X7 ~' w8 S0 n // time, we create a Schedule that says to use the4 ~( V- j; @5 }" d9 g# _; e6 R7 q* ^7 {
// modelActions ActionGroup at particular times. This
. I5 i7 [! X& _& w // schedule has a repeat interval of 1, it will loop every
9 D: J# u. J6 }& g+ h$ [ // time step. The action is executed at time 0 relative to. R1 F J' l8 d7 }) _8 E, X; w
// the beginning of the loop.: ^( @; Q4 [5 o8 v& k
" g8 ^! h: Y" C5 O0 G2 ~) C
// This is a simple schedule, with only one action that is/ O% t) y$ A3 L1 `
// just repeated every time. See jmousetrap for more( `& q- e. I: Z5 R/ y- I4 m; o
// complicated schedules.; l2 |& P) ^/ j% N
# x P4 _" P; f/ ^
modelSchedule = new ScheduleImpl (getZone (), 1);
. w; { _& S8 k: r. S1 _3 S modelSchedule.at$createAction (0, modelActions);
5 `* T$ D3 @0 I7 q( `) G- K
% x! z6 ~& t% P' L6 e# H4 o return this;8 n2 x2 `( R; A! Z; n- | y6 a# h
} |