HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ [( E9 {/ f$ K8 F$ t5 v3 j o8 j" X, l0 @4 j0 i
public Object buildActions () {- q- _8 p+ _: _
super.buildActions();
/ z+ c9 z2 }0 l0 ]* B0 t7 K$ P( t! S
9 ?7 f. U C1 r0 f // Create the list of simulation actions. We put these in
/ v7 G7 V2 L+ d // an action group, because we want these actions to be6 F2 b) z. Q o; n/ u5 R
// executed in a specific order, but these steps should
, l4 Y O4 R' O) @& R/ | // take no (simulated) time. The M(foo) means "The message# w) {" {, v+ Y8 r/ P
// called <foo>". You can send a message To a particular
4 N# v, x* u" l' b: | // object, or ForEach object in a collection.
) G X$ p; a( C7 S; \& P 5 `; t$ Y) m* u
// Note we update the heatspace in two phases: first run! x" p$ G2 a, d2 e8 O# o2 R+ R
// diffusion, then run "updateWorld" to actually enact the1 o& u, r4 w/ Q9 j4 x; A
// changes the heatbugs have made. The ordering here is3 l9 q! Y4 q. G
// significant!
1 T. @/ s, \! z& s a3 [8 v c4 U( t3 F- D2 [7 T
// Note also, that with the additional5 O, @5 P: z' |. D# m0 A( r: H
// `randomizeHeatbugUpdateOrder' Boolean flag we can" l/ y7 }( S$ u) W/ U) }
// randomize the order in which the bugs actually run& Q$ ?5 N* N. ~* |9 b
// their step rule. This has the effect of removing any
* v* T6 `5 e" Z- ?1 N0 v // systematic bias in the iteration throught the heatbug1 q/ m, |3 a* _1 e- f" L( e
// list from timestep to timestep
1 ?1 t- I( k$ u% V : f; ~5 `# I0 n* a z, v% a
// By default, all `createActionForEach' modelActions have* G, H" A' |4 y& B" h- @) \/ U
// a default order of `Sequential', which means that the
. `5 v4 `1 q2 f1 d1 l5 A" A // order of iteration through the `heatbugList' will be. d' \6 x$ q' v& n: G9 e5 H" F0 ?
// identical (assuming the list order is not changed
& d0 Z6 E9 S( X) y // indirectly by some other process).
7 l" Q( _: X9 K4 U- Y" ~; W 1 ^* Y4 O$ ~: y/ v/ ]7 q
modelActions = new ActionGroupImpl (getZone ());2 `+ j1 V6 C5 s2 }
, ]# W, D4 ^- ]4 a! \: q; |$ G
try {& U. ]3 d+ V9 c7 V
modelActions.createActionTo$message5 B! W- j$ X/ `1 d m* B, w
(heat, new Selector (heat.getClass (), "stepRule", false));; y$ N8 t. H9 x: t% k
} catch (Exception e) {
! A' v, [( A% d/ L) X# v- X; i System.err.println ("Exception stepRule: " + e.getMessage ());
9 |( s `4 ^7 ~; z+ T9 Y; v }
; X! o n2 F4 |) t, d1 X9 H' N/ W, w8 i5 c2 ]: \, M
try {$ _* U3 G6 b$ ?/ A4 o
Heatbug proto = (Heatbug) heatbugList.get (0);' U& ^& N2 f+ Q. w" K; x1 r
Selector sel =
; [' n( l6 T1 H# W: \4 Y6 J0 d new Selector (proto.getClass (), "heatbugStep", false);- A/ W& v g! U* F. y/ U0 q' r
actionForEach =, D/ Q! O: D4 |
modelActions.createFActionForEachHomogeneous$call0 y% P7 F3 u2 g' K4 y
(heatbugList,( q3 T1 `0 p! @* X" ~; J
new FCallImpl (this, proto, sel,
5 d8 ~: D R. @9 x! v" }; k6 c new FArgumentsImpl (this, sel)));
; T4 z6 D0 G' g2 {0 X* N. ~ } catch (Exception e) {& k: T; P$ `3 }& d
e.printStackTrace (System.err);
' P- N8 G; g5 n }( m9 p4 a$ \" L7 l, z( n" L) i& Q2 E
3 w( S7 R3 D, j& r$ }9 B& K# E( a3 [ syncUpdateOrder ();
/ ^4 |5 N [+ u$ X$ D
- ~% I; l- q9 _" m1 o3 O1 v( D try {
- t/ | P) E! B; X0 q# \# Y0 k modelActions.createActionTo$message ) j$ d$ V3 }& [2 m" ^6 L" T$ H
(heat, new Selector (heat.getClass (), "updateLattice", false));+ ~9 Z+ l5 @% T! ^7 k' t2 m
} catch (Exception e) {- a% @3 r- f9 Q# h1 v
System.err.println("Exception updateLattice: " + e.getMessage ());
" N5 {% t, p2 T5 m1 u8 `! c }( u" e2 \4 H+ H* V' |' ~5 X
) q3 O# J4 z+ r. P0 T M
// Then we create a schedule that executes the
" w% M' ]. u( d7 f // modelActions. modelActions is an ActionGroup, by itself it
% w$ y) s. j, @. i, y // has no notion of time. In order to have it executed in V) c3 Y6 J ^/ C: y1 s$ _
// time, we create a Schedule that says to use the
8 | ^' }. K$ f2 Q, O // modelActions ActionGroup at particular times. This+ b- Y& p+ S$ g0 B- Z% C
// schedule has a repeat interval of 1, it will loop every
& o1 P3 Y4 I5 A // time step. The action is executed at time 0 relative to7 t k- y9 n8 B, k1 Y6 |
// the beginning of the loop.
1 w. U( W6 i& }
2 s) B; z: R6 `; E+ s6 S9 q6 N // This is a simple schedule, with only one action that is
% U& k) a" ^. g2 u // just repeated every time. See jmousetrap for more
! H& k6 y5 I$ z; `( |- h5 l/ b$ c // complicated schedules.! `8 L. O/ ?4 D) | @
7 w7 V' G% q. Z0 B. Y! u6 b
modelSchedule = new ScheduleImpl (getZone (), 1);; E. n, D5 K' W2 R7 c( M& f- M$ n
modelSchedule.at$createAction (0, modelActions);
! b; ~! [; k) ]) q) \$ G: {
0 A) h# o: f8 N8 _ return this;
4 y' t+ t: [; w3 q0 c/ I H# T6 y. a } |