HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:: w% T% h/ z b5 `3 C
* l m; r* T8 Y0 O, X# F
public Object buildActions () {
0 t: b- u4 B7 D( E0 ?+ Y6 s super.buildActions();/ S* p f4 Y* H5 O* \
( o- d1 H \2 D2 P! n* Z: @) w" N
// Create the list of simulation actions. We put these in6 F3 e5 A* M8 [1 N3 E% p( `9 |
// an action group, because we want these actions to be5 s1 X9 \' F" R4 H0 {, s* G" D
// executed in a specific order, but these steps should; F" i" K* ^' D' d1 x' r8 j
// take no (simulated) time. The M(foo) means "The message! f! o9 J( h9 L/ r% F
// called <foo>". You can send a message To a particular0 }% _: W0 Q Q- [% A
// object, or ForEach object in a collection.
' B/ p" P1 a# M, L8 y
6 c( N) T1 H3 K8 T // Note we update the heatspace in two phases: first run
/ \; J1 F: ~% W, y5 x3 m6 | // diffusion, then run "updateWorld" to actually enact the
& L- N, g$ p0 _: _ // changes the heatbugs have made. The ordering here is
1 u6 ]" b0 Q3 r( G* ]4 V$ w // significant!) t- F" L$ b6 Y4 d
F- I# e/ z: Q: m2 ~: j/ p // Note also, that with the additional7 |; X7 F4 J' @9 ^
// `randomizeHeatbugUpdateOrder' Boolean flag we can8 T# z C+ R. Q! u3 C, _
// randomize the order in which the bugs actually run
$ y) E3 E" Q- a5 K // their step rule. This has the effect of removing any0 b3 i" w7 W& ]
// systematic bias in the iteration throught the heatbug4 d7 K) q t0 ]
// list from timestep to timestep
. A7 A* q6 {4 L. k7 _- c! V 0 Q) j9 ]- H( d! V6 e1 i- m* n1 O
// By default, all `createActionForEach' modelActions have
7 l7 t1 S i$ v) A c( C // a default order of `Sequential', which means that the
~5 }& x- @7 Y! x% V7 {5 g // order of iteration through the `heatbugList' will be! F6 f- G, |# E7 h
// identical (assuming the list order is not changed
# y3 \( H% i3 s l0 a // indirectly by some other process).
7 _) M! D& S+ M8 X# w
/ t/ k2 q q- ?. x3 B modelActions = new ActionGroupImpl (getZone ());
/ n" b* a6 T; B! h0 h! R3 V: N% w/ m9 h' T
try {+ W! N& G. J& P1 x+ L
modelActions.createActionTo$message9 \: K/ C; L0 ^
(heat, new Selector (heat.getClass (), "stepRule", false));
- o8 v6 D$ K( ?5 S2 _6 D4 H- B, x } catch (Exception e) {
6 ^: u' ~# y4 N System.err.println ("Exception stepRule: " + e.getMessage ());' B: r% M, m3 F# \ N
}
/ o5 S" J( d- q; H6 }) v% V9 Q0 }" m, ~% J" B8 `2 f
try {
! T$ Q8 M' ?$ R! r Heatbug proto = (Heatbug) heatbugList.get (0);
& J5 Z) y8 I- @! D5 \7 J Selector sel =
1 k7 o, U# x Q+ [ new Selector (proto.getClass (), "heatbugStep", false);* R! P" d: u7 u( K- R, X
actionForEach = i4 Z1 }/ Z3 F) G! T- d
modelActions.createFActionForEachHomogeneous$call
4 y5 C+ [% `" w- I( a0 R (heatbugList,
$ N H2 p; ?7 b8 r; h3 I) c) c. s new FCallImpl (this, proto, sel,
) z, A Z( F6 V# g. ~% V( W9 K new FArgumentsImpl (this, sel)));
* h, b/ R2 a0 T' n( I& N0 C } catch (Exception e) {
6 @3 G3 E# j8 k: J7 r e.printStackTrace (System.err);* b+ f9 A; n# r9 S- \# X( Y3 G
}& D, S. V: p' P
8 ]8 s7 `: [4 v) M; R syncUpdateOrder ();: D m ~! _6 c" Z1 \
6 N0 d) P. f' I; h7 K; v9 D. Y- A
try {2 w4 W1 R1 c6 L1 S2 x4 J( d
modelActions.createActionTo$message 2 B; L, |! y' |6 p# b6 z1 q, @
(heat, new Selector (heat.getClass (), "updateLattice", false));
1 Q# Z, O a3 b1 b, ] } catch (Exception e) {2 l- g$ m2 r8 [6 U9 Z+ P- {
System.err.println("Exception updateLattice: " + e.getMessage ());4 }, ^- x, @; T. t- b
}
j9 `2 C1 o0 I ; d& T2 S D W
// Then we create a schedule that executes the
. N' F4 _ K: D6 K // modelActions. modelActions is an ActionGroup, by itself it
5 r" V4 H- P! L& T // has no notion of time. In order to have it executed in
q+ A8 P* |. u: F7 f: u // time, we create a Schedule that says to use the0 X$ n6 J" Z, m% [
// modelActions ActionGroup at particular times. This1 v7 ]. U: U( [! n
// schedule has a repeat interval of 1, it will loop every5 g- p% x+ y5 S8 Q
// time step. The action is executed at time 0 relative to
; F# H0 l+ n- N8 q/ ^: o! y // the beginning of the loop.
$ T" w/ O/ ]; ^8 o
5 C: m! N0 P8 a* P // This is a simple schedule, with only one action that is! @" l* `* d, q q+ x
// just repeated every time. See jmousetrap for more
' L* ]7 l3 L+ f) P // complicated schedules.
9 |& U& J2 [* k$ g) w( n
+ M: P7 p, V- ]: v7 f modelSchedule = new ScheduleImpl (getZone (), 1);, z& v X% i) {6 L) m* k8 l) [
modelSchedule.at$createAction (0, modelActions);
7 ], u8 Q7 S/ Q3 V & T, f+ l5 C/ x1 m! o# o' L( ^2 y2 b
return this;/ F! K- `1 }* Q% R+ ^
} |