HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 x) o" z2 t6 B3 ~, r+ |$ D
* ]& p& D5 \5 o- m7 h6 y* g public Object buildActions () {
^7 E0 ]4 `, z: r* D1 m super.buildActions();
" ^, q! ^! C% n# p / |! g$ b9 j+ N, K2 U+ M$ \
// Create the list of simulation actions. We put these in
8 n9 {; k' ?; y2 k/ P // an action group, because we want these actions to be
& O. B( N, g& k# }# g, r // executed in a specific order, but these steps should
! k) w0 E1 e2 m // take no (simulated) time. The M(foo) means "The message. B, W9 V: V9 X9 e2 G( i `7 g" q
// called <foo>". You can send a message To a particular( C4 y! j1 ?# t, l( U- G
// object, or ForEach object in a collection.
- l( i) I9 C/ }# ~- U- i
n9 z1 k' Z6 R% j // Note we update the heatspace in two phases: first run
. w* W$ `, a; O- s // diffusion, then run "updateWorld" to actually enact the
4 H- S0 B$ }) c: L/ x b // changes the heatbugs have made. The ordering here is R* _$ [/ V* P
// significant!* P. L' |; v' ^% [; W6 X. E1 I8 g
2 S) d. K U; ]9 ]% U2 y2 @ // Note also, that with the additional
( A9 K* s7 g6 K4 B5 P // `randomizeHeatbugUpdateOrder' Boolean flag we can& k8 F2 f3 `& N& g
// randomize the order in which the bugs actually run
3 J+ ~ W0 O! f& D) ]% C // their step rule. This has the effect of removing any
) [& T' | e; j2 ]7 R // systematic bias in the iteration throught the heatbug9 C: ]4 O. {' B$ v+ Q
// list from timestep to timestep
4 \7 i9 f. d7 i/ g1 X; o( W
6 S7 A6 X. e7 d/ e9 } // By default, all `createActionForEach' modelActions have
' g! k- a9 O/ y% H7 `+ H5 @; G // a default order of `Sequential', which means that the
6 t* i3 a4 s5 c9 G* J. m // order of iteration through the `heatbugList' will be3 l* W; ]5 M, V
// identical (assuming the list order is not changed
% W( \ T$ o5 M8 _# X // indirectly by some other process).
% t# b' d% ?" T: u- p4 v( T
) R, o$ f7 }1 f0 p modelActions = new ActionGroupImpl (getZone ());5 M8 Z1 d* L( m3 G
2 @1 C( z t: D" W/ U try {% ~) j3 J% p- R4 U
modelActions.createActionTo$message+ [+ m2 z" P! c8 A# H: D" ?
(heat, new Selector (heat.getClass (), "stepRule", false));
3 L* q9 Q" o1 }0 }2 \4 X0 T } catch (Exception e) {
. L- q# Z* h- |; P" ^5 g0 c System.err.println ("Exception stepRule: " + e.getMessage ());" C. o) ~- ^. d6 n! p( {$ \6 ]( {8 x
}% w! u& E5 @" w* n! `- n% @6 G
) W$ [! l8 a; U; q( R5 r- g- P8 j7 M* i
try {$ b" v- d% ^; R
Heatbug proto = (Heatbug) heatbugList.get (0); o0 @* ]- |( g; o' D6 e& M
Selector sel =
0 D2 _) L- w5 v new Selector (proto.getClass (), "heatbugStep", false);
4 v9 V2 o% |& N4 i actionForEach =& R* l# b% F* @" @
modelActions.createFActionForEachHomogeneous$call
& X: K* w, a( l( Y (heatbugList,
2 N- S: E7 P$ H; f5 q new FCallImpl (this, proto, sel,
5 A2 H6 w( I4 F; p2 r' a new FArgumentsImpl (this, sel)));
* J6 U1 B9 t$ o% S/ o4 ]1 v } catch (Exception e) {
/ t* _1 V2 t) A. E3 v, b9 @3 G' i e.printStackTrace (System.err);
. f6 o0 s; V) b }" D" G9 X" ~+ I, V' p: Z w$ l8 U
5 t. L4 Y( G& L3 ^$ O1 r
syncUpdateOrder ();% x% W1 t/ O$ M9 U" K! I
2 E# N- c* H2 c$ ~0 \: g7 q! m try {1 `* H. V& {" K$ a! ^
modelActions.createActionTo$message
# z, T' G2 ]& G3 A0 n9 m% Z9 T (heat, new Selector (heat.getClass (), "updateLattice", false));
" Y" P" {* S: N- _ } catch (Exception e) {
! r* H3 E) H6 K% k4 X System.err.println("Exception updateLattice: " + e.getMessage ());+ g1 {$ z6 W/ g3 a! J% b
}1 t4 t5 J" z/ w
4 j ]5 @1 o J& n
// Then we create a schedule that executes the9 V2 S+ {3 i, k
// modelActions. modelActions is an ActionGroup, by itself it
, A! w' u/ m' e! ]8 w // has no notion of time. In order to have it executed in
2 f) i/ Y0 L( B+ g r, | // time, we create a Schedule that says to use the
- y7 e1 h) O9 H: T- b3 S // modelActions ActionGroup at particular times. This
: D* `, j: J* S5 H8 p# d7 L // schedule has a repeat interval of 1, it will loop every
- i) l7 V+ {7 N0 R // time step. The action is executed at time 0 relative to
) L. p& b% X( T. l- ^1 T4 y // the beginning of the loop.
) B! }0 b# Y; p/ s$ {/ e$ W
& w$ o" ~& s7 D( G) g; P8 S // This is a simple schedule, with only one action that is5 h$ S+ z3 r" Q1 W9 c
// just repeated every time. See jmousetrap for more
1 e6 H' M" n, Q // complicated schedules.: d" _# @1 C- A; \; K1 _
3 S3 V/ Y) ]9 O9 x" N/ U modelSchedule = new ScheduleImpl (getZone (), 1);2 s4 R, t) w7 {, s6 W
modelSchedule.at$createAction (0, modelActions);
- N4 X. v3 K, S : G4 e+ L- [$ {' h2 P
return this;
. G" \- j3 Y) z( Y( U } |