HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
6 Q) J1 [# e7 {5 k
. o. p- p4 z2 q+ B0 o* b public Object buildActions () {* M9 I1 F1 E b4 M' {( G
super.buildActions();
5 z/ ~0 d" I3 c U
, U: R. ^- Y1 ~4 p( a G$ T7 t // Create the list of simulation actions. We put these in
8 G2 c" B: r* t; A# M; } // an action group, because we want these actions to be' S& D7 p; ?6 R8 z6 a% G, R8 z
// executed in a specific order, but these steps should
+ S" Q- y) `- G1 q7 g" r4 i // take no (simulated) time. The M(foo) means "The message
- x' f4 W2 g& C6 @, } // called <foo>". You can send a message To a particular7 k) p& }: x+ ^# N2 A
// object, or ForEach object in a collection.* [1 v) g" x; A0 k; F6 J q
( `4 M8 J+ y4 E& ]1 h7 m) ~ // Note we update the heatspace in two phases: first run/ ?# h( O* \) P+ W4 H
// diffusion, then run "updateWorld" to actually enact the
4 k: y8 l# o% A8 G: U/ K0 I8 ~+ W7 l // changes the heatbugs have made. The ordering here is! B. _6 k! k: ~: E1 g2 O
// significant!
, e; ~5 S! P3 n5 X6 P/ x( E
3 a9 {* d. g- x @$ F+ K! @ // Note also, that with the additional
, R: n g! X9 I4 a$ i // `randomizeHeatbugUpdateOrder' Boolean flag we can9 @$ L8 F" ]! }% `$ Z3 \
// randomize the order in which the bugs actually run7 O9 |; `& x# ]+ o+ J+ e( t
// their step rule. This has the effect of removing any4 E% c' {1 b0 {2 }5 b: q
// systematic bias in the iteration throught the heatbug# e0 v: j& W1 E9 G
// list from timestep to timestep) O8 c( k+ z8 Q* _$ P! S
* F5 S- G: D9 N& _5 Q4 x. N7 [
// By default, all `createActionForEach' modelActions have! h% e! i( q$ U* z2 F4 U
// a default order of `Sequential', which means that the# P! q7 N3 Q4 v: D& }
// order of iteration through the `heatbugList' will be& }: F$ J! V5 {8 w# I
// identical (assuming the list order is not changed7 ]/ D$ {$ T: [: X$ B q0 [. C% E
// indirectly by some other process).
- g( K1 F) K/ p: A& t* D & I- _9 G* J6 A; e" H" e
modelActions = new ActionGroupImpl (getZone ());
3 o" j2 Z6 D) m+ |) e
! C1 M- n. \; S- U& ^ try {
; p. [* N1 @) r& E modelActions.createActionTo$message
( u8 N8 t6 y8 w/ Y$ s3 t (heat, new Selector (heat.getClass (), "stepRule", false));
( U7 H4 m7 F' j/ Q8 h+ v } catch (Exception e) {
$ m4 x( b; P; n! z4 u; @- v" }2 D System.err.println ("Exception stepRule: " + e.getMessage ());
{1 }# c6 f, {5 ~1 z' P }
4 Y# |( B2 H" h+ l( m0 O; m+ J4 |1 A8 i8 {- c. ^0 \/ l
try {* c3 P1 P. T2 l, G' y! K
Heatbug proto = (Heatbug) heatbugList.get (0);
- A v) D* P4 e8 |# n8 `5 s Selector sel = 4 G1 v7 _- D' D
new Selector (proto.getClass (), "heatbugStep", false);3 P8 V1 o5 d0 e2 v5 g- N
actionForEach =
8 G& w% N% n& u: P& @ y0 B" V" M modelActions.createFActionForEachHomogeneous$call9 q& D6 u0 F( p3 H
(heatbugList,
^+ \) W" ]+ V1 v1 k d new FCallImpl (this, proto, sel,
% X6 N' ^1 g% Y% k8 t+ S new FArgumentsImpl (this, sel)));
; e- ?2 L1 ^7 F& @/ b- z } catch (Exception e) {' @# ]5 m" T2 a+ W! G
e.printStackTrace (System.err);
* u, ]2 h) Z* V% ]% k+ t }7 L G0 }7 @7 {( d6 \3 U
1 j, F5 |* [( P* i5 n$ Y5 q w syncUpdateOrder ();
+ a. n* N& I/ {7 n
h5 v U8 A9 `3 @; I8 ~ try {: e8 U( c8 w. g1 k6 }! q
modelActions.createActionTo$message % |1 `% O9 N0 z( `6 c/ A
(heat, new Selector (heat.getClass (), "updateLattice", false));
2 I% M: z C1 M# _: S% c3 ? } catch (Exception e) {
. t4 x v* G$ q' c' e System.err.println("Exception updateLattice: " + e.getMessage ());
3 y8 _9 S# F; H }
5 A! \- T. q) y. _ 5 E+ ]; d3 B' T( o6 v9 H, R
// Then we create a schedule that executes the+ H f3 W# A; E- Q8 ]
// modelActions. modelActions is an ActionGroup, by itself it1 |, Q: _* V- `; e* _% {$ J* R
// has no notion of time. In order to have it executed in3 j% m, x) }$ `7 | Z
// time, we create a Schedule that says to use the8 b: H2 t6 ^7 R
// modelActions ActionGroup at particular times. This
2 w b8 \# M$ z/ [( s // schedule has a repeat interval of 1, it will loop every% D, X$ v- U: L9 }9 }/ ~+ K' R
// time step. The action is executed at time 0 relative to3 F2 [2 Q5 c! W2 `3 g" b& Y
// the beginning of the loop.- j" ^ s: I8 Y
7 G) A D/ Y" `* W( k0 w/ }1 K- ^; r // This is a simple schedule, with only one action that is; g; H) V' J! h: y/ x) ?
// just repeated every time. See jmousetrap for more
# x2 f6 H4 p7 Q, e l `9 d // complicated schedules.* j' O6 N2 q0 W, U# J; k0 P: c
3 Y a; f1 H/ @2 v2 C/ b modelSchedule = new ScheduleImpl (getZone (), 1);- o( P& r/ e8 M' _9 W; T" t# q4 ~
modelSchedule.at$createAction (0, modelActions);
7 b" B. j' K3 ~/ g5 ^2 G! h& U
' i1 D, i, c' T' O return this;
4 Q& g* m4 K3 t" M4 g" x+ r+ Y } |