HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% D- n9 f* e! v }6 @
' n& K" ?. k+ c0 k. c7 q public Object buildActions () {
5 |+ w! G E, n super.buildActions();/ i. c, Y4 L. y: {
_% e4 x& ^" ?9 q1 q // Create the list of simulation actions. We put these in
0 {+ ^/ d- O4 t ^ Q: ?: c // an action group, because we want these actions to be" ]5 c4 s1 U; e" c0 P a5 \8 K
// executed in a specific order, but these steps should
# L b* A+ L# U4 w$ K3 O // take no (simulated) time. The M(foo) means "The message7 X9 z+ N* n7 ~: U6 D G. e' {
// called <foo>". You can send a message To a particular
* G7 A4 q! m7 ~4 H( z // object, or ForEach object in a collection.6 W7 R7 a+ [, N& m1 e
4 r, n; T3 T- ^, w0 ` // Note we update the heatspace in two phases: first run
# ? c f# k3 `- \7 J) m' c( p // diffusion, then run "updateWorld" to actually enact the; N( z+ l ^2 d- u4 ]
// changes the heatbugs have made. The ordering here is
6 }# ^5 v* j4 A7 }: M // significant!
1 J5 J+ b' R6 A; N : \- a, a- k9 C# c( J
// Note also, that with the additional
( M- x5 K9 R4 I9 X8 [, L) ~8 ?0 _0 W: e // `randomizeHeatbugUpdateOrder' Boolean flag we can1 |' X& s j# N. ^9 F8 O, ]0 _1 K" u
// randomize the order in which the bugs actually run, L* V. @9 a6 [
// their step rule. This has the effect of removing any
9 A9 V# {, d6 U# w& Z/ }) K% ~! G // systematic bias in the iteration throught the heatbug F1 g/ t6 J4 [$ Y9 n8 w$ ]# e ]" M3 ?
// list from timestep to timestep
" l3 h' L. {0 S u1 X% P 1 w9 \4 O2 V2 Q6 D2 x( g: k0 t
// By default, all `createActionForEach' modelActions have: f$ J& S C) Q: c( ?4 h
// a default order of `Sequential', which means that the
; e. L' V) z8 o$ E* _ S // order of iteration through the `heatbugList' will be
9 E8 ~6 T9 Q* a // identical (assuming the list order is not changed, }) s4 f! |* D
// indirectly by some other process).8 Y- W$ E2 i4 b5 A4 p
$ f0 P% S! E: K, x" ^0 q
modelActions = new ActionGroupImpl (getZone ());
' [; l/ m4 ?( E# s1 `0 D6 A2 H2 c
. S) W" O- P0 R. I0 E. s6 T- Q, l try {3 t A2 }7 o' ^3 `
modelActions.createActionTo$message
3 X6 k; u0 v0 o7 m/ J( e3 \7 i. u, t (heat, new Selector (heat.getClass (), "stepRule", false));* O9 X2 ` o# X# @* V7 c- s, \
} catch (Exception e) {8 ~( a, Z5 ?2 L* H$ s1 H4 N
System.err.println ("Exception stepRule: " + e.getMessage ());7 S$ a. M* m; j4 g* E1 T
}
1 @0 v0 q& b7 [6 ]& j4 Y) k( ^7 A# _5 g- i! O
try {: \- T: _6 i0 H- @( l. W0 C
Heatbug proto = (Heatbug) heatbugList.get (0);
" W% Q+ Q6 }$ l+ E& }% c Selector sel = 2 x& q7 |& D! O( J7 t( F( J* x
new Selector (proto.getClass (), "heatbugStep", false);
* J" e1 E9 W0 w0 h) W actionForEach =
, j# f# D$ q: ^! `( W modelActions.createFActionForEachHomogeneous$call2 ?; p$ Z' i! ?' N: W
(heatbugList,
8 r4 c2 J) r. t- E new FCallImpl (this, proto, sel,
2 B/ U1 h3 e g: V" n9 P* Q0 } l new FArgumentsImpl (this, sel))); |+ x8 Z( C f; F* _: i& h7 H0 e8 C
} catch (Exception e) {
+ s! \/ R; t# \9 X1 Y1 E; b/ E e.printStackTrace (System.err);! A( c; O/ q/ q( r) U6 g+ ^
}
# q% f& E' `! q # o# F8 Q9 X" d, d8 ~
syncUpdateOrder ();
. L5 X' s5 n. ` L7 A( I% m4 _; B [' O5 C( K
try {
5 W0 k0 R) Z9 u modelActions.createActionTo$message
6 ^5 E. E* O4 _$ v (heat, new Selector (heat.getClass (), "updateLattice", false));
" N! t0 q: F% t. `+ x) U } catch (Exception e) {
! q+ i7 |1 I% {+ d System.err.println("Exception updateLattice: " + e.getMessage ());
" {) j( z0 Z- ^% X2 n }
6 N& i6 a: a( Q i5 G) I. f y * E% u! ~ ^; e9 ^3 U2 V
// Then we create a schedule that executes the( k( b$ Z8 N. z% I9 L, I
// modelActions. modelActions is an ActionGroup, by itself it7 D# v2 D Y- j; U7 F/ k
// has no notion of time. In order to have it executed in; X5 k: ^ T- N2 A8 i
// time, we create a Schedule that says to use the) H# B9 W! C. {3 A. P9 t2 w( W ?( K/ {
// modelActions ActionGroup at particular times. This; i. [' \. a, o6 I' n* y+ k
// schedule has a repeat interval of 1, it will loop every
. X; W: c7 U4 P, L4 z // time step. The action is executed at time 0 relative to' k' D+ w# H1 ? ^8 u: s8 o
// the beginning of the loop.5 A. g+ y' ]& E) H5 g
' M3 K, w) k9 a; N8 T+ _1 D1 L5 C // This is a simple schedule, with only one action that is& {* |3 f5 R9 k
// just repeated every time. See jmousetrap for more
- z5 K. Z( ?2 t* Y% | // complicated schedules.1 W8 y9 R+ c Z# Q! @2 A" K d
- S! t& G+ K$ b' `9 T modelSchedule = new ScheduleImpl (getZone (), 1);) D; ^4 ?# E5 m2 u5 [, S r
modelSchedule.at$createAction (0, modelActions);
, T9 w$ ^& o" e7 U 7 }6 r* c/ Z$ x- E: H$ @
return this;0 F9 Q3 p0 I1 ~& I( {. {
} |