HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
; W5 J7 T! F8 c$ `: F J+ v/ }3 s0 u
public Object buildActions () {
' t6 n3 f" G. D; R" _ super.buildActions();8 S( H2 Q! G- L. i0 Q- j
3 c8 l- \7 c7 M4 g/ T
// Create the list of simulation actions. We put these in
" Y, r- Y' S. j; I& P // an action group, because we want these actions to be! W5 F6 z2 m% s7 }, _" Z
// executed in a specific order, but these steps should- I2 W& s( {% s2 ` v' |
// take no (simulated) time. The M(foo) means "The message( R9 c8 _, Z# ^
// called <foo>". You can send a message To a particular
# F/ z1 W0 r& R* F) J // object, or ForEach object in a collection.
6 ?+ h# ]7 F0 W# R$ x- J 5 i& \7 ~4 v* |! e, o* g1 H; M5 W8 Q2 V3 D
// Note we update the heatspace in two phases: first run& M1 x* o6 F1 }! ?
// diffusion, then run "updateWorld" to actually enact the
. t o. H0 ~, X. o // changes the heatbugs have made. The ordering here is
8 f) ]/ T; A; \* G' d7 ]0 } // significant!: Y6 a8 V3 y) |- _$ m; h! j1 h
, L5 b, [, ` H, L% ?9 o1 m // Note also, that with the additional6 A6 P. V, A; z; }
// `randomizeHeatbugUpdateOrder' Boolean flag we can4 o/ }2 ?; m/ A! Y% y; N
// randomize the order in which the bugs actually run' `5 @/ B0 L+ g3 `- u
// their step rule. This has the effect of removing any
; l" g8 d4 N5 t // systematic bias in the iteration throught the heatbug
p( g% K& m1 B1 I // list from timestep to timestep
2 w/ L9 _) M$ s6 D
: i5 s7 X5 L7 V7 e // By default, all `createActionForEach' modelActions have5 b: S f+ `! r
// a default order of `Sequential', which means that the) Z" Q8 V' r( P s1 c$ L: ^
// order of iteration through the `heatbugList' will be: L( k6 T2 ?: E2 \, W5 z
// identical (assuming the list order is not changed& y% Z3 f4 d) c8 ~; u* p' @
// indirectly by some other process)." J& T1 p+ O# H1 t0 i
7 F+ i# e8 k+ `9 b4 `
modelActions = new ActionGroupImpl (getZone ());. }. C- `3 `5 U) m/ D
1 R* T! q- Z% \" T! P
try {
# a$ |# C4 R" ~2 e modelActions.createActionTo$message
. p! p; M% Z0 S2 ?0 m0 n* y# y (heat, new Selector (heat.getClass (), "stepRule", false));
- p& _7 P3 z7 J4 @ } catch (Exception e) {# N# ^$ q& W3 j6 n, S2 f5 j# b
System.err.println ("Exception stepRule: " + e.getMessage ());2 i( E- v* s& R1 O
} i6 ]' t7 [( d( L0 d
& G( X- {. ^) t3 A& A
try {3 B" `9 H' _9 e
Heatbug proto = (Heatbug) heatbugList.get (0);" T6 W$ s8 M3 S- v: y6 }- ^
Selector sel =
. {0 G- r$ c/ T0 _0 r% \, u new Selector (proto.getClass (), "heatbugStep", false);5 D- N& Z# _" {! i
actionForEach =' y2 g# E {8 z: @3 \2 K4 g
modelActions.createFActionForEachHomogeneous$call
+ B/ b) Q% C5 a2 k/ u (heatbugList,! a; L/ c( W; P8 E" {
new FCallImpl (this, proto, sel,6 j. o- N. z8 C3 V T6 H
new FArgumentsImpl (this, sel)));$ f7 N: Y4 F0 B
} catch (Exception e) {1 v. Z* q0 d3 ]- p
e.printStackTrace (System.err);
, w" y- M a7 J' q* H. U0 f }# ?7 {; ]1 \1 c5 ~" Q
* m. z$ V6 h) K- e6 r5 H- i syncUpdateOrder ();
/ w3 k. m1 N z8 S* x$ ]
' e1 }- t O$ m* E try {* g" F% x B1 W3 {9 Z
modelActions.createActionTo$message , o* ?! i6 x) u* e+ u5 j7 I
(heat, new Selector (heat.getClass (), "updateLattice", false));- ^ x& ^. z/ Z' y+ U- V) l. W9 ^
} catch (Exception e) {
' p" Q( P- U1 h0 E( B System.err.println("Exception updateLattice: " + e.getMessage ());
4 N3 {4 e1 h5 P# t$ Q$ p8 S }
2 e9 G* i0 M! D6 v, i - v$ T& V; \( g) ]
// Then we create a schedule that executes the
' i* }2 p I( E) M // modelActions. modelActions is an ActionGroup, by itself it7 {* V1 w( b8 u3 _; o/ z
// has no notion of time. In order to have it executed in
1 H" Z. h z o' R8 J1 X# o2 l // time, we create a Schedule that says to use the; l6 t" B- \' H
// modelActions ActionGroup at particular times. This# b' ?& A8 w& X. s0 a0 {
// schedule has a repeat interval of 1, it will loop every! }. }* V# g. v$ I7 ~ e& L% Z
// time step. The action is executed at time 0 relative to
9 m6 k) ~7 [; G- M- F // the beginning of the loop." q+ |$ g) @' |& U/ W
' l1 T8 k; p/ z% ?! H$ T6 i
// This is a simple schedule, with only one action that is
1 U: b3 ], k/ q6 E. C // just repeated every time. See jmousetrap for more
% v; m$ W4 q# X2 b' Z' E2 W: j // complicated schedules.! W, i( w0 w. Z: D- ?
4 i% S) }' x9 Y, K3 n3 `/ H8 t y modelSchedule = new ScheduleImpl (getZone (), 1);
+ L; M4 ]5 \: E/ V- h1 ]( C modelSchedule.at$createAction (0, modelActions);
- p5 }) ?, ~7 m % I, |4 ^: i: F/ D S. A' q
return this;
' {0 E9 T1 x l! V0 q } |