HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:8 R+ i# ~' l0 v) P$ X0 U
9 w$ s" `( C2 v$ e1 m0 c; | public Object buildActions () {+ n, @# z% x* \7 n# M/ u0 y0 J( k
super.buildActions();" w* v0 W# x' J
/ o# T9 k! I' ^7 Z" ^ // Create the list of simulation actions. We put these in& G. ]) F) V5 A4 r
// an action group, because we want these actions to be' y5 t( r. R, O$ C& f# d! c" j
// executed in a specific order, but these steps should! S3 Y5 ^4 ]# Y- j' ^& _4 H
// take no (simulated) time. The M(foo) means "The message
( h) M' r2 E! j# f // called <foo>". You can send a message To a particular
7 F6 @2 M9 ~2 r. W* T; |) }& ^ // object, or ForEach object in a collection.
2 a+ n$ K" h, h% _! M
6 G/ j$ R L5 V2 D // Note we update the heatspace in two phases: first run
6 X8 T1 ]2 V/ _2 K4 {7 f // diffusion, then run "updateWorld" to actually enact the/ x' B/ @( A) c1 o5 v! L* S K
// changes the heatbugs have made. The ordering here is+ a- t0 c, {% A( Z- P
// significant!% }% T& N" T- e! H
0 J+ I% O4 W, \- c- }! v( G // Note also, that with the additional, Z ~) E3 a1 y. h0 \
// `randomizeHeatbugUpdateOrder' Boolean flag we can
5 N- K8 @. h1 u# p- F7 | // randomize the order in which the bugs actually run8 {% X' ?6 g7 r1 ^# T& U
// their step rule. This has the effect of removing any
5 J" ]; n. R8 O7 E: z // systematic bias in the iteration throught the heatbug$ B; l4 c. U3 a
// list from timestep to timestep
1 {2 W1 K6 ?; z2 C
. q( y9 C: e% F: h" k& D2 E // By default, all `createActionForEach' modelActions have
' {! `4 v* A/ ~/ R // a default order of `Sequential', which means that the
/ a* q, G/ O# @1 G // order of iteration through the `heatbugList' will be
0 ~) x* d7 B: A; S+ R2 i4 s // identical (assuming the list order is not changed
L! K) n$ V, x+ Z. y3 l) L // indirectly by some other process).& m4 K+ J" L1 B6 u$ x( ~1 ?
3 \$ `) a- Q" P8 ~. F6 o; T modelActions = new ActionGroupImpl (getZone ());, c1 m/ E# H: D, ]/ o/ @
' D' C: o3 {) F$ k' G. M try {
$ [6 s# \7 w: ~, p" f modelActions.createActionTo$message
9 _, W. T+ L% o (heat, new Selector (heat.getClass (), "stepRule", false));' o- f" d$ T( T( \8 V) H2 H) ?
} catch (Exception e) {7 k" ]0 ]& t! g$ `2 [- T$ D
System.err.println ("Exception stepRule: " + e.getMessage ());. X, o; L- G9 @7 n) \) ]) }% Z
}
( B2 `5 t5 Q9 W1 ^1 A- ?; A$ h+ I8 @5 ^8 K' ^7 a
try {
2 b6 F; Z A! z* w4 S; [# b2 @ Heatbug proto = (Heatbug) heatbugList.get (0);
* O; l; I& {2 ~# p. o Selector sel = 9 i" |; W' q" M0 S, F, |
new Selector (proto.getClass (), "heatbugStep", false);9 t1 b0 L1 X5 q/ ~+ K0 i
actionForEach =/ q- g9 J, r7 g# S+ k
modelActions.createFActionForEachHomogeneous$call( I2 X3 Z$ F6 |" F s7 Y' H5 \
(heatbugList,
$ O0 D5 p# F% _3 c: O new FCallImpl (this, proto, sel,7 f, b @! j& w. f& L, }
new FArgumentsImpl (this, sel)));
) _4 C: n5 ]$ T* x S } catch (Exception e) {( n) P) C" Z6 X+ G3 N5 `: f
e.printStackTrace (System.err);
. b; ?/ a% `- O: I, G }% x* P- L: w$ I) a7 v2 ^
7 M( Q. U+ T3 @3 ]8 {
syncUpdateOrder ();2 M0 \4 ~/ v7 F$ V5 q
" K2 ]. \! u0 _ t5 P try {+ H' }3 g8 |4 L) d4 O2 ^
modelActions.createActionTo$message
3 ]7 I/ U0 w1 B6 Q1 ^ (heat, new Selector (heat.getClass (), "updateLattice", false));
' L5 ]" x0 W0 q. l( b5 T# }; F } catch (Exception e) {4 p% E. y7 f4 j( j* T5 D
System.err.println("Exception updateLattice: " + e.getMessage ());
7 B- Z6 X: r. }. q! J }
( s8 Q8 p7 w2 l: ~
3 R, r: o9 L$ S // Then we create a schedule that executes the7 P& e4 u3 R8 ]& p& ^/ i' b
// modelActions. modelActions is an ActionGroup, by itself it' m1 F a% q5 Z# _9 t
// has no notion of time. In order to have it executed in$ I( N5 x; L2 W2 b
// time, we create a Schedule that says to use the
7 A$ H5 D& E. K' m) ~. j // modelActions ActionGroup at particular times. This
* @" B. C6 X" j // schedule has a repeat interval of 1, it will loop every Y$ A; ~# }) Y* X
// time step. The action is executed at time 0 relative to P+ Z+ H" y/ {. P
// the beginning of the loop.
- J4 c) p& U8 ~- {2 z% g2 ?" m" x% _; h8 S: {, W3 p
// This is a simple schedule, with only one action that is
& L9 v# n. h3 C6 e) b3 {1 g // just repeated every time. See jmousetrap for more
" o6 i: [6 B* p1 t- y! \/ {4 w // complicated schedules.
$ I r# I4 q" g( M" V
( R5 R! X2 N6 K) p2 Y modelSchedule = new ScheduleImpl (getZone (), 1);
. R. w' p6 K3 E$ H5 B2 F5 Q) { modelSchedule.at$createAction (0, modelActions);
" w. W1 _" W( n ' v K+ d, R' t0 {# P+ I. ]9 @( c
return this;! J1 h1 i" b* L. a$ G3 ?% o1 v: [ |
} |