HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- B) d+ p k N' W2 Y+ q3 j
! M6 T @; o6 F( o% o4 \ public Object buildActions () { u' k% r7 n9 i
super.buildActions();
) A. S8 ]3 T0 V+ Z1 U8 c 5 z/ P+ J2 f% H7 D
// Create the list of simulation actions. We put these in
) m( k9 v( M9 d2 r6 a$ d/ Q // an action group, because we want these actions to be3 _" I9 d1 `# c0 [) o
// executed in a specific order, but these steps should# t% H& _6 x6 n/ `3 _ p9 r3 L
// take no (simulated) time. The M(foo) means "The message
6 a! f' q: y: K6 F' |! Q% m8 f // called <foo>". You can send a message To a particular0 h$ V, E9 u( o* F
// object, or ForEach object in a collection.0 _/ A7 a) C6 G% z4 Y/ A$ b1 m; u
+ ]# V5 {6 D) h6 Q/ s/ q' P4 u+ x // Note we update the heatspace in two phases: first run
% T& }! ^0 w/ O: `5 I // diffusion, then run "updateWorld" to actually enact the, p) q+ e) y9 {4 T4 I, ]
// changes the heatbugs have made. The ordering here is
$ M* j% j# r- S# A3 o& X" E' g // significant!) p/ V+ f% \, l6 U" m3 a
/ B7 v2 N* ^5 ^- \/ ~( j @: G // Note also, that with the additional; |4 X4 W+ X. m2 x' D! V; S/ Z
// `randomizeHeatbugUpdateOrder' Boolean flag we can
. i; w; P4 F5 x# |1 h // randomize the order in which the bugs actually run9 ?& E. F+ o- K2 Z# } z
// their step rule. This has the effect of removing any
2 \: q( U4 g- }: ]% U h- l1 ], K // systematic bias in the iteration throught the heatbug
4 V" M3 A; `: Z+ f // list from timestep to timestep
' @' Y; Z! J7 Q1 U% [. x1 i
( ^" C' Y! P( G" P // By default, all `createActionForEach' modelActions have" V4 k/ ?/ H, a! Q$ `0 F
// a default order of `Sequential', which means that the8 _( g4 k' L- b
// order of iteration through the `heatbugList' will be. Z' s3 I% s4 R& X1 a8 n* [
// identical (assuming the list order is not changed. s) V8 f& o* g+ `9 b
// indirectly by some other process).0 k* G7 b0 y6 ^; F' Q8 x
. L3 _' n) K* X
modelActions = new ActionGroupImpl (getZone ());- s4 Y3 L, H1 t; q# N$ ?
4 B, \* h" |8 I' F- I' Y$ [( c8 ^4 I try {
4 P* T$ h% a2 Q* i' E modelActions.createActionTo$message
8 J6 w) N# ~# l* G0 z7 v, i (heat, new Selector (heat.getClass (), "stepRule", false));. s# N' W! ?! F/ b7 h
} catch (Exception e) {
) c9 @4 I% f, S System.err.println ("Exception stepRule: " + e.getMessage ());
0 I8 P1 ?; F! V, X1 N8 W# d }* ]0 S1 Y2 C. E! \. R
# @ X6 F0 A) G: U$ _
try {0 ?8 H, M2 n# _& l* Q: ?
Heatbug proto = (Heatbug) heatbugList.get (0);* o7 K0 O) e0 g; p9 R
Selector sel = ! v1 B5 c0 a% }" @/ a3 c* D
new Selector (proto.getClass (), "heatbugStep", false);
+ P P& |; H J1 ]6 @( o actionForEach =
) z$ ?! C0 E. E# k: C modelActions.createFActionForEachHomogeneous$call
9 r+ t# V& j/ V (heatbugList,) M8 _, I/ r- T. d s! x
new FCallImpl (this, proto, sel,$ {3 Z/ X& R/ e
new FArgumentsImpl (this, sel)));& d8 d) X( N0 H% B
} catch (Exception e) { P4 M& M# N6 B4 d1 M
e.printStackTrace (System.err);1 B! k5 X# @1 i3 c( U+ W0 E( n' ~% B) Q
}
7 {5 I& R9 C4 y4 a; f$ r % Z2 u* D) O( a- s2 Q" A
syncUpdateOrder ();$ g$ A, F9 o! M/ o& _
. B! _; G" Z; N% o5 K! `
try {
( z7 p, C1 f( m modelActions.createActionTo$message
4 G2 s5 Y/ T3 T% n8 n5 s) s" } (heat, new Selector (heat.getClass (), "updateLattice", false));9 B" h, T9 p& p2 {
} catch (Exception e) {! U6 C, T, c) m4 h& p% a! N' H
System.err.println("Exception updateLattice: " + e.getMessage ());
, R+ [, V7 o; |" [4 C0 P$ W( @# s* U }! R2 M5 e+ a2 }) k
8 s+ l2 [1 N6 }. |4 {( ] // Then we create a schedule that executes the
& Y) m: [4 P4 f* T S# p // modelActions. modelActions is an ActionGroup, by itself it8 o0 E/ D3 J. g
// has no notion of time. In order to have it executed in4 \* {* a+ q6 t4 d! Q
// time, we create a Schedule that says to use the, x+ A& x4 y/ Q# W1 q7 P- d
// modelActions ActionGroup at particular times. This
5 q2 d- m" m- z a7 N8 [' Q // schedule has a repeat interval of 1, it will loop every! f# G& }) N7 t" a0 w/ I* u
// time step. The action is executed at time 0 relative to
, Y- O3 z7 @- x* E: O* C) c4 r // the beginning of the loop.
: z9 ~, t" T# `! | v/ @
1 N* U6 q, K! r0 |( F9 H6 [ // This is a simple schedule, with only one action that is" l0 k) [' {2 |# r t
// just repeated every time. See jmousetrap for more6 p6 |0 Q/ ?* A7 ~5 t
// complicated schedules.9 @' A! l* W, |7 H
; b% X, Z ~9 n$ X4 ` modelSchedule = new ScheduleImpl (getZone (), 1);
$ R7 K1 n J: o9 M' }- t modelSchedule.at$createAction (0, modelActions);+ G4 X4 K7 ]7 k
3 h. ~6 E) g: f0 E; y8 @/ @$ }" L return this;
' N: ]9 H! `( T& a# W* Z' L3 F! y } |