HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ f) F7 v: p/ ~5 w/ T$ |
/ {) _: d9 F5 e& Q6 m public Object buildActions () {' S' \# j+ S% [& S
super.buildActions();7 R3 X, i7 n! Y. E* r
5 {$ h5 ]2 z# g. K* O // Create the list of simulation actions. We put these in! _. h* ?0 o8 [4 N2 Q7 G
// an action group, because we want these actions to be G+ D/ w! G6 Z5 k: h* l, s Y
// executed in a specific order, but these steps should4 q r" d) y$ ^: ]" F
// take no (simulated) time. The M(foo) means "The message- W/ X2 z0 m, o/ `9 ?% ?% |
// called <foo>". You can send a message To a particular6 z t- r! T$ r' S3 T
// object, or ForEach object in a collection.3 q: S: K, k0 a4 @0 H
9 r7 X4 r, c, e* `/ m& P2 ?
// Note we update the heatspace in two phases: first run
; A. p; J, p7 y! Q- i$ W // diffusion, then run "updateWorld" to actually enact the# K6 t0 ]! e$ p, k
// changes the heatbugs have made. The ordering here is8 ?" |" @" [; X1 j& a
// significant!7 [8 U" `* h) W" D2 q
$ ]* j) M; S& h0 I8 O$ j
// Note also, that with the additional
5 F& U( ~( A# |* }8 x8 ]6 o8 [5 Z$ Y // `randomizeHeatbugUpdateOrder' Boolean flag we can3 K( q& x# d4 { [+ T
// randomize the order in which the bugs actually run
) A [% F- ]5 M, C3 F0 o: W5 _) Q // their step rule. This has the effect of removing any# g- r% W5 B- P+ e0 T6 U/ n6 ?2 c$ [
// systematic bias in the iteration throught the heatbug
' ?+ F; o9 B. i0 m! ? // list from timestep to timestep
; Y/ j0 W/ n' a, h& |4 V: M# @ - Z+ U! s5 ?# K! O6 J
// By default, all `createActionForEach' modelActions have7 v2 R4 a9 I) O2 ]6 G) z
// a default order of `Sequential', which means that the
5 |7 ]+ N7 w2 ^! c+ _# r7 j // order of iteration through the `heatbugList' will be( x) |# {0 W5 h9 B5 M* n
// identical (assuming the list order is not changed
- U- G+ M# Z8 f // indirectly by some other process).
$ |/ I: T$ }+ G' ?" L. h% m $ P" w. L$ b; p) m% O
modelActions = new ActionGroupImpl (getZone ());; b$ m, z. e! V ^
o- |5 G0 J+ V3 F try {+ o, F3 g) K4 M4 x3 w; Q6 B0 n
modelActions.createActionTo$message/ H8 I M: \6 v r8 ?! }+ E
(heat, new Selector (heat.getClass (), "stepRule", false));7 y$ h. G" B6 n4 Y
} catch (Exception e) {0 K% d. r! k& K0 D- u, T
System.err.println ("Exception stepRule: " + e.getMessage ());
1 D0 }' b' y# |; C* D }' z5 q, C1 J+ g" `, P
7 `! _4 L3 ?/ A% H$ d
try {7 K- I: h3 g# ?* z9 Q: w6 c
Heatbug proto = (Heatbug) heatbugList.get (0);
5 }2 @9 j' i) W! h8 p% y Selector sel = 4 F2 s4 W: o5 f4 M- Z& b
new Selector (proto.getClass (), "heatbugStep", false);
$ G3 p/ Z1 J: h/ A, T! r! l actionForEach =
0 U# C6 j5 l- f9 S modelActions.createFActionForEachHomogeneous$call, R R) z* [1 Z: D
(heatbugList,- O' S, g. L' U
new FCallImpl (this, proto, sel,
p* A$ f( c3 j4 I$ o new FArgumentsImpl (this, sel)));
! ]1 [* j# w* j } catch (Exception e) {
3 E! M- k& r; k/ \( f) _ e.printStackTrace (System.err);
3 s" l. [& H, h$ ~1 Q6 r }. f' C+ H* g' V( m' M; t
; b8 O9 c9 e$ Q$ @( U
syncUpdateOrder ();8 b1 t Y) C, u) \4 P" h% T/ u! }
) n( P0 S1 e6 U, a0 G: M& y
try {" i7 l r( z+ S, C) `2 ^
modelActions.createActionTo$message
5 `# d8 G0 F: F3 U- | (heat, new Selector (heat.getClass (), "updateLattice", false));
1 w9 f( G, U1 k } catch (Exception e) { r3 E* e2 [% `9 `
System.err.println("Exception updateLattice: " + e.getMessage ());
+ e w( i; `3 R0 k4 Y }; @3 s4 ]8 t! e$ R7 j# G+ k5 A5 q
* V/ ]& x7 o/ Q2 a7 ^ // Then we create a schedule that executes the2 s6 ~4 V' p7 \# d B5 G
// modelActions. modelActions is an ActionGroup, by itself it
' M( ]) r! B2 j) ]3 W) d // has no notion of time. In order to have it executed in
) W% j2 o1 l7 s // time, we create a Schedule that says to use the6 z% l; C% Q5 Z8 j, S* j6 h4 |% W! i
// modelActions ActionGroup at particular times. This
3 t: m8 j2 y/ K4 }8 ^ // schedule has a repeat interval of 1, it will loop every
3 C) V9 D- u/ a+ i+ N // time step. The action is executed at time 0 relative to
9 f- B* ]5 U$ C* S6 O- G. x // the beginning of the loop.
# D- @3 k6 b8 K" s/ L+ ?5 i7 o0 j; U# W) e
// This is a simple schedule, with only one action that is3 @+ P# S/ {9 v+ A
// just repeated every time. See jmousetrap for more
& Y/ f+ O8 [" U- O% p# O" o // complicated schedules.7 `, P; Z) K* f0 [: g _
7 a; i9 f( H/ y/ _: M% Z
modelSchedule = new ScheduleImpl (getZone (), 1);8 q. S8 U. B( A( U4 D" W/ S
modelSchedule.at$createAction (0, modelActions);2 u- k* ]4 j. C8 h L
2 Y1 D# K& z! L5 g( L
return this;
: Z' @. z# S% z } |