HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. R4 a! A3 z1 l- P( A& c) X
( @0 S* d' X+ f; a! { public Object buildActions () {( \/ D6 }+ A. b: R
super.buildActions();
$ y. A w5 f3 S0 A9 E1 S& l* k
8 q: Y9 o# D2 H) q' X5 v2 A // Create the list of simulation actions. We put these in: W, W0 j- h+ |4 L: ]
// an action group, because we want these actions to be7 r" w5 V/ U* |* q7 _, b# Q
// executed in a specific order, but these steps should
" i. _7 D! }# J* e) E- o: j: b // take no (simulated) time. The M(foo) means "The message
$ Z0 T2 z: a4 p* B) u, s // called <foo>". You can send a message To a particular0 v" M* a6 p) k3 k7 K* F
// object, or ForEach object in a collection.
+ m3 ~1 J0 p* A( R
; z9 s9 e# s. r6 Y3 ?1 G# d/ m& J // Note we update the heatspace in two phases: first run1 ?2 s9 q; D( X
// diffusion, then run "updateWorld" to actually enact the, p, Y c X, _+ T/ }0 G
// changes the heatbugs have made. The ordering here is
' d" [; W% E8 I- {9 k0 E$ x // significant!. C9 v- ~# I4 n+ O
) n7 m. f: U4 Z
// Note also, that with the additional- h3 u4 k# N/ @, e
// `randomizeHeatbugUpdateOrder' Boolean flag we can: a; X1 J+ h# l% L
// randomize the order in which the bugs actually run
# D f% D/ K6 ~) \5 \( K# }0 I+ f/ ^ // their step rule. This has the effect of removing any
g6 K) q" A, Y) v // systematic bias in the iteration throught the heatbug5 v7 C1 B; l& \5 Q" y
// list from timestep to timestep: [# n# P2 l- R; ?
; Y3 ?' r0 c! c+ {; Y* m
// By default, all `createActionForEach' modelActions have# k! w% H- ?* f' s$ ?
// a default order of `Sequential', which means that the: K! R& I# c7 L: N. W" _
// order of iteration through the `heatbugList' will be: u" V/ N; h; c# }$ Q- T7 H
// identical (assuming the list order is not changed3 c) K& V5 {4 S! B% Q- T2 H8 m
// indirectly by some other process).& z2 v$ h) W t3 [
; I$ U& h$ n I
modelActions = new ActionGroupImpl (getZone ());( y8 i- }& R8 M
+ t! s5 I( N3 d _1 d9 E A try {
( {: z* e' Y0 Q3 i modelActions.createActionTo$message
8 d7 t+ I7 R6 p (heat, new Selector (heat.getClass (), "stepRule", false));$ R+ B2 e i! z
} catch (Exception e) {: @4 k: _6 J+ f2 |
System.err.println ("Exception stepRule: " + e.getMessage ());
0 C- S# n8 k1 ?2 Q4 d) f5 L) \0 k/ ~3 S }
7 N/ J1 G. p0 ^6 l: @( A2 R: H2 ^$ [* h
try {
* K2 P X7 I) K& M" M Heatbug proto = (Heatbug) heatbugList.get (0);
; H4 V* c7 s/ p& R: [; `1 ] Selector sel = + d% f* @3 w( e# W9 ~7 E" ?2 P
new Selector (proto.getClass (), "heatbugStep", false);
3 ?/ N) n% l6 y6 S7 @ L actionForEach =
" m) I# i9 c6 i4 J$ W modelActions.createFActionForEachHomogeneous$call
0 K3 { {. w$ \; S5 r! W6 G (heatbugList,
1 i( s0 \; O Z* t. B( E3 ^+ s! [. r new FCallImpl (this, proto, sel,' T5 b0 S( S% N" x( q5 p
new FArgumentsImpl (this, sel)));
. Y, w! v. i, s+ I, e- c, e' p } catch (Exception e) {
h6 l; s7 g% s N! `% U e.printStackTrace (System.err);: {- i, Q% V, g! @
}+ r& y' C( _. \( M% q
2 T1 Z$ F r L! y& Z) _
syncUpdateOrder ();
7 e, E' Z: P; A: C8 {- F/ K$ |: W/ l! u+ w8 q5 Z0 q _
try {
# X( I5 E) g a' [ modelActions.createActionTo$message & Z: m2 Z, E" D9 l" V
(heat, new Selector (heat.getClass (), "updateLattice", false));6 G6 l! z% J$ p4 _3 q5 ~9 H5 _6 D; ?
} catch (Exception e) {, c/ L* n- L* t, f& H: W
System.err.println("Exception updateLattice: " + e.getMessage ());0 p u& Z/ u3 R
}" _! [9 P2 v( f2 Y, W/ G/ R$ I: F
$ B, K3 Z# v* g1 K* Q J$ x
// Then we create a schedule that executes the
$ B6 z& u6 x- l/ W- h9 Q4 G // modelActions. modelActions is an ActionGroup, by itself it
1 `/ E/ g6 ~5 |) A6 o5 `* W1 @% U // has no notion of time. In order to have it executed in: V! j* `% s, z7 L3 z) V
// time, we create a Schedule that says to use the7 Z' U3 N7 B" N: S
// modelActions ActionGroup at particular times. This' {$ t4 Z( U$ y7 ]7 F" q
// schedule has a repeat interval of 1, it will loop every. K' h$ U- \2 e# m; g! v& C& B
// time step. The action is executed at time 0 relative to
6 {% y) r( z- M // the beginning of the loop.
% }" O$ Z1 C5 C. ~& ]7 f8 J4 L% D6 p) O! Z( h
// This is a simple schedule, with only one action that is8 O* U3 ?0 Q6 ]" z
// just repeated every time. See jmousetrap for more
( z2 Z9 L4 A) i/ D1 ~ // complicated schedules.4 O# Y4 J, g0 `) G2 p) l
# F8 n9 ^/ C" l8 F& M( w modelSchedule = new ScheduleImpl (getZone (), 1);
+ u& v# H4 H* ?6 _ modelSchedule.at$createAction (0, modelActions);
/ i t* l: G1 R( M9 F2 S
# X( I3 A6 `- A$ p6 N return this;
/ ^5 X9 T4 D- y& w% d; F) t } |