HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:. F3 p% N% ` N( @3 M( D0 v
; R+ n& p% |4 t# a- { F- B
public Object buildActions () {) B, q! X6 y3 ~, I9 N, p
super.buildActions();2 C. U7 f6 J' L1 E8 y; z$ U
2 v! G! _) O2 X, _" g
// Create the list of simulation actions. We put these in
3 H( e ^- ^1 X: _) ` // an action group, because we want these actions to be
$ z& R& b6 I1 L% a( L L // executed in a specific order, but these steps should$ Z# J% ?$ ^& O: \7 a; H* h
// take no (simulated) time. The M(foo) means "The message" ^2 O4 g) A2 u2 ~7 m; I
// called <foo>". You can send a message To a particular9 `3 o0 N( m, e0 z
// object, or ForEach object in a collection.8 J+ D% q4 A5 D" O2 p. s
8 B: T5 \/ n+ Q* X \
// Note we update the heatspace in two phases: first run
8 I* M$ b! A& U) W# e) ?' | // diffusion, then run "updateWorld" to actually enact the- Q+ L8 Q, u( R @5 n9 m1 ?# b
// changes the heatbugs have made. The ordering here is/ w7 X; E* k9 z) q' l
// significant!0 {' I. P' q9 k. _, q8 l
8 H4 Z5 X( ]- E9 L
// Note also, that with the additional
& Q0 [8 E6 E3 k0 V1 q' A // `randomizeHeatbugUpdateOrder' Boolean flag we can
4 U, W" [+ n" j4 P7 ^ // randomize the order in which the bugs actually run
3 P- U2 n2 P0 n; L$ O$ J, { // their step rule. This has the effect of removing any
2 u' p9 N9 w- \& x' l8 C // systematic bias in the iteration throught the heatbug
$ r# l6 o2 M9 A. Y // list from timestep to timestep. A' Z2 @& j" Y; b/ A
7 }9 @$ N9 Z2 l# c // By default, all `createActionForEach' modelActions have1 x* B8 P- h; Q# Q; l; m: K
// a default order of `Sequential', which means that the8 E2 h$ J- B- i
// order of iteration through the `heatbugList' will be
) Q& ~+ i X* u6 ^ ~2 a8 @ // identical (assuming the list order is not changed2 W+ D3 ~$ M" p4 G& Y1 z
// indirectly by some other process). K8 W2 z+ Z; r" B
+ k! Q* P+ U4 Z& G3 P modelActions = new ActionGroupImpl (getZone ());
9 Z5 v$ k$ g- p4 n; V" O- ^0 } j: E$ Y, o4 L! k
try {
0 X2 |0 G/ e( ^( F; x modelActions.createActionTo$message+ }" v1 u: l4 b5 _, X: \) _
(heat, new Selector (heat.getClass (), "stepRule", false));( F f* `5 R5 }. c% r( t
} catch (Exception e) {
; f& j/ [& Y. c4 p5 Y System.err.println ("Exception stepRule: " + e.getMessage ());
4 X- r: I; A3 i% i/ Q }
- @. z# g6 m+ P. ^
1 F# Q' j- _ j) y/ |0 B try {+ K. T; ~& F& q4 D+ i! `3 I
Heatbug proto = (Heatbug) heatbugList.get (0);7 L: X( \% j0 A1 \4 b
Selector sel = - a6 ` G6 W8 d! r
new Selector (proto.getClass (), "heatbugStep", false);
( C7 z) @0 C0 `& V actionForEach =
0 L/ X" Y8 v- h8 t' Q modelActions.createFActionForEachHomogeneous$call3 a9 H9 d( g' R
(heatbugList,
3 X* D3 E9 n6 |1 b* R: c new FCallImpl (this, proto, sel,
& ^3 Q r- C& s6 O+ x* W4 m new FArgumentsImpl (this, sel)));
2 q7 e4 a* W6 u. D+ T2 d0 h" R. L } catch (Exception e) {
( u* {5 m0 g0 g5 [2 o8 J; _ e.printStackTrace (System.err);
& g. ~0 G0 e4 }* {2 u4 W$ i. n' a }
% H: x0 ?) o j. @# e! r
3 H2 t3 ~' g7 H0 | syncUpdateOrder ();
0 c u% e4 W" ^3 `' v) Q6 ^$ M& c2 D2 z4 ]. T
try { [9 B- T2 w( w' K6 W/ L8 I
modelActions.createActionTo$message
% V% m) F. X! ?* H- M2 \* j (heat, new Selector (heat.getClass (), "updateLattice", false));
; e0 D" V2 x& [ } catch (Exception e) {
* T6 M, }5 z! ~$ n4 q, t& s9 x System.err.println("Exception updateLattice: " + e.getMessage ());
$ P. ?* t3 e6 a- K, h+ r }
- j: ?9 D: S& ?, { $ V7 B5 G6 G0 l/ J) z0 W6 _
// Then we create a schedule that executes the
& F) w3 g) Z& v1 x" P" x( Z0 l/ f // modelActions. modelActions is an ActionGroup, by itself it
$ l8 p9 s& Q6 {- ?% s4 {1 D) ? // has no notion of time. In order to have it executed in
6 o8 q# O8 ~) |0 C // time, we create a Schedule that says to use the+ c, H/ \' O5 n
// modelActions ActionGroup at particular times. This& p( Z' Y! S# ?" F; H# s
// schedule has a repeat interval of 1, it will loop every" Y8 l a4 r" C$ K! ?: o; l2 |
// time step. The action is executed at time 0 relative to
7 t& v! [' `$ }# Z/ T% T, c c/ f // the beginning of the loop.3 D# g5 A% u4 E0 t8 t& E& k3 t* S
/ g5 X3 ]: g4 |- j8 e. `! W/ L // This is a simple schedule, with only one action that is7 g4 x6 U, x- M% F( J# E: e) }
// just repeated every time. See jmousetrap for more) \3 n6 w6 q' N- V' ?7 A5 _% \$ C, Z c' |
// complicated schedules.
4 v( n e5 Y' k4 t
; H& D+ S4 p) V3 j modelSchedule = new ScheduleImpl (getZone (), 1);0 t! ` G' d3 p, @# m1 g: B
modelSchedule.at$createAction (0, modelActions);9 h/ |, _) ?9 P" E' b
9 B! m; C% J) Y' Y
return this;
7 o' ~7 n/ L3 t5 g) N } |