HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:( t# ?& m. t& J4 f! Q+ s6 a2 u5 n
' ?. ?! g* y* Q
public Object buildActions () {
6 n6 \% C/ |0 G. u3 h super.buildActions();- ~1 ~/ q+ U$ f% f5 ?( P0 I3 F9 H* C9 P, h
* G/ D Q( `8 d+ }9 h# _: C
// Create the list of simulation actions. We put these in; r2 N6 s9 u! a D O
// an action group, because we want these actions to be+ B% U {: s( U; f4 r7 F
// executed in a specific order, but these steps should+ p/ j- S" ?. Q
// take no (simulated) time. The M(foo) means "The message( w4 S* N& ^" N0 C$ c% y9 o
// called <foo>". You can send a message To a particular
9 k T4 h/ G( J4 { // object, or ForEach object in a collection.
* I8 e" c$ F! I4 Y O
# ? V' ?0 o' B; O3 s // Note we update the heatspace in two phases: first run
( S0 @% b( N7 L& P // diffusion, then run "updateWorld" to actually enact the& [! }2 B" p/ z0 U6 i# u, G3 Y
// changes the heatbugs have made. The ordering here is3 u0 P8 s1 L0 W- `3 ^
// significant!
' W6 i+ b3 U& J, F4 s5 a# z " G& V9 f7 Y7 H% U( e3 T- w
// Note also, that with the additional3 a- w. i& R# p8 y. f2 e) ~
// `randomizeHeatbugUpdateOrder' Boolean flag we can' \' s# d x6 h+ R
// randomize the order in which the bugs actually run
' A! s3 N8 {/ l! {7 j; x& p // their step rule. This has the effect of removing any
/ I( f) \8 p3 t9 C3 B! V // systematic bias in the iteration throught the heatbug
( [0 ^+ U* ~" D; E7 w // list from timestep to timestep8 A" P+ q5 E( {
- x* w; c- O5 q9 L r
// By default, all `createActionForEach' modelActions have9 t3 H2 G: d. L' {( e% i- q E7 @
// a default order of `Sequential', which means that the2 B* E) N% K& l! t: `
// order of iteration through the `heatbugList' will be
' ]# Z5 A9 t) k* @# y) o/ z // identical (assuming the list order is not changed
9 ?4 Y' u3 |& i: d2 ?/ c // indirectly by some other process).
* [ J+ m- H) M0 B/ p7 U7 ?
# E. {5 z; x* f* P modelActions = new ActionGroupImpl (getZone ());
3 z6 C, o7 X& E
5 b) W0 {& z+ z8 J2 _* O7 C try {
) P$ s, M1 Z/ O3 L6 k% f0 b modelActions.createActionTo$message
' w9 s0 Y9 Z+ [ K$ U) K (heat, new Selector (heat.getClass (), "stepRule", false));) b; l$ I) @: O2 o1 f8 l
} catch (Exception e) {
$ h5 f' i) e' P6 _: L) K% j% p System.err.println ("Exception stepRule: " + e.getMessage ());, r! B% \+ ]/ t5 o/ R
}
$ e3 o0 C$ r! t0 M* o: J, U ~& J
1 q# n" Y/ |; c8 a8 v try {0 w9 O/ E& b5 h
Heatbug proto = (Heatbug) heatbugList.get (0);
! B- w8 U T+ I0 h) W% \$ i Selector sel =
: a& p& x4 T" y; H9 T! W& R2 j/ i new Selector (proto.getClass (), "heatbugStep", false);8 k+ w6 B: b1 z+ v. d- [
actionForEach =
1 i8 A7 u3 t6 u9 } modelActions.createFActionForEachHomogeneous$call3 F8 V8 m1 a6 T6 j2 C
(heatbugList,* v q. `3 B' e- Q. w
new FCallImpl (this, proto, sel,
. r$ l8 T% ?5 \* d. A6 n% u D new FArgumentsImpl (this, sel)));
0 f8 b5 u0 l4 G( C } catch (Exception e) {9 _! v. R8 y& D* C4 |
e.printStackTrace (System.err);
d$ Z+ y# _+ R5 H/ o }
' T) }5 c- M+ m7 M% v
, L5 t! c( n g/ W& G3 [3 Y/ F syncUpdateOrder ();4 {: h @' P6 H" T0 }8 \: {
( Y2 o2 u. C ` try {
5 J+ }) O! _( [+ h modelActions.createActionTo$message
4 v* z- Y' b& w' I2 }9 z- D (heat, new Selector (heat.getClass (), "updateLattice", false));. n' E1 m" G) l2 w0 r1 h
} catch (Exception e) {
9 F1 F% i' r4 }) y3 ` System.err.println("Exception updateLattice: " + e.getMessage ());
8 d5 s7 I2 \# Y* i. l F. s }
+ S$ n7 B% k0 T4 f7 Q . c& t! G( R# U
// Then we create a schedule that executes the
- H, C' H+ D! R // modelActions. modelActions is an ActionGroup, by itself it
" [0 A" x2 u" b; g$ z // has no notion of time. In order to have it executed in4 a9 p) t/ J$ } k
// time, we create a Schedule that says to use the
7 N: }& i5 l7 c // modelActions ActionGroup at particular times. This
$ ?" S1 M5 d. }1 f2 }8 z // schedule has a repeat interval of 1, it will loop every
5 k6 N9 D* k% h- |) \% ]5 `& T2 c( l // time step. The action is executed at time 0 relative to; U4 L2 ]6 l( T: N6 x. _
// the beginning of the loop.
$ w! z9 K6 D' i+ w; S! K. ]) |5 z% Z* n; H
// This is a simple schedule, with only one action that is
$ @0 ^$ t4 U( | // just repeated every time. See jmousetrap for more& R: M' E: u" V2 l4 k
// complicated schedules.
3 e. r2 P6 x `) ]9 _. t# j; e; P
6 `% ^) F" k+ e' L modelSchedule = new ScheduleImpl (getZone (), 1);
7 U' U* k' {4 {2 \4 b. t modelSchedule.at$createAction (0, modelActions);
. C9 }/ K+ R/ @5 Y2 {1 z
& n H; ~( K7 l8 S+ m return this;: Q' c2 a/ d8 p
} |