HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# H, I# _ Q: }9 q& c+ q+ P
6 k" o' u2 c, o5 W public Object buildActions () {
7 A* k2 [: U0 o d. x super.buildActions();! ?1 g! Z. q! [
( K; [( ]+ T2 w // Create the list of simulation actions. We put these in: ]$ L" G7 b7 ~, g% c
// an action group, because we want these actions to be
9 M' p7 m8 Z3 t! v: z6 | // executed in a specific order, but these steps should1 u/ V" L5 H* y" {
// take no (simulated) time. The M(foo) means "The message. h: ^1 h& ~! E% d: k) b
// called <foo>". You can send a message To a particular
8 g9 ?0 {7 \: c // object, or ForEach object in a collection.
7 w0 c; M/ \3 _0 U& F 3 Y$ M% f. y' m& W6 O8 ~0 q. @" C: |
// Note we update the heatspace in two phases: first run
9 T# A* J9 C+ W; r+ {: [. j // diffusion, then run "updateWorld" to actually enact the* R& h+ l9 Y, w0 Q( L) b1 q* J) q
// changes the heatbugs have made. The ordering here is# u0 M2 c) ^3 {
// significant!
$ @4 I7 L5 X0 j% j ) }5 C W) _# H; v
// Note also, that with the additional" Q8 r- e- z* Q9 t
// `randomizeHeatbugUpdateOrder' Boolean flag we can
+ ~8 G8 q9 t) u' ? // randomize the order in which the bugs actually run/ I1 _( F) W1 ]; Q6 T- T
// their step rule. This has the effect of removing any
# F* [2 T/ i e/ o8 X( E // systematic bias in the iteration throught the heatbug
0 I% o& |4 S* K) I! K // list from timestep to timestep. ]# I) o" h7 t4 M, ^5 K5 Z% V
% `0 a% V5 b- F2 R! x) Z; l
// By default, all `createActionForEach' modelActions have
0 S4 E: e, k9 `. @( B+ T // a default order of `Sequential', which means that the) F; D: E# P4 z% A
// order of iteration through the `heatbugList' will be
" D8 K0 _1 E7 ]" W$ a" R // identical (assuming the list order is not changed
. x9 Y6 O6 }9 l# w // indirectly by some other process).' x- ]7 C& @2 a# i
/ L' p! P. D3 g! n% q# f modelActions = new ActionGroupImpl (getZone ());% c' N7 d7 N- F- B( r
% O" k' X, ^# ~2 T* F- |
try {
U8 [: E# E! v modelActions.createActionTo$message
" _' k7 E- M) I" C! c' S- z9 G2 N (heat, new Selector (heat.getClass (), "stepRule", false));' l( X) ]1 A" g' w5 {4 Y% V# A" Z$ S
} catch (Exception e) {
( W$ e5 b8 O* v- w5 s( y System.err.println ("Exception stepRule: " + e.getMessage ());
C' R+ W2 e. I& u: ~4 v7 z }1 e! t8 i x) J- \, G
% U& s# d: B. P) J5 } try {
( v; k0 f* e& _$ {, J( ^7 D. ^! H Heatbug proto = (Heatbug) heatbugList.get (0);- a) @" k4 h; p& u% N' K
Selector sel =
! c( | M4 M% M, E) Y N# x new Selector (proto.getClass (), "heatbugStep", false);
4 Y7 `8 g. l% \6 s7 s actionForEach =/ l8 n y0 D8 M; y/ B' K
modelActions.createFActionForEachHomogeneous$call3 W( D6 e0 i/ X
(heatbugList,' t) N8 q# V! X, W8 J6 u
new FCallImpl (this, proto, sel,
8 d6 l6 m' u! Y0 E Z: k4 K( I8 e new FArgumentsImpl (this, sel)));. U& v; M$ d4 K5 w ~' n5 Y
} catch (Exception e) {
6 k/ t+ X# F9 f' B; O3 c+ K e.printStackTrace (System.err);
. z- l9 W6 y. o! U1 }# D3 a3 b }; d6 `" c+ s4 `( C# s3 b
O- I1 Y+ k) K0 s
syncUpdateOrder ();5 A( C0 D7 F5 a/ @! F: Y
8 a. t7 y% e- w: B
try {
' x7 F2 b) E4 \8 S6 R modelActions.createActionTo$message
5 W. q7 P2 O2 c; w (heat, new Selector (heat.getClass (), "updateLattice", false));
' A3 r+ V4 v: ^: X7 `# a; a- r } catch (Exception e) {
1 h5 [: Z! c' a! {0 E, r System.err.println("Exception updateLattice: " + e.getMessage ());
( [( d/ S4 j) S, F& w }( U- ^% X, P- p1 d% A, J
, z$ y" L5 \6 T7 Q // Then we create a schedule that executes the
- d, u, w9 b- _, ]4 N- ? // modelActions. modelActions is an ActionGroup, by itself it
3 x. j5 ?+ v4 z" x8 ]- D% Z# u // has no notion of time. In order to have it executed in$ c8 t0 B; n3 j. E& E( {2 M: V1 `1 q
// time, we create a Schedule that says to use the, ^( _. l- E) @+ M! X
// modelActions ActionGroup at particular times. This3 H3 z# A5 ]: G+ n
// schedule has a repeat interval of 1, it will loop every% K) {0 e' e6 y& f* s% T) E
// time step. The action is executed at time 0 relative to$ n1 W& H: ~6 ?. q. t: \. r$ o
// the beginning of the loop.0 A" {* z$ `; H2 B' r8 O1 Z9 o
# T+ j' b# F w$ p' ?2 q% C // This is a simple schedule, with only one action that is
" i: F5 y# U; g // just repeated every time. See jmousetrap for more
6 N2 @* [7 [- i4 V; T8 a9 F // complicated schedules.0 S D2 C8 E1 `( b- @5 A
/ R1 m Z" ?$ k& P& l& h
modelSchedule = new ScheduleImpl (getZone (), 1);
@8 ~2 _1 k2 z* c# U modelSchedule.at$createAction (0, modelActions);
5 i# y1 }9 q8 Z, C - n+ O; s; s9 d' P+ p, ^
return this;6 y4 P0 s; g5 D
} |