HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:( ]- h. Q* `. W( _) C
6 M. ]; @% S: ]( A" F* V public Object buildActions () {4 L0 N5 j. ?9 w/ W; ~, u
super.buildActions();/ s; X2 p! J, ^6 S7 V% e8 G
9 x. e0 h: f; ` // Create the list of simulation actions. We put these in6 Y$ d S7 U$ d8 p% H- N3 m
// an action group, because we want these actions to be
7 J, | l& @ M2 @, t' I // executed in a specific order, but these steps should
3 R5 v6 B$ A+ @ // take no (simulated) time. The M(foo) means "The message* b# ]* A% |! `% c( w2 c% t. J
// called <foo>". You can send a message To a particular
' k& ~* H, O( _0 H5 s3 V% } S // object, or ForEach object in a collection.
1 \' r) m) w8 ]- G) i8 u0 h 7 K4 j8 l0 V: e
// Note we update the heatspace in two phases: first run
* G- ]; ^* u; ~9 x/ p // diffusion, then run "updateWorld" to actually enact the
h" h3 \4 Q' h: z/ h8 @ // changes the heatbugs have made. The ordering here is5 S' L3 b( M) F
// significant!
' i. t J- [ n, P8 ~( S/ Z! ]' [2 |0 ~
7 p9 f- |/ k( _7 g8 u8 }: } // Note also, that with the additional n z' E- Y$ W
// `randomizeHeatbugUpdateOrder' Boolean flag we can
+ y/ g2 c2 ^$ o2 }& { // randomize the order in which the bugs actually run5 ~: G! l$ L: I: F6 x6 h0 R3 G3 i
// their step rule. This has the effect of removing any
2 C/ {7 `" O2 d. p9 g: x! a8 E // systematic bias in the iteration throught the heatbug2 O+ h% `/ \2 r7 S
// list from timestep to timestep6 K0 R( ]3 f/ L( Y: s
7 z) R0 i5 l! O; F; a1 {5 R // By default, all `createActionForEach' modelActions have
# `& ]% a& n: L4 s4 B // a default order of `Sequential', which means that the
+ `" F% X; H8 c' D/ d6 ` // order of iteration through the `heatbugList' will be
- I9 u- c% j! e# J+ [& o* t* v // identical (assuming the list order is not changed: o7 P: [9 T y3 _! N4 X9 Z, L. m
// indirectly by some other process).
& a) _% u; C. E6 D5 F' {: ~" c : J; J0 D b! R4 A! d* ?$ `
modelActions = new ActionGroupImpl (getZone ());2 s$ H* i! a8 D8 }" w- a! z
) M2 H. D# L. B. Y. G( c- F8 A
try {$ u0 E7 ?' y. l+ ]
modelActions.createActionTo$message6 ~: b4 h) Z. D/ w8 i
(heat, new Selector (heat.getClass (), "stepRule", false));
$ p! F, J6 D- s% t8 l4 ] } catch (Exception e) {9 ]. M: T( X0 `% [% W% t6 d
System.err.println ("Exception stepRule: " + e.getMessage ());
, t b3 J9 ^; } }$ n8 y0 V- x/ ]0 z( @% {
3 M3 m$ w: h9 ]6 E% u try {
6 N; V3 v) \8 M8 {) C/ |1 d Heatbug proto = (Heatbug) heatbugList.get (0);
8 F W! w* L1 g2 F* r Selector sel =
o6 r& a4 W) q new Selector (proto.getClass (), "heatbugStep", false);' [8 T# w& z Q, k: @
actionForEach =
0 H0 F, k2 e# ^# N* m. b: h* N modelActions.createFActionForEachHomogeneous$call) a1 W% x4 ]4 S
(heatbugList,+ l0 _* W) l( p" ]& i9 N
new FCallImpl (this, proto, sel,: {9 o& B/ p3 W8 y5 I' q* K
new FArgumentsImpl (this, sel)));5 P7 o, `; o9 h; s
} catch (Exception e) {
9 t$ i4 v; R8 j6 T7 j q e.printStackTrace (System.err);
+ j9 b% W9 G+ x v2 N4 X } T- `$ S R2 m/ ~, O" e- {
. A2 f0 L6 [# o syncUpdateOrder ();
) G" m P+ F8 B, U$ p; k+ Z) p) }% }: v1 V/ K8 x) k
try {. L; g* x* h7 X) v9 |
modelActions.createActionTo$message : K; W4 y6 ^% [
(heat, new Selector (heat.getClass (), "updateLattice", false));
" |9 V" [' J% G$ @5 V. Y1 x } catch (Exception e) {
6 w0 ] w; A( F8 W* v, @ System.err.println("Exception updateLattice: " + e.getMessage ());
" s3 O3 M8 M- O4 ^8 g }2 U+ ^% n' Y6 g+ C' F) {, `
3 Z, P0 [% O) N; T) e
// Then we create a schedule that executes the! W& P4 j8 S2 }+ F/ g
// modelActions. modelActions is an ActionGroup, by itself it% ^& I7 |0 e* q1 q1 i1 }, C/ {
// has no notion of time. In order to have it executed in
% W$ h6 G$ A. j // time, we create a Schedule that says to use the
! ~# Z! [' Q$ P3 i // modelActions ActionGroup at particular times. This
- ^3 x6 u3 I2 N* t( S% o // schedule has a repeat interval of 1, it will loop every
$ B J6 H& }- R. q! L" `9 N // time step. The action is executed at time 0 relative to
! }8 } N, ]% M: E0 g' ]7 |$ T7 F // the beginning of the loop.! U8 ?: s; Q& s0 t4 }
" c% L, A `0 X, V! F7 V# S- ` // This is a simple schedule, with only one action that is6 o4 u2 i, \. G j
// just repeated every time. See jmousetrap for more9 r+ ]. K s j. G/ M5 Q
// complicated schedules.
, i) w$ V N% k * e/ K$ ?. @- R3 ?9 U
modelSchedule = new ScheduleImpl (getZone (), 1);
4 ^9 I/ E" q0 [6 _ modelSchedule.at$createAction (0, modelActions);% ~& b5 l5 Q! l- q. K
6 }3 q( X9 B8 x/ x1 k
return this;
+ S9 k/ y) Z5 [& X } |