HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; L$ v( h+ L. ^2 U9 p$ j, B
0 q& [% _" R/ W+ c+ I- F) J public Object buildActions () {
: _$ q* Y' e+ c+ g7 S* B; u super.buildActions();
/ n% o5 q7 @+ |1 R 1 j4 I7 k. ?" l* \9 c
// Create the list of simulation actions. We put these in# b5 I- S; p* @1 W, A }" {
// an action group, because we want these actions to be, {1 f3 J, n! J# H' ~9 T; g, c4 w
// executed in a specific order, but these steps should# M1 U! U c# V: k( H( b
// take no (simulated) time. The M(foo) means "The message$ E0 a6 I. k6 t6 ^! p. s& N
// called <foo>". You can send a message To a particular
. `# ]; \. R: L1 }2 l: D // object, or ForEach object in a collection." D3 ~3 p3 {$ R/ }0 W' l
& ~$ }2 [" p- P
// Note we update the heatspace in two phases: first run# ^0 j6 u1 i6 n0 r0 Q
// diffusion, then run "updateWorld" to actually enact the
0 F* M. d$ [- K) J/ s2 ~+ I: ^6 S // changes the heatbugs have made. The ordering here is; m0 n# S$ ~1 a, `$ V# d& A$ r/ u
// significant!: e, ]* L# A, L r" z( A; Y
; U$ y# P; u# s // Note also, that with the additional1 E4 J- J; u" _1 ?, ]" c# T! P
// `randomizeHeatbugUpdateOrder' Boolean flag we can& C) t$ n) ^# {% l5 [
// randomize the order in which the bugs actually run" Y- S7 I/ t5 y. w
// their step rule. This has the effect of removing any
0 |) s3 ]7 ^0 R: \6 X5 x // systematic bias in the iteration throught the heatbug
% s1 b6 j7 I. b! e- ^ // list from timestep to timestep
8 P- a1 r1 V: c; T$ B9 [/ U' |. p
2 M. S/ _! k; F // By default, all `createActionForEach' modelActions have
; n+ U8 G* l) ?7 r* K- _ // a default order of `Sequential', which means that the" R$ Y; q8 k0 A6 G
// order of iteration through the `heatbugList' will be
" \+ I' F0 z$ _, x7 t // identical (assuming the list order is not changed
- H9 _ h' @+ D // indirectly by some other process).5 r) _! v. F/ `( A) @ M3 O1 e T
; D+ s8 |0 x& ^( x# N modelActions = new ActionGroupImpl (getZone ());/ M) z. @$ E8 y4 H q( b
Y3 L, t3 S& U& k7 i; d" u6 a
try {1 Z/ z! O* H+ ^9 e
modelActions.createActionTo$message
8 {$ `4 X# d: {/ _6 U+ t. S (heat, new Selector (heat.getClass (), "stepRule", false));3 |7 g+ U, A7 S f: S/ S9 _
} catch (Exception e) {. c) j; I3 ~; d# M, q! T
System.err.println ("Exception stepRule: " + e.getMessage ());( l1 z: o S- m2 f* f5 ?. @
}0 k4 r+ _3 x; n, k: w+ g
a- T [1 {4 M3 A% e try {
$ r4 O i8 R; N( J( r: E Heatbug proto = (Heatbug) heatbugList.get (0);) e5 z2 m5 H) D' _1 i
Selector sel =
4 u/ f) s# p0 k/ x ^: a! ? new Selector (proto.getClass (), "heatbugStep", false);
' G4 U$ M4 z6 t* P* ~ actionForEach =) k. p {" t7 k7 J" S' _
modelActions.createFActionForEachHomogeneous$call
3 Q0 o8 ]7 Y o' F6 D* N (heatbugList,7 `/ V( q8 S/ J% A4 \1 H) g
new FCallImpl (this, proto, sel,: _8 H7 T% j# N6 v4 C
new FArgumentsImpl (this, sel)));
$ Y) e c z/ S" ^. ^ } catch (Exception e) {( C- a$ d& j0 P6 P1 T$ W$ E/ C
e.printStackTrace (System.err);! e) Z) N) o/ ]& h0 y4 f8 L
}4 U/ _: m; P" n5 {: p
* k7 |% ?! ?0 k$ N( s) l
syncUpdateOrder ();2 n+ x6 w+ U5 O. s9 [0 F+ e" g4 w
* Q' q7 e6 X" a try {
) M" m) n1 s' J; P* H4 p# M* B( |+ \" w modelActions.createActionTo$message
# A0 {% q! @- L( \. K1 h9 L$ ` (heat, new Selector (heat.getClass (), "updateLattice", false));
8 c" X( x" ?' |% d1 j! w" k3 R- x } catch (Exception e) {
% l( f" B4 Q5 c System.err.println("Exception updateLattice: " + e.getMessage ());
8 |1 |) \0 }- I" o }
0 S* q/ Z5 u5 j4 V; j
3 [1 x: y- ~1 D$ X6 N- c4 i // Then we create a schedule that executes the" k( \( b1 W' b
// modelActions. modelActions is an ActionGroup, by itself it
9 e0 V% B) ]* E: f* B9 x( H4 A // has no notion of time. In order to have it executed in
8 T( Y% T0 T7 l4 R // time, we create a Schedule that says to use the1 Z$ K( ^' J/ W. L
// modelActions ActionGroup at particular times. This5 {3 H- n1 i' z# F9 h
// schedule has a repeat interval of 1, it will loop every( m5 ~6 Y. O% m& A9 k
// time step. The action is executed at time 0 relative to
* b0 h, [0 G* C // the beginning of the loop.
7 B6 V# y6 V* ~% {! J( L$ g6 b/ n+ @: Y! J) S) a$ z' K
// This is a simple schedule, with only one action that is4 |* }, H7 t; Y+ n9 y0 r5 ^/ P
// just repeated every time. See jmousetrap for more
* E2 l8 c$ A1 l' p% i // complicated schedules.
: }. h; ~- I) b' p" t5 R- b; n0 [, m 4 ^* B" Z1 `# k: Q5 r( Z
modelSchedule = new ScheduleImpl (getZone (), 1);
3 }' Q+ K4 g- |. Z modelSchedule.at$createAction (0, modelActions);7 W8 X9 \1 ]5 k3 i# H
8 E: q9 O/ t0 E0 y return this;+ n$ G1 I7 Z7 ~
} |