HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% u) J7 B o9 b( B1 ]' f% A2 _6 o8 P- ~* V1 V" y9 N% `
public Object buildActions () { j1 r0 U! V4 k' Z
super.buildActions();
0 }) I( e( ^2 b! G! z. Q ) D5 \. ~& Y0 ]; `; P6 |0 ]: }& y
// Create the list of simulation actions. We put these in) j9 z; H5 o& E
// an action group, because we want these actions to be
2 t) a) k! N1 Y# j" H" k/ q // executed in a specific order, but these steps should* `' X8 Q( Y! o, @
// take no (simulated) time. The M(foo) means "The message
; U1 g$ D% {+ B& I // called <foo>". You can send a message To a particular4 l5 b# p- U' ^6 c* a$ v! Q8 ?
// object, or ForEach object in a collection.( \+ J( U3 p, |7 R4 _8 {
) w8 k8 ]6 k/ d# n# Z/ P
// Note we update the heatspace in two phases: first run1 l* K' b6 S6 E5 x( }9 J" L5 L- e
// diffusion, then run "updateWorld" to actually enact the
7 ?/ `9 X0 K2 ?- ] // changes the heatbugs have made. The ordering here is& y4 \; |1 e0 n9 z4 k. ]) }( l
// significant!/ \/ Z3 j! o4 _5 h! ^; l; x
1 _# l# u' B9 y0 a: q2 r
// Note also, that with the additional% C6 U5 N% R; z1 u: d
// `randomizeHeatbugUpdateOrder' Boolean flag we can
1 Z5 c( i9 w8 _, C. P: m8 V* p // randomize the order in which the bugs actually run
' F5 D" r: _) E. v: V- Q // their step rule. This has the effect of removing any- r' [3 [% @7 \% y9 j
// systematic bias in the iteration throught the heatbug J+ Z: `: m) U* T
// list from timestep to timestep
H* z9 g* O( ]9 \4 N$ i
5 |2 l8 Z9 I8 s" s( u* s // By default, all `createActionForEach' modelActions have
( B) \2 j$ N) {2 w6 Y6 F // a default order of `Sequential', which means that the& X) B; y6 c4 _; f! K9 [' f; J1 O
// order of iteration through the `heatbugList' will be7 c; m; \3 G3 [* j
// identical (assuming the list order is not changed
) q- {2 A# Q1 C0 S( Q' ^ // indirectly by some other process).0 c/ Z9 A$ J% u. e- x8 i
' l! I9 `* z z$ H! h2 M# ` modelActions = new ActionGroupImpl (getZone ());
! |4 k9 u) B! `( I4 [* u: k3 A* a. |! r( A9 @0 [* ]# D7 u; y
try {
; a& r$ C5 [* d) t7 Q7 T modelActions.createActionTo$message% h2 H% T: a( i! R0 G
(heat, new Selector (heat.getClass (), "stepRule", false));
8 v: j8 b2 K) F9 { W } catch (Exception e) {
2 l8 f2 E$ C7 V7 U1 ^ System.err.println ("Exception stepRule: " + e.getMessage ());, H8 |9 A- v* x- _. \% Q9 |
}
% f8 o- t4 b1 R4 v3 W' u
^! ~2 U% X/ K; S. i0 l try {
2 i6 I V n/ x0 @: N" U5 ~5 J; w- ~, N Heatbug proto = (Heatbug) heatbugList.get (0);* k) f# M7 |, E
Selector sel =
, a3 s; P! S" w new Selector (proto.getClass (), "heatbugStep", false);
# F& H+ J( z/ k& p$ W$ w actionForEach =
8 |2 y1 l2 n- I8 W/ g6 n modelActions.createFActionForEachHomogeneous$call
0 L/ P6 V5 _# i, g: @ (heatbugList,
1 S$ W' Z, ^& ?- M# W new FCallImpl (this, proto, sel,9 [* r4 o' {4 e' p
new FArgumentsImpl (this, sel)));" J( ]4 L2 \) J' M* d) D) R
} catch (Exception e) {
" [ u* z6 t: g( y) p e.printStackTrace (System.err);2 P' M Q3 U; ]1 Y5 O% [+ B4 w
}
( R8 V! Z- P0 b6 w2 w2 X
6 l4 J/ ~9 M& x: W syncUpdateOrder ();& m! q( x5 f8 @3 i- c9 e1 {
* \, k! u9 ?" `; P7 W4 @6 S try {4 s$ T8 A- T. Z# K
modelActions.createActionTo$message 4 Y4 v% l. b6 s+ `+ F0 r0 ]3 Y, a, o
(heat, new Selector (heat.getClass (), "updateLattice", false));
6 U* i/ e2 t7 I: ~: ~) u8 [ } catch (Exception e) {
6 ^$ k' d/ M; c( Z. d System.err.println("Exception updateLattice: " + e.getMessage ());
! `3 d- C8 W, b5 |& ^9 ^" d }6 Z! O4 y6 v4 S' L' k5 `5 Y
8 A) X4 ? y, g8 f/ P* I# [+ N% ? // Then we create a schedule that executes the; Q% ^; V7 L* J4 Q v* A0 B
// modelActions. modelActions is an ActionGroup, by itself it7 T1 [' H0 G( u+ |& g8 y* c" N
// has no notion of time. In order to have it executed in
& M y) ~/ L# N9 k9 m/ J* } // time, we create a Schedule that says to use the
$ D9 t$ `" i$ a) B* X // modelActions ActionGroup at particular times. This$ t5 c" U; l0 t" Z, K
// schedule has a repeat interval of 1, it will loop every
2 P, M0 a* R: r \6 G3 e0 a // time step. The action is executed at time 0 relative to. D) D, E9 y7 m+ Z- r
// the beginning of the loop.
% _) b1 c8 G7 q) `2 ^+ A7 g
) J K% H+ q2 g* ~6 n& F2 ^' _5 ~ // This is a simple schedule, with only one action that is( R; C) l4 {& U' F
// just repeated every time. See jmousetrap for more
1 l- n4 h! R. Q& M2 u$ f // complicated schedules.
. d+ U" Q& W% R; @* U% a9 \2 l
1 C3 _9 \8 K$ m0 S; k5 ?* ^ modelSchedule = new ScheduleImpl (getZone (), 1);
( M' k# s3 W9 V, R modelSchedule.at$createAction (0, modelActions);6 V/ }/ N- R' m+ O& d; `/ E
/ D6 H2 p f* b) V1 R) V
return this;
1 K2 }) p, B( E } |