HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* W) H4 p$ {: I: `
5 A m2 {" I6 k" B
public Object buildActions () {
9 }4 U2 R* s7 f$ k: l, _2 \ super.buildActions();
+ z- E( a, {. D2 U$ K) ? . f" l+ J8 i8 r0 [+ p
// Create the list of simulation actions. We put these in
$ r0 ]# o0 O m: b( G // an action group, because we want these actions to be7 U* s8 V& {+ f, I8 v6 M
// executed in a specific order, but these steps should
3 c+ I4 t {! D( j5 h+ ]( r, C // take no (simulated) time. The M(foo) means "The message4 { k6 }$ Q* B7 R) S( J P( @( `
// called <foo>". You can send a message To a particular
) a# _1 C; W' H3 }2 P // object, or ForEach object in a collection.
+ y2 e& a$ X, d- k9 J
1 q# z6 ~% F, @& N4 c- L! _ // Note we update the heatspace in two phases: first run
1 p. W- {( ?" m9 Y# f // diffusion, then run "updateWorld" to actually enact the- l9 x; n' M8 b" L4 `0 ?
// changes the heatbugs have made. The ordering here is: |4 X! I8 z" ~0 Y- L$ o
// significant!! @0 \! ?" ?3 I6 ~8 E
' ?8 u$ \; d/ |! ]# C; _8 C // Note also, that with the additional" x0 [9 [& g' B4 J- t* u
// `randomizeHeatbugUpdateOrder' Boolean flag we can" b9 j/ l5 V2 e c$ m8 u
// randomize the order in which the bugs actually run$ ^+ [; ]; J. D6 J* B' r
// their step rule. This has the effect of removing any- p, u+ }8 d5 i( y3 f6 ]
// systematic bias in the iteration throught the heatbug
5 T7 [6 T0 M! |0 I* [ // list from timestep to timestep
$ Z; d) n# `# ^$ D' G- u5 g
3 ?' N" M* Z- F' Z! J; ^ // By default, all `createActionForEach' modelActions have
; f6 g* c& W' n u2 W+ o2 j // a default order of `Sequential', which means that the1 F; m& {3 ~8 A( \# P
// order of iteration through the `heatbugList' will be
/ I. {5 k1 G: B // identical (assuming the list order is not changed- e5 U5 ]3 {& e# L' ]2 k
// indirectly by some other process).
3 ?8 I5 Z w/ e' o. r
# t' A x5 Z/ M( x modelActions = new ActionGroupImpl (getZone ());
; u- k% y4 c7 x T {0 ~' B4 Z& M( ]+ N- x9 N4 |
try {/ I2 N- h' T L
modelActions.createActionTo$message
& w7 [2 E* o% y (heat, new Selector (heat.getClass (), "stepRule", false));8 J) Y# g F8 \' l( `
} catch (Exception e) { }0 U0 a1 K; q9 v- }- |7 k, w/ W
System.err.println ("Exception stepRule: " + e.getMessage ());& p+ [6 x- n& a. `
}
' Q/ o/ g6 ^" V% y& G: v, s& l8 z4 w- x3 y& g; A# P
try {
- w @2 Q" C6 Z9 k J9 R3 K Heatbug proto = (Heatbug) heatbugList.get (0);
. k. i5 A0 U3 {" D; Q4 i T- Q+ S Selector sel = D q: N N3 s6 e! P! x6 t6 u
new Selector (proto.getClass (), "heatbugStep", false);
7 S2 T: _8 f' }$ \% x) ?; M( C actionForEach =
! O. ?$ j( ^; t modelActions.createFActionForEachHomogeneous$call
& ] ?0 c% N! _8 e) W# l Q( | (heatbugList,
Z o) t8 B2 Q* Y- m6 [ new FCallImpl (this, proto, sel,
+ f7 b8 s+ {$ {- m3 u- O2 t# ^ new FArgumentsImpl (this, sel)));$ V. r! |' O/ W/ u$ E! o& E; x! N
} catch (Exception e) {
% j) q- v1 X: F! K! V# U; { e.printStackTrace (System.err);; H4 Q6 {* `# H P& j+ J+ }# N
} n; X7 T" q* p- K1 h
: y0 K/ v0 | B/ m
syncUpdateOrder ();; O+ N2 ^. s9 ~( {
( w0 B# N0 ]5 n" B
try {
5 ~, C* H0 `. }8 } modelActions.createActionTo$message
& f, q0 P% j* z8 { (heat, new Selector (heat.getClass (), "updateLattice", false));
$ X2 x9 v1 _$ ~& x6 I3 ] } catch (Exception e) {
) D! r, W& ~* Y; }$ B0 ~, \) V$ O System.err.println("Exception updateLattice: " + e.getMessage ());# K' _2 Q' Q7 _' u/ [% N
}
9 i; ~- i6 a0 L# ]; u8 Y m) `, R& e* u) W( O5 H8 ~& R# H
// Then we create a schedule that executes the. {5 d! t9 R7 r0 ?6 m1 b+ t
// modelActions. modelActions is an ActionGroup, by itself it! o4 {' S/ M7 f) m. @
// has no notion of time. In order to have it executed in; w' Z4 m- W8 q9 }1 U$ F
// time, we create a Schedule that says to use the' G0 @ [ ?* D2 n7 q
// modelActions ActionGroup at particular times. This( O5 ^( H# W4 W+ o, @. [7 o
// schedule has a repeat interval of 1, it will loop every+ k: W$ D; Q9 w- z
// time step. The action is executed at time 0 relative to/ v/ r2 x+ h6 `
// the beginning of the loop.9 H4 a: b @& q- Q
: m3 H$ g. q3 K7 N X% a/ `& s* Q
// This is a simple schedule, with only one action that is
6 j3 V1 B+ J( h- P* P8 k // just repeated every time. See jmousetrap for more
( |1 n) R( }6 n( B- ~4 A! h // complicated schedules.
% I6 x. r) \/ j4 m& S
! Y9 D8 [1 w! B: `: z ]2 O8 a modelSchedule = new ScheduleImpl (getZone (), 1);/ B! _0 j- _/ R' X
modelSchedule.at$createAction (0, modelActions);
( N, u8 F2 h5 h- y, `
r2 A% N8 U' E- D% y return this;
9 D7 b1 Q% {7 j e: r, j } |