HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- U7 F- R' I3 g1 E
+ U4 d- q5 o# ]; b& I
public Object buildActions () {
$ C6 U0 | `3 n super.buildActions();
0 t: v' Z1 Q; t# }( v1 v3 @
8 D u/ Y# j5 o // Create the list of simulation actions. We put these in
5 p0 G* r8 f5 P5 R8 K! \6 H. \ // an action group, because we want these actions to be
3 ~! z; s+ L( n' d5 O4 X // executed in a specific order, but these steps should% D1 X" t- s. J/ c# Y, Z( W
// take no (simulated) time. The M(foo) means "The message9 O- c, k2 R* c: p. ?! [/ M) o
// called <foo>". You can send a message To a particular6 u7 u- ^6 {6 X( v
// object, or ForEach object in a collection.
3 r8 ^( S' b2 j$ t: x7 K
0 |" `) `) _& }2 l) R // Note we update the heatspace in two phases: first run
3 Y' M& F1 ]' t3 ~ // diffusion, then run "updateWorld" to actually enact the; M3 C2 c/ v/ j
// changes the heatbugs have made. The ordering here is
, b/ {3 u$ U. s- L3 a7 [ // significant!
6 W4 h3 j2 I: T - B X: X" T" f, u/ ^) M3 @0 b
// Note also, that with the additional
4 d2 p. \ L$ p: l$ e+ @. ?' l- v // `randomizeHeatbugUpdateOrder' Boolean flag we can6 F& w: E; w$ W! `1 v5 e5 R/ b1 g
// randomize the order in which the bugs actually run
6 f0 n6 y9 X: q8 M% b, | // their step rule. This has the effect of removing any1 j8 H c1 c% Z0 X. r
// systematic bias in the iteration throught the heatbug
; X0 q- o* K: q // list from timestep to timestep
- S- C$ {$ y( R3 ^( d, `. K6 I( w
* f( ~: W3 D& t% w: ^# d5 T // By default, all `createActionForEach' modelActions have# V* X. {: a0 J
// a default order of `Sequential', which means that the
0 Z8 }# ^8 p4 x // order of iteration through the `heatbugList' will be; E( t4 F9 k: r: m- J6 y- L
// identical (assuming the list order is not changed
9 }) N# a. n+ S2 t // indirectly by some other process).) }$ p5 G, Q% e
: Z* ]& e+ A. A
modelActions = new ActionGroupImpl (getZone ());
4 {/ I9 g5 k# V- q. A
- |9 N' x- u% ?4 f" }/ Y% C try {
* B c& y& w2 V+ F modelActions.createActionTo$message: [. W+ L, _2 A1 ?
(heat, new Selector (heat.getClass (), "stepRule", false));* \# q$ s6 I/ n+ M' u
} catch (Exception e) {. l& y6 _; w. G* l! u9 R
System.err.println ("Exception stepRule: " + e.getMessage ());
' R! r, B* ?: F3 Q( T. O }9 i) g$ C% e" ^$ |( u) s1 f. Q7 y" h
. I( W* V# c4 ` try {! c( Q$ D, g1 [1 v4 i, I3 L
Heatbug proto = (Heatbug) heatbugList.get (0);
; c6 Q1 o) B! x' x% ~. N: U Selector sel =
2 q9 W8 ~# K: T) z new Selector (proto.getClass (), "heatbugStep", false);8 t+ |3 k0 F! F; g# H' i4 t9 u
actionForEach =
9 Y; X9 W, L" D9 g2 y! I G modelActions.createFActionForEachHomogeneous$call
0 e) \: N; X9 O1 C a! q' m (heatbugList,
~! Q' r9 y( y5 ?, j/ D+ c new FCallImpl (this, proto, sel,$ E' y( X w+ Y3 _
new FArgumentsImpl (this, sel)));
3 O" M0 q W7 x" b7 Y } catch (Exception e) {
: O8 K( R+ I6 W& c { e.printStackTrace (System.err);
4 r% i6 i& @$ {" {3 ~& {* R }
* O5 E; u5 T" V0 A 9 W7 V& ~4 g# ]) t/ Y# U& L2 [: `
syncUpdateOrder ();: m* F" |% O% j" g0 z2 q: L
8 p! C3 `2 ?. l' f try {
2 U) f; ^ }; p5 ~ modelActions.createActionTo$message * G) Z& n! [, T5 S) T: `
(heat, new Selector (heat.getClass (), "updateLattice", false));2 s. \1 L9 [5 p9 A( N
} catch (Exception e) {
. O4 ?* k w& N System.err.println("Exception updateLattice: " + e.getMessage ());
K' E: A8 o0 S) W/ C2 ^0 i9 O" F }
5 D3 U" M2 P0 f$ O/ B / s% h2 D' O. E7 f
// Then we create a schedule that executes the
% F- {# Z( h/ b4 i$ G! j // modelActions. modelActions is an ActionGroup, by itself it, `7 ]; t# _/ I; U& j
// has no notion of time. In order to have it executed in
' k3 O2 H) \4 H/ t1 D // time, we create a Schedule that says to use the9 y6 _- L) ?- t1 D
// modelActions ActionGroup at particular times. This8 J7 |0 f, a7 A2 j
// schedule has a repeat interval of 1, it will loop every
9 r6 W- V$ S8 g, X( u // time step. The action is executed at time 0 relative to& }9 X/ M, ]' }) Z0 Y+ }- @
// the beginning of the loop.3 Z# R! H% K* {7 \% F6 {0 X
9 g4 y" L$ W5 f // This is a simple schedule, with only one action that is6 k! a3 m6 b! J4 x; d5 ~. ^! ?
// just repeated every time. See jmousetrap for more& d/ R* t4 L" ]1 y6 w+ V- R1 k9 A8 t Q
// complicated schedules.
D8 E: a* n% Y, h
: O$ k. ]+ ^5 [9 P; j) e; F modelSchedule = new ScheduleImpl (getZone (), 1);& {& H; G9 p6 ]: H
modelSchedule.at$createAction (0, modelActions);
9 _- r0 O$ _+ u7 D
) O G4 _+ P& G7 A* L/ X return this;% Y7 |8 R% `, l1 }) j
} |