HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 R) O4 s* P) e7 L* O5 d
* I% `8 F1 n1 D% Q$ E6 s public Object buildActions () {
) x; E: Y- E6 j$ |/ j( v super.buildActions();
( {' h. v) X9 |. A+ U9 U% S5 W
! ?6 H' x% a+ A7 ~ // Create the list of simulation actions. We put these in! g) ?) N0 U$ n# G2 `8 K- b
// an action group, because we want these actions to be/ x+ i* K$ i. Y0 ? @! r0 z
// executed in a specific order, but these steps should/ J7 n' R K5 T
// take no (simulated) time. The M(foo) means "The message2 e4 F" q" |6 _/ {, B* O
// called <foo>". You can send a message To a particular7 H0 P3 J- Z. v" H2 Z' o) e& {' l
// object, or ForEach object in a collection.- O( K8 U) b3 e3 v7 i
. K% o/ V4 {' k, b // Note we update the heatspace in two phases: first run0 ^* P* z+ i6 ?# \% S; h3 T
// diffusion, then run "updateWorld" to actually enact the
. o" @1 H# B( a // changes the heatbugs have made. The ordering here is& ~/ ~& M x6 K0 j
// significant!+ w/ M1 q3 V+ D6 Q( U( {; k
$ `6 d. l2 \& _
// Note also, that with the additional
# @# l# o/ P7 D# y // `randomizeHeatbugUpdateOrder' Boolean flag we can
# v% Z) G: N6 V" A( O1 D. t* o2 V // randomize the order in which the bugs actually run" u5 [' ]$ ^- F2 L9 y4 S
// their step rule. This has the effect of removing any
- r h7 O% S" Z8 R2 X3 R // systematic bias in the iteration throught the heatbug8 o. }+ d6 |/ p7 k& [( ~0 c
// list from timestep to timestep" V }6 \3 p1 o( q
" ]+ Y ~/ s2 U: L1 n7 O
// By default, all `createActionForEach' modelActions have5 j! W+ Z2 c4 e! T* d
// a default order of `Sequential', which means that the. m! {8 {" m, R1 ?. h0 | k. v
// order of iteration through the `heatbugList' will be
2 d( C: G% |$ A; n: S. L$ v- X // identical (assuming the list order is not changed$ a" I& b1 R! Z- t, ], H. F! h
// indirectly by some other process).: w0 L' ]' i, _
! Y% I$ j$ Y% ^+ ]. I modelActions = new ActionGroupImpl (getZone ());2 \0 r6 K( W: [ t* P3 j
1 ?% W' b q; O( e% K% P1 a try {1 `# l1 |3 U5 Q2 S; l
modelActions.createActionTo$message
# G/ U6 o- K& i: H5 h (heat, new Selector (heat.getClass (), "stepRule", false));
0 P: K- `, }$ N! _( ?; Y } catch (Exception e) {
r* K7 K7 s7 q! c# u System.err.println ("Exception stepRule: " + e.getMessage ());
/ Z( \0 g' s" B2 P5 I. ~+ k }
, V- U* i6 y- x9 Y6 Y( e
" K( j" ?; k$ q) ?+ Z5 Y ? try {1 B3 v0 q" R8 [- [" M' B$ D$ |
Heatbug proto = (Heatbug) heatbugList.get (0);9 \% g; E5 x9 O8 \
Selector sel =
2 O: e& P4 L$ b+ D3 A' g& J5 N new Selector (proto.getClass (), "heatbugStep", false);6 d3 Z& O! f3 m* J* R
actionForEach =$ d# H. Q1 C; U- k) z% ^
modelActions.createFActionForEachHomogeneous$call
% S+ H, I: o4 b6 m* p6 F (heatbugList,
- T6 [7 ~; m7 n new FCallImpl (this, proto, sel,) A0 {; x. I, G3 [; b" I6 Q
new FArgumentsImpl (this, sel)));7 ]! J- s# o* V G- {0 {
} catch (Exception e) {
. \8 I1 C' y. r& n4 J! \ e.printStackTrace (System.err);
8 E! S S) G- X" q3 F9 [ }
5 e7 `% Z7 |7 C$ L% x K, o
5 X& ^ G/ u; J4 Q: G: F, y2 P syncUpdateOrder ();
" m" v+ f: ] K; x$ s7 Z& F9 M& p
( g" j3 b' g4 A try {
( d4 p7 \4 T; ]* J8 x% c6 t/ H modelActions.createActionTo$message
0 M2 } I2 n7 H, q (heat, new Selector (heat.getClass (), "updateLattice", false));4 o( g6 J$ Y3 h
} catch (Exception e) {
, Q/ l: \* z5 @3 u% t0 n0 ]6 K8 i8 I System.err.println("Exception updateLattice: " + e.getMessage ());
) U# ]3 ~7 ?) ~5 }" f" c* t! G }+ L: O& u1 H; }6 t5 g2 ]. A
/ u2 Y0 r% z+ l9 @ // Then we create a schedule that executes the9 I/ ^- r4 H+ J. t0 w$ R8 k' @
// modelActions. modelActions is an ActionGroup, by itself it% W ^+ d; d6 z
// has no notion of time. In order to have it executed in
. A/ k$ |! O6 ^" F9 d2 F // time, we create a Schedule that says to use the
5 e4 o7 y0 ~0 U1 t) ? // modelActions ActionGroup at particular times. This
3 d9 @6 Z4 H- Z# @+ M* j // schedule has a repeat interval of 1, it will loop every
* D0 o6 P& z: A // time step. The action is executed at time 0 relative to3 Q: w: S# b/ o3 f# \6 _
// the beginning of the loop.
5 W4 H% J) ^- W6 V5 N8 }3 p8 q k1 p( |/ |6 @+ l1 r/ v4 r& F
// This is a simple schedule, with only one action that is
! m2 o# t8 _& G4 r p // just repeated every time. See jmousetrap for more
, G3 N7 e* j3 @" Z2 J0 S // complicated schedules.
3 t0 W( X; g; P4 w
5 Q1 d8 N/ a5 [& E9 B: } modelSchedule = new ScheduleImpl (getZone (), 1);
6 }. F: z; M: B# Y modelSchedule.at$createAction (0, modelActions);8 |! D; N& j; U/ ]. s
! z9 U7 m; \5 _3 J6 b7 n- k( I return this;+ q4 F! O4 F$ v: k( O2 A
} |