HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) G4 R& _* |$ T4 l; b4 D/ h
' _2 a- o: T% f, c5 m% f; r- X8 n4 { public Object buildActions () {
& q% x# z, u4 V% K super.buildActions();2 D" p) a" R1 ?" J! S; f! _
. Y6 ~& U7 B- g- r3 r' z* b. r- [4 H // Create the list of simulation actions. We put these in
* x( ^9 h# ^. |$ K" f9 i" F // an action group, because we want these actions to be
7 a* ]% h) O- u& j: s // executed in a specific order, but these steps should
) x! B0 M- l9 r // take no (simulated) time. The M(foo) means "The message
. a9 o t& J* v2 j- C! r // called <foo>". You can send a message To a particular
7 h: W, r- h( Q# P* | // object, or ForEach object in a collection.4 J( W9 @7 o, ], \
1 R, \* V. i) m; D+ A
// Note we update the heatspace in two phases: first run' C. E# P- b1 P+ f0 ~4 x6 m" z
// diffusion, then run "updateWorld" to actually enact the0 }& K1 n% l2 x3 c! [ O0 y* u
// changes the heatbugs have made. The ordering here is
& v* j9 o) i% c5 j0 t( Q // significant!' P( h) U8 a6 Z6 [2 b) `" _
- c ?5 B( a" \' |+ h7 { // Note also, that with the additional
8 j0 p/ U) Z. T4 y# g4 u) B // `randomizeHeatbugUpdateOrder' Boolean flag we can
j2 k2 n1 E b8 c- F! b2 N // randomize the order in which the bugs actually run
2 G- w% `, o1 v2 @ // their step rule. This has the effect of removing any
+ l/ J! i. Y4 G+ K- g: {% z // systematic bias in the iteration throught the heatbug
+ [! E) x5 {! i- _6 {( j; n // list from timestep to timestep
; P6 w. o) d$ q& m W0 I, @ F: d& v, D
// By default, all `createActionForEach' modelActions have
' w* t: @: k' |# v3 Q1 m" H( v: g // a default order of `Sequential', which means that the
0 k0 S) I8 ]% x5 _ // order of iteration through the `heatbugList' will be
5 t$ S) j3 O" g. T0 i // identical (assuming the list order is not changed
* D0 F" n. {) u( {7 X! c // indirectly by some other process).
" p- H2 ~6 M" e$ p9 t( z& {
" M& X4 a5 T( x% z% }& \8 r modelActions = new ActionGroupImpl (getZone ());* B, ~/ v1 m, E' s; Y5 A4 ~
6 t3 ] T- l, I- r! P+ K try {
2 M1 O$ P8 I; ` modelActions.createActionTo$message- O8 i% T1 q" w9 j8 r0 v
(heat, new Selector (heat.getClass (), "stepRule", false));. E8 ^2 U5 c' v) A# z1 c" l
} catch (Exception e) { W( l- C) q) f) r$ T8 c) l. o
System.err.println ("Exception stepRule: " + e.getMessage ());
3 b# u3 J- b V) z5 y }, A" C* J6 {- D, k% |
$ r- a* Z3 _7 }! x, I
try {) s; f H1 }; R/ L, ?
Heatbug proto = (Heatbug) heatbugList.get (0);% c- l* x' ]/ U$ N
Selector sel = 7 e; M8 O' ^& D S: i2 N! ]8 _
new Selector (proto.getClass (), "heatbugStep", false);1 B( Z! E5 g) o# \* ?3 H
actionForEach =
$ D6 R: T0 M1 @! V) |/ W" n" Y+ U' J. ` modelActions.createFActionForEachHomogeneous$call* B7 z& i! a- ^- c: x
(heatbugList,6 A: [( t; Z N+ r+ N9 X, d
new FCallImpl (this, proto, sel,
. q t& D+ e, p new FArgumentsImpl (this, sel)));
6 w; x3 C' T' U& j } catch (Exception e) {
8 `" u* n! i( F1 } e.printStackTrace (System.err);
9 k7 @2 m% k) ~3 e! F) z: h }- T' w* }' N L9 V+ A
7 q9 j9 T2 T& J% l3 ? syncUpdateOrder ();
2 r# h; ], M k9 N
! \+ o8 M. E3 B try {
6 M) N( N4 x, K' g. { modelActions.createActionTo$message 2 `: I( D2 Q- K5 @, m
(heat, new Selector (heat.getClass (), "updateLattice", false));/ O& u+ s% h+ n! q+ G# ~
} catch (Exception e) {
! s% a ?1 K/ a( Q) \- v Q System.err.println("Exception updateLattice: " + e.getMessage ());# d9 F! t7 g, z. j2 t
}
1 W" _2 @7 J. P" Y. N5 B. v p0 N2 Q
8 e, p8 q* I- h0 O0 ^2 a. x // Then we create a schedule that executes the
1 d5 |0 V6 m+ e5 ?7 j% ]; T // modelActions. modelActions is an ActionGroup, by itself it
; Y5 s' T e3 a2 f S7 i // has no notion of time. In order to have it executed in
# t% ?) u( e9 }+ | // time, we create a Schedule that says to use the# g* N+ Q2 X2 q8 ^
// modelActions ActionGroup at particular times. This
1 Y* V/ g2 X/ u+ D( G // schedule has a repeat interval of 1, it will loop every6 X3 p8 p7 K) x4 r
// time step. The action is executed at time 0 relative to2 G, w; U, m0 Y u0 y
// the beginning of the loop.
4 m/ j; D- I8 |4 a5 z8 m1 L0 y$ R+ U) t& H) r
// This is a simple schedule, with only one action that is
. Z7 _+ _& Z% ^: w. S' T, b // just repeated every time. See jmousetrap for more6 w0 t3 I7 f" U5 Z1 B$ T$ m: a
// complicated schedules.
8 ?$ D/ v' u) b. M( [- D# Z! X
5 G' u% o6 ^( I7 S2 H5 b modelSchedule = new ScheduleImpl (getZone (), 1);
/ t* F8 h3 Y( p/ I/ M& g modelSchedule.at$createAction (0, modelActions);( M4 P! K$ g& u9 X- R
* d- p% X' [# k* h" e W
return this;
' ?/ \6 {" D2 @6 v3 V1 U6 [3 F } |