HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:1 o% ~0 _( }1 P3 o
. w5 F* v& |3 I3 D% } public Object buildActions () {) G9 G. C/ E: @- v- Y& i
super.buildActions();
8 X6 ]9 S1 Y! V, U( f7 F & ?7 E3 U N# k' r
// Create the list of simulation actions. We put these in9 {. I, y1 p5 p; E0 U* N7 X+ ?
// an action group, because we want these actions to be2 R, f4 i2 s4 R- |9 c' i" B P
// executed in a specific order, but these steps should
; S3 |; z* J2 d' _ // take no (simulated) time. The M(foo) means "The message
: `$ q! G. W. l0 H // called <foo>". You can send a message To a particular& p: c1 J1 }' i% W+ @ N' C
// object, or ForEach object in a collection.
9 L; ?5 |# o, M% h0 d/ r 7 G/ d$ x3 h5 A
// Note we update the heatspace in two phases: first run
8 a( T& M0 f) C& c; D // diffusion, then run "updateWorld" to actually enact the
: y, A, M% \5 |7 C7 M // changes the heatbugs have made. The ordering here is$ _& ~2 J# E* B; I
// significant!
) ?( `; ?% F0 `
! c/ L5 h' F- g. r/ j4 @ // Note also, that with the additional' r. o: A, K, N( m" J ^
// `randomizeHeatbugUpdateOrder' Boolean flag we can1 Y* }) \- l7 |) k C; d
// randomize the order in which the bugs actually run
7 Z1 D: h' F" S8 @' o# a& V6 c // their step rule. This has the effect of removing any
6 ~) q+ o- n+ \ // systematic bias in the iteration throught the heatbug
?! n1 P9 x( b. V2 o // list from timestep to timestep! F* P/ s! `2 T5 v- O$ I' d
/ Z* C; c# p- M5 l; n
// By default, all `createActionForEach' modelActions have" R. o$ J7 x8 K |5 a S C) r5 ]
// a default order of `Sequential', which means that the1 f" N( D, B1 _3 L0 Z- @+ v9 q
// order of iteration through the `heatbugList' will be
7 W' @ `) |$ z! { // identical (assuming the list order is not changed$ K* K3 I1 y4 c" S5 s
// indirectly by some other process).9 h; |5 } ^8 F% {3 j0 x7 B
" H) q, g# j$ r1 R6 W6 H# {
modelActions = new ActionGroupImpl (getZone ());( g( f `, o5 f
6 Z7 z# E: S. Z u% u* D try {
2 S9 B( V7 d$ s3 i. O7 X modelActions.createActionTo$message7 L b/ M( b6 ]0 i
(heat, new Selector (heat.getClass (), "stepRule", false));
7 g# Y, T. y. x } catch (Exception e) {9 [" n( t7 q0 A, q. B
System.err.println ("Exception stepRule: " + e.getMessage ());
, T* \% G9 o J( Y0 c r8 C; M }
3 z) D/ L2 o1 D: J; l$ R* X4 y6 G2 J5 M: B c
try {
% I5 a9 b# b) d, H: s Heatbug proto = (Heatbug) heatbugList.get (0);5 T# s# b) U+ i; t0 t2 Q/ f
Selector sel =
( P" |& n- H) b. F% n new Selector (proto.getClass (), "heatbugStep", false);6 h, h; _- S! l ~5 L, R
actionForEach =
, _9 o) E& }+ G( \0 U modelActions.createFActionForEachHomogeneous$call
7 S) M* F3 ]7 W5 i. g: a1 m (heatbugList,( o5 q0 G1 G" w4 e9 N) X! P+ c
new FCallImpl (this, proto, sel,
t9 ~8 h1 P; C8 Q8 e: ?3 m new FArgumentsImpl (this, sel)));
+ r6 Q: [. {6 @) Z: V+ _+ Y4 h } catch (Exception e) {& \9 ~% Z$ V- r! o
e.printStackTrace (System.err);8 K3 {6 C, n4 m; q6 o5 I' ]
}) b4 d; V* A. o0 x8 t& [
! p* c( J$ B+ Z
syncUpdateOrder ();
' g: E) s5 v3 O. P& K$ R
$ }6 J" B0 h; h1 E try {
3 `: p9 k( j' `- t/ z& N modelActions.createActionTo$message 9 T) @0 q$ o5 Y
(heat, new Selector (heat.getClass (), "updateLattice", false));
9 q8 ?* k! ~, Q1 Q2 B* K } catch (Exception e) {
# R% B( v) u0 |. _0 s$ n System.err.println("Exception updateLattice: " + e.getMessage ());( F+ W1 j' T: g1 d* x
}! H' i5 C% r$ k6 B% g9 r# _, G
3 ~( K( ] w2 a$ t // Then we create a schedule that executes the
9 q% g# T6 _# K# z1 m // modelActions. modelActions is an ActionGroup, by itself it
9 ~ n. Y1 N) c" j3 }+ A // has no notion of time. In order to have it executed in
6 P0 [" j) V! ^5 g // time, we create a Schedule that says to use the
4 S3 X5 s# A: R9 P0 V // modelActions ActionGroup at particular times. This
- @& |4 w) U/ E // schedule has a repeat interval of 1, it will loop every
! x+ ?) `( r% [' I6 _' c6 Y // time step. The action is executed at time 0 relative to9 R7 `9 O) y6 e+ ]) ]
// the beginning of the loop.' p' ]# } f; q2 j' y
0 b; z. o9 G- J5 O; ]2 R! o- r8 j
// This is a simple schedule, with only one action that is
# @- a+ F1 I- K& N: s" ^0 k // just repeated every time. See jmousetrap for more3 y+ S* w1 X$ Y
// complicated schedules.
8 a0 ?# C- k* a
4 Z/ h [; i0 ] modelSchedule = new ScheduleImpl (getZone (), 1);
& U9 q0 I+ ^( F3 {% t4 A' h modelSchedule.at$createAction (0, modelActions);
5 w0 T7 B8 Y2 V& v) Q n2 L% x
& j+ j- V% q* s# k2 I+ F return this;; R1 p. w3 S! A1 h( [- P. t
} |