HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
8 E# o9 B& V) o# _: _" G* `" j* {/ E5 T; O
public Object buildActions () {
! Y+ E0 {" ~- s$ c5 ` super.buildActions();) f2 I* t/ d3 Y, a! d& B
( @1 r4 O+ F. q- U // Create the list of simulation actions. We put these in# Q# o$ S7 L' w" p) a, f
// an action group, because we want these actions to be
; w% ?+ C5 U4 v; i% a; B0 D // executed in a specific order, but these steps should# A, s9 ?" u7 |* K
// take no (simulated) time. The M(foo) means "The message8 |0 n) u7 \# m; S* V
// called <foo>". You can send a message To a particular9 l2 g; J# d( `: @& P+ k: ^
// object, or ForEach object in a collection.
* U5 A5 P, i* o/ n' f3 o% D1 r
% w# c0 N4 s4 X8 {: A7 L5 U: | // Note we update the heatspace in two phases: first run+ N& W3 m! I! w9 D5 f1 R- s
// diffusion, then run "updateWorld" to actually enact the
; x' R, @& O% ]" c. F // changes the heatbugs have made. The ordering here is9 H2 |, R8 ~% W, D
// significant!
. z1 d$ ~% F9 `9 O+ L * ?: i( O v. W' n2 W& q
// Note also, that with the additional9 b3 E( `6 h- l7 P' d* W: V2 A
// `randomizeHeatbugUpdateOrder' Boolean flag we can
. \2 l e/ a# R0 } m1 t& \ // randomize the order in which the bugs actually run. K! x4 [% Z9 g: l
// their step rule. This has the effect of removing any( G9 j. N7 R% P# l) H
// systematic bias in the iteration throught the heatbug& p' `& ]) v7 U+ h4 `$ O- f
// list from timestep to timestep+ Q8 K8 c, g, ?3 Z' n$ s
: v) w3 L) _6 l. h& |9 \
// By default, all `createActionForEach' modelActions have
2 C; P9 E @$ h7 d k- r. ^ // a default order of `Sequential', which means that the2 t; c; F( B( F" Q" B* p- g+ W. D
// order of iteration through the `heatbugList' will be' y9 j2 J' n; X5 s
// identical (assuming the list order is not changed' M J/ P/ c: G1 O x
// indirectly by some other process).& D0 V; n* u* Q, b! @8 h( W' U
) K- [; @7 i( X; {
modelActions = new ActionGroupImpl (getZone ());- O6 _9 a/ [9 |$ @9 ~. E- h: k4 U
7 g$ r% U/ P0 U5 Y; z% _8 o try {
! b$ I. a$ ]8 @: i& b8 C modelActions.createActionTo$message
& o" j# l8 P; K3 ^' V# k; Y (heat, new Selector (heat.getClass (), "stepRule", false));
2 [0 Z' u9 {8 E3 w, n2 x } catch (Exception e) {
3 O7 J# |4 D& L- {7 x5 m* D System.err.println ("Exception stepRule: " + e.getMessage ());/ A7 F+ D4 o' Q! z
}2 E9 ^/ z- H( d1 }
* n9 M7 m) g: I9 D8 p# b try {8 k4 h! ]. u: R' O. @- Z/ A
Heatbug proto = (Heatbug) heatbugList.get (0);. [- L9 s, C+ U v
Selector sel =
& m" R" G$ C! W1 @1 L+ C new Selector (proto.getClass (), "heatbugStep", false);
% z7 B5 C- S- d actionForEach =0 n0 N! i8 S2 _& g
modelActions.createFActionForEachHomogeneous$call
# j b; B; k4 k: B! U& {7 z+ [# i, A (heatbugList,& Z1 [3 H Q4 C% r$ K3 V
new FCallImpl (this, proto, sel,
7 h; p. f1 F n# [0 c$ W* p new FArgumentsImpl (this, sel))); M+ B0 ~2 r" Y' o
} catch (Exception e) {6 i8 d/ @# Z0 o8 o2 E
e.printStackTrace (System.err);
$ W/ {* X2 E/ |4 U7 d# K }5 s5 O! U; C6 ^* C* M$ y R( `* ~
5 b* O% [% e- A
syncUpdateOrder ();' r6 {+ ~: p3 j
( D% p! W) E0 m0 Y* D# z4 c try {
0 `* q" s1 Y$ Y4 l% H6 h modelActions.createActionTo$message , X& N# b( D9 G, g) v6 H6 J
(heat, new Selector (heat.getClass (), "updateLattice", false));( B% K9 B9 _) l2 L( U& f8 l6 v
} catch (Exception e) {
, V; {4 q* D8 t, i System.err.println("Exception updateLattice: " + e.getMessage ());
5 n$ p8 m, G+ K& t* X9 C- y1 v5 F& K }
/ d! R D, z! ] ^# o' _% G& @& i
// Then we create a schedule that executes the
! ]2 ?/ w- p) M5 |( ]$ ~ // modelActions. modelActions is an ActionGroup, by itself it
6 q& y9 K" ~, M6 T3 o6 i // has no notion of time. In order to have it executed in
' O' s8 y+ ^% G4 Q- }* Y7 k( _ // time, we create a Schedule that says to use the
0 Y) S9 M4 z/ @5 o4 m // modelActions ActionGroup at particular times. This: C" r8 F; F9 i8 r; L) ^7 ~* X
// schedule has a repeat interval of 1, it will loop every
H3 u: @' q: o+ N6 @$ T) ~ // time step. The action is executed at time 0 relative to
) w! S5 w5 `: T6 X3 r. j3 W' r) W" b6 X // the beginning of the loop.
* ]5 O2 ?9 Z. K, Y4 C. P6 e* l8 b! e: d9 e
// This is a simple schedule, with only one action that is
5 D" H1 B, V7 m // just repeated every time. See jmousetrap for more! C5 O- W2 d8 |, ?2 c
// complicated schedules./ q# r J6 e9 N' W9 T* Y1 y
- c1 } C& l( ^7 x
modelSchedule = new ScheduleImpl (getZone (), 1);: M0 ^* h9 l" G
modelSchedule.at$createAction (0, modelActions);2 x1 ]' H; h8 s" e1 h3 d
- u9 n/ u" g2 J+ C return this;
3 Q& {% i( I( @ } |