HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
2 x- G1 |5 C7 s- L; e e) Z3 [; B- u4 @0 R- P1 P2 Y7 H
public Object buildActions () {
/ r. M( v/ v- w* x5 {6 p super.buildActions();
# W( H! O- |- f9 h. I
3 [! |. o9 @0 D; o2 r$ e // Create the list of simulation actions. We put these in3 t4 g, y& G$ x
// an action group, because we want these actions to be+ V, a2 Y4 m: d$ N1 e- q
// executed in a specific order, but these steps should
8 q7 z# _4 e" |: e: W7 t3 @ // take no (simulated) time. The M(foo) means "The message9 c2 n0 C. J3 K$ q& h+ m* l
// called <foo>". You can send a message To a particular
' B+ v. H% ^! n: r1 I5 } // object, or ForEach object in a collection.
+ o) D5 n' T u
$ o! H2 `8 Q% s% `. `/ j; _2 w // Note we update the heatspace in two phases: first run
/ h/ o9 B2 P. v' | G6 | // diffusion, then run "updateWorld" to actually enact the' _+ z( R0 X+ V% V3 f: R
// changes the heatbugs have made. The ordering here is; U6 _* d5 ]1 z& ]+ S
// significant!
" ~- o# C% \6 Y, }% Z 8 Q4 z% x/ i# u* G9 J
// Note also, that with the additional
, e3 T/ I3 d# g3 K // `randomizeHeatbugUpdateOrder' Boolean flag we can
/ I4 o2 k9 D6 d7 ?) z- r' i // randomize the order in which the bugs actually run
: ^$ Z6 u7 _* T7 h8 c% \& ~1 @ // their step rule. This has the effect of removing any w& I( W: k6 ?( X7 s% ?6 C8 P
// systematic bias in the iteration throught the heatbug+ y7 {) }) }# ^' M9 G
// list from timestep to timestep" N) j i% h, ^# `& j; d! r
3 Q/ b$ _' p' P+ l" X- Z
// By default, all `createActionForEach' modelActions have
1 A6 F( u+ D. b# r: D // a default order of `Sequential', which means that the; U* Y% ~: ]; e! y
// order of iteration through the `heatbugList' will be
! j& F0 A7 A& f6 Z8 q; h // identical (assuming the list order is not changed1 U, b1 g: H3 G+ q" j
// indirectly by some other process).
0 D4 R/ j f/ \
: s/ V! d7 p4 M0 n G1 L modelActions = new ActionGroupImpl (getZone ());8 E/ x c9 \; F
0 t! p) h4 | n" a1 p; {% q
try {
+ K$ ^) T' K+ o7 } modelActions.createActionTo$message
; l* s6 r$ b: q: ^) a: h" J8 q (heat, new Selector (heat.getClass (), "stepRule", false));
" T/ |6 _6 B3 y$ }# q } catch (Exception e) {
8 U% n7 S/ G7 O System.err.println ("Exception stepRule: " + e.getMessage ());
- s% \/ { P$ c: V( @7 g }
, k/ j- U1 J# @+ }% D5 W2 }) P+ f) q# E; x
try {
S! C5 a, ~" }9 S) L0 ~ Heatbug proto = (Heatbug) heatbugList.get (0);3 Y3 K2 ~8 Z. S( A; n1 ]
Selector sel = + Z( V7 R9 v/ s2 }
new Selector (proto.getClass (), "heatbugStep", false);
7 O3 y% G" H2 z: \$ k9 p actionForEach =
2 L% \: N, t4 O) C/ h" i modelActions.createFActionForEachHomogeneous$call
2 D$ P* }$ Q* O& R3 ]! ? (heatbugList,
+ l" O( n ]! k* T V. a; v5 ?# D; s new FCallImpl (this, proto, sel,8 S9 a1 R) W# _5 O M' \: P! j- o
new FArgumentsImpl (this, sel)));
1 P8 V4 z$ c/ h7 H0 w9 V } catch (Exception e) {8 `% v! u7 d8 X) e6 K
e.printStackTrace (System.err);7 I$ M8 z, Y( F; @; k; Z
}
8 x2 _& c) j* u4 [ # Z& y$ O6 b) i$ p- }! W* J# W
syncUpdateOrder ();0 G, I3 K$ E" p8 H0 M, A5 H
% s/ a' m* x0 ^. ]1 ~% a
try {7 v6 X9 M: b$ @. k& I8 b1 H* O( l
modelActions.createActionTo$message
& [8 Q: S% o6 v7 m f (heat, new Selector (heat.getClass (), "updateLattice", false));% D7 N, g8 A" x2 b1 d% {
} catch (Exception e) {
3 W- V, c1 N/ c. L+ o System.err.println("Exception updateLattice: " + e.getMessage ());
8 K5 l" A! \ o0 e$ A$ `4 Z) s }+ d' ~) j- I; k3 Z8 Z9 ~8 }) @4 ?
/ y1 a/ d4 c% ?. P W1 _7 C // Then we create a schedule that executes the a6 o" h( z3 K; @' d$ H$ g
// modelActions. modelActions is an ActionGroup, by itself it
& D% l4 N, f) V: Q // has no notion of time. In order to have it executed in$ I( p5 V" W- J2 P" k$ F
// time, we create a Schedule that says to use the. L) S7 T& x2 h/ }$ G( `5 b
// modelActions ActionGroup at particular times. This5 T5 V/ R( T) Z) z# O
// schedule has a repeat interval of 1, it will loop every9 V; D/ I; D( D0 P/ _* \
// time step. The action is executed at time 0 relative to0 U; Z* A8 W N% I2 n8 Z5 L# A
// the beginning of the loop.
- Q7 F! |/ O& S% J# g
3 U) N4 o7 v( P+ b) }# G // This is a simple schedule, with only one action that is
3 w, e* ?5 }& {( t // just repeated every time. See jmousetrap for more+ a0 l' T: H4 d$ Q
// complicated schedules.
% V. C% q7 ]; n) d6 C% U% ~
! f7 h7 T! j! o5 a modelSchedule = new ScheduleImpl (getZone (), 1);
% Y' P/ g& c+ t, z7 W" S6 H8 M" \ modelSchedule.at$createAction (0, modelActions);1 ?3 n. G2 |1 |$ q- P# A
9 X) W, b5 g! J, F return this;
4 l1 g, y8 P! m& s# T" e. H- {4 \' O } |