HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 s; C% s$ ?# ?- |5 R
- A) }: F8 g" G) Z$ c1 V public Object buildActions () {) V5 @( B# a& r, Z: Y/ b
super.buildActions();$ I. D. h+ l- L6 D: E/ L! v
4 v4 z( }5 ^( p" p7 d4 w) | // Create the list of simulation actions. We put these in
; S- O `7 }5 i // an action group, because we want these actions to be: p, C6 T! M; ]1 p5 B/ x
// executed in a specific order, but these steps should
) _/ K1 W1 Q; i% h9 a! n7 [ // take no (simulated) time. The M(foo) means "The message8 c4 \/ o" o, n
// called <foo>". You can send a message To a particular$ I% F& V( h& h
// object, or ForEach object in a collection.
! W" G5 o% o; j( ]& L% m
7 s7 q7 ?4 L2 v2 B# Q: _+ z // Note we update the heatspace in two phases: first run" F$ y0 {) Q4 N
// diffusion, then run "updateWorld" to actually enact the8 _1 p' m5 p. p$ p' Y: B
// changes the heatbugs have made. The ordering here is' S0 S; j& ]1 v: F; X
// significant!$ e+ z4 N4 t' X
0 x6 P" C- z* a; O' A j: T
// Note also, that with the additional
[$ o- d# T- Q5 k0 E7 F: i+ _! _ // `randomizeHeatbugUpdateOrder' Boolean flag we can* R( h- ?8 e; O* p
// randomize the order in which the bugs actually run3 G6 S/ @5 E2 [; d
// their step rule. This has the effect of removing any
" i& K$ E$ k* i7 R. [ // systematic bias in the iteration throught the heatbug
3 ]! I' r, g6 }+ m. R // list from timestep to timestep7 V. t. B' \. p: t; r3 n
% ~. s. `8 V! o3 V
// By default, all `createActionForEach' modelActions have
' s. r1 y9 q5 R# Q% b6 L // a default order of `Sequential', which means that the7 @/ y3 `' O# \* |* G- X" ]
// order of iteration through the `heatbugList' will be6 Q& g% R$ c% q) C8 o6 r
// identical (assuming the list order is not changed1 P: I: e* B% { X
// indirectly by some other process).
. R0 f6 z5 u( l& w3 c+ ^& E" D
1 s8 U. }0 [7 j2 b modelActions = new ActionGroupImpl (getZone ()); P: k) j0 A! J: g( @3 _6 ^" i: P
: ?0 Y. m4 r9 W9 e6 t try {- i0 O" I4 g2 P& ` J2 V; b1 h2 a
modelActions.createActionTo$message/ W9 g0 D% u; J) w% j; p& K: y" y2 L3 k
(heat, new Selector (heat.getClass (), "stepRule", false));
* f# b; P+ x5 j' ^ p" d } catch (Exception e) {
5 w1 Q5 C! \) z1 I System.err.println ("Exception stepRule: " + e.getMessage ()); @+ ]9 H9 D& u; T J; _- J
}
7 Z% r; ^ f# Q
8 k" X3 M$ Y; s5 I) o try {
$ n5 h; Z- c% l7 {: s5 R Heatbug proto = (Heatbug) heatbugList.get (0);0 u" X; x' ^5 Y. s% _$ i
Selector sel =
4 H. j+ Z8 o1 u0 ^$ Q9 f new Selector (proto.getClass (), "heatbugStep", false);
+ C# t. X2 U% W, q0 y actionForEach =- ~" t% i. @+ t
modelActions.createFActionForEachHomogeneous$call
1 d$ T4 J7 j! J/ |1 t (heatbugList,0 S6 ^: R7 S. ~9 R7 f' E5 O
new FCallImpl (this, proto, sel,
8 n( G& }$ ^: v. ` new FArgumentsImpl (this, sel)));
{' W2 e" _9 }, G# _ } catch (Exception e) {
( x+ K/ G, M5 U e.printStackTrace (System.err);
; R* p2 K7 U1 [( `: L8 [ }' k$ \ F& R# |3 x0 n
, M- J4 C) b0 ~$ V4 a
syncUpdateOrder ();2 b, z' }- `! ?
( Y7 ^2 F8 U' M8 \* f8 w1 I5 ? try {
! p- D+ u: ^/ R8 \0 V modelActions.createActionTo$message
; _1 e5 X" c9 _% T& C5 L (heat, new Selector (heat.getClass (), "updateLattice", false));
& \/ m- M' V% G. H% E } catch (Exception e) {$ K7 O, i, k1 `9 D4 m6 m
System.err.println("Exception updateLattice: " + e.getMessage ());
# G8 X z/ i% ?( U/ A; x% k' x6 a& | }* }$ a, u& x$ c. U# {
# L6 i( H$ {0 [" N, G // Then we create a schedule that executes the9 Z0 P' E/ q% u& {7 y" @- [6 s
// modelActions. modelActions is an ActionGroup, by itself it( g; @5 n" }" x( C+ w: Q
// has no notion of time. In order to have it executed in) A9 b; A' K9 Y3 S
// time, we create a Schedule that says to use the
1 F; v; L2 ^- r% o6 t, e // modelActions ActionGroup at particular times. This
% U" f" T# \2 | // schedule has a repeat interval of 1, it will loop every4 ` H- i1 b- O0 R8 p- K9 Z! ^9 M
// time step. The action is executed at time 0 relative to
3 k I' G# a7 W! E' a3 a; ` // the beginning of the loop.
* z. u8 p1 b8 C% J* |
3 @- y7 a8 z0 k2 f) X# a // This is a simple schedule, with only one action that is% v, Q. k, `" d7 R/ x
// just repeated every time. See jmousetrap for more
; l( l4 j7 t+ t7 s3 a // complicated schedules.5 C; o- M8 Q9 b* Q" o
) o) x8 W, z* ~: I# o0 [6 u* r
modelSchedule = new ScheduleImpl (getZone (), 1);2 J1 q3 e6 }. c; h+ v' X
modelSchedule.at$createAction (0, modelActions);
- L1 u6 U# N% \8 }' Q) T
& \9 U; F+ W/ Q6 D6 G( T- ?' z9 X return this;2 l" r* K$ T* v, \
} |