HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- Q) I& l8 W2 b3 s
) N2 g6 ?% p% d public Object buildActions () {8 @1 h& g2 k; |) H9 s" X, x. y3 j
super.buildActions();4 q& J+ A; T% F; {* \: \( O
( L& Y4 K4 e% w: \+ f% Y6 g+ c7 P // Create the list of simulation actions. We put these in
! ]9 @2 L- ] X5 R! ^ // an action group, because we want these actions to be
: O! n+ N/ Y- |3 Q- i& { // executed in a specific order, but these steps should
! K8 O7 J; M0 i, r* p6 C // take no (simulated) time. The M(foo) means "The message
* |5 B8 J& H4 ` V! X" u( y! n2 l3 N // called <foo>". You can send a message To a particular
* R4 K. a% j, X) k // object, or ForEach object in a collection.
2 o! l2 w9 Y6 v1 o8 @ h% ]
" p5 x, i. Y# i- C+ | // Note we update the heatspace in two phases: first run
+ k; s& c2 G8 x0 L1 W // diffusion, then run "updateWorld" to actually enact the8 s+ e5 e. K0 O4 T) }
// changes the heatbugs have made. The ordering here is
( S/ ?0 q! @; f8 P- w0 d9 ` // significant!2 [& T! N, D4 u2 M l L; F& S6 p
/ B) w- n$ K8 W; a4 [) p' l% T8 B
// Note also, that with the additional8 Q" J$ r- ` u& f9 B& _' }& k/ x
// `randomizeHeatbugUpdateOrder' Boolean flag we can
* G3 i1 }9 l8 U, c. Z // randomize the order in which the bugs actually run4 V1 C3 ~, _" i8 r; q
// their step rule. This has the effect of removing any! B" [* b5 L2 d; T
// systematic bias in the iteration throught the heatbug, N. O/ p+ q4 l; `- _' p1 T
// list from timestep to timestep
4 A# b D' r k# K/ K2 w
# D9 D4 E6 ?' U/ c) ^ // By default, all `createActionForEach' modelActions have% X3 L4 E6 y6 b/ A9 W
// a default order of `Sequential', which means that the8 O* _ W7 b1 W, l& C# U
// order of iteration through the `heatbugList' will be! d) e2 u# l `0 s& b
// identical (assuming the list order is not changed: |+ b( e6 Q- a' E" {# p7 V3 _
// indirectly by some other process).
- K k9 @$ |6 u& l* q9 T4 I- B
* N" a4 p6 {7 U/ p N+ N7 \$ v modelActions = new ActionGroupImpl (getZone ());
2 A2 ^0 M7 i8 T# _. [/ L* ]; F3 u) y. [+ B9 p9 a3 k3 w* i
try {
2 d; ^" f, r# l# I3 ~ modelActions.createActionTo$message2 L' `% E. I* ]- U) F ]
(heat, new Selector (heat.getClass (), "stepRule", false));
( {& Q; \+ x3 f) a+ @# v } catch (Exception e) {
. g9 M9 E4 _; E5 o# J System.err.println ("Exception stepRule: " + e.getMessage ());
( O4 D" U1 M: o& Z% g( J }/ p. s2 K [* D
7 j5 J( ?0 _8 b% _, G1 L# t. Q
try {# ] e( j8 u+ n- y0 h8 X
Heatbug proto = (Heatbug) heatbugList.get (0);* ?* Z' [1 P7 T. V- V
Selector sel =
4 F, Z0 h( V o new Selector (proto.getClass (), "heatbugStep", false);! ]- w. w% l; \+ j9 z( `% |& N$ C0 w
actionForEach =
' m; u; t: Y7 s: K7 S3 b7 Z6 y; Y modelActions.createFActionForEachHomogeneous$call
4 |: m: L$ ~+ o+ Q4 u (heatbugList,
1 a# b$ A5 {7 S) m1 g' V new FCallImpl (this, proto, sel,6 k1 y, f( y6 [
new FArgumentsImpl (this, sel)));3 w$ {5 o" ~' J8 @+ i2 i, l5 S! ~3 w
} catch (Exception e) {* q5 k6 ~" n% N6 Y3 b
e.printStackTrace (System.err);: X4 B* y; R- n* Q2 k3 _
}5 l- A( M& o1 W3 s V
- R- Z, h6 n1 }& H
syncUpdateOrder (); y: e) D6 x1 D1 C
3 E. S% ?- t& N9 z try {0 P1 j/ c. X+ l
modelActions.createActionTo$message - K5 |; [% S$ [ c5 h
(heat, new Selector (heat.getClass (), "updateLattice", false));
+ W7 q# e; X9 D1 h+ Q* f6 y$ i } catch (Exception e) {
& o0 i1 G6 r q( Z System.err.println("Exception updateLattice: " + e.getMessage ());% k$ s% x( S: m% g- L6 U
}- g5 l9 l. i& W
# m2 ] I3 f3 R) y* l // Then we create a schedule that executes the
8 Q. x1 b3 }; j# l; Q* {0 b9 k$ i // modelActions. modelActions is an ActionGroup, by itself it
6 J) [4 T6 z8 {/ s# q1 \& p' n' L/ I // has no notion of time. In order to have it executed in$ U+ @! l9 S' t: f
// time, we create a Schedule that says to use the
" u- B# x! W# x2 q+ q) o' Q( z h& M // modelActions ActionGroup at particular times. This: y( S0 V6 u. ~) B0 I0 ]% ~9 G& j+ O
// schedule has a repeat interval of 1, it will loop every& b0 N' U& W6 P8 t0 z3 r
// time step. The action is executed at time 0 relative to
0 a8 k- ^) u2 t9 D D5 ~% E // the beginning of the loop.
6 Q" j) t! t2 _3 E) m) E$ V8 w3 t+ k7 B1 I0 d8 S9 ?
// This is a simple schedule, with only one action that is
: ~, z" f0 B5 G a v/ b9 R" ] // just repeated every time. See jmousetrap for more
6 \0 f/ x8 t4 r ]5 _7 m0 h8 a1 \ // complicated schedules.
0 @. }6 Y1 h* `4 _
; A X/ ^, j, b9 Z2 u, s7 I$ e' D modelSchedule = new ScheduleImpl (getZone (), 1);6 @; a9 H7 ^0 K" ~6 v
modelSchedule.at$createAction (0, modelActions);
: L" B- \" Q0 P, I% q2 F" ~0 `1 e 5 F7 S; I6 ^# H$ H. V6 G1 t; B
return this;9 z9 C% w8 C$ H3 C
} |