HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- G- r2 f4 J. {1 t8 i6 N' x7 `6 u" O9 G( N1 w
public Object buildActions () {
% b# M$ N' L7 B super.buildActions();5 ?6 I& X' P, E x
0 O( E, P6 e0 {5 S& k3 R1 ? // Create the list of simulation actions. We put these in4 S& ?4 z' I! a+ g X( k3 g5 l
// an action group, because we want these actions to be
! C% `0 `2 C. o2 c. e // executed in a specific order, but these steps should# @# H, |( r" @! {0 A. P
// take no (simulated) time. The M(foo) means "The message
d+ C' r0 t \) i" P // called <foo>". You can send a message To a particular
2 h( D9 O/ h; N6 | // object, or ForEach object in a collection.
) Y) D, @2 X9 W! I& `8 @+ ` ; }6 Z- s) |; K8 i. F0 X
// Note we update the heatspace in two phases: first run( L% _* }6 z2 W
// diffusion, then run "updateWorld" to actually enact the
' O7 L2 o" P8 O' k // changes the heatbugs have made. The ordering here is! A% R: ]" t* [" m
// significant!
: s* `. M0 |1 z7 K: h
' k! l* W$ {9 k: L7 z" {3 _+ b // Note also, that with the additional
N" X4 C$ ~4 _6 }- d+ Y // `randomizeHeatbugUpdateOrder' Boolean flag we can& l- p i* n& b, U
// randomize the order in which the bugs actually run
1 R% U( B5 X/ ]- L ` // their step rule. This has the effect of removing any
2 j0 p X$ E# T, a' ?4 `* Z // systematic bias in the iteration throught the heatbug
$ I8 l4 n: Y: Y5 V2 } // list from timestep to timestep
) O2 |- W. N8 r$ \. @& P ; T9 v0 T1 N: z/ g4 L
// By default, all `createActionForEach' modelActions have
- p0 k/ N- d; \8 z3 g& h // a default order of `Sequential', which means that the9 l0 y: h9 L- e' m7 ^
// order of iteration through the `heatbugList' will be- ]! L1 t: R! p) u% y
// identical (assuming the list order is not changed; O h% ?, w& w5 W% x
// indirectly by some other process).
/ Y9 d K' Y. _+ E* ~+ W * D6 j( H7 e* A+ ]. W% V
modelActions = new ActionGroupImpl (getZone ());
/ H' [3 A. b8 E- L. S2 H" K7 C ^: H, Z6 @5 D0 ~ q8 _$ }/ p9 N3 C
try {
3 ~, c, O w7 ?4 q# T modelActions.createActionTo$message* m+ y( e, P- b) |% Q1 Y4 Z! s3 v
(heat, new Selector (heat.getClass (), "stepRule", false));( `: A& y3 L$ _- X( x' Y3 T& ?- x$ ^
} catch (Exception e) {
6 p7 Y+ a' v- ]# v5 K System.err.println ("Exception stepRule: " + e.getMessage ());
$ u. s0 a2 i7 V' Y }
, p2 [% R$ j) S5 [" x9 k1 F. F7 Z
try {. @, V1 ]( J- x8 K2 g* K
Heatbug proto = (Heatbug) heatbugList.get (0);( [/ s& \ T2 e( W/ ]
Selector sel = - S. C( c$ J& a( W
new Selector (proto.getClass (), "heatbugStep", false);
/ M8 H/ I' _; W+ n4 E7 w actionForEach =/ j( i( _; }) u
modelActions.createFActionForEachHomogeneous$call
5 r. l4 L2 j3 E (heatbugList,
8 H& o7 s6 F) ^2 ~8 D' E. \ new FCallImpl (this, proto, sel, m8 q1 q9 p% {) L5 o
new FArgumentsImpl (this, sel)));
/ Z- l0 J3 N- B4 W* x/ }$ d } catch (Exception e) {9 b2 q+ p$ d2 v! E
e.printStackTrace (System.err);1 C0 x% Y; n/ z K3 X
}4 a: r5 A3 z/ k# Q! e
7 p1 h9 ] v: l6 L0 F7 a: ]: v/ T! R syncUpdateOrder ();& N, [6 p; n% y3 f
) b+ A: O% N- L- l. @ try {) |. l0 `4 K; X6 i7 H; K: r/ T
modelActions.createActionTo$message : b+ V/ T- ]2 H
(heat, new Selector (heat.getClass (), "updateLattice", false));
" u3 H1 n7 ~' I } catch (Exception e) {
3 X) U$ v# Z# e) a7 g System.err.println("Exception updateLattice: " + e.getMessage ());
# F9 t5 U- K/ L. o1 q }
) r( N" k) z& M# I
7 \3 Y% ?: o8 T! K8 t4 W* t* S // Then we create a schedule that executes the+ Y8 `* X; Z6 z7 t6 x( Z& [* M H
// modelActions. modelActions is an ActionGroup, by itself it
k) I- I( S! i: N4 S0 ~$ [: h // has no notion of time. In order to have it executed in
4 E# H% b" ?8 ]3 J" ` // time, we create a Schedule that says to use the8 [- L2 L5 O5 l. f8 X) ^" G
// modelActions ActionGroup at particular times. This8 R# Y( [+ J- o7 I f
// schedule has a repeat interval of 1, it will loop every& ^: Q5 N+ ^/ r- _, r3 _! o
// time step. The action is executed at time 0 relative to! h0 H- t: I. v+ f
// the beginning of the loop.
* W3 c. |# r1 d p5 y# u- j* s6 B! i
% H4 |& b0 u7 Z* y% u4 T // This is a simple schedule, with only one action that is. K. ~+ U' Q' T8 v
// just repeated every time. See jmousetrap for more/ y3 k! X% `, W: b4 c. D1 W- Q% g
// complicated schedules.7 o) P' t J' m% c1 b0 l* f' w
! ~* Q6 `% w6 B" A8 {5 ^4 X, s modelSchedule = new ScheduleImpl (getZone (), 1); v! ~9 | j' p! {/ b
modelSchedule.at$createAction (0, modelActions);1 F( V/ T/ I# l
1 \. C/ Y) d* U return this;
: \& ^' M. N0 |$ i4 X3 F2 ^8 Y9 x } |