HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 v, t, s9 q8 T. ~' M# Q+ b
$ {+ u: t- ^ H/ u, { public Object buildActions () {- g) u# y% N1 |7 k, ~- g
super.buildActions();& C9 F. A' W+ T
' a! d' H8 j* J( e, u O // Create the list of simulation actions. We put these in; F3 h3 L/ j# C7 @* X2 b1 S A4 V
// an action group, because we want these actions to be
0 k4 t0 y1 {$ |+ m1 \- n // executed in a specific order, but these steps should4 v* o) B/ i9 w7 h& @
// take no (simulated) time. The M(foo) means "The message
Y' _, x3 |5 ~/ }% M& N. x' P // called <foo>". You can send a message To a particular
x9 z, o" M$ R. f6 H1 W // object, or ForEach object in a collection.
- M8 P/ d/ v# u5 g 2 a8 R% d4 G$ h4 x# _# r% e5 w' V
// Note we update the heatspace in two phases: first run" u3 S% f0 E, |8 I+ m" v
// diffusion, then run "updateWorld" to actually enact the. [5 A4 B5 Y R2 x9 H' [: t; B. W
// changes the heatbugs have made. The ordering here is u4 r/ r" V1 o* `7 W
// significant!
' W5 b$ O: V- @' [$ p! M1 n " b% u5 x1 w% e4 C
// Note also, that with the additional
" @4 b8 \# ]5 x$ U9 J1 x // `randomizeHeatbugUpdateOrder' Boolean flag we can; u: q. t! e& Z
// randomize the order in which the bugs actually run/ n- a+ j. W/ E/ | N# \( B
// their step rule. This has the effect of removing any M- A, j _9 z, R# h$ {
// systematic bias in the iteration throught the heatbug
* ], h( G0 }( T. ^# {9 f // list from timestep to timestep) G9 @- c2 w5 L0 Z
: D2 Z/ L2 B$ U: s/ X' r' | // By default, all `createActionForEach' modelActions have: r7 X' r0 b* I' h
// a default order of `Sequential', which means that the
+ z4 i F5 u3 a6 B' Z, R // order of iteration through the `heatbugList' will be) ?: \5 o0 r4 R& s7 d4 G4 ^ P
// identical (assuming the list order is not changed
& _& M+ l% `! B4 |6 c( g3 ~7 b7 s // indirectly by some other process).+ Q0 |5 g0 e/ w. y9 R0 C+ @
# r, Q0 D; f+ t! V. p' q
modelActions = new ActionGroupImpl (getZone ());3 e, j @3 ]! o4 M6 y8 v/ B
) B3 C- Z. ^! o
try {
; n; m: \1 Z2 A: ?5 k V1 x modelActions.createActionTo$message
+ m6 X. Q6 n. P9 e+ {1 W6 A& ~ (heat, new Selector (heat.getClass (), "stepRule", false));: l. Y% {# V6 |3 y4 I5 z- ?
} catch (Exception e) {0 T4 {2 W# E1 W T5 X, V
System.err.println ("Exception stepRule: " + e.getMessage ());3 m1 @! Y" z4 k- V
}
' x* y0 h3 N7 q% u7 \
3 a% M) [; ^7 H( Y. i try {; ^9 W% S7 x4 q
Heatbug proto = (Heatbug) heatbugList.get (0);
b7 t3 u9 W% q0 e Selector sel = * N1 _' E8 V$ B7 g& e8 ? ^
new Selector (proto.getClass (), "heatbugStep", false);- {1 s. q1 L, H
actionForEach =7 X, g" c$ Z& J: r- Z$ G
modelActions.createFActionForEachHomogeneous$call
. \7 j5 D, e, L6 S6 v' y (heatbugList,& [2 f5 L, z4 |. r Q
new FCallImpl (this, proto, sel,# G5 U$ e( r+ x# T# j
new FArgumentsImpl (this, sel)));- K! r7 B1 ~- n6 r6 r, W
} catch (Exception e) {
% ?3 n- s9 k! C% K9 H0 R8 {7 A# n e.printStackTrace (System.err);
2 D5 F5 P3 E- s8 a }8 W( y2 m, l' f+ c& T j( c/ s: C1 J
5 I7 j* a' `" N) r* J" l; W* S- G2 H
syncUpdateOrder ();- w2 \* H1 d& [1 g7 T, ^
; V" r9 C: I4 @
try {
9 O! q& |4 f7 j9 @) ` m modelActions.createActionTo$message
* p! \' _9 _% j1 C( g (heat, new Selector (heat.getClass (), "updateLattice", false));5 h9 U! b3 A0 Q5 |( v
} catch (Exception e) {! X ^' ^ X' \) u* f
System.err.println("Exception updateLattice: " + e.getMessage ());
$ w5 Z0 n2 _9 j0 Z* J1 c }+ F6 ?% p: A( |$ G0 c; B% M5 q
: t/ }! j; d" q2 O- k
// Then we create a schedule that executes the
# Q% V$ H& B+ Z( y0 p // modelActions. modelActions is an ActionGroup, by itself it6 A, J! v* i" n _
// has no notion of time. In order to have it executed in
) \" M5 m( y# L q& m // time, we create a Schedule that says to use the
& W0 c8 R7 l$ C: P9 s) F' K // modelActions ActionGroup at particular times. This# L* j1 P$ g0 R d( K: O
// schedule has a repeat interval of 1, it will loop every
. U' s( t& ]$ p! H- S9 x // time step. The action is executed at time 0 relative to( U. Q& U% v& G- U$ _1 K/ ]( T0 X$ `
// the beginning of the loop.
/ P$ I- n# w/ N( g) i+ Y, O6 \) E* p& W% Q, Z# S- q
// This is a simple schedule, with only one action that is
. e/ K d# i0 W* z4 R- C4 y6 v9 M // just repeated every time. See jmousetrap for more
- t6 w f/ X8 K* {5 D // complicated schedules.
+ h4 `" r6 S. Y, ` ) I i! ^- S* W
modelSchedule = new ScheduleImpl (getZone (), 1);, X: y% M& R7 r: I& X; c) W
modelSchedule.at$createAction (0, modelActions);5 A1 K$ I' q1 S2 ~
: H' h! _- |! x/ R
return this;
4 [. U. |2 w5 Q/ u } |