HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% o) C, F, E% x" M# d
! O2 Q. y9 C c6 |( W public Object buildActions () {
$ m3 Y7 q0 f2 @, I/ M+ [( ` super.buildActions();
# Y2 m; s# s- M
7 h; X0 P7 Z5 J1 K/ y& Y! K/ ?& a' q // Create the list of simulation actions. We put these in7 R8 o g3 A. E" {& C
// an action group, because we want these actions to be
( Y" H9 ^8 H- i' N // executed in a specific order, but these steps should
4 F A% p; s9 o! A3 k4 W0 A- \/ i // take no (simulated) time. The M(foo) means "The message
$ L3 Q% d2 C5 I/ j, ^: d2 s // called <foo>". You can send a message To a particular& ^% _6 y1 y6 G M$ l; U
// object, or ForEach object in a collection.& f+ \: Q. r7 S% a) n$ I! e' Z6 O9 J
" p0 `* J- F B; B& M- i) j // Note we update the heatspace in two phases: first run+ B4 I0 g. l$ s, K5 j$ j5 G
// diffusion, then run "updateWorld" to actually enact the
2 ~" G+ ?) s1 I, ]: k2 V // changes the heatbugs have made. The ordering here is
9 B6 n6 ~+ i: Y" {2 n, U // significant!" |6 T- m6 `) y$ c" a) D
( x1 d& e* l1 c% o* r* H5 M
// Note also, that with the additional$ A- h4 Q4 [1 s, L
// `randomizeHeatbugUpdateOrder' Boolean flag we can
W6 r+ }, S t) Z1 l; v4 g // randomize the order in which the bugs actually run
' H# B7 I; I% T* ] // their step rule. This has the effect of removing any
5 U/ U+ _* ]" z" x9 R$ |! M // systematic bias in the iteration throught the heatbug* l! D; e% M6 ]: Y& d9 K
// list from timestep to timestep5 Q0 k1 ~# Q. `: R/ r
; ?: B4 u" O( N- `
// By default, all `createActionForEach' modelActions have S6 P& P* Z! I1 b9 `& a2 b
// a default order of `Sequential', which means that the5 K% ? W2 s' L( w- g9 U9 A! {8 c
// order of iteration through the `heatbugList' will be/ r' d. B- U6 g0 R2 N
// identical (assuming the list order is not changed
& n7 }. z* @1 h: T" h$ Y ^ ` // indirectly by some other process).
* i( s4 N _9 j' {. u& R4 p; e ? 9 v+ v) _% J0 `
modelActions = new ActionGroupImpl (getZone ());( V" s* R4 {( k1 w& B& V0 D/ x6 b* V
+ `' [9 N5 l7 ~9 G! F8 @" h
try {1 ?! s0 |* ^: i: T. V
modelActions.createActionTo$message/ a- q" j. W9 p
(heat, new Selector (heat.getClass (), "stepRule", false));
1 p6 h6 {) M; r2 X, J* |, X0 Z* N } catch (Exception e) {! [2 e& W' R8 S9 U% M$ d
System.err.println ("Exception stepRule: " + e.getMessage ());
9 |% k+ G! C2 I8 Y/ z6 s }
/ g% C# s" x8 \' C- ?) H. X4 a6 `/ L; ~" ]" j# ?3 v" V9 q
try {
& F: c. t. d8 B- O4 L0 Z Heatbug proto = (Heatbug) heatbugList.get (0);
9 _4 p) o7 D) r2 Z7 D5 L Selector sel =
' e ^; \8 N5 _' l9 V( Z: b( c new Selector (proto.getClass (), "heatbugStep", false);
. I1 m1 y4 N' N `* d! r( {6 a actionForEach =1 h8 u- V- N! r. k, X+ p. W
modelActions.createFActionForEachHomogeneous$call
5 `, U4 Z4 t( f3 q (heatbugList,% F1 M- F; E2 I
new FCallImpl (this, proto, sel,. Z, K& ~" k) e- V
new FArgumentsImpl (this, sel)));
- `! R* z |* U0 c' d6 Y } catch (Exception e) {! D4 h9 K5 i2 Q0 P* _9 q' _
e.printStackTrace (System.err);
1 C, G& ]1 k4 u/ F1 P6 ~* B% S }
5 G4 r# X( m% n+ Q: i9 Z, K
# J2 Q0 T3 [! G8 E s- f syncUpdateOrder ();4 O% @8 J6 @* Z
. R7 E1 V2 ?0 X) e( i+ w' i! z' m
try {$ i' O- R$ w: p8 W) Z4 T3 x
modelActions.createActionTo$message
6 ~% w: n' f8 ?4 J9 a (heat, new Selector (heat.getClass (), "updateLattice", false));
0 F7 L# m0 a! i: ?4 F, G } catch (Exception e) {! `9 e& M- s+ _3 u# W: {
System.err.println("Exception updateLattice: " + e.getMessage ());7 e/ T& l* X. Y+ M$ }6 l
}
$ Q; z3 ~' ~1 S, {& [1 W
+ u4 u/ U* C3 c- I* C, v6 J; C // Then we create a schedule that executes the
7 [) \* ~9 D( X9 |0 G; J2 i // modelActions. modelActions is an ActionGroup, by itself it7 S: ^* c& Z* I- D
// has no notion of time. In order to have it executed in1 ?% ]! D9 W. z2 d t
// time, we create a Schedule that says to use the/ G9 M; r: Q3 C( p. l0 z1 X% i
// modelActions ActionGroup at particular times. This
6 l" p+ W1 A" D# S3 N2 k" v // schedule has a repeat interval of 1, it will loop every
$ {6 l% I7 o9 i // time step. The action is executed at time 0 relative to9 J5 e' f/ x9 }2 T
// the beginning of the loop.
0 V L( i# A4 k0 s/ R0 J* u: T* F' x" T p* W
// This is a simple schedule, with only one action that is
1 M: Y3 d, p9 C7 x // just repeated every time. See jmousetrap for more
- a7 x4 X6 C; Z8 q9 }7 z // complicated schedules.1 Z0 G( t! j- U- ]
2 U6 S0 A0 c, S$ i4 a e modelSchedule = new ScheduleImpl (getZone (), 1);; n# F2 F$ F' o- l' i) t7 e% N+ j. {
modelSchedule.at$createAction (0, modelActions);8 H$ O. I% w/ K5 A/ ~1 W
' S f! c0 u9 K, D9 f7 C* r
return this;
& n/ y; e& J! ]+ G( H4 }) \ } |