HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ s( S: S, [1 C' ]. q7 V
$ P; b2 x$ q5 t public Object buildActions () {
1 [( h( g f' r/ Y; g super.buildActions();
+ }, ?" `9 `" e: T $ q* }; r3 Q4 I5 [( y' L
// Create the list of simulation actions. We put these in
. |7 `( ^' k0 X0 \5 T+ Z2 g& K8 i1 y // an action group, because we want these actions to be
w( [; S$ j3 C) q: R/ } // executed in a specific order, but these steps should
+ [' l. }/ P* Y6 Z* v // take no (simulated) time. The M(foo) means "The message) m9 F# J0 Z; G9 p( T' x
// called <foo>". You can send a message To a particular. q0 L" C# S4 T; O: p+ z1 z
// object, or ForEach object in a collection.
1 x, W( o: M- B; M0 v/ o
; P, f1 x+ Z% i# }$ l // Note we update the heatspace in two phases: first run) S; F5 U- o/ [* ]/ M1 A- y
// diffusion, then run "updateWorld" to actually enact the
0 I; }) g1 E8 B // changes the heatbugs have made. The ordering here is; J! b' \! T1 q0 K) N
// significant!9 o( c2 }" D/ L, S( w
. h- ?4 O4 B. ^% @& g4 T
// Note also, that with the additional4 H/ P; ?% K; F1 K6 O* F, ^3 V
// `randomizeHeatbugUpdateOrder' Boolean flag we can1 C. |) b7 Q- I- v- B
// randomize the order in which the bugs actually run3 S6 K, {! o5 x1 _; i# o3 @
// their step rule. This has the effect of removing any; p( E# O6 Q% @3 ]4 M8 ?& h
// systematic bias in the iteration throught the heatbug
" q! y; g: r, X/ w // list from timestep to timestep: g; C3 C- p1 ^; \- y: k
+ L8 i( b% X( `9 [4 I$ b A // By default, all `createActionForEach' modelActions have) y8 e* W& ~4 x9 p2 B/ P0 n
// a default order of `Sequential', which means that the+ k6 S7 l" `) z/ H3 D" J% g' \
// order of iteration through the `heatbugList' will be
6 @9 L. T* Y& Y5 X+ l. ?8 x& x8 \4 ] // identical (assuming the list order is not changed. i8 Q; C9 a6 r' J: v
// indirectly by some other process).; j& d9 h, A/ I
9 c+ X) p2 _6 ^ modelActions = new ActionGroupImpl (getZone ()); Z- D1 b8 x$ _3 N
" ]& [- |% x1 ]9 {
try {
9 L1 ^: {* n4 G* `, n modelActions.createActionTo$message
8 [( I4 E9 ^ a9 B0 Z (heat, new Selector (heat.getClass (), "stepRule", false));
. f0 s, ?: L; b/ y; c } catch (Exception e) {
/ \/ t4 H9 D6 r7 H) I, R System.err.println ("Exception stepRule: " + e.getMessage ());
}' p5 @: m* y o' f }+ M X5 P* D( S/ k! E
- X+ ^- y4 k0 ]& b& K
try {( m1 i; F. G5 [ h/ R- J4 v
Heatbug proto = (Heatbug) heatbugList.get (0);
- l5 u. ]4 O5 f* K$ w0 f/ V Q. }0 G Selector sel =
) T, Q" R" T! U; S/ c+ c$ m) { new Selector (proto.getClass (), "heatbugStep", false);
5 I$ y6 p: Q4 I$ [ Z3 y" A0 C, O8 b actionForEach =
0 U! _) X1 _+ a! K9 a0 u modelActions.createFActionForEachHomogeneous$call
3 l- W& z, n: Z. S9 O (heatbugList,
5 p* Z, G" v" S5 u/ F) d. l4 K new FCallImpl (this, proto, sel,
- H1 B; a8 B6 X5 |* n new FArgumentsImpl (this, sel)));
9 f: u% l6 [) I" z; ^& c1 H } catch (Exception e) {
% f$ A& \' w) X1 Y( h* P1 [ e.printStackTrace (System.err);
8 F9 N6 a! W" ~6 G+ J }
; S- i- g. T. d# `
$ S7 O$ s: x& A' Y1 T5 ?; [ syncUpdateOrder ();0 q1 h1 o' K4 _, v
% k) i# Y! {$ }* K try {
$ z, Y, n7 g: c% V0 X' `0 E4 { modelActions.createActionTo$message m, `8 J; j7 t1 }4 T/ n5 b
(heat, new Selector (heat.getClass (), "updateLattice", false));' a/ u- u$ _2 Y6 U0 [5 L% W% w
} catch (Exception e) {8 ~+ [6 R5 r. g
System.err.println("Exception updateLattice: " + e.getMessage ());) ~" ]8 z1 ~6 r: W
}
; n- V t5 Z( j o- Z1 L ! ? @, \1 R: N* h
// Then we create a schedule that executes the
1 v& d& a+ u6 S" }* w) B // modelActions. modelActions is an ActionGroup, by itself it
# T. b' D, W g$ Z# K // has no notion of time. In order to have it executed in
4 C K) F, l4 S4 S( u- x // time, we create a Schedule that says to use the
% Z9 v" B, [! Y // modelActions ActionGroup at particular times. This5 i3 u/ _% c3 {9 `+ Q7 `. U3 u u
// schedule has a repeat interval of 1, it will loop every
7 \. P2 b9 w) q# K4 O! g7 } // time step. The action is executed at time 0 relative to
6 O3 D, z, B# K* w // the beginning of the loop.1 a5 d9 r, G; r" q" x" g
8 e" i# f; b% R! A& e // This is a simple schedule, with only one action that is
1 ^7 ?9 }/ z0 |9 z& b/ U: | // just repeated every time. See jmousetrap for more8 w( J5 |5 }3 S- ~, b+ n: Z
// complicated schedules.
6 ^: R( `' u5 U' \) N9 G1 [
' P7 l& Y* y/ a% {" C modelSchedule = new ScheduleImpl (getZone (), 1);; y1 [1 n! Q, O/ B: T! h; ?
modelSchedule.at$createAction (0, modelActions);: w. [( u/ c% N$ _! h6 i
* b$ I1 j) |% E2 g' U: @
return this;: H4 t5 d7 F7 |8 ]1 H M
} |