HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. M* S/ ~$ f0 W$ p% U, a& v3 ^. ?2 c' D" c6 R8 g' u
public Object buildActions () {
3 c% W9 S2 w3 `. l* e# u$ i5 O super.buildActions();) I# H1 n. U3 v
/ y" k& w P2 r2 u+ l
// Create the list of simulation actions. We put these in C, [, j. Y6 q6 I3 _$ ]& D' s
// an action group, because we want these actions to be
! c, X1 h. g; @* W, z1 r4 ^ // executed in a specific order, but these steps should2 G. Y g0 F/ f, X7 |* J
// take no (simulated) time. The M(foo) means "The message+ J7 ^9 s% C2 Z, ^* K7 ]
// called <foo>". You can send a message To a particular) A- H9 R) l1 x+ `9 j O
// object, or ForEach object in a collection.; P" n4 d- `3 M W
, u R$ }1 \' e1 |& G // Note we update the heatspace in two phases: first run" f( w6 _$ m* J( k$ _
// diffusion, then run "updateWorld" to actually enact the+ u! x9 @% z4 }
// changes the heatbugs have made. The ordering here is
+ W4 b# D9 b# M" D: K. c // significant!! c5 W& c- J7 j8 a
7 i* e6 T, T/ |7 F7 V. i
// Note also, that with the additional
& t. v& L' b. W4 O1 A$ |. W0 t! i // `randomizeHeatbugUpdateOrder' Boolean flag we can
6 _/ N4 S& H% f3 S; n5 L // randomize the order in which the bugs actually run: B' j3 w0 C9 D2 V% A$ |
// their step rule. This has the effect of removing any
6 T5 D4 t0 c% J$ D" o1 j1 ? // systematic bias in the iteration throught the heatbug
! J# [ `7 @; k7 E* U2 p // list from timestep to timestep
|' {4 S& \. b3 i, O7 t7 v# L $ h4 o" I( Z. q; `" C, o
// By default, all `createActionForEach' modelActions have5 u% i2 K1 g. S p# n
// a default order of `Sequential', which means that the5 [+ ~: A& ]( U% A
// order of iteration through the `heatbugList' will be) K+ V; ?% n1 r% ?
// identical (assuming the list order is not changed( {3 ?, V/ z8 m9 x
// indirectly by some other process).% Q. ^% c! a* [1 R4 z
3 Z0 Q$ P4 w+ ^% E% M9 ]
modelActions = new ActionGroupImpl (getZone ());
# Q1 N$ c$ c T) d1 W$ x4 k" \+ f4 j
try {7 q& p( L% ?# }) o
modelActions.createActionTo$message
# S. \: e# q$ [7 |, F# i- | (heat, new Selector (heat.getClass (), "stepRule", false));+ u ?: p4 N$ n9 \/ F/ Z, {% {
} catch (Exception e) {
3 \) ]& v( N0 c$ O8 f4 l0 \" N System.err.println ("Exception stepRule: " + e.getMessage ());- }9 z2 w9 n/ \1 k3 l6 D
}% \, q0 y" m3 [: v* z% d4 ^
' d7 c/ w8 T' C3 s; @. \1 G
try {/ \1 u; m0 |$ }+ R) J5 T) ^6 L
Heatbug proto = (Heatbug) heatbugList.get (0);
, r- ~/ f/ C! k. B2 T7 q, P- \ Selector sel = : z6 G5 p. i+ g2 {1 b0 K
new Selector (proto.getClass (), "heatbugStep", false);
2 E) B0 S2 K G# K7 J" ~7 c/ t actionForEach =, v y( n+ d2 [5 S& e B
modelActions.createFActionForEachHomogeneous$call. Q* m/ o$ T, f J R7 O
(heatbugList,
5 e* ]% V$ u% t1 E I new FCallImpl (this, proto, sel,
& ]; S+ ]$ {- q& M) c4 M i; H z new FArgumentsImpl (this, sel)));
( @5 s0 D4 o) w6 ~: A! s } catch (Exception e) {
, d2 B3 L0 V# e e.printStackTrace (System.err);
6 V& z+ K8 P4 W4 _! \4 C) B! R }1 m7 I \* n0 C: E3 M
% s, s& j" ^( ~9 E6 V syncUpdateOrder ();: E Z+ b9 |2 F. o& j! l
r1 Z( @4 W$ r @8 C1 v, f try {
" H! e7 L, y8 O: g modelActions.createActionTo$message
5 |0 m b' z& d (heat, new Selector (heat.getClass (), "updateLattice", false));
0 w' b5 u- A# |( s } catch (Exception e) {
/ @2 A P. h) y- p( R* p System.err.println("Exception updateLattice: " + e.getMessage ());
0 H, R* I0 K* T- `! A1 D( `4 \, X } {1 r) u; P( A/ c
. q1 ?/ {3 O7 B. ]) Z7 h- p // Then we create a schedule that executes the
- }& N# p* J) i$ Q+ n: K F // modelActions. modelActions is an ActionGroup, by itself it
- S6 G0 V8 j+ Q& G' C // has no notion of time. In order to have it executed in4 F8 k$ [" F' t! C( p% }, }8 {& `
// time, we create a Schedule that says to use the
$ a: q+ V5 G* \$ |: i% k // modelActions ActionGroup at particular times. This
: C8 @3 {6 s) b* ~% M& t // schedule has a repeat interval of 1, it will loop every7 F- c5 M# f: C
// time step. The action is executed at time 0 relative to5 {! a8 I. w, |
// the beginning of the loop.4 z$ m. g$ N7 F% O, r
( y' t6 f; \! p2 w7 B // This is a simple schedule, with only one action that is$ Y* F; j9 L: q0 D1 }
// just repeated every time. See jmousetrap for more
H& U: s+ g1 Q8 C9 w$ T // complicated schedules.
0 R1 M1 s s+ ^
$ @% P/ u9 M- U modelSchedule = new ScheduleImpl (getZone (), 1);
- E5 x2 ~4 x9 q, [ modelSchedule.at$createAction (0, modelActions);
4 w5 v4 f) O. ~, J! M- Y - m/ [ b% f; U
return this;3 }5 U" x8 ~# E; f* S$ ~# D3 [
} |