HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
$ Y4 }/ y* r# r8 t0 J$ Y3 c
! R0 Z1 W7 u8 h public Object buildActions () {
2 o1 s2 O1 ^( C$ f super.buildActions();! L* J+ x3 ?: N( }, S
# e4 V- ]7 ~" f& d4 |
// Create the list of simulation actions. We put these in
% g' h' S8 T9 ?0 P0 [7 r // an action group, because we want these actions to be* o+ J& ~: x; s) P% R
// executed in a specific order, but these steps should/ }. K( ^) X% [$ [4 G c; m; z
// take no (simulated) time. The M(foo) means "The message5 L- U, V# {1 O/ {8 ]
// called <foo>". You can send a message To a particular4 @' K! [) J9 Z' r
// object, or ForEach object in a collection.8 R7 y# s! ]7 r
+ G) x6 u0 d0 R
// Note we update the heatspace in two phases: first run
# z& H5 O) E, N6 z, {1 H // diffusion, then run "updateWorld" to actually enact the
" d( t5 Z h: Y a // changes the heatbugs have made. The ordering here is
! n$ a# n( K- q7 ]( m // significant!
& G: x$ ~% L8 {
! @8 w% F& O. ?( d/ g2 x // Note also, that with the additional
9 M) O$ S% {" e& j9 @3 ~ // `randomizeHeatbugUpdateOrder' Boolean flag we can
5 {1 i3 Q+ @% H' k3 C // randomize the order in which the bugs actually run
! W, g' e& k4 U. a // their step rule. This has the effect of removing any
; }- C7 q4 T" z y // systematic bias in the iteration throught the heatbug7 @7 F6 @2 f* z5 c& v
// list from timestep to timestep
+ E+ ]- T2 ?% Y* ]; m- @- |
, e3 G" s6 E) x6 Q$ `+ t // By default, all `createActionForEach' modelActions have' ~2 v& s; o& p% z
// a default order of `Sequential', which means that the
+ W% C2 r1 O7 c // order of iteration through the `heatbugList' will be* ^6 S/ j8 M+ F( F7 R" X$ y! A
// identical (assuming the list order is not changed, c' e3 S: m8 z- j! L; P
// indirectly by some other process).4 L" R( b! J, X+ k, A D/ a; h
1 S6 V0 \, q5 n5 S- K7 ]. b
modelActions = new ActionGroupImpl (getZone ());
6 z" k' r/ L6 i0 Q
. J. l9 t# \$ B" _) ` try {$ A2 u( J7 M7 N( _- u; b1 h1 a' C
modelActions.createActionTo$message' w D! v. P' |& J6 [
(heat, new Selector (heat.getClass (), "stepRule", false));
- f! b/ Q O2 N o; R& [ } catch (Exception e) {+ m/ K4 v* q3 \8 b4 e
System.err.println ("Exception stepRule: " + e.getMessage ());
( u& X6 |$ ~' g6 B, V! R }& C! V2 R8 {7 V, ^# M/ W0 @2 y
4 r8 u$ `" A! s: k
try {
* ~$ G }( x1 E' w1 I u Heatbug proto = (Heatbug) heatbugList.get (0);
; Q t. }, N# Q Selector sel =
y/ P( M7 r4 @% k2 `- x new Selector (proto.getClass (), "heatbugStep", false);1 b0 m# t7 Y! u* Z# ]
actionForEach =6 Y: B0 l! q' Q3 i# F' G4 k
modelActions.createFActionForEachHomogeneous$call( ?7 O3 s/ A5 H: b1 i
(heatbugList,8 {+ k- Q5 _1 H6 j F6 Y
new FCallImpl (this, proto, sel,
( @* j7 }2 f5 w9 t; J" s% D6 c new FArgumentsImpl (this, sel)));
7 ~; F4 b& j' J( F) i t } catch (Exception e) {7 i0 b. _8 E1 D$ N3 j
e.printStackTrace (System.err);
" A8 i5 y6 x( _9 Q }% ]& L& }6 B' G. i5 M
% {" C, n3 v# ^+ K$ K$ q syncUpdateOrder ();- O& S1 `3 ]' x% c% J, \, G
6 g+ K4 F! E! j8 q try {3 P v1 n: W0 [( A& C
modelActions.createActionTo$message
: s; g/ C; P+ p (heat, new Selector (heat.getClass (), "updateLattice", false));# |) ?# S" W6 b$ f
} catch (Exception e) {
, `; U; d6 |+ b1 }9 w6 W4 _. U; T System.err.println("Exception updateLattice: " + e.getMessage ());% w% W. r0 a2 U% u% c% n
}
( @, d* z1 j* R. i& t : ]: _! `# ~* Z2 O& x% Q) }! c q) K
// Then we create a schedule that executes the; ] j% g: [6 _6 i/ F
// modelActions. modelActions is an ActionGroup, by itself it& }- i' i" q: n4 x3 g1 E7 a
// has no notion of time. In order to have it executed in( x5 [, G6 A% E9 f* F7 v
// time, we create a Schedule that says to use the7 h8 C. I1 T% L/ a. ^; S% y1 J
// modelActions ActionGroup at particular times. This6 u( j9 g! H4 U$ ?
// schedule has a repeat interval of 1, it will loop every6 k+ d" O8 p, P! M4 N! ?
// time step. The action is executed at time 0 relative to2 @" D$ [/ R& F' W7 h3 H4 D1 H
// the beginning of the loop., j$ @! r. v5 `! o7 L) \* H
3 H+ b" r2 J2 \+ ? // This is a simple schedule, with only one action that is
( M: v/ c" r4 a* a* S* u% \/ j! o // just repeated every time. See jmousetrap for more# }/ p# d+ @; `9 A
// complicated schedules.7 Q, d9 ~" L# D T6 }7 g
* o( r% d) s @, E: Y modelSchedule = new ScheduleImpl (getZone (), 1);, [8 L3 x9 m, G& j' K* L
modelSchedule.at$createAction (0, modelActions);
7 W/ \' X7 Y4 b" y( [ t
5 D) P6 H6 Q2 g7 Z8 T$ U return this;
* q! S6 |$ I% f/ ]- M; d } |