HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:( i2 Y7 g# t, s/ X
2 B, }) m8 z* b0 g public Object buildActions () {4 n8 w5 n2 X, j/ p5 C: Q# E% T% A3 w& f
super.buildActions();
) `& L$ k. I# O5 U2 |$ p! |4 q7 U
& i! u6 ?. i! N7 N4 l1 m3 O // Create the list of simulation actions. We put these in
* _4 ?$ N7 Z- B# y* C% o+ Z // an action group, because we want these actions to be
( s0 q( l6 \0 i5 J! O$ m // executed in a specific order, but these steps should; n" X5 T0 K4 C& ]3 t. c1 u
// take no (simulated) time. The M(foo) means "The message
& U& z, f+ F6 a; X8 i) h1 G) i- W4 w // called <foo>". You can send a message To a particular
) ~2 i8 S+ }2 |2 T4 Q6 Z // object, or ForEach object in a collection.* q4 b6 e0 j& I+ b, [/ o
' }8 |6 ~! d( I3 c; d2 v ^ // Note we update the heatspace in two phases: first run* \. @* O6 Q) F0 ~/ T7 v
// diffusion, then run "updateWorld" to actually enact the
; V- `: v h. {1 d% Q // changes the heatbugs have made. The ordering here is1 B* _$ N/ J# `5 M) }6 v
// significant!2 c& S V0 R, W1 G
/ G2 v9 M% M2 o0 |% c* ] // Note also, that with the additional. f/ ~5 }1 e+ H8 p1 x; J
// `randomizeHeatbugUpdateOrder' Boolean flag we can
2 X! q, G$ W4 M7 J8 k$ j" F // randomize the order in which the bugs actually run
2 x1 m I* p' r! [: H5 p6 ?+ C$ r- k // their step rule. This has the effect of removing any W0 `+ y" w0 i# q: m
// systematic bias in the iteration throught the heatbug
4 w# x7 u8 P, K6 @* Q* R O W // list from timestep to timestep& g$ O5 [# G& @9 @8 Z
$ U% w7 L7 J+ L9 }
// By default, all `createActionForEach' modelActions have
' z+ ] a {& f3 f! u: d // a default order of `Sequential', which means that the
, K2 x9 B5 J3 R, B7 `' u) W' }& d. } // order of iteration through the `heatbugList' will be3 N' y. m& F# Q. v2 u) m
// identical (assuming the list order is not changed4 j! {: [' f3 v. ]5 W" e7 E
// indirectly by some other process)./ e1 y% y% \+ Q/ o) `8 S
3 e5 H. C2 M& R modelActions = new ActionGroupImpl (getZone ());
: J- L5 H8 S0 Q6 M6 d% d. N9 Y
: x: A+ x7 w& l; I# \# {3 [+ ` try {
9 F N; l! Q% w4 `$ j, b3 K modelActions.createActionTo$message. V+ l @/ l' H! J* o
(heat, new Selector (heat.getClass (), "stepRule", false));, F! U5 ^# {" g; i6 r* V
} catch (Exception e) {( ^$ L$ O1 B6 @+ o2 F9 A
System.err.println ("Exception stepRule: " + e.getMessage ());
% i2 n0 b8 W9 X" { }
+ x F/ y4 w. V8 Q, i+ j- G3 K3 d! T2 m" c
try {- P7 x0 N: @+ w3 B# k+ K
Heatbug proto = (Heatbug) heatbugList.get (0);
$ o4 ]" I L2 r Selector sel =
* ]( g: B4 r; I% D, w0 F new Selector (proto.getClass (), "heatbugStep", false);
; \: Q9 o& H$ Y8 X1 @* ~' D7 C! G# E actionForEach =) R& g: u O5 {" _" o6 U; }
modelActions.createFActionForEachHomogeneous$call
^( I% v- l; P( T1 B# j (heatbugList,' a, [& ?+ m. r( {) Q& U7 b
new FCallImpl (this, proto, sel,1 x& f3 ~7 [$ z. n, N
new FArgumentsImpl (this, sel)));
& q7 |! W9 W8 U' i! G } catch (Exception e) {
/ V0 c6 y2 Z1 c+ {2 K3 R0 F3 j S e.printStackTrace (System.err);
0 u+ i, u. x; P$ W* d4 @# @ }( H! z: z) K4 u+ W% `4 g
( T- Y9 n/ [4 o. P2 [- [; B, R! J
syncUpdateOrder ();
# l; j h# a' ^: Q4 r& }5 R2 D3 c$ p, r3 U3 s& Z. s
try {
$ L0 ~3 u/ o0 W; g modelActions.createActionTo$message 6 z" H* u0 s& @
(heat, new Selector (heat.getClass (), "updateLattice", false));9 I1 c7 X9 ^% T+ _, N$ W1 u
} catch (Exception e) {
8 J4 l; I7 x- V8 U9 ?2 b( ~& o System.err.println("Exception updateLattice: " + e.getMessage ());( {8 v! D' P, [8 k" y2 j" U! V
}% q# ~) V# j8 u0 R0 H. e: u% `
" c+ [3 s5 R: o9 D7 {7 v; v' r // Then we create a schedule that executes the
5 }6 u( a3 o5 N) a // modelActions. modelActions is an ActionGroup, by itself it# t. z9 h% b% d& ~, c
// has no notion of time. In order to have it executed in
3 x' [6 s, I' j; N5 ^ // time, we create a Schedule that says to use the6 i8 U0 q& j% G2 o
// modelActions ActionGroup at particular times. This
q6 `1 s) J% \7 ~& w // schedule has a repeat interval of 1, it will loop every0 K' h" d% n6 F2 L& o7 ^3 j% ~( H4 H
// time step. The action is executed at time 0 relative to
% F4 ?! ~6 s4 @1 p* X: E* I // the beginning of the loop.
8 K* l5 S9 _$ r9 I* P' n2 E$ o* Q6 V& m1 U4 w
// This is a simple schedule, with only one action that is- ?/ ?$ ]! C3 b4 n3 h, Y8 X
// just repeated every time. See jmousetrap for more$ V! V1 ~" u( U1 v, l0 m( l7 w0 T6 n
// complicated schedules.
$ f& C/ Q q: y; q. {
1 f2 K6 I/ k3 A. D4 [! Z modelSchedule = new ScheduleImpl (getZone (), 1);
/ O$ A8 @2 b0 o4 m1 J+ u, v modelSchedule.at$createAction (0, modelActions);6 V0 L8 r) ~+ w$ X
" C, F1 B1 o$ h) `6 U; L/ C
return this;" J2 |7 W6 V& o6 q
} |