HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 o$ z5 e0 f7 R, L9 f3 q* N( V- o. H+ a {( _# l" y2 t
public Object buildActions () {' F+ W* N2 d- n. k
super.buildActions();
/ ~7 m* U8 ]9 y9 R% e
' R/ e* s& {9 K" x/ c1 n! ~ // Create the list of simulation actions. We put these in
8 S& M' n! `7 c // an action group, because we want these actions to be
; G! z2 p3 j% R5 K4 I1 z& r' f1 m d // executed in a specific order, but these steps should
; q6 S m& c) H9 L // take no (simulated) time. The M(foo) means "The message/ A5 V1 C0 V% X7 X6 j2 p
// called <foo>". You can send a message To a particular
7 Q# i3 o2 {8 f // object, or ForEach object in a collection.
+ b* _7 x7 e8 Q/ y" M
3 @2 M- ]8 a% l* e9 A" P7 G6 L1 i7 a // Note we update the heatspace in two phases: first run
9 o& t+ f' L* w7 k" c7 s // diffusion, then run "updateWorld" to actually enact the
- \; n5 J& m* ^- ? // changes the heatbugs have made. The ordering here is
% N' |- f& b$ X' R# q W3 L // significant!; V- X3 t4 C. q! e! e- Q& t5 n9 S
: O+ C' H. ]- z% O8 L
// Note also, that with the additional
: Q9 \6 u0 Z& S# D; x // `randomizeHeatbugUpdateOrder' Boolean flag we can) i4 f9 i0 e+ F
// randomize the order in which the bugs actually run" X4 }7 b! a. Y" ^
// their step rule. This has the effect of removing any
6 x; y" _6 f( S/ r$ L5 A // systematic bias in the iteration throught the heatbug6 v- _7 \, z; b3 B6 B
// list from timestep to timestep
. {8 Q8 S: v6 }, o$ x J* T' T8 }* \" Q# Q: Q
// By default, all `createActionForEach' modelActions have" i% S( j/ X& p6 o4 H* X- i
// a default order of `Sequential', which means that the
! C3 ~/ k f3 T. J* [; S# E // order of iteration through the `heatbugList' will be
' C' i2 f6 n' z; I8 l2 K( k // identical (assuming the list order is not changed
7 K+ ^/ l+ d" L // indirectly by some other process).9 Y# H6 G& w! `
) E" O5 S5 o1 R modelActions = new ActionGroupImpl (getZone ());; o- X6 x- G* g* T; ]. w" E
% w7 L' j4 ?; F- l; v0 ~0 x
try {
# E' n+ D; F7 I8 j/ P7 Q, K modelActions.createActionTo$message
( v" }: L+ W* r& p- E+ s; n: _ C& e (heat, new Selector (heat.getClass (), "stepRule", false));
0 i s. J! n& g- R9 D } catch (Exception e) {
3 w3 e. I0 m% k; |4 g6 W System.err.println ("Exception stepRule: " + e.getMessage ());& p" o" l( }* z; d, j2 C
}% p- U# c3 X! k1 ?2 O
. x9 b. M9 d+ A7 k5 ?
try {. k4 Q5 g5 _, \7 W6 v; h1 d& W
Heatbug proto = (Heatbug) heatbugList.get (0);
: B- S+ U( ~3 z" {" F# T, S Selector sel = : _8 w( Q% F7 Q* M- k K! f
new Selector (proto.getClass (), "heatbugStep", false); p6 Y& j i/ a, O. T3 d+ y f
actionForEach =# n% u1 {$ Q# a0 Q" h
modelActions.createFActionForEachHomogeneous$call) D. |' q' P g/ _8 a
(heatbugList,
6 s) b7 ^0 v+ z" P9 e2 ^0 a new FCallImpl (this, proto, sel,4 x! |1 _5 ?* H, Y5 f, S4 n
new FArgumentsImpl (this, sel)));
?6 M7 F: U* p3 c } catch (Exception e) {
& |: s" j v$ y& F6 K e.printStackTrace (System.err);
# W( i0 S0 W v$ ? }
" t( ~% A4 @) Q! N' o. f( \3 |* [3 T' ^
0 C" i3 f- d; }5 ]& ]- h a0 N2 g3 q- _ syncUpdateOrder ();0 @' `& h5 a% T( k1 F
3 J, R( ` C* h* c4 ]& ^
try {- Z* w1 J) Q4 U! r! z
modelActions.createActionTo$message - e+ d5 \$ R2 e7 e
(heat, new Selector (heat.getClass (), "updateLattice", false));
) I" m" g6 `; r$ O3 `* A$ \: V0 L) p } catch (Exception e) {4 s8 h; c9 _; @+ R$ K- V
System.err.println("Exception updateLattice: " + e.getMessage ());
; e+ ?* r2 y& l: C7 ? }
! `# ~5 m s* B; G ]6 i& A( a% ^! q3 o" K
// Then we create a schedule that executes the( j9 v! t: U7 y% P4 g& A
// modelActions. modelActions is an ActionGroup, by itself it
0 `( }' x- G# x6 }5 H8 S // has no notion of time. In order to have it executed in
2 P/ r5 F) K- @/ ` // time, we create a Schedule that says to use the
8 O4 |& {/ w) d: s& @) b) p N* K // modelActions ActionGroup at particular times. This
7 f3 e9 f8 @% f9 F: u8 N4 [+ m // schedule has a repeat interval of 1, it will loop every
$ ~; r5 K- m, n: w // time step. The action is executed at time 0 relative to
1 n$ S5 l" P5 u // the beginning of the loop.
7 f4 a+ S: u; n2 F; ~
4 k: ^5 s8 B9 N7 v1 l // This is a simple schedule, with only one action that is4 _: N; b, a/ |: t
// just repeated every time. See jmousetrap for more
; m( K" T7 h. c( j! l+ Y0 T // complicated schedules.; p* H+ w: G9 v0 S: W. f
5 s5 m/ o' z8 m, P' o' R2 U modelSchedule = new ScheduleImpl (getZone (), 1);
: N- a5 O& A) P6 T# K modelSchedule.at$createAction (0, modelActions);7 q. [, h& Q6 p. A$ v$ B5 N! v, M
9 j y1 y0 B! m; h
return this;
0 |# X0 h( D7 @' ^& Y) P } |