HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 B% o- B s7 d7 A) _. @/ ?
E- r' S1 d- o) o1 t
public Object buildActions () {! X1 m) R( i& Q/ |8 A
super.buildActions();. H# h) _7 q; }" n1 [9 D
0 P9 k- r. C8 q& F5 c // Create the list of simulation actions. We put these in+ p" U, H6 r7 x# O$ }' g0 g" y& C$ H; R
// an action group, because we want these actions to be
% S# _; {5 H( K% R3 F3 K8 f9 p // executed in a specific order, but these steps should, Z X. O5 ^( F- w2 n, j t: V$ P
// take no (simulated) time. The M(foo) means "The message6 H% x3 X' e! I% C! \+ g
// called <foo>". You can send a message To a particular
0 R. ~8 F/ w; C$ { // object, or ForEach object in a collection.
- _, S* Z, r* z1 u9 d* l; w( j# Z
; X( J; g) y: [. @ F! }8 @/ e. [ // Note we update the heatspace in two phases: first run7 m7 E6 n# j. o5 R1 ^0 g
// diffusion, then run "updateWorld" to actually enact the
" J, t `1 h9 a // changes the heatbugs have made. The ordering here is; Z; [ n3 H* F
// significant!; I7 x' K# `* e( ~# K3 |$ w
) h; d" l5 V' ~. Z
// Note also, that with the additional
3 I. [* G, C) Z1 H2 S // `randomizeHeatbugUpdateOrder' Boolean flag we can
7 N c8 H8 t7 |7 x // randomize the order in which the bugs actually run
1 ^" ^. D6 j9 \6 v& \2 M // their step rule. This has the effect of removing any4 W9 q5 `, t# V }1 ^
// systematic bias in the iteration throught the heatbug
5 J; X! l: @1 R( e7 b" ]) m // list from timestep to timestep
8 d- _9 N# L \8 E. b7 y 4 d6 T, l% ~! R" w; L! {: A: T) u
// By default, all `createActionForEach' modelActions have- m# R+ h& i2 E6 F; {3 K3 W
// a default order of `Sequential', which means that the* a& h* d6 U6 F; F/ _
// order of iteration through the `heatbugList' will be
4 C0 X& a/ } ] // identical (assuming the list order is not changed
5 v# \/ w: S! o% V // indirectly by some other process).& d+ {8 M% T6 u
/ y- G# o) N/ p/ k3 _3 l% {- A modelActions = new ActionGroupImpl (getZone ());( s9 [2 k+ z, `* c5 a: G0 U, a
- e7 e( o+ h# Y$ ?" \
try {$ l! q7 c8 c! k( |( H5 h0 f
modelActions.createActionTo$message' x) E6 m" p" v+ i0 X: O
(heat, new Selector (heat.getClass (), "stepRule", false));" k5 I( u4 Z5 D8 k7 D
} catch (Exception e) {
p: H. x* D5 `' e, ` System.err.println ("Exception stepRule: " + e.getMessage ());
. D7 @ d: ~/ v }) Q: A& G" {! ]( M' F- Q
i) k6 Q0 b' y
try {7 k& T5 h+ l0 D/ r
Heatbug proto = (Heatbug) heatbugList.get (0);. f( H: {. r3 m. T' E
Selector sel = & W. d% E2 f+ B" W1 m9 @: |1 g8 |
new Selector (proto.getClass (), "heatbugStep", false);
0 P" X5 [6 u! x. B5 E actionForEach =
: p! p6 V* I' [$ |: ^) K5 T modelActions.createFActionForEachHomogeneous$call
2 R Q; v7 }, R1 i: I! @! ^5 U i7 @ (heatbugList,
6 v* R- R0 z: X, [ new FCallImpl (this, proto, sel,' i% @" P- b2 Y+ B5 G
new FArgumentsImpl (this, sel)));
. [- b% |" J% B2 L; Z3 S! b } catch (Exception e) {
2 l8 p s7 q( t8 i% K e.printStackTrace (System.err);
' o- w$ v2 v# A+ x+ N8 i }
5 a6 L; X/ Z# S/ X! o5 J3 Z ( b: c/ p @3 s% z U/ k; E
syncUpdateOrder ();( `: |6 w3 t# d* N1 ]: y8 @
# }8 ^. m$ M7 f) [7 _
try {3 I/ x! [8 A1 l Q7 @& x
modelActions.createActionTo$message ) y3 [! k! L& L; x! p) ^
(heat, new Selector (heat.getClass (), "updateLattice", false));5 S8 d f4 ?; X: C$ y
} catch (Exception e) {, p2 ?3 O- q7 a7 k* a7 \
System.err.println("Exception updateLattice: " + e.getMessage ());' b9 E Q" ^3 {" B! q
}/ E) I1 G8 k5 x% F, z
q! O1 a7 C Z2 q1 q // Then we create a schedule that executes the
" e8 U; b, y* e x* l. d* W5 t // modelActions. modelActions is an ActionGroup, by itself it. A7 H: n G. [; Y) v, s
// has no notion of time. In order to have it executed in
$ G& k4 U! x T$ o0 _ // time, we create a Schedule that says to use the1 C2 }, ?; {' r E
// modelActions ActionGroup at particular times. This
5 a/ k) J5 M ?3 p // schedule has a repeat interval of 1, it will loop every
; N3 B1 ?' ]) H# {. v // time step. The action is executed at time 0 relative to
2 b$ T2 e# R2 i+ \" |( C9 } // the beginning of the loop.* `3 T& [% j& R3 G5 z3 ?/ n3 o Y
! C: C+ C8 T+ [ // This is a simple schedule, with only one action that is) A9 g+ N, N# A q; `/ m3 H
// just repeated every time. See jmousetrap for more* q" A+ ~1 B( z! Y0 h" Q
// complicated schedules.4 S; h' U# Z! r1 `
. O1 V: u9 A. X) V! |' t# _
modelSchedule = new ScheduleImpl (getZone (), 1);
+ j' R, [0 N+ l6 _% j _6 A# u3 Z modelSchedule.at$createAction (0, modelActions);, J" w& W/ ]* l1 O8 c! B' C( d
' ~# t5 f2 I9 l% r4 p+ _9 T
return this;
- r5 w* s K, H; ^, l g } |