HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
j! t: R& d& ]! Q2 _3 z, u5 C
% j) L5 B* t4 ~$ H2 F public Object buildActions () {
1 ?- }4 s! u; y. n6 H- R4 _ super.buildActions();
1 A) S9 E' |6 D8 B8 b/ S
/ Q5 }. S) t4 @" D // Create the list of simulation actions. We put these in
9 g- e" p, X- d, n1 ? // an action group, because we want these actions to be, A6 w f K* `8 Z) g2 Z$ {' T5 C
// executed in a specific order, but these steps should
& @& \) Z! K: Y // take no (simulated) time. The M(foo) means "The message, h7 c2 ~% O+ n ]
// called <foo>". You can send a message To a particular3 r$ k8 @3 D' \7 C+ C6 V' ~9 h
// object, or ForEach object in a collection.
0 J' R0 I! D |! p6 \( X 6 Z4 v2 Q& ~# p
// Note we update the heatspace in two phases: first run8 S1 }. X# B1 K) l6 W
// diffusion, then run "updateWorld" to actually enact the
* \! V5 X$ ~5 T( F3 ]) x; | // changes the heatbugs have made. The ordering here is- [3 t" d5 Y4 A) ^7 v9 B
// significant!
) M5 T1 c% S. c4 y t
8 G1 b# ~( b' \! Y& F4 ` // Note also, that with the additional/ \. _5 L$ s6 E
// `randomizeHeatbugUpdateOrder' Boolean flag we can
\$ _6 C% O" \( e3 t: C // randomize the order in which the bugs actually run' L; `( {$ Q- J( l5 K
// their step rule. This has the effect of removing any: [# I7 Z' @/ ]( M- y7 Y+ u3 |
// systematic bias in the iteration throught the heatbug
$ k2 a$ S. S+ T, b2 r // list from timestep to timestep1 G- a& y; P0 a6 V$ n" N! w
) f# u; _; g5 X8 Z4 S/ M
// By default, all `createActionForEach' modelActions have
. i8 K. C b8 u8 M6 V // a default order of `Sequential', which means that the
3 ~2 k2 `" m. V6 P // order of iteration through the `heatbugList' will be+ I. E8 L5 U* P- d* ?5 F3 X) p
// identical (assuming the list order is not changed
! I3 T0 B3 _4 l // indirectly by some other process).: V/ E% r N1 ]7 f
G( q8 h, P( g4 E5 m5 @* t, w1 o modelActions = new ActionGroupImpl (getZone ());
0 b, b! s2 f# t9 e( n: x I' Q# I0 g( P$ t1 ^; W6 r
try {
3 H0 z1 n3 f: w& v modelActions.createActionTo$message
* _* z3 z( X' D; ^9 `/ ^6 z, N1 h5 Q (heat, new Selector (heat.getClass (), "stepRule", false));* K+ F, L: q1 O% D8 c2 ~7 V
} catch (Exception e) {' r8 a6 X0 v' X+ N
System.err.println ("Exception stepRule: " + e.getMessage ());
7 \* F5 H& U$ M' k o }
/ Q7 { n+ f) E- m" y0 F" {4 o( I" k( k
try {
2 w6 [! B. N7 g- q. l v Heatbug proto = (Heatbug) heatbugList.get (0);' k$ R k. |: Z% ~+ E
Selector sel = 2 A( a h8 t, x: d, Z2 M
new Selector (proto.getClass (), "heatbugStep", false);6 H# u! j, B$ g+ l( c b
actionForEach =
- v) {" G6 A5 v; D0 d, a E1 W modelActions.createFActionForEachHomogeneous$call
$ w$ ]$ v3 m* G. D (heatbugList,( N. E6 ?+ I% J! w* m0 b W9 G
new FCallImpl (this, proto, sel,
0 E% h- W8 H# Q7 ^( Y2 D& l) B new FArgumentsImpl (this, sel)));( Q2 Z: F* o# w$ M' _4 ?
} catch (Exception e) {
2 W: z$ o2 `5 u L( D: f- s8 m# Y e.printStackTrace (System.err);
' r# J4 F# Q2 ~, G. { }
5 d8 W7 \; ~: i2 a+ O' b% o " m3 j9 E6 g: \; l4 u; v5 Y/ l
syncUpdateOrder ();5 S; D1 z- P1 f4 `9 ?6 t1 P# b
5 `: o* L: R& Z% M3 r try {8 A. Y" r3 c7 k/ B& h" Y3 H
modelActions.createActionTo$message
# s" v/ x5 l! W/ a6 v (heat, new Selector (heat.getClass (), "updateLattice", false));7 ?$ S1 \; ^+ o1 t, T
} catch (Exception e) {
$ S+ \8 V& H, a) K8 [% I' D System.err.println("Exception updateLattice: " + e.getMessage ());
|( a" {0 Q) u* S8 ? }+ s( b, n0 {( d; }; q4 O5 f
+ s* m2 K. V* c& p$ G: ]6 c/ K% {9 k // Then we create a schedule that executes the& l; r" c) y& K0 _
// modelActions. modelActions is an ActionGroup, by itself it
% l: H# U3 x3 w$ l. t, q: Q // has no notion of time. In order to have it executed in
8 U( f# y5 L3 t! ~ e- X. J: H7 @ // time, we create a Schedule that says to use the1 o1 o) q% b- X
// modelActions ActionGroup at particular times. This' H# c8 J" {5 x$ z
// schedule has a repeat interval of 1, it will loop every Q" I* i8 G0 B. d! Z
// time step. The action is executed at time 0 relative to
1 o) W9 _# s5 S) T+ e9 A // the beginning of the loop.
% p5 x1 P2 ~! W; N! U
3 B) V3 j. x0 g1 c6 a r3 v, p // This is a simple schedule, with only one action that is
: M" ?2 J, c+ C" y // just repeated every time. See jmousetrap for more# U) j0 q) @& H7 j* J. v
// complicated schedules.
2 h, w7 ?( k7 \4 V3 |
d& x: P [! z modelSchedule = new ScheduleImpl (getZone (), 1);
: |, s* t8 x" T) W+ ] modelSchedule.at$createAction (0, modelActions);1 m) m; N( r b& x3 H
% A/ R: i) a6 {5 ?2 |7 A8 |
return this;
7 Q# H; w4 u7 `9 i: X- q; ?& C1 E } |