HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:( X" s1 i9 h1 t0 D% u
0 h1 j2 R. h8 f9 W public Object buildActions () {. r$ O+ k$ {, u. `+ d' R
super.buildActions();
. E4 R9 p) G* \2 T3 G
- V- p$ x8 s. P8 h F0 j // Create the list of simulation actions. We put these in
; B" Q5 e7 L- K+ H4 r* @ // an action group, because we want these actions to be
8 a# ~) k6 Q0 E" i. | // executed in a specific order, but these steps should" ` l& q4 F0 a2 ]
// take no (simulated) time. The M(foo) means "The message
$ }, D) U9 ?1 G9 P4 w // called <foo>". You can send a message To a particular
2 f3 T: o0 S- A$ F) K6 f% ^9 M# y // object, or ForEach object in a collection.7 V4 O m3 s( w) _% L
; H5 m/ q5 X+ _" N0 d! w // Note we update the heatspace in two phases: first run- t: n2 E- J/ s* F! j
// diffusion, then run "updateWorld" to actually enact the
. J5 \$ a. J2 Y/ g3 O1 Y9 c8 b5 @ // changes the heatbugs have made. The ordering here is
F. A* S: o9 c/ X8 a3 N5 G // significant! a" V0 q' t6 B
" V4 s& _& u$ i( G8 c$ G // Note also, that with the additional C) u) k: `# G$ i% O7 b6 S) Y
// `randomizeHeatbugUpdateOrder' Boolean flag we can& C: r# f5 u4 m6 C7 y" |7 g2 U
// randomize the order in which the bugs actually run+ x R$ q- U+ ?% ?, v. R$ Y0 h
// their step rule. This has the effect of removing any
1 c+ q4 g# J8 I+ {- ~9 O6 X // systematic bias in the iteration throught the heatbug, q2 r* v% t/ ]: c* @. i
// list from timestep to timestep
( y8 E! r$ C f! B
7 e# H) l0 J+ f) \) I& n% q2 w6 O // By default, all `createActionForEach' modelActions have
% Z: V* R; T- e4 h# h3 K1 i$ S // a default order of `Sequential', which means that the
$ D5 b; d6 L( ^9 W* ]1 } // order of iteration through the `heatbugList' will be' `& o. e; v; u5 u
// identical (assuming the list order is not changed9 B, J3 g: \6 l7 s9 a
// indirectly by some other process).9 d2 W9 B* f2 s7 Z) J2 M& S" L, f
% Q" n% m5 T2 ^# b* } modelActions = new ActionGroupImpl (getZone ());
4 y! i4 O9 P2 p2 X5 G! Y4 i
2 A. m: X! k# o+ P3 {8 ? try {
" P# Y9 p0 F& B, E# V) k1 D modelActions.createActionTo$message4 I5 f! y5 O8 m U% x2 h. @
(heat, new Selector (heat.getClass (), "stepRule", false));2 b9 S: ]& M9 X5 R
} catch (Exception e) {
3 O- i$ N6 q$ E7 y7 B9 U% l System.err.println ("Exception stepRule: " + e.getMessage ());
: V; S" o3 ]% i+ y5 X }
7 W2 e- L: p' f# l! m: u7 ] Z& N$ V
x8 Y( S( f3 y try {: w; P) S3 y- ?' _) n( k
Heatbug proto = (Heatbug) heatbugList.get (0);
' W% c" w5 m+ k, j- y7 c3 Q Selector sel = $ Z F, o' X0 K
new Selector (proto.getClass (), "heatbugStep", false);
7 v! m* o/ c5 l8 z actionForEach =/ K% X- K) [+ z" R3 f
modelActions.createFActionForEachHomogeneous$call& t; V7 I; t/ V' B. Y7 h
(heatbugList,
+ Y* Z6 R( m' ~/ X+ z: O new FCallImpl (this, proto, sel,7 c* m) [- @8 a0 @2 Y# c8 j
new FArgumentsImpl (this, sel)));
V& P# G0 h6 o } catch (Exception e) { a, _1 b6 x( d' w
e.printStackTrace (System.err);1 V8 y+ [6 p! ^; d
}
. C3 i0 f7 h, `8 O' g
4 c4 c/ p+ O& h V* b syncUpdateOrder ();
' p. L3 j+ e1 ^! K4 G7 y; u- X1 P% I( M9 \6 ^* p
try {& V+ ^6 E F% h6 B; r8 q# g8 M
modelActions.createActionTo$message
. W2 M# Z) ?! b (heat, new Selector (heat.getClass (), "updateLattice", false));% M" C" ^' x y! {
} catch (Exception e) {
' c7 m0 w- X) a* f1 N System.err.println("Exception updateLattice: " + e.getMessage ());
8 j4 m3 ^9 {/ X, \$ Q }
1 U* |# P* c4 N4 _
X B& g0 E4 }4 { // Then we create a schedule that executes the
% a7 N0 ?7 P0 r3 p/ j // modelActions. modelActions is an ActionGroup, by itself it' n/ B! |/ ?% W( V
// has no notion of time. In order to have it executed in5 F5 g- d. B9 s' g8 ^
// time, we create a Schedule that says to use the3 h( F# T2 |& ^0 W0 F. G
// modelActions ActionGroup at particular times. This
) w" V9 V( Y! ]- z( j0 Y // schedule has a repeat interval of 1, it will loop every
$ ?3 a$ L' ?3 y3 o/ g! f* H // time step. The action is executed at time 0 relative to
- E. L4 b- E/ ` q; O // the beginning of the loop.
2 m% m$ }: \4 o( i. ~+ R% c; |" L, e: n4 b$ J* H7 {: R
// This is a simple schedule, with only one action that is2 M) L# R1 Y# z, D3 A, k$ W( l
// just repeated every time. See jmousetrap for more( x- x7 \$ `" o1 |
// complicated schedules.
5 F% e: {' C( E: w7 S
' G( t0 A" C6 N modelSchedule = new ScheduleImpl (getZone (), 1);7 ?* n9 M9 n* ]# s/ T8 ^% f
modelSchedule.at$createAction (0, modelActions);
1 h% X8 E# y5 Q6 h6 L
& ^+ g' w& ^) t4 u; o return this;
' L! q: j2 M# {" @, u } |