HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ _0 d5 W% |% g1 v
( y N$ M9 W3 ? public Object buildActions () {
; J2 \) W: _+ i' _2 l super.buildActions();
7 v5 x" v2 Y5 {+ X& B2 u
5 d& v4 ^% `7 V% D* ]* G // Create the list of simulation actions. We put these in8 E( T$ u1 L T
// an action group, because we want these actions to be
+ E! @3 L* x. H' Z: s- |$ |5 @ // executed in a specific order, but these steps should9 e- {, ~: b6 s
// take no (simulated) time. The M(foo) means "The message
) D4 o0 @' O% l C1 t2 ? // called <foo>". You can send a message To a particular
# O4 q% A0 L" y ]8 G, I // object, or ForEach object in a collection.; E8 l. F1 P; g6 {, f
+ `2 |0 @ F8 f X // Note we update the heatspace in two phases: first run# W& ~, B' ]1 D" t/ Q W! C. B8 X' x
// diffusion, then run "updateWorld" to actually enact the
) z0 u( P' J0 P4 F1 L // changes the heatbugs have made. The ordering here is
4 v0 R* ]3 _" C- K( g3 h0 C; v" Q // significant!. v+ S; W7 C$ m2 X
) e/ i& B; \7 E: j8 _
// Note also, that with the additional& m! X0 s6 d7 m2 U2 v# W& }0 h1 M
// `randomizeHeatbugUpdateOrder' Boolean flag we can h" n: }" t8 K; p+ `! x
// randomize the order in which the bugs actually run
' |0 k" W, ~- U! W! I2 F# H // their step rule. This has the effect of removing any
9 \; b& v0 Q9 L3 I' i // systematic bias in the iteration throught the heatbug
/ Y: a! g7 }: l" S- }, t // list from timestep to timestep
/ ^+ S; q& q: H
n0 K) R% D A" {% l6 i! ^ // By default, all `createActionForEach' modelActions have
, s2 H, d5 ^6 H$ W // a default order of `Sequential', which means that the' f5 y. E/ o2 S4 o- ^. E! b
// order of iteration through the `heatbugList' will be; D/ J! _5 f3 |) G# E1 A- H" Y4 q
// identical (assuming the list order is not changed
2 ^$ o1 j$ t" w$ d- j; L // indirectly by some other process).3 M" v7 G. \ f, J! }
2 r' ~+ m5 `2 Q4 `( i V) U modelActions = new ActionGroupImpl (getZone ());- k% [& @# j% \& p$ {
9 j0 {1 B( ?9 @& [! j) f
try {3 z b) ?1 G3 D3 ?3 t# A6 b
modelActions.createActionTo$message
: @" |7 ~% L+ F/ y3 l& H- ?3 H' _ (heat, new Selector (heat.getClass (), "stepRule", false));
* B8 x9 x5 Q! Q' h# r2 e } catch (Exception e) {
- e; ^$ I0 Z+ [; h* K System.err.println ("Exception stepRule: " + e.getMessage ());
8 {! m7 I4 i- Y ?0 I5 [: b3 u, v; w5 ~ }
. @- P8 x- g# r0 A8 R( v
: K/ ^ k' \- q$ Z8 z try {
. C2 i& u* [* Q# i Heatbug proto = (Heatbug) heatbugList.get (0);- R5 s2 l) u5 p, B
Selector sel = * q7 K0 f' Q& W3 D2 `
new Selector (proto.getClass (), "heatbugStep", false);3 n' f8 r- [1 ?9 p. O7 O4 j& q
actionForEach =
) v. d Q) q' V' h4 V. t1 s modelActions.createFActionForEachHomogeneous$call9 w: e6 ^' |* s; R+ v
(heatbugList,
9 K6 b' q$ G% e# s5 g& G new FCallImpl (this, proto, sel,+ N* ^6 i2 U+ ^* o( Y
new FArgumentsImpl (this, sel)));$ O0 A$ ^& P8 W7 E+ g/ h$ m
} catch (Exception e) {
* G" N. J. B/ Z e.printStackTrace (System.err);
, E9 S' s6 h; Y# i. ?/ T& ?8 V }
3 T0 `9 b3 D6 |, c' I" ?/ z& g
1 {+ F9 f4 a0 Q- l syncUpdateOrder ();. `! ?. H+ t8 h. M
) ]! f T+ h! @0 R try {
/ ~3 D* d. M* G& i9 J, N modelActions.createActionTo$message
) Q( B7 E: J Z) P# [! k9 ` (heat, new Selector (heat.getClass (), "updateLattice", false));
0 J0 N* p" s! u# \1 W2 U+ s0 L } catch (Exception e) {
) _, m$ \* e& q4 M3 d0 a System.err.println("Exception updateLattice: " + e.getMessage ());4 Z8 a6 S$ V D, [( b% d' x
}$ r$ P. H( @6 n
2 [) Q. A- ~1 h: g2 n% A
// Then we create a schedule that executes the- M2 v+ M8 B! @/ N+ M7 t3 g# G
// modelActions. modelActions is an ActionGroup, by itself it6 H1 |) O( Z* O% i7 K( [: R
// has no notion of time. In order to have it executed in
* j: A* L+ T' L9 Y2 [8 w // time, we create a Schedule that says to use the, ]- ?- B. S, l0 h% Y+ u
// modelActions ActionGroup at particular times. This
8 n2 }3 @& h# o& \0 y- C9 v! T // schedule has a repeat interval of 1, it will loop every
% |2 X0 Z# V5 T, z( N- _2 p Q // time step. The action is executed at time 0 relative to
8 k! h% N3 | M& J* J1 [ // the beginning of the loop.+ k7 @1 K1 ^& H8 m5 y+ f- e
+ T2 b6 o5 I5 ? // This is a simple schedule, with only one action that is; w7 k0 P( h$ V p- }+ e; S
// just repeated every time. See jmousetrap for more ]& i/ V. X$ p2 [
// complicated schedules.
7 z% V* Q: m. I) H$ E' A5 ~2 S . S2 O( N$ l( v* Q
modelSchedule = new ScheduleImpl (getZone (), 1);. y" p0 N8 W& b; |8 N5 _" Q
modelSchedule.at$createAction (0, modelActions);1 g3 R+ ` x" S( K Q
+ b4 Y M5 E3 c# V) V2 n return this;& R h* H4 \) r7 H
} |