HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 g( `. _# S3 ?. v4 E; I
; [' |2 G) h6 O1 X public Object buildActions () {' {( I$ O+ @1 j. p" i+ [% B8 i$ l
super.buildActions();9 t- L: B: S* A4 t7 T" b) p( u: [9 S6 d: y
1 T& M3 U+ B/ S" F
// Create the list of simulation actions. We put these in
- w: e6 ?" l7 R // an action group, because we want these actions to be4 e8 p5 ?6 q4 Z: i' ?2 A3 ~
// executed in a specific order, but these steps should9 \+ l2 k/ {2 `3 w, v. O
// take no (simulated) time. The M(foo) means "The message9 `) @/ v' d, v" l$ N" P
// called <foo>". You can send a message To a particular- }4 S! s1 Q( k3 l: t
// object, or ForEach object in a collection.
! ^8 r7 x" l" M9 i- Q 9 b4 H+ H& } y% S( m2 h8 g
// Note we update the heatspace in two phases: first run
& t" G: Y9 t& `6 g // diffusion, then run "updateWorld" to actually enact the
" S7 z8 A0 `# }+ _7 z z% h // changes the heatbugs have made. The ordering here is
% Q1 T7 t; h, ~$ c! h // significant!
/ u [( r* }. y0 y# ?6 ?7 Y
6 s) p& K$ K* x- \1 B' N* v // Note also, that with the additional1 N0 V! H% J. n& f
// `randomizeHeatbugUpdateOrder' Boolean flag we can$ A+ y2 p- g" @5 G2 C
// randomize the order in which the bugs actually run
5 N x$ X' G% M# q' T' f+ p // their step rule. This has the effect of removing any* F$ C" g( r4 T( P; n6 y
// systematic bias in the iteration throught the heatbug
% f& m' s0 V" }: ^ // list from timestep to timestep; w: E6 u; Z2 D2 O9 D" P
5 t4 M R( N" r
// By default, all `createActionForEach' modelActions have& x9 e# T9 Y1 [
// a default order of `Sequential', which means that the! a* X9 g/ y/ @& r. M% {$ E: K) b
// order of iteration through the `heatbugList' will be( h# Q* A1 }' o( }1 e! c5 A% p% J
// identical (assuming the list order is not changed
% D9 g" s/ ^: T7 T6 t // indirectly by some other process).# [& ~8 F& R k9 r( Z
$ X8 _5 l* m9 a* g9 w+ J modelActions = new ActionGroupImpl (getZone ());# A0 u; \6 p2 M5 b5 H$ L
: K2 z/ t& \4 ]( x1 a( V try {
9 L9 R9 L3 A { modelActions.createActionTo$message, H" [ R" o- Q& s7 G
(heat, new Selector (heat.getClass (), "stepRule", false));
- ?6 P! a" `8 V7 k } catch (Exception e) {, r+ }: N2 M2 z; n$ C8 ?
System.err.println ("Exception stepRule: " + e.getMessage ());7 x8 p5 h6 \- k6 y1 I$ M0 R
}0 s- W; o5 w. \9 @) X5 y; B- N
8 _: C* b$ o. G' X; k) J5 o try {
( o( m# s# N# w; p* ^7 ~9 @" Y Heatbug proto = (Heatbug) heatbugList.get (0);
1 h7 C/ J0 E1 v0 c Selector sel = ( @5 J" F& X* U" ?" L3 W) Z
new Selector (proto.getClass (), "heatbugStep", false);' @* j' c7 U+ H7 M
actionForEach =& X2 f) C( N* [1 W- L
modelActions.createFActionForEachHomogeneous$call
( I$ t7 g4 l+ t7 p, o S7 U (heatbugList,* v: B6 e; m- t' Q8 F6 ~
new FCallImpl (this, proto, sel,
( ~: l {7 ^% ~4 ? ` new FArgumentsImpl (this, sel)));
4 |% e! n! w5 d9 f% h } catch (Exception e) {
# h9 j' Y) `8 C+ X8 C5 U/ F. P e.printStackTrace (System.err);, x& D5 V# o7 ?( l2 A( U
}0 q8 O, o! _1 E. ]
) S' J& O9 I* \* g D- I z: P( m. d
syncUpdateOrder ();
4 q# |/ c9 ~8 k& u, F+ Y4 s( u# n( u" `
try {& e' }- y' L3 T: r: v
modelActions.createActionTo$message
# k1 [- h: q/ k1 V: u7 k (heat, new Selector (heat.getClass (), "updateLattice", false));
# ~* I. _2 ]' f Z( h) V } catch (Exception e) {, F2 U( u2 P3 m% J
System.err.println("Exception updateLattice: " + e.getMessage ());" f2 q% ] }$ ]! B3 a2 D% A
}& C6 i9 j) a( z8 L1 p7 m6 u
% C1 y8 i9 i0 l w# Y$ h
// Then we create a schedule that executes the! `5 A4 a3 _9 T) C" F/ Q
// modelActions. modelActions is an ActionGroup, by itself it0 K g& _! c" @
// has no notion of time. In order to have it executed in
8 r: r/ g' X6 B, N // time, we create a Schedule that says to use the C" x6 w! @8 W0 ~- n' r) o T
// modelActions ActionGroup at particular times. This. k+ q0 K! g. Y+ ? f# ], ~. w
// schedule has a repeat interval of 1, it will loop every
' J v5 B' y+ J // time step. The action is executed at time 0 relative to
, _6 _ O1 f; f! Q1 a9 I* s // the beginning of the loop.
+ d( |* Q- V0 J& `$ B( l; \3 k9 w! J! R; E/ Y
// This is a simple schedule, with only one action that is
H- C9 Q5 x# Z1 Q0 l9 D) T // just repeated every time. See jmousetrap for more
5 m% w) P7 p3 S2 i2 E7 [7 D' g- Y$ ` // complicated schedules.
* Y( u! z4 e/ X3 w1 M1 c3 h 3 i7 v# L8 H* L
modelSchedule = new ScheduleImpl (getZone (), 1);
! g ^/ u @& r) H! o/ v8 ] modelSchedule.at$createAction (0, modelActions);
9 w, D" h# Q# X
7 y7 u7 B2 d% m' N; U2 F. K return this;
9 j. ], j$ o0 o+ X# r7 L1 [1 {2 E, Y } |