HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( Z8 `8 B W# m2 I" F% r8 h) m2 g1 h7 |2 C
public Object buildActions () {
: z* }* z5 j/ ` super.buildActions();1 ~+ \9 y: l; f5 }
* o& r0 q" X0 e4 L2 ~ P7 F // Create the list of simulation actions. We put these in
- e! |2 O3 s8 m/ R$ W3 h& v // an action group, because we want these actions to be
2 A+ I$ r( d9 o6 U8 X, n; H5 K' G1 | // executed in a specific order, but these steps should
1 ~# v1 ?6 z# A4 v5 L // take no (simulated) time. The M(foo) means "The message7 x6 n6 z) @! S, S1 O- m
// called <foo>". You can send a message To a particular
3 c6 u! t/ y8 ?4 y9 Y) p1 E9 X // object, or ForEach object in a collection.7 L3 s8 I- ~, D0 A$ M
6 R% Q5 k O4 x* ?/ L) M2 ` // Note we update the heatspace in two phases: first run
( t, t) F- l: K; i% u // diffusion, then run "updateWorld" to actually enact the9 `: T0 G+ i/ Y5 R5 M* X
// changes the heatbugs have made. The ordering here is$ C T( R- k9 a" X/ P- I
// significant!8 G. e; W9 t/ }% j7 {
. E7 J$ u9 q, Q+ r% o' ~' i/ s+ o! L
// Note also, that with the additional
. g' h% u) e5 b. Z9 S1 g6 _4 m // `randomizeHeatbugUpdateOrder' Boolean flag we can
R* ]% D& e7 C4 C4 M, O0 w // randomize the order in which the bugs actually run
3 C- h& `6 |4 z3 |% c6 x$ o: K // their step rule. This has the effect of removing any9 F G, A2 \% m5 D( O
// systematic bias in the iteration throught the heatbug
3 \9 w" D: ~& I( M1 \( ?; {' d // list from timestep to timestep
; @- c% G; U5 D+ J/ e% F/ G
$ Y8 ~' b* G, w // By default, all `createActionForEach' modelActions have
: j. n4 E' h. A7 n: `5 k. P% V // a default order of `Sequential', which means that the: {, U. d9 P- H3 g, r
// order of iteration through the `heatbugList' will be3 p/ S {4 k0 C. f9 s
// identical (assuming the list order is not changed
! [# N* O& Y. b# N/ o // indirectly by some other process).
7 v8 `+ p/ R: c9 a4 D
) t% u! ?. z% I3 r; C3 ?8 F modelActions = new ActionGroupImpl (getZone ());
. v, N% P& R; n$ B. F$ h+ S; j3 @0 H) X7 y0 Z) r
try {
+ E+ }" y4 s; u& R* K. e modelActions.createActionTo$message* D6 ~: k$ J1 H, o1 B
(heat, new Selector (heat.getClass (), "stepRule", false));
" w. l1 P9 [3 d& n } catch (Exception e) {+ Y) ~ q! \% [
System.err.println ("Exception stepRule: " + e.getMessage ());
3 Y( Z% f* g1 r1 p7 }# U( D }3 ]$ n J3 D; N1 C* N9 O
; l0 N* a l. |* i, V try {
7 v8 j W2 P y& @! a( `# G5 F Heatbug proto = (Heatbug) heatbugList.get (0);
# _3 X. I) l. i8 H Selector sel =
+ p8 s- B8 ?. U/ ?1 | new Selector (proto.getClass (), "heatbugStep", false);. ^* L0 E& p* n1 p% |, t4 P
actionForEach = W: p& U2 H" n; k, X; c& h+ M) j4 w! w
modelActions.createFActionForEachHomogeneous$call S9 l' Z/ I9 }
(heatbugList,% A& I& K: g n% `8 C
new FCallImpl (this, proto, sel,
5 b, v; E6 z W& z. E, R new FArgumentsImpl (this, sel)));$ g9 O6 O6 N9 }
} catch (Exception e) {
! h: T8 Y, B" C6 W- t' ~ e.printStackTrace (System.err);' h% o, g, O5 ]* S( L) [: a
}% {* G6 D6 }& Y9 X+ H6 Q% G/ F
3 b! {' q" Y. J( e' X2 n syncUpdateOrder ();, ^, y( T" t. [* k$ [
) O! `7 h1 Y, m7 M" ~
try {) F2 d! W$ y3 t- F$ \. p% I7 U
modelActions.createActionTo$message
0 k: i% N4 a! y( c% ^0 ` (heat, new Selector (heat.getClass (), "updateLattice", false));
# _$ }& {# L& d3 \: Y- @- I" K6 N8 I } catch (Exception e) {
) S, I8 o+ p. v. |+ s System.err.println("Exception updateLattice: " + e.getMessage ());
$ O) T+ e+ ^, t }" }* Z( P0 T& ?
0 |$ \2 H* n( L; C // Then we create a schedule that executes the! f5 H: F/ S9 i" V, C& Z
// modelActions. modelActions is an ActionGroup, by itself it* S9 d. }9 n: s4 ]( }+ |, d
// has no notion of time. In order to have it executed in
- C) E" D& P; a4 B5 M // time, we create a Schedule that says to use the
2 V1 Q, X P& n8 { // modelActions ActionGroup at particular times. This; b y) `3 P+ M$ A8 y- F) x1 s( z
// schedule has a repeat interval of 1, it will loop every
, u! R# ?( D0 o, A // time step. The action is executed at time 0 relative to
# b# P* W/ U5 ^. k2 F // the beginning of the loop.' H7 d% ~, }9 X3 A+ `
; P8 V( z8 `$ y* Q
// This is a simple schedule, with only one action that is+ r) E* Z/ ?0 [3 R7 f/ J
// just repeated every time. See jmousetrap for more5 B+ ^# @8 d0 K2 w
// complicated schedules.3 T. T! g. ^; E8 E0 f
% X2 T$ ]' P, f& M modelSchedule = new ScheduleImpl (getZone (), 1);) g- j# | [, f, N( N! ?
modelSchedule.at$createAction (0, modelActions);
" T% c3 c% e/ ^0 X' H 2 u& S/ D" N+ z5 w
return this;9 A: Q( E) P/ d$ [- [1 K+ Y: B$ d, f
} |