HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
S0 f* V4 U1 Q3 H: o: P- E/ S3 X- V8 |/ R1 s! y& O5 z
public Object buildActions () {
" s0 G( k0 f1 {$ @ super.buildActions();
. m ~1 w9 W- A
: o8 \! ~% {+ h, y // Create the list of simulation actions. We put these in3 F6 ~. h2 x+ e6 J
// an action group, because we want these actions to be7 E! N, |+ ]0 Z: s/ [* d3 u( @
// executed in a specific order, but these steps should
1 E: Z1 w) h5 {5 B6 q // take no (simulated) time. The M(foo) means "The message
4 B S. k9 o' \$ T // called <foo>". You can send a message To a particular# P# V$ [' S. \: |& T+ K
// object, or ForEach object in a collection.
) u! d3 P- [8 f- p" _% y
! \2 t- G1 W F( p1 D // Note we update the heatspace in two phases: first run3 G4 e; U m! e0 B* m( P
// diffusion, then run "updateWorld" to actually enact the U" t( g( C4 M5 J
// changes the heatbugs have made. The ordering here is
. V- z M# Z" }: P4 R4 \9 F // significant!
* }4 M6 o& ^ ^( ~# {. N0 K7 Z6 T " K2 T5 b- O1 l( n
// Note also, that with the additional5 j( Y( \7 R" _- V
// `randomizeHeatbugUpdateOrder' Boolean flag we can! v$ Q3 F. P# J& g0 i7 }
// randomize the order in which the bugs actually run! A) [$ l" H2 |, P
// their step rule. This has the effect of removing any5 o, ?4 K+ y/ i, E% u) s
// systematic bias in the iteration throught the heatbug2 L5 M( w4 \* }/ |. f8 L' h
// list from timestep to timestep
7 g! [3 ?9 O* V$ S3 {& a" v$ h
- E* F. ?3 N" t9 `) b: N1 s; p: z1 }+ | // By default, all `createActionForEach' modelActions have
' Y4 n# U8 J9 g0 ?9 n+ [7 y% C // a default order of `Sequential', which means that the! j" B, f% q( C: }, F5 g
// order of iteration through the `heatbugList' will be& g m2 P4 `- S3 }% g! ^
// identical (assuming the list order is not changed
) y) s- @2 a$ F; S& L( J6 @ // indirectly by some other process).) P# @7 ?0 w6 G# u
) k) f5 B" k" B G( o modelActions = new ActionGroupImpl (getZone ());/ v3 C: C! Z0 V0 u) D9 C
* Q8 k: y5 J- s' y; } try {
: Q$ A# K6 B) X3 Z modelActions.createActionTo$message
, K9 |; |: [% i- H (heat, new Selector (heat.getClass (), "stepRule", false));# _: ^. O+ _5 F: @# a
} catch (Exception e) {
7 P" W* z- t; a% b- C* K0 e System.err.println ("Exception stepRule: " + e.getMessage ());0 s5 h) C3 q0 a. c
}/ O% K* Z, M3 `" t- E, J0 r" l
4 P! Z! p! J9 i# G' _
try {- }) q: z4 n/ y; |& T5 U0 m
Heatbug proto = (Heatbug) heatbugList.get (0);
; H( V7 w1 h! _, S B Selector sel =
3 `' R7 e7 D. a- K+ X new Selector (proto.getClass (), "heatbugStep", false);- `: D n' H* j
actionForEach =6 w! s) ?) M" d2 O0 L7 w+ @
modelActions.createFActionForEachHomogeneous$call1 T, {/ Y0 n$ t( b; J9 C
(heatbugList,7 z" U$ E( |, l2 E s: ~, _ q
new FCallImpl (this, proto, sel,
% R2 r; w+ ~9 W6 W: N9 q, H* d9 l new FArgumentsImpl (this, sel)));; [4 X8 t2 { L7 l6 r/ N, E1 |* |
} catch (Exception e) {9 L+ s# A8 R0 z$ B- B7 G4 w7 N& Y
e.printStackTrace (System.err);9 V7 V2 [! O2 A* a6 u3 V( E
}5 Y# v1 R; e; Z( y
; N" B( T$ e9 V. I3 f; V
syncUpdateOrder ();
! }6 ?2 g3 F* x; n3 g
$ D8 i' q" ^% E! a( m Z try {
# t o9 y( I' ]! W U modelActions.createActionTo$message
& S. v7 O. X D J2 ~' Y8 T2 s (heat, new Selector (heat.getClass (), "updateLattice", false)); m8 i4 r t: q u
} catch (Exception e) {
, |4 d9 T, {; Q% l2 W! K System.err.println("Exception updateLattice: " + e.getMessage ());
. k9 f& b' ~! P5 S( G7 `( q9 o }$ b) M" `# }9 i4 q
" d! d2 R& _: V# r% y
// Then we create a schedule that executes the7 N. l* d- b9 R2 V
// modelActions. modelActions is an ActionGroup, by itself it" D( c. j! x) f& v& ~4 a$ `/ L
// has no notion of time. In order to have it executed in" N; T1 W7 m; \: f$ {
// time, we create a Schedule that says to use the- ~, A. \" w& ]) M' e! b* W$ s
// modelActions ActionGroup at particular times. This
! o* q' u$ Y+ C6 v$ h // schedule has a repeat interval of 1, it will loop every% y0 l# _1 J; c& }9 E( h4 h7 M
// time step. The action is executed at time 0 relative to5 h. c: [9 q- c4 {5 r# y2 Q
// the beginning of the loop.
3 ]6 n7 k7 Z7 k4 W
/ U0 X c+ H0 h/ p. J# J7 K // This is a simple schedule, with only one action that is+ T# H- Y/ ^& ^) f% e, E
// just repeated every time. See jmousetrap for more9 x1 d$ |( _- y, q* O0 M
// complicated schedules., @* {( ~9 t" |8 J; d7 {8 g
, ^2 I# ], f$ h+ \: L modelSchedule = new ScheduleImpl (getZone (), 1);
8 L* d2 f' s& N6 J. Y modelSchedule.at$createAction (0, modelActions);
) Z5 ]4 j* p9 u+ `6 U- ]+ d + w" {7 m t/ M& x) P0 {; Y9 k
return this;
! ^ g7 [3 |$ ^+ ~( u% H1 `/ R } |