HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: w* O5 Y. T" @3 {
" S1 q9 b) S& c! P+ L public Object buildActions () {
2 x, e8 A$ z; T$ \7 ? super.buildActions();# ?: p1 ~0 G Z. e
" a& q3 A' W- ~5 A# ~) u; F0 m
// Create the list of simulation actions. We put these in, O6 q2 ^2 x- g% k. V" a, l
// an action group, because we want these actions to be! M) h& F1 T- y. c# ^* p
// executed in a specific order, but these steps should% o3 b8 N" E$ }& Z
// take no (simulated) time. The M(foo) means "The message. O! U/ @. F3 Q% [4 A9 S
// called <foo>". You can send a message To a particular% u0 O) m( }4 a+ q& R8 K3 o
// object, or ForEach object in a collection.* X! v0 k. t5 D. @6 K u. e
" {1 }1 `# o3 P; h% _) N { // Note we update the heatspace in two phases: first run
/ K9 }4 [! p( f% ?6 d5 J0 v# L // diffusion, then run "updateWorld" to actually enact the" B, ]! ^" f- R3 h. @( m$ N% \# x
// changes the heatbugs have made. The ordering here is
" Z) k, ?/ t3 ^9 |, ?( Y // significant!
- n4 Q% L ]" I4 |5 z7 b* {5 x
* v+ X' w8 D0 ~: w8 r // Note also, that with the additional
. `6 B% [& N }3 Y5 z" S3 Z; ^ // `randomizeHeatbugUpdateOrder' Boolean flag we can5 b l. x( x/ y) }
// randomize the order in which the bugs actually run
& m7 k5 s7 P# d+ }1 G // their step rule. This has the effect of removing any
0 T1 c; n" a N // systematic bias in the iteration throught the heatbug/ P! y$ w3 Y: y6 u0 m
// list from timestep to timestep
6 J7 i6 R4 U$ ]. Y' o
* f5 K. C1 m, L7 [3 V! k; ~9 l // By default, all `createActionForEach' modelActions have
3 M* U& ]0 t( G. q y, g2 A4 |; R // a default order of `Sequential', which means that the
( q a) q# G c f5 P- o // order of iteration through the `heatbugList' will be9 V3 Y: G+ o1 [$ v8 T8 v
// identical (assuming the list order is not changed
3 _8 g! T o0 U& S. F // indirectly by some other process).
$ O9 R) W4 y: s1 i& ~4 d! S: D ; o2 F% D) p6 C' i! B2 r+ {
modelActions = new ActionGroupImpl (getZone ());
% ^* p* N5 D% P5 j. N7 [1 L- ?5 O4 E
# _6 e8 S6 t2 _4 F& D" G try {
" Z. L( U# B7 P) A! o/ B7 W- f3 \ modelActions.createActionTo$message2 [: L. u1 C5 o
(heat, new Selector (heat.getClass (), "stepRule", false));9 a) ]/ T! ^6 c+ p+ M2 A4 T: u4 j
} catch (Exception e) {
( z) V! d1 S2 s' B- g: j# y6 z System.err.println ("Exception stepRule: " + e.getMessage ());
' u- y9 Y9 b g9 T }
% Z% d) o2 F: `6 p) z( M' ?* j1 j
+ a% r0 I& [: f7 Q, k try {
1 @- S! @0 T# {' v5 ]) t) Q Heatbug proto = (Heatbug) heatbugList.get (0);' ]! S+ A) l; ~+ b& g
Selector sel =
& o+ H' Y1 z6 j5 B0 g* n( n9 @/ c new Selector (proto.getClass (), "heatbugStep", false);
/ B3 L5 M. y4 z actionForEach =- G/ R4 v9 b, Y) @
modelActions.createFActionForEachHomogeneous$call1 e: Z a" ?5 G2 E) \6 m$ y
(heatbugList,
3 y5 q- h7 _/ K* h new FCallImpl (this, proto, sel,
- X9 |3 ^7 x* U new FArgumentsImpl (this, sel)));6 c; r6 v/ K9 ~+ v# f4 k
} catch (Exception e) {
, h& u% x; p6 G e.printStackTrace (System.err);/ Z- k1 s) M' F6 d: @' F
}
" r& n2 Z% s: e9 w) j2 l 6 \7 @# k Z" B
syncUpdateOrder ();
$ F0 W- e( ~7 U4 \
1 k& G0 c9 V" B0 O+ o2 j try {
# x; {, B* w$ O& U modelActions.createActionTo$message ) h$ q& @% m3 h0 }6 ?/ R$ z, F
(heat, new Selector (heat.getClass (), "updateLattice", false));: R& U2 e; C0 ^- p8 `
} catch (Exception e) {
2 j* z/ \/ J. D# j c; e System.err.println("Exception updateLattice: " + e.getMessage ());6 t, P0 m/ m4 F# ] R+ @
}
3 J/ g& S# u8 q# g: o3 O# b8 J3 l. G 6 F& P$ a. B n. P- z9 ~5 o6 u3 e
// Then we create a schedule that executes the
* |6 N9 u/ E: A" V4 ~& K // modelActions. modelActions is an ActionGroup, by itself it
' L; K. |4 ?6 [$ `7 R // has no notion of time. In order to have it executed in
p) q' _% [( V/ j7 X4 D9 v6 j& ~ // time, we create a Schedule that says to use the
% o, o2 S4 b) a( ^3 c4 D7 B // modelActions ActionGroup at particular times. This
& B! l3 S& Z! l" J // schedule has a repeat interval of 1, it will loop every
2 W* ]9 [" K1 s' P" `( G8 Q // time step. The action is executed at time 0 relative to
% A. ]' i& {4 `' P* e // the beginning of the loop.
$ E8 `( n {0 B' ^6 s& L
$ c% u# m0 d6 D& ` // This is a simple schedule, with only one action that is
: c/ V2 T# |% }+ a) f+ ^5 W // just repeated every time. See jmousetrap for more1 ?( T1 H2 c, X# O
// complicated schedules.4 f: k1 X, @0 r, V1 [
5 j4 G" O' O2 ~/ ^- D& B
modelSchedule = new ScheduleImpl (getZone (), 1);4 C s' g0 o8 U
modelSchedule.at$createAction (0, modelActions);
4 k( r0 I4 g `4 p" H7 C
6 ~1 p4 w+ G% ^ return this;" A E) e- o3 s
} |