HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( j. c) [( O. r$ J" t' Q
7 H. \. _, l- V3 g; G& W7 g public Object buildActions () {
1 }, b! p/ i1 K super.buildActions();/ ^8 i0 E. ~4 @
$ t3 F' E( I. Y' ^5 h // Create the list of simulation actions. We put these in3 s0 C) \( I+ N: P# ~( s1 U
// an action group, because we want these actions to be( G1 @# ]0 `: W
// executed in a specific order, but these steps should
$ E; Y, O. ~0 j // take no (simulated) time. The M(foo) means "The message) I* K5 ]6 H. A. v: h; C
// called <foo>". You can send a message To a particular2 @0 @% k% Q" H/ T6 f4 X$ P
// object, or ForEach object in a collection.+ i- x8 h' q% f8 S3 `, ^) K6 P2 Y
9 m$ L9 \6 t5 d9 O" A7 Q+ E
// Note we update the heatspace in two phases: first run
# L% y% e3 i3 ?; |) ? // diffusion, then run "updateWorld" to actually enact the
( E4 j3 Y1 r0 t! _* g1 u! e6 E // changes the heatbugs have made. The ordering here is
8 N# K- K- H1 x7 b // significant!
) M2 R; V! H; @% {
1 q( v4 f5 n3 {: d5 F // Note also, that with the additional
/ J* n; y6 r( a8 T9 [+ ^) L# ^3 D9 u // `randomizeHeatbugUpdateOrder' Boolean flag we can
! h$ L+ x6 a, d9 ^ r- D // randomize the order in which the bugs actually run
3 ]9 Y1 ^% M4 @/ w% v // their step rule. This has the effect of removing any
% K6 q. U. M- f1 k6 C // systematic bias in the iteration throught the heatbug! R/ f' Y, X' u/ b0 H
// list from timestep to timestep
" f" t: N' i9 N/ [- k* t3 a " W$ k0 h- i; [4 [
// By default, all `createActionForEach' modelActions have% V" X$ {0 c/ y. Z
// a default order of `Sequential', which means that the3 L& E* V# D3 q. Z) i2 R
// order of iteration through the `heatbugList' will be
: P R/ J' n. p7 A* G // identical (assuming the list order is not changed2 A# M' Z: ]; z0 k; c. `
// indirectly by some other process).
; M; e. U0 N* T 6 ~, M: S8 A+ B- i2 C
modelActions = new ActionGroupImpl (getZone ());1 p9 x) u8 I, z0 k
3 x* A! j: P) [# B4 W try {+ C) ]$ h; F2 R4 w) M4 j+ [
modelActions.createActionTo$message }( e$ e e& j; m7 e x( j! Q# p
(heat, new Selector (heat.getClass (), "stepRule", false));
! C- s* {) D N) x" m } catch (Exception e) {
4 a( I+ A5 C' \, C( R* C System.err.println ("Exception stepRule: " + e.getMessage ());! q: B5 Q l( y3 O2 u# a' N2 k' W- j
}' D) T( r% M! u Q, [) j5 V
0 e* [2 W/ O. n% y
try {
( b6 O" `* j$ T, U Heatbug proto = (Heatbug) heatbugList.get (0);" \/ @) M! |( L& ~0 ^6 {
Selector sel = 5 |5 y0 q4 H& }# a
new Selector (proto.getClass (), "heatbugStep", false);
# @: f8 F8 S" K( G2 c! z% ]# \6 C- n actionForEach =
9 N8 |7 @+ n: p1 T5 F& { modelActions.createFActionForEachHomogeneous$call7 p6 K+ [# A: ^+ s# ?% r# \3 O& H& ~
(heatbugList,
2 F& \& R7 y5 J6 _" h4 X! u new FCallImpl (this, proto, sel,: T2 a1 F/ Y; |1 c( ~- J- H- ?
new FArgumentsImpl (this, sel)));
) k) [) o M( M( G; A$ W7 U } catch (Exception e) {$ E1 H; U' D0 A/ X' ^: [
e.printStackTrace (System.err);
4 Q s9 f/ [0 C+ S: n" T4 ` } \8 o' I8 j9 @0 D/ t1 }
7 e' j% z/ F: Z: Z
syncUpdateOrder ();
( u& B. m) @% \5 V5 j! C
. v: ^9 i' g: m. n* K try {& L, L9 ^5 Z. ]3 u" v2 a$ T! S
modelActions.createActionTo$message
) a, |+ C0 o, _' @ (heat, new Selector (heat.getClass (), "updateLattice", false));6 d+ K0 G: e/ [: V
} catch (Exception e) {
1 q1 l6 V3 K4 e7 u) F" p System.err.println("Exception updateLattice: " + e.getMessage ());; `& }8 @) M+ e. f4 `/ O% f9 c
}' D% \% W' M( E0 U* G! k- k1 F* ]
' o1 w/ O8 x2 c8 r // Then we create a schedule that executes the
% \) |6 y% U5 F8 L9 F // modelActions. modelActions is an ActionGroup, by itself it
& H8 o8 K8 ?# X // has no notion of time. In order to have it executed in
3 i, s* p- o3 I, i; O+ l6 n // time, we create a Schedule that says to use the
3 X: F+ S3 b/ k. s$ C // modelActions ActionGroup at particular times. This8 P6 l7 U* r2 t# Y2 s3 r- L
// schedule has a repeat interval of 1, it will loop every
* h0 X. R$ Z) G; N( r+ b // time step. The action is executed at time 0 relative to
9 }5 Z. w; M- y' I9 A( \* l8 L // the beginning of the loop.0 o- W% v1 d2 x" e3 r g
$ J/ U9 D5 `/ o; E& h& c) n // This is a simple schedule, with only one action that is" G- P/ j5 v. N" }4 S+ @
// just repeated every time. See jmousetrap for more
8 i; n7 H0 _% K* H7 ~5 c0 y // complicated schedules.
6 C4 ~# {( l+ M. j - f/ e0 v. c$ p: X
modelSchedule = new ScheduleImpl (getZone (), 1);
4 r" w" K# Q! E6 j modelSchedule.at$createAction (0, modelActions);
6 B1 `" ] [* t, p ; P7 v, `5 d: l6 K
return this;* e+ E4 Q4 J6 X0 Q1 y- L
} |