HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
6 N ?1 `* e' v7 X7 Y
! d/ b$ [, [4 i4 M! m- { public Object buildActions () {! R& {! g" E) X. v3 ?8 y+ B
super.buildActions();- A& q: M0 N% ~; B; Y/ c1 V% W3 p. T; y
G, J; T2 v R* `1 ~; W
// Create the list of simulation actions. We put these in8 |; L" i# D- A! u/ o
// an action group, because we want these actions to be# W. }8 c! @9 b1 d0 k* v+ w
// executed in a specific order, but these steps should" d- h# D+ g9 ^5 y; c+ c. f
// take no (simulated) time. The M(foo) means "The message
5 y2 j$ h3 K5 B! M // called <foo>". You can send a message To a particular8 f. X# O B- W# d L/ N3 P/ y. U5 n
// object, or ForEach object in a collection.
& a( T! ^7 u1 s) T& n* \( x; r1 t* L & q& h: b5 v" B
// Note we update the heatspace in two phases: first run* W; c5 R; X$ F$ q, V+ f9 n
// diffusion, then run "updateWorld" to actually enact the7 }8 H" f! w3 V1 z
// changes the heatbugs have made. The ordering here is
4 ~! e$ q. _# y // significant!3 F( e, {# U7 `. a1 e1 j- c
9 }/ a6 @" _0 \$ D$ L8 @; U( t. o // Note also, that with the additional! N, f/ t5 S& d! I( I/ A
// `randomizeHeatbugUpdateOrder' Boolean flag we can/ ~) u# M2 U+ e7 B
// randomize the order in which the bugs actually run/ V) V$ f4 I1 `: e7 P+ D: y. k% M" }
// their step rule. This has the effect of removing any4 K; ^* p0 ?# @& J, b
// systematic bias in the iteration throught the heatbug: K# j G9 k+ P
// list from timestep to timestep# D% b* N) ?, i( g; O% v& P
) P2 ]/ E0 ^2 O, A6 Y
// By default, all `createActionForEach' modelActions have. R1 a; c* f' P" a: d+ f
// a default order of `Sequential', which means that the
1 H- |* a( C- W9 K. G* C // order of iteration through the `heatbugList' will be
. H9 |6 N+ n8 I1 G5 E // identical (assuming the list order is not changed: p- S" x8 U% k2 v+ f0 O0 |
// indirectly by some other process).. ]& o/ y& S. D- v* v
" C/ v- i+ H' N3 b N4 k
modelActions = new ActionGroupImpl (getZone ());9 ~) |' o2 a! A. B2 t; Z
_$ b! m% p n& t" O$ a try {
0 {. U! ?2 z3 u+ \- O8 ?* W modelActions.createActionTo$message
9 } D' x2 w/ d (heat, new Selector (heat.getClass (), "stepRule", false));
' }$ ]- U4 q4 e+ }5 J" s; l } catch (Exception e) {
/ c. F$ u. t9 e1 g# R System.err.println ("Exception stepRule: " + e.getMessage ());# t: @7 Y' J2 o" n; h6 L" p
}# O- h! G1 o7 P2 s
# B$ ]( H* {, i6 L$ s# R, p2 X u% _
try {) z2 P- Q: h6 n% J8 X
Heatbug proto = (Heatbug) heatbugList.get (0);5 ^6 W: {' R2 f0 O! @
Selector sel = 4 |1 n' ?7 [8 E& t/ S& ?
new Selector (proto.getClass (), "heatbugStep", false);
1 k. Y" h; `8 w4 F% }7 o& U actionForEach =
1 B. s. h; X) s/ S, A& r" }$ e modelActions.createFActionForEachHomogeneous$call
" W5 M9 M8 e5 j, N+ {8 `, q (heatbugList,
! @# b4 @: }5 `& W2 W* C) n" x0 l+ w: G new FCallImpl (this, proto, sel,
. ?' ?( Y2 a: _& F7 N) z new FArgumentsImpl (this, sel)));; ?- P' o) h1 {5 [
} catch (Exception e) {) p- c5 u6 [. X1 t- d# C& d
e.printStackTrace (System.err);* N+ G% k9 L& d
}
+ f' p' M: o6 g" N 8 `( X1 Z& ]* I! H+ w2 n: {
syncUpdateOrder ();
1 B" r3 s8 Z7 f; z, X
1 w) T# R4 Z* M( f& m, a try {3 s" L4 w9 x; d z! V
modelActions.createActionTo$message # x5 R* s7 |# J0 |
(heat, new Selector (heat.getClass (), "updateLattice", false));4 ]: R: x4 G7 ^) S
} catch (Exception e) {# V) T: R' [7 Z3 [) b
System.err.println("Exception updateLattice: " + e.getMessage ());
( J' x- K- M- c# o+ N }; M" J6 K% }, L+ |! s" C' u/ S
: A' {* a, ]$ S* T
// Then we create a schedule that executes the
8 ^+ I! Z/ |- N" F0 K# B // modelActions. modelActions is an ActionGroup, by itself it+ R2 [: ?/ d" Y9 X) G' e) {3 |) b
// has no notion of time. In order to have it executed in' Q! Y: Z7 j) L& u' _! A
// time, we create a Schedule that says to use the1 W7 E4 p. J4 d) C& o# G
// modelActions ActionGroup at particular times. This/ r* I% a; v' ]% Q* \; b+ L* R1 j
// schedule has a repeat interval of 1, it will loop every
" K9 ^/ j: f7 k8 v // time step. The action is executed at time 0 relative to: B" s( D% |* N2 l0 @8 K' M
// the beginning of the loop.) z5 T( o5 L8 X! M
+ U# s3 M* T2 m // This is a simple schedule, with only one action that is
* v1 W7 \5 X' x" h; ] // just repeated every time. See jmousetrap for more
" H( T ]# Z, X8 g // complicated schedules.
6 k7 v2 W& G9 W$ _ ! s, l9 p1 E8 s, ^, O# {' y4 A- D! R
modelSchedule = new ScheduleImpl (getZone (), 1);0 _ c. c0 t& N p
modelSchedule.at$createAction (0, modelActions);
0 v. z! }* p) X3 J, x D, f3 Z
" X) B. l! m/ d% x3 d' g& r return this;
3 n7 C7 ]' S2 P0 A% Q6 N, U } |