HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% k. t' a4 ^( W, \
# s5 A: X7 o# l; V, `0 u public Object buildActions () { T0 F3 h X; x& L: N$ h
super.buildActions();
3 S3 B/ a+ h! ?+ c/ S( X$ o 4 l) F4 l7 ~4 I8 ]& J' K; K- F+ U% Y
// Create the list of simulation actions. We put these in7 a( y2 j7 y# K f2 s
// an action group, because we want these actions to be3 z2 K1 M3 k) L1 ]6 w
// executed in a specific order, but these steps should
. |5 P& U. n) f1 ?8 p! y // take no (simulated) time. The M(foo) means "The message% O) _3 p3 k1 J$ n& ^7 K/ k7 {! W/ J
// called <foo>". You can send a message To a particular
& r' q: l! e, T/ Z6 s8 R; D: f# c+ c // object, or ForEach object in a collection.
, o% E7 |( h* j1 n: |& u \
4 T5 w) t, S+ Z2 G; D // Note we update the heatspace in two phases: first run
# s( @0 V% ^( Y* l7 V2 q/ l // diffusion, then run "updateWorld" to actually enact the4 ` F% \2 z* l( z8 \
// changes the heatbugs have made. The ordering here is2 Q: E0 a# A+ ?. E* g+ U
// significant!) E! e+ W! l& ?' R0 s0 _
5 b6 C" U% ]0 ]8 T# o0 N
// Note also, that with the additional/ C7 [8 f6 O! Z. d- ~
// `randomizeHeatbugUpdateOrder' Boolean flag we can; E# m" r- q( y$ O9 _5 ^& k7 {+ l
// randomize the order in which the bugs actually run, I8 t0 k, d9 Z5 c" W! I9 `9 n
// their step rule. This has the effect of removing any; p+ I" T: ]# j8 v: I ^" ^
// systematic bias in the iteration throught the heatbug
; L. V6 N- D/ X) S: q9 |$ t6 G" y // list from timestep to timestep! Y6 v! z9 l! Z% d8 t
0 _8 Y2 z/ G1 S6 I3 n- V5 J3 ^ // By default, all `createActionForEach' modelActions have* u, R) y3 I: f) C8 V2 Y
// a default order of `Sequential', which means that the
1 v# d1 j, N9 B6 g; |/ M5 }1 `8 A! X // order of iteration through the `heatbugList' will be
2 e4 g3 I" ?, f7 m // identical (assuming the list order is not changed
, l5 ?" P0 g$ i% o8 Y" H // indirectly by some other process).
4 U M0 i2 g- \, O. B3 O! o 5 p, v/ y+ W% h
modelActions = new ActionGroupImpl (getZone ());
. t. J( C' v# [) H; j8 T
, b* Z$ m8 q6 m: c# j; { U* K try {2 a# b- B5 n% @6 @
modelActions.createActionTo$message
+ }4 ~$ D4 u7 E, S$ E$ @' M (heat, new Selector (heat.getClass (), "stepRule", false));
/ Y7 I5 h4 ^ {0 y } catch (Exception e) {
' a7 e" c3 |) |" e9 {) } System.err.println ("Exception stepRule: " + e.getMessage ());7 b6 X6 X: j4 [. @* G
}$ Y0 @. R+ Q& S: c
" V' M0 Q& X4 G# w2 }7 I
try {& V3 _& w) w& Z# \
Heatbug proto = (Heatbug) heatbugList.get (0);
) E* n1 A5 y1 U6 a Selector sel = # w- U# ~) L; L9 L7 D
new Selector (proto.getClass (), "heatbugStep", false);
1 |( Y C. w1 D' z# x) L# a actionForEach =
: f$ a3 A9 ]* {# O1 |6 L modelActions.createFActionForEachHomogeneous$call
6 l8 ?& G* u7 g! x0 J (heatbugList,
1 B2 D* b. ? j* d$ |9 y new FCallImpl (this, proto, sel,3 a$ c. Y0 d- Z
new FArgumentsImpl (this, sel)));
( u S) a a6 G; u3 s } catch (Exception e) {
: D( W! B8 Q- l3 Q+ O& {# {! Q- [ e.printStackTrace (System.err);, k3 x& a' h6 g" [9 y1 o
}" {" {, B. h( d" e9 j$ u
' y6 O6 Y$ W: \
syncUpdateOrder ();6 v, T& M ^- a9 B9 |
& Y/ P! Y- v% `
try {/ }+ {: q$ X1 g; E) E
modelActions.createActionTo$message
' K& ?, l3 Q* o6 a, U4 s0 s (heat, new Selector (heat.getClass (), "updateLattice", false));
& R" y+ F8 g8 Y6 C4 B0 J } catch (Exception e) {
) ^& q" e0 f$ ~- O System.err.println("Exception updateLattice: " + e.getMessage ());# Z3 [6 G: Q: e ~/ M
}
9 Y) l( A( t0 g$ u $ ~: u& h; H% h9 D
// Then we create a schedule that executes the$ x1 }2 o3 ~! X$ j+ Q5 s; g
// modelActions. modelActions is an ActionGroup, by itself it
& J5 }( o7 _& g1 r$ v // has no notion of time. In order to have it executed in
$ E5 I( Z: v, R/ I5 G // time, we create a Schedule that says to use the
- m X% ^6 R% V: X // modelActions ActionGroup at particular times. This
; `- k: N& K+ L // schedule has a repeat interval of 1, it will loop every$ i' b x2 m0 T7 C @
// time step. The action is executed at time 0 relative to! p% S, x' i# T7 K" Z/ R
// the beginning of the loop.
4 l( a4 X2 i* f3 |& Y4 A* H4 O" _7 h8 g, d
// This is a simple schedule, with only one action that is
9 t6 ?9 \- P5 j7 }- D // just repeated every time. See jmousetrap for more- I ?) h' L. |6 o" C
// complicated schedules.
. m" C: N, H' y; c+ j + D: H+ }- }7 w
modelSchedule = new ScheduleImpl (getZone (), 1);& {8 ~) y. h8 c9 Y; Q
modelSchedule.at$createAction (0, modelActions);1 v" ~' [9 \& h. D, H
7 P0 Z! Y g8 i0 u
return this; r5 u. c; R7 |9 Q) {1 s! n" A- \
} |