HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 | r- _0 r4 z8 ~% t( C" [5 q
: j) c/ F2 j& f" D public Object buildActions () {
! {- k/ u( \6 p+ b' C1 M6 X super.buildActions();
2 ^ V) h, F7 z5 |' X$ L1 U 5 I. z- x& r- o# A9 f. H9 B. \' z# x# W
// Create the list of simulation actions. We put these in
3 @; V0 _ g' c) ?1 |, ?$ e) g // an action group, because we want these actions to be
: j( }8 B4 T9 C8 T& q E6 U // executed in a specific order, but these steps should
8 i, G" |+ z& m2 K9 M // take no (simulated) time. The M(foo) means "The message
' d3 @& h+ Q9 d' v" s" c( y // called <foo>". You can send a message To a particular
1 ?9 H& Q: A/ P7 t- C // object, or ForEach object in a collection.' z' |$ D, G6 _* W
( ^0 ?4 ]1 \, t0 A* B+ Z: V+ F5 J( s // Note we update the heatspace in two phases: first run- c: [$ F( @% o1 t+ S
// diffusion, then run "updateWorld" to actually enact the
' J! Z: u6 R' W! c // changes the heatbugs have made. The ordering here is4 M" X, P: R/ o$ |) `
// significant!
! G+ n X4 S1 D* T5 w
) o: ?$ K1 Y, ^7 f7 R* ~/ x // Note also, that with the additional6 T) C& o" [+ x3 X0 u$ A
// `randomizeHeatbugUpdateOrder' Boolean flag we can
- @# G* ?& A! ?9 O5 A // randomize the order in which the bugs actually run
, q) w! F- Y1 p# ^ // their step rule. This has the effect of removing any
; q: |! r5 f2 ?! \9 g, y2 Q // systematic bias in the iteration throught the heatbug
8 V3 I2 K+ }0 [ a i* F // list from timestep to timestep7 l R; L. |9 Z( U) Q
" |& \9 j4 _8 n$ I
// By default, all `createActionForEach' modelActions have. P3 a e {/ H7 K
// a default order of `Sequential', which means that the. ?! ?1 N: h) V: T) a
// order of iteration through the `heatbugList' will be+ H* I6 N( \ {$ T5 w5 G: r" a, _
// identical (assuming the list order is not changed
5 A! j. c" `4 V { {, ?( Q // indirectly by some other process).
# u+ f2 J7 e% j p# c6 Z: _$ H+ p* @
. {9 z: z" u' }# m+ ]4 a modelActions = new ActionGroupImpl (getZone ());
: Q: E( U, u {7 ]8 G8 a' R$ |& Y' k! j2 a7 q
try {
- V. n. R3 C7 y: g6 P modelActions.createActionTo$message
" }: r+ |3 x9 d2 j (heat, new Selector (heat.getClass (), "stepRule", false));& B$ V0 C- V0 n; W$ H3 L
} catch (Exception e) {. i: w I; Z4 C' W# W8 L$ w- g$ t
System.err.println ("Exception stepRule: " + e.getMessage ());
: v# R! h& [6 u( m" |3 } }9 P, j) ]+ w# v9 [3 R- U8 {
# R- r6 |- G% E& z
try {
0 f( T) Q5 s0 z( |. ~ Heatbug proto = (Heatbug) heatbugList.get (0); E P" o/ v3 X8 b, `% `
Selector sel = 8 X/ ]: K( u, Z e6 r% ^# g5 {
new Selector (proto.getClass (), "heatbugStep", false);
6 Y- X+ t' T! v: @% a% N! h# k actionForEach =
% C- \1 i: s3 m8 s1 N& M9 z% B modelActions.createFActionForEachHomogeneous$call
/ C! D9 L* u$ t (heatbugList,
o$ M0 s8 V: w P7 [6 P new FCallImpl (this, proto, sel,
# ~( T `! |0 N2 u new FArgumentsImpl (this, sel)));' Q! f3 S/ I3 L9 ?( ^
} catch (Exception e) {" j3 }/ n& n2 U" A
e.printStackTrace (System.err);/ S; Z5 y0 C4 U3 I/ V
}
9 X4 i" L7 i& b' T7 [
|$ M$ [( K; |& H% ^# y# [$ n6 M syncUpdateOrder ();
) ]6 x7 c) C- H$ z3 _3 ]3 Q: ~) k1 W) ~) \
try {! L3 D- ^& `5 Z# k& d+ B& ~- O5 K
modelActions.createActionTo$message 8 d5 s6 R' b8 R; v7 W' ]$ H, _% B
(heat, new Selector (heat.getClass (), "updateLattice", false));
1 I X0 S, R9 ~, o; P5 g3 g6 I) [ } catch (Exception e) {
9 w% v) X; H. F0 r System.err.println("Exception updateLattice: " + e.getMessage ());& A, P; x3 Z/ l, ], d1 C- O9 _
}
( {( ~ [8 m0 A
" L! ~8 P! a: P3 q) P) m // Then we create a schedule that executes the* L4 ?7 c( z& r0 K5 l" ^
// modelActions. modelActions is an ActionGroup, by itself it
9 E. F& s. x H# @. b9 D // has no notion of time. In order to have it executed in' v0 n* O6 w# |/ h: m# g$ A; j6 L
// time, we create a Schedule that says to use the Y: Y: T& H, J* @9 b% ^
// modelActions ActionGroup at particular times. This
, s6 u1 i% F1 {# [$ c; r7 H // schedule has a repeat interval of 1, it will loop every; u+ `, [; I* p( h8 f
// time step. The action is executed at time 0 relative to' P& E' E8 ]! M7 Y3 P
// the beginning of the loop." h2 I5 G, y0 h9 o& o
8 f2 D: M& i+ n. _+ g; z
// This is a simple schedule, with only one action that is
$ R1 U) S' s8 a# K$ P' w // just repeated every time. See jmousetrap for more6 S2 L! q9 h o- l( @
// complicated schedules.
) N, [2 g9 p0 U' A
, h$ Y+ U M& ?" {9 K2 g+ c modelSchedule = new ScheduleImpl (getZone (), 1);
7 H% ?, _, }7 f# c$ `7 i. W modelSchedule.at$createAction (0, modelActions);! a% r8 @7 \- p% `" [3 S" o
+ w) |# C! J6 Z- \' x! d return this;- g; G U; S3 ?# o8 e+ d+ K+ a1 y, {& D
} |