HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ J7 T7 U( B+ Z9 `* s- u3 Y
. z3 i/ H ]- Z4 b public Object buildActions () {- D% Y# a5 ?. P
super.buildActions();
- A* n4 U6 q, c4 x$ C- \1 ]; j$ ^
% Q0 c0 V: L7 E- o' ]2 F // Create the list of simulation actions. We put these in
, j& x6 Q* Q6 d8 ~/ F // an action group, because we want these actions to be- ]; n% j: V5 K) x- _5 K9 `
// executed in a specific order, but these steps should
: K4 ]$ N9 U; V' _ // take no (simulated) time. The M(foo) means "The message) W V2 W* n4 i9 B
// called <foo>". You can send a message To a particular( ?" S: Y3 ~+ B% w
// object, or ForEach object in a collection.; {: d A2 n( h* T( S0 w- ~* z
j% x, Q, t Z6 E7 o- ]1 g4 `
// Note we update the heatspace in two phases: first run
6 E! y I; C) {* D- G; Z9 S // diffusion, then run "updateWorld" to actually enact the
5 l# K1 U* K2 K9 A$ |2 d: X0 ? // changes the heatbugs have made. The ordering here is
3 ?. n' X7 D# `7 o+ n // significant!( }! r& R5 F2 G; Z( `
: S4 x8 G5 \9 k1 I& n# t
// Note also, that with the additional8 I2 N1 A. l/ t9 S& G$ U) e
// `randomizeHeatbugUpdateOrder' Boolean flag we can
, Z6 c; _. e# D$ d# x. v$ R // randomize the order in which the bugs actually run4 M+ S, t y& A' q# j! ?
// their step rule. This has the effect of removing any
! E# |. {2 z) y // systematic bias in the iteration throught the heatbug4 P. {1 f; V8 n/ D- c3 H
// list from timestep to timestep
: _4 O% H: _8 l2 h* e8 R V6 D/ X 6 @) J1 S0 G! p# x8 _8 y
// By default, all `createActionForEach' modelActions have
1 J' a" H) U) u, I/ k; o+ U1 f/ C // a default order of `Sequential', which means that the
' }1 D" A( C$ j1 w5 y // order of iteration through the `heatbugList' will be
2 n0 h5 ~& N; x. @/ [ // identical (assuming the list order is not changed
8 b9 s( ?: P9 R$ q // indirectly by some other process).% ]' V1 |" _4 c2 c9 G% A! [
( I6 t: q0 k& Y* M6 ` modelActions = new ActionGroupImpl (getZone ());
; L' P( F/ x5 H* m
% o0 x& N/ N" G! T7 ?, r* n$ P2 C try {
5 {' \; h0 v3 t( D* y/ ^0 e modelActions.createActionTo$message
4 c) J0 p, j+ g+ T( Y, ~# L4 S (heat, new Selector (heat.getClass (), "stepRule", false));1 A! j9 e) U# f4 {
} catch (Exception e) {' t. O& H. j+ l
System.err.println ("Exception stepRule: " + e.getMessage ());
; [: J' ]; s3 s }
( R' e; y m* N; v& V2 g h# b9 Y$ U& \, `; |/ I6 g
try {
9 f( G; R: Z* X6 @1 _* n2 V0 z Heatbug proto = (Heatbug) heatbugList.get (0); ]. d- I3 G \! M2 z& r# h k2 u! T
Selector sel =
! j) f0 o) y. \' Q2 L) p new Selector (proto.getClass (), "heatbugStep", false);" o+ b' n0 H' w3 r/ @6 H
actionForEach =
4 N. p7 n; [9 ` modelActions.createFActionForEachHomogeneous$call
! V v. F0 A: d6 h v0 G (heatbugList,4 @+ ~0 z8 P7 c7 Q% m: n$ j
new FCallImpl (this, proto, sel,! n" M! p6 `% M/ Z, N5 j. ?
new FArgumentsImpl (this, sel)));
9 l5 h8 m& k' l! H0 w# L5 g } catch (Exception e) {
4 O/ F) Y, A# |6 N: H e.printStackTrace (System.err);
+ A: o' s, y* }6 M6 Q& b* o8 S }8 K' f0 j( G) \
% g6 \& D0 O" y, I* w
syncUpdateOrder ();
Q. A- `2 K8 A" h3 j- m$ U' I$ `; e' B) ]
try {
8 O% [7 K. {! `) _2 V( z modelActions.createActionTo$message
7 }9 m# C4 j' g. K5 _' c% S (heat, new Selector (heat.getClass (), "updateLattice", false));3 M1 c" X. I* N' Q
} catch (Exception e) {
7 v* ~6 {9 M5 X: V% J0 w System.err.println("Exception updateLattice: " + e.getMessage ());
; C. y/ F9 V: n. I; K8 J }& {3 c1 A, Z$ Y% H
8 U% ~. {- C2 n, q5 f
// Then we create a schedule that executes the6 m9 o( O4 w9 E, D- W+ ]" ^ W, o
// modelActions. modelActions is an ActionGroup, by itself it1 p7 H$ E% x) i4 X
// has no notion of time. In order to have it executed in
+ q) Q) A% J$ h# p5 ` // time, we create a Schedule that says to use the
# o/ w+ E3 n! z- W0 B r0 T* R // modelActions ActionGroup at particular times. This
) j! y% W( [9 {& y1 P. J/ _) Q // schedule has a repeat interval of 1, it will loop every/ Z3 R+ ?4 b& \" q& g O7 L
// time step. The action is executed at time 0 relative to$ D; l. B9 b6 @1 v; Z
// the beginning of the loop.
' n/ o: p g' Q" z0 X' }
/ @1 \1 A: ^7 g- B! d // This is a simple schedule, with only one action that is
7 |- t: v* N9 w, `' q# m# _ // just repeated every time. See jmousetrap for more
- v$ Q! V0 A, B9 H // complicated schedules.
( R* {* k/ p( E9 J& h9 W) q$ ` - H2 b3 k0 z9 T& G
modelSchedule = new ScheduleImpl (getZone (), 1);3 s5 o4 A4 F5 }" N; Y& T y6 r
modelSchedule.at$createAction (0, modelActions);
2 Z8 o2 n, ]! \9 s3 A # J! S2 I0 b% n7 F; t+ ^' f
return this;- c( K2 n: L8 H0 X
} |