HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:' r7 A: U. h3 n+ j1 E
; O e% q4 R5 |% [- ?5 g public Object buildActions () {+ V# X1 N0 U/ _9 z1 k
super.buildActions();2 [* m x" ]3 h3 N% f$ M( D
" U# T. Y! ]% b4 ]
// Create the list of simulation actions. We put these in4 A, m! |6 ?+ _$ q$ w4 i4 W1 f9 w
// an action group, because we want these actions to be! a- |& Y, g! _- B
// executed in a specific order, but these steps should6 c: {4 y* X$ H, d7 @& t# X% ?
// take no (simulated) time. The M(foo) means "The message
5 y: e" R z& Y6 @ // called <foo>". You can send a message To a particular9 I; O- M+ |6 E k. a+ {- h& h
// object, or ForEach object in a collection.2 w2 @6 J+ z P5 i
3 @) q- @) e/ O9 a! E( ` // Note we update the heatspace in two phases: first run5 U1 O1 A0 {9 n/ T
// diffusion, then run "updateWorld" to actually enact the
) B: W# R7 t, ?; D, v // changes the heatbugs have made. The ordering here is
% ]0 q" O& ^, m* Z // significant!* V1 R: | H& }) V3 y
" F) a9 Y; R# ?8 l: j- M8 }
// Note also, that with the additional
& k- Y$ I3 S2 a: } // `randomizeHeatbugUpdateOrder' Boolean flag we can
) m; I+ \2 S/ M // randomize the order in which the bugs actually run% P$ k* k6 h# I0 S. I; l
// their step rule. This has the effect of removing any2 ~6 o3 V' o( o1 v9 N& q
// systematic bias in the iteration throught the heatbug$ j- N8 F2 X, q
// list from timestep to timestep
- Z) v9 Y) d) \6 r9 J5 }& g+ X& i 9 S1 Q% r: l4 Z
// By default, all `createActionForEach' modelActions have% s* M/ U4 Z% C* m6 o# F
// a default order of `Sequential', which means that the8 Y/ s! S4 Y% \2 _
// order of iteration through the `heatbugList' will be( R5 K9 w5 `% ]
// identical (assuming the list order is not changed. s# K1 r+ T2 U1 ]* m8 L; @2 ^
// indirectly by some other process).7 x# B# B& t3 C! i3 M+ I a( p
- L. f8 P" x" k modelActions = new ActionGroupImpl (getZone ());! n, M& ?4 ?- P u" X
7 d% e& f! n: u2 F3 X: `* f( u- U; A( X try {
# ~) ]+ ?. e4 ~( ? modelActions.createActionTo$message) N% x# A. Z8 E0 D
(heat, new Selector (heat.getClass (), "stepRule", false));! x8 g, I; G' W- }" Z
} catch (Exception e) {) `4 t2 X( A4 c. n; j% c9 M2 b" n$ g
System.err.println ("Exception stepRule: " + e.getMessage ());& o. X# B3 l" \; d
}$ y, s' i, p$ q
7 g$ }" q% n* J" L; ?6 B3 j- d8 ~
try {
7 Y8 @3 r0 s- P; p4 ^5 w Heatbug proto = (Heatbug) heatbugList.get (0);
% P# v% e9 J& K Selector sel =
3 J V0 e3 ^# u, A& s# s2 _ new Selector (proto.getClass (), "heatbugStep", false);- J( h0 n$ K* P9 C3 b5 X
actionForEach =2 K& r5 B: \$ l( }
modelActions.createFActionForEachHomogeneous$call8 v) i6 Z" F2 k3 i" F$ I
(heatbugList,/ i$ Y D; [, X) H4 g* f" n a! ~
new FCallImpl (this, proto, sel,
5 A9 l- l$ \6 ]4 v new FArgumentsImpl (this, sel)));
5 P: Y I! d' ~3 ^3 t& z) n } catch (Exception e) {) o( r1 }/ w( |, p! G( L! T, x- o
e.printStackTrace (System.err);
% q: x6 x- Z; I, U' z# H }) s }6 }% Q) m- F; i$ z. ~
% n( w1 S( E3 g' J% [) e- B
syncUpdateOrder ();
) R1 N& G( A: A, k- d8 R+ t2 U# F9 K9 a
try {
3 F1 c" T, }: |- }- d1 i modelActions.createActionTo$message $ r, [4 d% n- y
(heat, new Selector (heat.getClass (), "updateLattice", false));
v+ v$ I7 ?8 r4 g* R8 r } catch (Exception e) {
* Q+ _9 c% A( m' \0 e5 v System.err.println("Exception updateLattice: " + e.getMessage ());
: E. l: p7 d. Q( N( M' ` } l# J; G1 R( R" x2 ]
& H/ k+ Z$ V }" @1 ]# n0 C. X( H // Then we create a schedule that executes the) c7 Z/ I& R+ \2 P' y$ J
// modelActions. modelActions is an ActionGroup, by itself it* {3 D8 N/ P7 [3 a
// has no notion of time. In order to have it executed in) L! S% |& m( b( {/ w
// time, we create a Schedule that says to use the
/ A m% p1 X: x t( N) o; d // modelActions ActionGroup at particular times. This
. M) g4 d4 ^: |- [) Q // schedule has a repeat interval of 1, it will loop every
$ Y1 j' c7 J% m8 Y+ q& x5 s' s5 E$ @7 F // time step. The action is executed at time 0 relative to% v) }$ o; [+ C4 _( P3 N' B
// the beginning of the loop.
3 J$ W' r n$ V9 `. o, B) q
& F8 L3 U2 {- }! D8 A) V // This is a simple schedule, with only one action that is5 b2 J+ b: U( J
// just repeated every time. See jmousetrap for more
" k. L4 u) g4 V: ~& r // complicated schedules.( i, z+ W7 J9 l; x- z
4 _2 K( A& ]" G9 }( I$ T$ g9 \- k
modelSchedule = new ScheduleImpl (getZone (), 1);
5 P6 R6 }# y9 Q" X! _0 U2 A) D modelSchedule.at$createAction (0, modelActions);5 f& {% R, J m& v
: X! |+ a& B7 F. y4 n return this;
' Q6 ^# K& \$ E1 d: o7 C } |