HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:: E1 I8 M9 E2 l
3 p( e. @' r3 n" N- A6 A
public Object buildActions () {5 K5 V9 G: G! A! v& g' e- t
super.buildActions();
! [( c# f( w2 P0 J: q: Z) N( p ! `! z4 \9 g' k2 |
// Create the list of simulation actions. We put these in
( w4 C, a0 m; ^" i // an action group, because we want these actions to be9 t5 N! ?0 h6 P( d1 T; f) N7 A2 b6 U
// executed in a specific order, but these steps should1 @6 O( c7 n% ]
// take no (simulated) time. The M(foo) means "The message
& u% b ?6 Y5 M% C // called <foo>". You can send a message To a particular
- X- c) y' r) A9 x- ? // object, or ForEach object in a collection. k) h( F' e3 m, H3 s: j
- f& A1 g% Y. U
// Note we update the heatspace in two phases: first run
4 P4 v8 m% m' f7 b( p0 h# s // diffusion, then run "updateWorld" to actually enact the0 I! U8 A i# ^, c$ x0 H0 T" K& D
// changes the heatbugs have made. The ordering here is' z$ O+ W' k; f: B5 N4 N- B
// significant!
; J4 T; n* f j6 l6 C 8 J2 P; x8 u ] j7 |& c
// Note also, that with the additional
( L' r d/ S) N$ k# b // `randomizeHeatbugUpdateOrder' Boolean flag we can4 J( N' |' V5 w: s; i f! C3 O
// randomize the order in which the bugs actually run z. \4 ^+ g% ]5 i
// their step rule. This has the effect of removing any: Y0 z# }, M [! D6 g
// systematic bias in the iteration throught the heatbug
& S4 _0 E! u" i0 G$ S9 l$ r // list from timestep to timestep
5 A7 g- j* g! O5 Q$ G8 M7 V: U5 ] 1 r% v# N0 V$ y& Z& S* u
// By default, all `createActionForEach' modelActions have
! y5 Q2 i2 n* c! M8 h // a default order of `Sequential', which means that the, }+ D& V" G7 W ^+ W/ B1 R& Q; F% d
// order of iteration through the `heatbugList' will be
( Q% k4 f4 U$ z // identical (assuming the list order is not changed7 {' y3 j& p: R% P% E
// indirectly by some other process).
/ \5 Q- D4 E* @% }5 a0 }
2 U: O U( }8 i: V* ~# [. ]5 p6 B3 m modelActions = new ActionGroupImpl (getZone ());1 s; a7 X% M* k
& U+ x. V) i- L2 ?' e% A
try {: a0 b' w/ C0 S% n" w
modelActions.createActionTo$message
% u- p7 Z. L9 M3 `9 I5 Z' _ (heat, new Selector (heat.getClass (), "stepRule", false));# P5 C, N. k% P5 A% _
} catch (Exception e) {
) a F$ ^- e1 ~/ ~6 H+ `9 p System.err.println ("Exception stepRule: " + e.getMessage ());
' ]+ {( A3 ?8 ~6 y6 ~ }8 V5 N8 `8 h- X
. F6 G( y, V$ N0 F& B0 C try {
2 I: n( Z n; I. N* d3 v+ w Heatbug proto = (Heatbug) heatbugList.get (0);' Y, O, T: C* t2 f
Selector sel = 3 G, b" e6 n. U4 L
new Selector (proto.getClass (), "heatbugStep", false);9 N5 N5 g# q: {" w
actionForEach =% g; c/ _7 D& |" O
modelActions.createFActionForEachHomogeneous$call
9 L$ R# z. ^- O. Y5 N' w+ s1 @" n (heatbugList,$ p' n1 |% n4 O0 Y) `
new FCallImpl (this, proto, sel,
8 t# T( F5 Z3 Z) a2 x; q new FArgumentsImpl (this, sel)));
- T2 [( [1 x, \. ^! l- [ } catch (Exception e) {' z* p; W4 q: X& P/ q2 ?' {0 E
e.printStackTrace (System.err);/ f6 q0 S7 ?& \8 A
}
4 f/ t/ e( y8 K6 w 6 k0 X+ r2 O' n1 o- h
syncUpdateOrder ();. @% z- I9 [5 `7 G& W# E4 ?
6 X. A6 y- x5 C b d& a2 C' E try {
/ j6 ?( w9 [3 p- A! \* D* G modelActions.createActionTo$message
- s0 k; }: \& o8 n m (heat, new Selector (heat.getClass (), "updateLattice", false));0 S) C- ~9 M3 f' k
} catch (Exception e) {( i) q6 M4 |1 S' [
System.err.println("Exception updateLattice: " + e.getMessage ());- c, p& G- j/ [3 S+ ]' I2 Q& W9 l1 ?
}
3 Q" L! X+ u+ v S
b5 K4 p5 ?# }: s9 h1 H. }* L3 y // Then we create a schedule that executes the: ^/ J0 l& L( m% e0 }4 {) t" a L
// modelActions. modelActions is an ActionGroup, by itself it
8 Z& g0 R; a! [. n // has no notion of time. In order to have it executed in
) P. w6 v. B1 n0 B7 T8 w2 D0 T // time, we create a Schedule that says to use the
8 o: A; ` J* t5 y( [ // modelActions ActionGroup at particular times. This! m- @( {1 W' R5 b& A# }7 l4 ?9 @* i
// schedule has a repeat interval of 1, it will loop every9 {: m7 s' ]) F: P# O. Q
// time step. The action is executed at time 0 relative to
# z- b2 Z1 I" s# ~% M // the beginning of the loop.
7 {; G6 d* E: |& P
' W Y) V# c% S* K4 E& F // This is a simple schedule, with only one action that is+ t7 `) K+ L* i6 A; ]' k
// just repeated every time. See jmousetrap for more
( m" I0 h5 e; W; v! [! }- x7 T; H" _ // complicated schedules.
7 }7 `+ k9 }9 e8 D6 k 2 |) X( I. w! o' ?- X
modelSchedule = new ScheduleImpl (getZone (), 1);
) h" x4 ]& p, c- I, I modelSchedule.at$createAction (0, modelActions);# |; u9 H- ?: b: N; Z$ \
" c' a' g9 |8 m5 E" t
return this;1 i& K$ @ x! k4 j' _5 c. b
} |