HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
; u. H F9 b' {
3 Q" a' E; ?# G$ F0 o" o( K9 L public Object buildActions () {
$ R7 b9 ^1 D5 Y super.buildActions();( V' O! \6 h* r
" m- M( M5 a) Q I5 C8 J0 C // Create the list of simulation actions. We put these in/ B6 y# p7 @6 x* `9 q
// an action group, because we want these actions to be
3 a9 y p; u6 M3 z1 P' H1 I) B // executed in a specific order, but these steps should
7 k6 I- v; |4 Q+ m' r Y- ^0 V // take no (simulated) time. The M(foo) means "The message0 ]2 Q% V: |! n# D; Z
// called <foo>". You can send a message To a particular b6 ^& e; ~: e) S
// object, or ForEach object in a collection." }* y# }" z2 e! M/ A2 t
) U1 f/ Z5 [8 {% c6 P
// Note we update the heatspace in two phases: first run
, C3 s h( A$ L // diffusion, then run "updateWorld" to actually enact the, ~9 B' R+ R, B
// changes the heatbugs have made. The ordering here is
" ^) @* r) \+ G" C- Z% N8 b // significant!
: Q8 J! W& X, k: M+ q 9 H4 m& R" Y) K0 n
// Note also, that with the additional
; v5 d! O- m0 `# `% V" t) e // `randomizeHeatbugUpdateOrder' Boolean flag we can
$ t, J1 |, |, {4 Z7 h // randomize the order in which the bugs actually run) w, w# y; V7 x9 @6 L
// their step rule. This has the effect of removing any
" i: [: W$ n$ S8 N // systematic bias in the iteration throught the heatbug0 n/ ` |! b" v' P; r3 I# A; `
// list from timestep to timestep" S1 ^# R6 T8 K" C0 ?2 z- B/ y8 i
. a2 L S/ T; N // By default, all `createActionForEach' modelActions have u* o- _( G* ^" d7 L# z$ R$ _* i( x
// a default order of `Sequential', which means that the. G9 g$ T5 \+ M7 N
// order of iteration through the `heatbugList' will be
, e2 D7 [/ y/ ^5 u // identical (assuming the list order is not changed
" f& O$ V4 v# Y6 u' z // indirectly by some other process).9 a$ E/ ~" h9 W% k' j5 l
0 I: Y8 ^& s# D) q1 U' M# B modelActions = new ActionGroupImpl (getZone ());5 U, ~9 j3 w1 k2 a0 V
7 l" g" d' h |4 i r6 y" y$ O
try {
( q& Y; w; ^1 U4 K! @+ d) j modelActions.createActionTo$message$ T4 Q, ?6 \. G; M4 m& z
(heat, new Selector (heat.getClass (), "stepRule", false));
+ d8 P, S* I4 e8 D f; y } catch (Exception e) {
- q' q7 y) a/ U- O. Y( d- |1 t3 n System.err.println ("Exception stepRule: " + e.getMessage ());1 ~- K3 ^, r1 B
}# s0 ?" W: b/ m, }: }
, H+ T4 j7 J i# R try {7 I( E0 U8 Z- x- F' I
Heatbug proto = (Heatbug) heatbugList.get (0);
4 r3 g" o: |8 Z9 G Selector sel = * w5 F) j1 J! J4 c7 w: U
new Selector (proto.getClass (), "heatbugStep", false);
1 F4 D- Z- K l) ^& _ actionForEach =
& y0 ~5 {- P6 g8 U modelActions.createFActionForEachHomogeneous$call$ A" a p2 X1 [( o% K: V" N
(heatbugList,
D/ V" s1 | Y* @* n* d new FCallImpl (this, proto, sel,
9 u. ?1 r+ m% K2 k1 H new FArgumentsImpl (this, sel)));
9 G2 P) o# [/ l4 N } catch (Exception e) {
. b; J# B2 g' ?" G0 R e.printStackTrace (System.err);
4 P* K: n" c/ V. Z5 t9 e! k4 l }
2 x8 p% f7 w" d. Z3 |6 x ( N% w/ h) f) g5 a
syncUpdateOrder (); y u3 x/ x4 z" y. e( _; V5 }
5 Q+ s% B6 j0 Q8 S try {3 y7 W, A/ t9 i# a7 R; ~
modelActions.createActionTo$message
# Y; n9 L7 D) |/ ]" { (heat, new Selector (heat.getClass (), "updateLattice", false));( d) [, B7 D' [) {% s/ A$ @
} catch (Exception e) {
; d$ n0 A1 y. ]! q/ `( I System.err.println("Exception updateLattice: " + e.getMessage ());
9 Z; G+ I: N6 ?' ]. } }+ `/ r s( S" R8 k
" n: B3 |; b; `* X8 s( ^
// Then we create a schedule that executes the9 E5 }( B* h' G% y% Z5 T
// modelActions. modelActions is an ActionGroup, by itself it
* i% K E/ c& ]* ]$ r1 ]* t // has no notion of time. In order to have it executed in
[% M1 ^& a6 t7 }; M+ q7 o* F* K+ } // time, we create a Schedule that says to use the
9 v. p; x8 k( T. H W% h) W // modelActions ActionGroup at particular times. This
9 z6 V6 q3 i+ ?4 k3 |" x9 }, R3 X // schedule has a repeat interval of 1, it will loop every- o9 p( F9 c/ \3 U( M" K, [0 [4 H* ^
// time step. The action is executed at time 0 relative to
2 L* E+ B# {4 p q: U3 O5 [$ L) v" t // the beginning of the loop.! P6 f5 z0 G" N4 d; l- T$ w
$ T% W/ F% J. C% k ? B5 c9 O
// This is a simple schedule, with only one action that is* C* _! g# |- V5 k7 i
// just repeated every time. See jmousetrap for more7 L4 ]- k: E5 O* \, o; h w
// complicated schedules.
$ E, S- q' h8 L, o# w
8 L) p* x& y2 W4 H' n) S K modelSchedule = new ScheduleImpl (getZone (), 1); j* W' m: [2 @& Z
modelSchedule.at$createAction (0, modelActions);
! t! X7 ~, J S) Y6 ^ 4 x. g/ a' ^1 R
return this;
* A# `$ t! R1 o0 B- l; ^ } |