HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# a9 `* x# p; S1 E+ Z: Q6 Q8 W
* m Z9 f! C3 _' Q public Object buildActions () {
( r" a" n( x" n super.buildActions();" k4 L- i; S7 a: P2 }' T/ ]
& N$ R5 g6 x' Y6 w9 t3 P7 j
// Create the list of simulation actions. We put these in
4 k" b& A/ ^$ b/ f* V // an action group, because we want these actions to be; Z% r( n& \! L2 `8 x( g1 [( p: j
// executed in a specific order, but these steps should/ l& W3 A. K& v$ M1 @, V
// take no (simulated) time. The M(foo) means "The message
+ p$ H+ \9 i. P0 x( t // called <foo>". You can send a message To a particular: o0 r# c5 V E5 i: |; K, G( M" G
// object, or ForEach object in a collection.' _5 w/ M, j0 X
0 b7 f9 w; p6 W& Z
// Note we update the heatspace in two phases: first run
9 j3 t0 {% t$ B$ T4 X // diffusion, then run "updateWorld" to actually enact the6 a `0 ]2 C6 u2 Z
// changes the heatbugs have made. The ordering here is
$ L7 C9 y# \- l( F( x: Y3 z // significant!
2 U# P6 U1 k+ I1 \+ G4 l J2 y H& ]& M- o" R5 R( p% k
// Note also, that with the additional
: w9 _; J2 `6 k9 g$ V; D8 y8 v // `randomizeHeatbugUpdateOrder' Boolean flag we can
# o9 |$ g, \' X8 B# g // randomize the order in which the bugs actually run
1 B; h: j! _/ i$ E+ i; X& u // their step rule. This has the effect of removing any
) w- t! `/ @9 Q // systematic bias in the iteration throught the heatbug
9 y8 M3 U- |3 S7 u( |( V // list from timestep to timestep
6 F( [4 h; a2 F" e, P! w
1 `: }# E9 `$ o3 j" v! X) ^# E; _ // By default, all `createActionForEach' modelActions have
1 M& Y1 P) ^$ }) ^9 S // a default order of `Sequential', which means that the# x- z1 ?% J' ]- N/ _+ C
// order of iteration through the `heatbugList' will be: Z5 g! U2 x+ R. g% E
// identical (assuming the list order is not changed4 Q9 b/ h* M G8 r) k: g" a
// indirectly by some other process)." ~5 g2 @/ h/ _/ K* o, O
. x3 F; T7 _( A3 V0 L
modelActions = new ActionGroupImpl (getZone ());1 K( V3 Z2 C3 [% s# W
# v: f9 j C( }- m5 P8 @2 X
try {
& A/ a; X- d Z; q+ N modelActions.createActionTo$message1 t* ^3 y D& D5 O
(heat, new Selector (heat.getClass (), "stepRule", false));! B+ T5 f0 \% C8 B3 y
} catch (Exception e) {; _' T. D$ Q( c8 m3 U5 o: e
System.err.println ("Exception stepRule: " + e.getMessage ());* l9 I- ?$ s6 p5 Z( j8 s, B7 h
}
" F- Z" y& ~# M: m- _% o, C- L% P: u7 U' F5 n' C
try {' Y2 d/ ~2 ~1 \9 `: Y% p
Heatbug proto = (Heatbug) heatbugList.get (0);; L5 Z8 ]/ {: k0 p: E
Selector sel =
0 D6 \7 n Q2 u' t3 k/ y. j new Selector (proto.getClass (), "heatbugStep", false);
6 g0 J3 |5 ~& M8 G actionForEach =
. ^. `) z, R6 l+ K0 v modelActions.createFActionForEachHomogeneous$call
( T3 q. [3 B5 i% N& D! V9 n8 I (heatbugList,! q4 W+ T* G# b4 S1 }) L
new FCallImpl (this, proto, sel,
0 t8 d1 ~4 `2 d% S9 f new FArgumentsImpl (this, sel)));
, b- ~$ X+ z, X- [* N. U } catch (Exception e) {
7 E% l% {! d$ C7 M2 p e.printStackTrace (System.err);
+ U6 t( E: `1 R* E6 G, x }9 E. J3 ` r) K0 T# L. }
1 Z2 x6 d1 U: \/ j syncUpdateOrder ();
# R# L+ U4 y }0 a
* ?2 q w5 s1 r; O try {4 H5 g! T, D O
modelActions.createActionTo$message + m; k' [. v, g" @# |- B
(heat, new Selector (heat.getClass (), "updateLattice", false));
" ?% w3 }; J3 w P6 B } catch (Exception e) {
0 j# ?5 a4 t: t6 j, Z8 } System.err.println("Exception updateLattice: " + e.getMessage ());1 ^4 R% }9 v' j2 c- U1 s
}
5 Q; N8 E- K+ j" N& H7 |# M
& p: Q* K, e7 A // Then we create a schedule that executes the
( ~* B4 \9 K$ }6 F; k, R' S* w // modelActions. modelActions is an ActionGroup, by itself it
* N+ i1 {- @! S% w5 D7 o // has no notion of time. In order to have it executed in) |7 [- e- e' {9 Q
// time, we create a Schedule that says to use the
% _' T$ l- |' f2 O // modelActions ActionGroup at particular times. This
; r, f. z( }/ O& }3 w1 Z9 S) Y5 c // schedule has a repeat interval of 1, it will loop every& w. ]$ N5 M: o* }
// time step. The action is executed at time 0 relative to
& P# x( V& Z7 }4 ^; g4 t4 s // the beginning of the loop.
: Z2 P9 Y3 u" j
' _# n+ ^- Y! q: b // This is a simple schedule, with only one action that is/ a9 z* T! [! O
// just repeated every time. See jmousetrap for more# L7 v: a, X; h9 C
// complicated schedules.; S/ |% t: |% H2 ?8 z/ q
' ?1 b8 V S$ q% n# i' ] modelSchedule = new ScheduleImpl (getZone (), 1);
- _. K* N0 {7 N modelSchedule.at$createAction (0, modelActions);$ V1 c/ Z- \" \" \
5 }. R/ ]" N; O% S% n
return this;
. q J5 L: S) ] } |