HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# [5 b X! a. o7 W: I: L7 ?
+ k& f3 m7 F+ T, I4 x8 b public Object buildActions () {
9 H* u/ Y6 h3 X+ y- J super.buildActions(); O0 w; C ~4 ]# g; d
5 u. k. z8 N, ?8 \4 K
// Create the list of simulation actions. We put these in
M6 R7 p9 i7 M2 @1 g- I% Y! N* | // an action group, because we want these actions to be. e8 x r0 H# b4 Q. H; @
// executed in a specific order, but these steps should
$ j( ~; L- {9 M3 Z# e; p6 V // take no (simulated) time. The M(foo) means "The message
8 r' U" n% B; ?3 v1 U4 n% n // called <foo>". You can send a message To a particular9 } y0 D7 \: M Q
// object, or ForEach object in a collection.' n* r$ x. V6 y5 ~( |
8 q O: O' q1 f6 s // Note we update the heatspace in two phases: first run
7 K' }. B* O( B8 t1 k3 Y: ~/ G- _ // diffusion, then run "updateWorld" to actually enact the2 ]- o$ J; W: l h" g2 q+ O0 J$ C
// changes the heatbugs have made. The ordering here is( z2 ]* W, `( h! F/ G# ~
// significant!" B6 d9 I0 n; K/ l4 n, C' L. o& }" a
. ~3 N# Q# @+ C/ u // Note also, that with the additional
/ O) ~% E, a6 _3 q4 ?: L3 n // `randomizeHeatbugUpdateOrder' Boolean flag we can' y% j$ Q8 x' T9 |, T
// randomize the order in which the bugs actually run" l9 }% o! e. ~
// their step rule. This has the effect of removing any
8 h; z# W; n( M9 _ // systematic bias in the iteration throught the heatbug8 u( K) C: O+ x0 n8 X
// list from timestep to timestep
* ]" w" U5 f& c4 O) M; D+ E* C# E
# n: x( Q/ M0 g M; o8 _' q& a // By default, all `createActionForEach' modelActions have
* X0 m6 L' }" A( c. H // a default order of `Sequential', which means that the
7 w: o( ~8 Q& E // order of iteration through the `heatbugList' will be6 q) c% G8 @. G( [8 O
// identical (assuming the list order is not changed
1 Z! m5 s$ |5 Y2 a; L. p2 z! C/ O // indirectly by some other process).
4 o! h4 A1 b& ~
! q- M% S$ T- p3 p0 B$ ~" U4 u7 [ modelActions = new ActionGroupImpl (getZone ());- K& u! m" w J& N0 J# P! B- w p& x
. c7 g+ t$ O* G% h6 b1 i% R
try {9 w' \0 h% ?: H
modelActions.createActionTo$message
; X7 G4 h! M E. _0 q (heat, new Selector (heat.getClass (), "stepRule", false));
. \+ T0 ^' f* C! K- S- h: i. ^ } catch (Exception e) {4 B/ U: s" u4 f5 Y$ q! k$ C
System.err.println ("Exception stepRule: " + e.getMessage ());3 _9 k' o9 f/ A
}% O+ r$ L* h! F" i+ U
/ _- M7 W/ e9 G& j- X; N9 G9 J try {
: W0 X1 l1 C+ K8 k. [( r7 R. N x5 P Heatbug proto = (Heatbug) heatbugList.get (0);
* |; u7 P( E' { Selector sel = ) K$ V+ d4 @" B0 _& |. c
new Selector (proto.getClass (), "heatbugStep", false);
; F N! G' ]# G ?# O" ~ actionForEach =
' p, d6 ~: B# C5 G# @, N modelActions.createFActionForEachHomogeneous$call4 P/ `7 u: n+ I: M M" D
(heatbugList,) m# N7 W6 k$ R5 }
new FCallImpl (this, proto, sel,
' G& A( L U, w& a: [$ R new FArgumentsImpl (this, sel)));2 v/ H/ J2 R$ i. O% \4 `8 g, \
} catch (Exception e) {
. K; }# Q0 ]% _1 ], b2 i [ S: S e.printStackTrace (System.err);7 z$ w v/ e* J, ]. Z. M T
}% E; [5 Z9 }, T- x) n# r% d
& ^3 @3 O9 h. [0 C* W
syncUpdateOrder ();
; b8 r, m8 u6 Q$ ?& B1 y: M+ ], d8 K5 b2 f3 v
try {
# M& L% s6 M/ j modelActions.createActionTo$message ' f8 `% u* A/ N) U N; O
(heat, new Selector (heat.getClass (), "updateLattice", false));
5 c; E! w* O2 I& f8 m+ x } catch (Exception e) {# Q, K) @- `; [8 k! f4 t
System.err.println("Exception updateLattice: " + e.getMessage ());) g- G. A% j/ L# Z* Z1 |
}' q8 _8 M+ }; l/ W7 K# c' a
$ p/ X0 {/ u8 v' {+ [
// Then we create a schedule that executes the5 j% ^* u K* @1 w# P7 G, {1 l8 i
// modelActions. modelActions is an ActionGroup, by itself it
& A4 |. E5 t+ Z$ b- y$ [% @4 y2 b# w: ^ // has no notion of time. In order to have it executed in3 G# b% W. i/ [9 Q3 k& B5 M
// time, we create a Schedule that says to use the
" j6 q3 }9 {- |" ]0 u( b // modelActions ActionGroup at particular times. This0 d- _1 c7 R; @! [" s3 ]
// schedule has a repeat interval of 1, it will loop every2 u4 U, q/ Y' C1 K5 }; F
// time step. The action is executed at time 0 relative to
) _9 d4 [2 \+ n8 w0 P, T // the beginning of the loop.7 p2 Z( R$ W/ J
& f9 l' L1 f3 P1 E
// This is a simple schedule, with only one action that is; c! g) P$ p/ n' h& \+ B
// just repeated every time. See jmousetrap for more
( r$ w! w! f' h4 q( f1 Y0 l2 d // complicated schedules.
) P8 m( s( N7 H4 Q/ l 0 D; p: `, _/ ], A
modelSchedule = new ScheduleImpl (getZone (), 1);, G! G* K- c* N9 t- J" c
modelSchedule.at$createAction (0, modelActions);$ t# q# L. g8 Z7 D4 S4 U4 P
* P1 y' |# _, l6 d return this;1 p. Q7 Q6 ]4 {# \2 t' m1 v0 w
} |