HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, i( z8 w& q' g, X$ d+ c9 r' s
" a( i; |2 M4 I& c" a0 R, X; V8 {& b+ C public Object buildActions () {+ `* M# J5 p2 r' C6 r
super.buildActions();
2 `# U. s. S' z( D% D1 D
' }( b9 l9 O/ T6 m# c4 X* G // Create the list of simulation actions. We put these in7 B6 q! r6 h9 @- ^! c" |0 g+ _
// an action group, because we want these actions to be
% c1 S: H# ^$ `3 ]) i // executed in a specific order, but these steps should+ d% C! z) w. D+ V
// take no (simulated) time. The M(foo) means "The message
& v% }. Z0 c$ O" W. }2 [ // called <foo>". You can send a message To a particular% B4 ?9 o3 c! g* Q" A2 I
// object, or ForEach object in a collection.
) h! m- }8 V4 a/ L ! a, D# S+ e* b% d D8 B
// Note we update the heatspace in two phases: first run
) V; l- a- p6 w // diffusion, then run "updateWorld" to actually enact the
2 Y ~3 P0 ]! V1 _3 T8 l // changes the heatbugs have made. The ordering here is
! ~ H# t }/ h$ K1 ^1 B h" M // significant!
4 S: g0 V1 y+ j
7 s: _1 q2 Z! c- O$ t" K% e // Note also, that with the additional
' f2 d u1 ^, F# ? // `randomizeHeatbugUpdateOrder' Boolean flag we can% D' W! L0 {7 p$ }% N8 w
// randomize the order in which the bugs actually run
! J* R8 m5 A" A // their step rule. This has the effect of removing any
0 P, o1 H$ P# m$ f5 g7 ^& r // systematic bias in the iteration throught the heatbug8 a0 p; Z+ w3 X; L( c. t7 o/ ~
// list from timestep to timestep9 }- s: ]5 k, s) [" I. E4 c
4 v5 Y$ g) w! t% x // By default, all `createActionForEach' modelActions have
: R6 J' u1 U+ w5 |, h3 ~ // a default order of `Sequential', which means that the
5 n4 p8 ~- } c* r, }& k: ?$ \( b // order of iteration through the `heatbugList' will be
. Y, f. X" ~/ y9 j6 r, c" r0 o; b // identical (assuming the list order is not changed
7 r1 \# L9 X. b7 N3 [. H // indirectly by some other process).# ?; J+ H' G& e9 y
8 X4 i; e$ Y+ J! g4 ?# }2 e
modelActions = new ActionGroupImpl (getZone ());
& Y5 W. g3 _! }, ~) a0 F# _# F+ S/ P2 C6 m! C: }
try {
8 }/ b2 [1 I2 u% W W# Z modelActions.createActionTo$message! I* b* b& f+ y
(heat, new Selector (heat.getClass (), "stepRule", false));- D9 M3 E2 t* U6 y- ]/ S1 O
} catch (Exception e) {- w- `6 V$ B, O; [
System.err.println ("Exception stepRule: " + e.getMessage ());: v! z- u9 ?0 k' L4 n( q9 }7 B5 {: Z
}
3 h% ~2 M$ f1 E) i9 P# P8 D
( \% I; T5 n9 L0 Q( z+ U& F2 v try {
" ` K# c$ d: L6 ?6 a4 c; g Heatbug proto = (Heatbug) heatbugList.get (0);
( S" J+ D: B6 H! f d3 M Selector sel =
9 b" C7 p3 F/ i4 I, i0 l$ s new Selector (proto.getClass (), "heatbugStep", false);" R+ _% |. k$ U I" h7 O, e
actionForEach =
, M. A7 S: T7 o( `& M( F modelActions.createFActionForEachHomogeneous$call* J4 h3 T1 S |: C" T
(heatbugList,
: x' o# j: U! H- N% A+ `# y new FCallImpl (this, proto, sel,6 J- s! U% I2 P4 Q' R8 u
new FArgumentsImpl (this, sel)));
" ^1 P/ y2 W e* J9 S } catch (Exception e) {
o$ b" e9 G# d0 B+ h4 a. s" [: x e.printStackTrace (System.err);
, {5 X/ ?# l1 c; H+ G/ C( h }
* u& l8 ^9 ^; P5 }2 [
6 l! q0 v( K2 X: ]! d+ ^ syncUpdateOrder ();! ]& W: H, g. t- Z: k& _- f5 u% M9 U7 q
. a ^& S* G2 N& S1 _ try {2 Y( I! a3 B' V
modelActions.createActionTo$message
$ ]7 j' w0 k, e7 G# }, O (heat, new Selector (heat.getClass (), "updateLattice", false));
2 D# ~6 Y) \+ Q0 h+ R: G } catch (Exception e) {. e# ^5 k2 r, I# w1 Y0 O
System.err.println("Exception updateLattice: " + e.getMessage ());3 [4 ^/ ^7 W4 h
}) K. t( S/ Z. X$ {/ m- D& X9 N
p+ ] O' e! k
// Then we create a schedule that executes the
% U( {- G9 h r0 t5 J8 l // modelActions. modelActions is an ActionGroup, by itself it8 E- D* c/ d) n& C
// has no notion of time. In order to have it executed in
' v- F" m9 A V# I) g) m6 p8 f // time, we create a Schedule that says to use the8 [0 V$ r. J& v2 [6 U) f" x
// modelActions ActionGroup at particular times. This
& }( Q2 g2 y+ x // schedule has a repeat interval of 1, it will loop every
' x1 {/ M: _! s4 u) C% x T: D // time step. The action is executed at time 0 relative to
5 ~: i) Q) K! e9 k // the beginning of the loop.
6 p* k* Z [. k# e. |' m% F8 O3 ?5 l: @/ w& f- x# J
// This is a simple schedule, with only one action that is* J1 Q7 B# @" a/ b2 X h# p: z
// just repeated every time. See jmousetrap for more# ]5 J# s, B, @1 u* y% N* o
// complicated schedules.5 l8 b. m, G s6 h: j9 K
5 @6 g, M8 K& r( \; A& U6 l- @ modelSchedule = new ScheduleImpl (getZone (), 1);
+ V) H/ \- @( { modelSchedule.at$createAction (0, modelActions);
. ] B+ O. Z* [5 M) {* i & y- I5 b) V5 Q% z2 w; r
return this;$ |* T- o1 k3 {- f; a
} |