HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
6 X" ?! a! P* ?; G" e/ I: F
% T) R2 Q2 ~# s6 P9 [ public Object buildActions () { ^8 \; {5 X* Z, @
super.buildActions();
( X# S8 Z- V$ K) e ' M L/ D& z+ N% e6 k/ B% q% _+ X
// Create the list of simulation actions. We put these in Q- l6 N! Q$ n) D1 p
// an action group, because we want these actions to be
2 |; @; w: Z( v: t$ j, u0 x // executed in a specific order, but these steps should
$ [# [- w2 d x1 d/ B' n- U // take no (simulated) time. The M(foo) means "The message
# t" w- f! i: c% q" g" F% U // called <foo>". You can send a message To a particular
1 Z6 B- Z: [, c$ {& [5 V // object, or ForEach object in a collection.
& ]& {4 g& r* U) M: u- p5 m6 q0 R& G 9 D+ q3 |: Y5 W6 E8 L
// Note we update the heatspace in two phases: first run
$ n( k8 o( }, D2 Z1 S2 R$ u6 ` // diffusion, then run "updateWorld" to actually enact the
8 x! J/ ^$ ?; p* i! G' e0 L) V // changes the heatbugs have made. The ordering here is
9 a4 y/ Y: ]7 \' b // significant!6 d t5 m% c9 N( K7 U% ^
2 J' I1 \' S1 C1 m( D0 [ // Note also, that with the additional/ M0 L! [) y" @+ j
// `randomizeHeatbugUpdateOrder' Boolean flag we can
/ m) N9 i. x; g3 F* I // randomize the order in which the bugs actually run
* J6 r$ ]* u5 m* J // their step rule. This has the effect of removing any
2 |7 v! T( `' w% j! {" r // systematic bias in the iteration throught the heatbug
8 w% _7 G7 r; u2 w // list from timestep to timestep$ j u" v+ h9 w7 G2 _
1 Z. N1 e* @1 [; z) d. J0 i7 n // By default, all `createActionForEach' modelActions have9 f! J5 u" O/ D' ^+ Z
// a default order of `Sequential', which means that the
$ r0 x0 _" w! e& y o // order of iteration through the `heatbugList' will be2 K4 f5 v$ ?4 ?' k1 F
// identical (assuming the list order is not changed
8 j+ E2 _4 t& p3 A // indirectly by some other process).
0 w ^- b( i' ~
! }( y* d* W% R+ j modelActions = new ActionGroupImpl (getZone ());
/ m6 u' _6 Z$ ^7 {3 [. D! [
?7 u& B' n8 _: Z) G try {" x6 H% w& s* \
modelActions.createActionTo$message' Z& N+ {# s- V' U1 P
(heat, new Selector (heat.getClass (), "stepRule", false));
8 h1 i4 M3 v& o2 {; B; t6 D: t7 j } catch (Exception e) {+ S S; I3 X. z0 m
System.err.println ("Exception stepRule: " + e.getMessage ());4 n( I1 S4 B& S) [
}
* y% P# p: L1 z( k( Q* m8 D& T1 h! \+ U& {7 v
try {
6 y7 l; C& f/ N/ Q" F/ E' ] Heatbug proto = (Heatbug) heatbugList.get (0);( `, F+ v+ U# K0 J. h1 i3 V+ {
Selector sel = 5 |# B2 c5 \0 N8 p4 v( |
new Selector (proto.getClass (), "heatbugStep", false);! y7 _ B9 d! k
actionForEach =
# n, {. h3 | M% @ modelActions.createFActionForEachHomogeneous$call4 {' h$ F+ ^ o; \2 \! |* q
(heatbugList,
- p" d; X2 t( f! H new FCallImpl (this, proto, sel,( y+ O0 f- m- I1 D2 k
new FArgumentsImpl (this, sel)));% P* B0 Q' l1 m3 L7 ?/ w3 M
} catch (Exception e) {; S. k2 I P1 m j
e.printStackTrace (System.err);
7 A1 B9 M. V6 m, e/ C: H }
, V+ l" o3 n/ C! f! {. v2 c" o 0 ~# \( k& G# Q$ y6 W
syncUpdateOrder ();+ R5 W3 b6 ?& x X
. m' ]6 p% V) v! J
try {
9 i) w- J# M+ ?6 q$ u1 s modelActions.createActionTo$message
" g- G# |1 h. y$ S+ r, t. V (heat, new Selector (heat.getClass (), "updateLattice", false));
5 L! u" P& W6 j: g* r% c } catch (Exception e) {7 d' R" ]% `9 Q/ r ~4 j+ e/ _/ L! e
System.err.println("Exception updateLattice: " + e.getMessage ());
+ `' i- O* s6 A/ o0 O }
# l$ l# d4 N7 d8 X( F - V; k5 m3 F4 X H# k2 R
// Then we create a schedule that executes the
) b( L2 Y# e6 ~; ~7 @% k5 Z // modelActions. modelActions is an ActionGroup, by itself it% x. Q' a0 a: @7 w) D
// has no notion of time. In order to have it executed in- x; a2 C8 K' X! h& I- C9 v
// time, we create a Schedule that says to use the1 Z3 Z0 S1 b X1 W3 H$ L
// modelActions ActionGroup at particular times. This$ B/ z( Z( f& ]8 C2 V
// schedule has a repeat interval of 1, it will loop every Q v, y* N L* N t
// time step. The action is executed at time 0 relative to7 e% }1 r4 z- G/ B! j
// the beginning of the loop.
4 S* O5 w. Z( Y1 B& Z- G9 C+ H* Y7 J+ @
// This is a simple schedule, with only one action that is
/ `& n3 x) w! s: W' A // just repeated every time. See jmousetrap for more8 ]2 O' B) X/ A. Z
// complicated schedules.
p& ~( l$ r+ n* |
+ A8 g+ C* y' h% g5 s. k" y7 | modelSchedule = new ScheduleImpl (getZone (), 1);5 U/ w8 B+ z ~
modelSchedule.at$createAction (0, modelActions);
% M; v: f* K% _' S: R6 z( { ' {. L& z6 k* _
return this;& c# c* Z9 H' P: G( {' O
} |