HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 R# _! R% l: `8 b, X8 B
- B4 a2 {, \5 e4 n2 @
public Object buildActions () {8 z: f! W3 E% t6 U4 Q- o& R
super.buildActions(); w( R0 A, `' l4 j2 H
: f2 K5 x: S- d3 a2 z( u // Create the list of simulation actions. We put these in
2 j" f L( e- `2 X: F // an action group, because we want these actions to be" m: c- |3 e$ E, B
// executed in a specific order, but these steps should& Y6 C: {8 b( K- {. c4 n6 E
// take no (simulated) time. The M(foo) means "The message
% i; R) c v/ V0 P8 \. x( u C // called <foo>". You can send a message To a particular' e/ q6 B0 J2 k
// object, or ForEach object in a collection.: @5 z% x0 S- m
! K8 f ]) C' b( i( y; t, C2 Z
// Note we update the heatspace in two phases: first run
a0 ?2 L& e3 ~$ n9 V! c1 } // diffusion, then run "updateWorld" to actually enact the
. Z, @3 [. y0 q // changes the heatbugs have made. The ordering here is
6 f, x8 ~: M( R // significant!
7 w3 q0 R7 H& o; x
+ d% I& K# n) P* o6 z // Note also, that with the additional$ z; S& \& o8 s0 u: s/ [8 t3 w; o
// `randomizeHeatbugUpdateOrder' Boolean flag we can) ]. r r3 X3 h. ]
// randomize the order in which the bugs actually run
# Y. b$ F$ G+ u5 M i) { // their step rule. This has the effect of removing any+ X _6 F# \2 _' t8 h' j2 R" b
// systematic bias in the iteration throught the heatbug/ @' e0 |1 I. N. ^4 F# y' f
// list from timestep to timestep+ I& Z* [! K0 C
- h* W4 m# \5 j& M' q ]4 ? // By default, all `createActionForEach' modelActions have
+ p! Y# D5 z; ~6 {$ P% N // a default order of `Sequential', which means that the
$ ~! L/ ]$ I7 d5 x // order of iteration through the `heatbugList' will be
* ~) l# I9 K& k% a, L // identical (assuming the list order is not changed
- \# @3 H7 D9 E6 l/ T8 i // indirectly by some other process).
2 v1 p% O3 U. G- D: ]: y8 M* |
/ {; `! {1 j. W4 S ] S modelActions = new ActionGroupImpl (getZone ());2 \' u2 y: _8 V* D N3 o
3 M- b7 S1 d5 T/ [9 m! L0 f. L
try {2 l$ O% W3 C6 w4 b6 n
modelActions.createActionTo$message
+ y9 y: H6 c& j+ h2 B (heat, new Selector (heat.getClass (), "stepRule", false));/ ` Q j7 d2 {( y+ c' E: s4 Y
} catch (Exception e) {
0 g! G5 _& t8 y System.err.println ("Exception stepRule: " + e.getMessage ());4 i0 K( M- U2 u8 d/ ]1 I6 x
}2 B. u; q N9 F3 I3 V W: \( C
% K: J, c" O! [, I4 e9 w
try {; ~. b9 N- J$ c/ _# O) Y- M
Heatbug proto = (Heatbug) heatbugList.get (0);
1 o0 B& e* U7 ~$ H6 S, n Selector sel = + |2 g6 d# S! C, l
new Selector (proto.getClass (), "heatbugStep", false);
3 ]/ c! r& h! p* K( V actionForEach =
! b) @5 C5 N; ~6 {: h2 G% n& ^! R: | modelActions.createFActionForEachHomogeneous$call
+ } }# f$ v" F0 W (heatbugList,
9 o" c$ j0 R' Z0 L new FCallImpl (this, proto, sel,
9 I. B( @3 T8 c% J6 K3 b. l new FArgumentsImpl (this, sel))); P+ i. N; t( R; @8 \" s4 \
} catch (Exception e) {
( N% j0 T/ t1 ?. }: I e.printStackTrace (System.err);
W$ @2 h- y4 o, m1 x# K: u% ?% Y }
) o% W- c& ^: S5 V
' z* w* g' X7 q$ O9 c, r& u6 X syncUpdateOrder ();8 o: l. e) ^9 L6 P( v
4 Q: T _: X5 F$ u/ @3 ^- ^# B
try {$ ?6 e) B8 z! q" b2 Y
modelActions.createActionTo$message 2 ~7 I, I: a$ o8 p6 e2 y) g
(heat, new Selector (heat.getClass (), "updateLattice", false));
. V% t: ^& b- w; b0 B# s } catch (Exception e) {5 h6 z: `7 @7 b" d
System.err.println("Exception updateLattice: " + e.getMessage ());
) d0 C" c( |: \( X. I a1 {% m }0 B U, \& Z: ]6 D
) g. l& B& j! h. c: u
// Then we create a schedule that executes the3 P2 n: p5 y* Q/ Q' R
// modelActions. modelActions is an ActionGroup, by itself it
( X* Q ]5 Z4 C% W" o5 a // has no notion of time. In order to have it executed in
* m1 n5 d3 g6 i // time, we create a Schedule that says to use the
' D; y2 `0 Q. C. m: k$ n; R2 y // modelActions ActionGroup at particular times. This
# y4 i S7 V1 D0 B5 } p // schedule has a repeat interval of 1, it will loop every3 y* b2 j; P# F! O- r9 ]1 O/ q& D
// time step. The action is executed at time 0 relative to, Z6 r8 d- V9 m. D6 y7 a0 Q3 f
// the beginning of the loop.
: n0 u, q8 t% i& l
! K, F8 N2 v1 G. ?; J // This is a simple schedule, with only one action that is% W5 i* `& S8 i* b: E
// just repeated every time. See jmousetrap for more
Q- n2 L1 a- F // complicated schedules.
' q o* f2 ^! K) b+ d( V( K # p* M2 O7 K3 u* i; W8 }5 e
modelSchedule = new ScheduleImpl (getZone (), 1);: L+ o U1 e% l$ C
modelSchedule.at$createAction (0, modelActions);5 G6 [; E, J. Y- `, D
/ |2 H3 J0 V& L
return this;
! p/ X! r4 L2 K* ? } |