HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
) u2 r0 q7 b% ~/ W
" b( c& `) p) H public Object buildActions () {
) q/ s% k: T) _/ U7 U super.buildActions();
6 ~( X' n X% \! @ s/ N. Q0 v
+ V* C5 q- ^' P# a' d // Create the list of simulation actions. We put these in
( @: g3 X b. ^$ e // an action group, because we want these actions to be
- \0 V8 \) Q1 `% P3 I& h2 I. w$ n // executed in a specific order, but these steps should
+ h1 i1 u! d" u6 b // take no (simulated) time. The M(foo) means "The message
( v [2 ?. K! w7 D // called <foo>". You can send a message To a particular
/ t. V8 b; z' e. ~2 W3 ]3 u // object, or ForEach object in a collection.
" ~6 I7 \" j' G9 I
; p6 E* @( U# M* t3 Y$ Q // Note we update the heatspace in two phases: first run+ B$ k- |' t" d
// diffusion, then run "updateWorld" to actually enact the8 }% M8 N* m: @9 W) Z
// changes the heatbugs have made. The ordering here is2 O, A# _" [, V) D, K. J
// significant!% m# ]* R' a) j' s* `# Y" `
# L) K2 Z% t8 i // Note also, that with the additional
! u* {0 Z8 n' n8 }; C- V, T // `randomizeHeatbugUpdateOrder' Boolean flag we can; `4 ~4 J g _
// randomize the order in which the bugs actually run" p1 K9 I7 d; J( g& S
// their step rule. This has the effect of removing any0 L" ]: Z+ C4 f4 B, F
// systematic bias in the iteration throught the heatbug6 }5 T8 \3 a$ p3 }6 e" |
// list from timestep to timestep; |! B, B' ]1 d n* a
8 T& ^2 b: L: j, w- R // By default, all `createActionForEach' modelActions have
5 d! Q7 l$ I x" G$ k // a default order of `Sequential', which means that the# M. Y* E6 O/ k. y
// order of iteration through the `heatbugList' will be
# x, w0 e/ v! M' c. s. ]+ |8 u // identical (assuming the list order is not changed q: Z; c) B2 V" q
// indirectly by some other process)./ J! z! j, g) n+ v
" p7 Y, ~( _8 L' y% ?
modelActions = new ActionGroupImpl (getZone ());
* S: C [6 O6 ~2 A7 c' m1 g$ N' i* Q! b* Q, _( z4 K$ Y
try {
$ k0 n; ^3 B' q4 \' M modelActions.createActionTo$message
; g$ | o) j% E* W% u" j (heat, new Selector (heat.getClass (), "stepRule", false));/ z! e1 |! W- G4 E$ h' |
} catch (Exception e) {; X( X1 T7 d4 g4 G6 B- ^+ Y" K
System.err.println ("Exception stepRule: " + e.getMessage ()); n+ C1 S. \0 J/ y/ u, O4 ~1 k
}
5 x. g s3 ^ K0 U9 K
* ?; p0 q" U; m8 J' Y8 P. Q- x$ G try {8 n% I6 i. i: s2 ]- A/ E2 W
Heatbug proto = (Heatbug) heatbugList.get (0);0 Z9 |0 i" L8 Z# Q/ `6 ~# S: E: w$ z
Selector sel =
0 w+ d/ U: f( U" x* {- ? new Selector (proto.getClass (), "heatbugStep", false);1 M Z; ?1 r3 R
actionForEach =& i( Z: Z+ f0 y I( |4 M# Q/ D
modelActions.createFActionForEachHomogeneous$call- M4 ]8 L% c' L7 Z; \8 I( a4 _/ A
(heatbugList,+ ?3 f) x6 b% w1 A: r0 |( _& x
new FCallImpl (this, proto, sel,
/ {0 \5 u ]# V% D2 \/ D1 D new FArgumentsImpl (this, sel)));
, k; y6 C, w1 V) Q- ` } catch (Exception e) {6 A4 j2 I( @9 h" w0 A. ^: d
e.printStackTrace (System.err);
* ^$ @, \/ r$ t( a8 z: m }
1 h' @# i% [6 r; O4 Q/ ~7 } 1 A$ S2 D2 |3 l# b% \5 [
syncUpdateOrder ();
0 M* U! F( B" f- Z q$ g
6 W& L. F3 `8 ^, l try {
' a% H& G+ `8 w" \* x3 H7 E modelActions.createActionTo$message
9 k2 K& D2 y2 ^; @ (heat, new Selector (heat.getClass (), "updateLattice", false)); t+ m+ M. X) g9 Z. ?
} catch (Exception e) {( L) w, g" Y. w, n) a3 k# u
System.err.println("Exception updateLattice: " + e.getMessage ());
) P; C% [" _' Y' y }
" h E5 T9 \6 j! h
. ]) M* b' i: `1 g5 t) ~6 g" L // Then we create a schedule that executes the6 I8 O$ N; U8 G, W* E3 v" {
// modelActions. modelActions is an ActionGroup, by itself it6 g7 G- n. a0 z# e. x
// has no notion of time. In order to have it executed in
/ `* m- G6 X }8 K) i0 e // time, we create a Schedule that says to use the
- `0 Q5 e* l) V7 O( Q' J1 c // modelActions ActionGroup at particular times. This7 y& ^ @+ X4 y! l8 f6 @- m1 {8 j: ^
// schedule has a repeat interval of 1, it will loop every
% O7 X/ P9 ]! ]0 y0 C // time step. The action is executed at time 0 relative to
8 G$ D( q* W6 T+ q/ C' [) d! v // the beginning of the loop.% Q0 ?4 b+ M$ Q8 A) r7 O0 L) R" G
) Y$ ~2 z+ i7 t$ c0 q. w, i9 F! @
// This is a simple schedule, with only one action that is" c- |! M: T: n' E' j1 p% F
// just repeated every time. See jmousetrap for more
8 L9 v$ V- F- P/ `" e // complicated schedules./ H4 `. \2 \) o
t2 x! Z4 v1 y' W/ s6 Z. d7 B modelSchedule = new ScheduleImpl (getZone (), 1);
^4 O V' }/ }. K) K modelSchedule.at$createAction (0, modelActions);
J1 D, W$ ?% L* H1 I( ?" Q 0 \! F' Z% V0 k; d
return this;
0 c, I* T1 f( |+ V7 a6 { } |