HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 v0 S/ H9 w) }1 W) T
; F* C( R# Y: Q# ` l) y
public Object buildActions () {8 }% r. N/ e% a6 ] v+ ?' v7 C
super.buildActions();2 W+ ?- X$ f3 Q- B
2 S. Z3 l+ ]2 \ J* W
// Create the list of simulation actions. We put these in) p d. l7 E; G2 e
// an action group, because we want these actions to be" M. h+ Z% `: J' Y
// executed in a specific order, but these steps should
) g; Q, p4 P; O5 Q% o3 M( {. C // take no (simulated) time. The M(foo) means "The message
9 n/ M+ G( C3 S& @ B, `/ B2 G) X- @ // called <foo>". You can send a message To a particular( B1 @1 T1 X3 V. Z
// object, or ForEach object in a collection.
! E3 z* d- b7 v% I4 F0 D& Y * g5 B' I' T, |7 x
// Note we update the heatspace in two phases: first run
2 C0 `' f! m$ i- O9 B // diffusion, then run "updateWorld" to actually enact the
3 w3 i" N0 w* J" L // changes the heatbugs have made. The ordering here is
/ T e8 G1 c) @7 W) _4 X0 G // significant! h' @/ T& S4 w2 ]: B. c
% y+ g, y/ z& \3 i // Note also, that with the additional
! z9 ~2 {5 @) } // `randomizeHeatbugUpdateOrder' Boolean flag we can( o! K- @& G: r# N6 |: v$ E
// randomize the order in which the bugs actually run
' F/ W H7 y1 ^& b3 M1 W) q // their step rule. This has the effect of removing any* @0 [: j; E7 U7 v
// systematic bias in the iteration throught the heatbug
Z! r) F) A& m& l% L // list from timestep to timestep
" p. ^5 K: \. ?# n+ B5 f/ ]$ p ! c0 l( o" ~* d& q
// By default, all `createActionForEach' modelActions have
+ q( V7 e8 M& j( s // a default order of `Sequential', which means that the: ^4 Q' I; K8 k H! m" ]% o4 Z
// order of iteration through the `heatbugList' will be
& A" [; R. g! n, R/ m+ F4 w( S6 ^ // identical (assuming the list order is not changed2 v9 U& ]- O% Q! h
// indirectly by some other process).
3 U2 c* U! \6 }: M; ^$ V ; m! P: l7 e( j' l/ i2 z
modelActions = new ActionGroupImpl (getZone ());
/ ~! {5 @ z# N* z' k2 X0 h+ z' X' z9 Z. u
try {
8 B4 {9 r& K9 a" c' w2 a9 X! H modelActions.createActionTo$message/ h* Z/ G0 B) u5 }
(heat, new Selector (heat.getClass (), "stepRule", false));- J9 E8 J- p, J' E1 Q. Q
} catch (Exception e) {7 t7 C6 s, x- {) A9 ?7 P# p
System.err.println ("Exception stepRule: " + e.getMessage ());8 b0 }8 A: o' o( i
}' Y# c3 T" Q2 l
' I2 n% b, e: x4 t, T* `+ l D try {( k; x0 \9 L) w& U7 O, O% J
Heatbug proto = (Heatbug) heatbugList.get (0);3 e1 B5 F1 o, u9 y& ?
Selector sel =
+ H; a7 j6 M3 v( c/ @1 ]! @4 u new Selector (proto.getClass (), "heatbugStep", false);. V* z; C; V8 D- T0 v8 z% N
actionForEach =
8 J) l8 w$ ~: O" k5 `: o% j modelActions.createFActionForEachHomogeneous$call8 ^6 t: Y# _. P- E
(heatbugList,# Y5 l4 F: S8 t, f6 i5 k, P) w( \$ S
new FCallImpl (this, proto, sel,& X/ d6 r- }- m0 Z3 V2 E4 A3 T6 H
new FArgumentsImpl (this, sel)));, S- \' D8 R& p9 ^9 r+ E$ y% Q6 z
} catch (Exception e) {
" b, O* _' [: T e.printStackTrace (System.err);
& @/ @* v) T) m' C) v }; g$ B/ e0 ?: ~# G+ V
/ \! Z3 s* s: q syncUpdateOrder ();
& f# E) b. S) n. L9 [& V
" x; g# f5 R9 v try {
4 M1 d$ e$ v6 e) ^5 m modelActions.createActionTo$message " Z0 S9 s4 e$ w/ z! B& D8 k7 B
(heat, new Selector (heat.getClass (), "updateLattice", false));0 ^* e1 W( n5 A, W( |* G; [) D
} catch (Exception e) {
7 t9 y% ~+ j2 D# e' \ System.err.println("Exception updateLattice: " + e.getMessage ());
, v+ ^9 b/ s Z# u1 v( f }
+ \! A0 p7 C/ j. g5 e" w 6 r+ g6 O' q' `3 i4 Z4 i7 U
// Then we create a schedule that executes the
1 }6 Y o2 H: D: p9 I z- k3 b0 z // modelActions. modelActions is an ActionGroup, by itself it
* R8 j$ j! x5 H' a& {' ]& V6 {4 g // has no notion of time. In order to have it executed in3 |( y7 u: r% G) t( @6 }4 V- W
// time, we create a Schedule that says to use the( Z# ?9 o) W% E# f
// modelActions ActionGroup at particular times. This
7 @' q7 z( F" f; A5 {2 c8 q" F // schedule has a repeat interval of 1, it will loop every
/ h, a* i; `9 r0 w // time step. The action is executed at time 0 relative to
# I- ?" M5 q3 l# f7 u/ N // the beginning of the loop.0 h- {: ? C( Z/ k4 x( W
. e0 {/ S. g6 O. H- {4 L: C // This is a simple schedule, with only one action that is
3 {+ m+ l; V; u# x% x8 o' G) d // just repeated every time. See jmousetrap for more
0 i; k3 ]6 Y) q // complicated schedules.# j& R' Y: l3 A$ y- r, p
* g+ E3 l ^% H9 X" n$ }& @% z modelSchedule = new ScheduleImpl (getZone (), 1);
' U8 ], u; Z% W' | modelSchedule.at$createAction (0, modelActions);
5 H2 C4 K7 g3 ^: B8 d
1 y4 _% D8 y9 w5 f1 Q. U; \ return this;- m! }$ d" |% Z* r- p: H# y% O& u
} |