HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. a3 R G$ E; u% R/ ?" C
# P# W% ?9 S& ~3 Q1 m public Object buildActions () {2 E8 k* a- T7 c( {
super.buildActions();
|9 p; Q* t5 K _( W! q7 [% d0 e5 t $ I7 W9 w5 B7 c. G- w& n$ r+ a
// Create the list of simulation actions. We put these in
/ N' g' ~% f' E' l // an action group, because we want these actions to be
# `+ {. H. i! P& K; h' P: p+ y+ v! T7 _ // executed in a specific order, but these steps should* o# b9 m# R5 m9 Y9 O/ E6 A
// take no (simulated) time. The M(foo) means "The message
7 ]; E3 k5 x7 r/ ^ // called <foo>". You can send a message To a particular
' y, D. R: O2 o1 O% V // object, or ForEach object in a collection.
5 o2 {/ Z6 \4 r7 y) ~
# w( y x, s+ j1 H // Note we update the heatspace in two phases: first run2 e" J4 ^- H4 j- Q8 X+ |, S! b
// diffusion, then run "updateWorld" to actually enact the$ w4 K$ L* D Q
// changes the heatbugs have made. The ordering here is
+ P% s+ v! A- ] // significant!/ R9 g* C I5 k, s7 I8 l
+ B1 w- f* [9 ^+ B) l; F# ~5 k // Note also, that with the additional- i5 S/ A5 t: e5 t
// `randomizeHeatbugUpdateOrder' Boolean flag we can6 p3 V, M4 s3 l5 F4 {$ v$ V
// randomize the order in which the bugs actually run
; L w- X2 }0 j // their step rule. This has the effect of removing any
; s% D2 ]. L9 |3 R- X& Z _8 o // systematic bias in the iteration throught the heatbug! B- F3 C; d7 _2 K. {1 j
// list from timestep to timestep
0 W1 E7 y; x* e1 z7 s 2 ?) ~! ^" Q# V
// By default, all `createActionForEach' modelActions have# k7 |4 Q5 m, P4 R2 \: D# N
// a default order of `Sequential', which means that the
" U | J7 }! S; R4 ]) x) i, c // order of iteration through the `heatbugList' will be8 p/ B. ]6 }/ y& q; |: ]# `
// identical (assuming the list order is not changed8 X- q+ P8 I/ n) _) C0 e' ^7 v
// indirectly by some other process).9 i: V c5 V9 ]7 e5 _0 t+ G
& i) F. S1 Z J P% f3 a2 o7 m modelActions = new ActionGroupImpl (getZone ());
/ c' x2 _7 Q% p; L% `* G
9 R! f: |* Z7 R: V7 N. C try {
" D3 w( O0 J3 S+ w. R modelActions.createActionTo$message
) g9 t/ L* X3 C# B2 ^ (heat, new Selector (heat.getClass (), "stepRule", false));1 T0 M1 P- ?- m2 N1 k2 ~1 R
} catch (Exception e) {4 n+ s N# H& {' Y% k4 {* z: @( q
System.err.println ("Exception stepRule: " + e.getMessage ());
- d; a: l; H* L9 b: K' t }; ~; u1 S9 D, k& O
; f9 [8 }' t7 n; h& z
try {. T/ B0 Q( ]& u. A0 ?! c
Heatbug proto = (Heatbug) heatbugList.get (0);
2 f3 _) D( G4 C Selector sel =
! J# R" U7 a6 ?# { new Selector (proto.getClass (), "heatbugStep", false);3 ], Q S; ^0 r/ c8 l
actionForEach =# ~( ?8 A3 y- a6 C9 ]+ A+ _; i; e
modelActions.createFActionForEachHomogeneous$call6 e$ s: b/ N- r; ?% x
(heatbugList,/ h( f+ u H( D! V; v
new FCallImpl (this, proto, sel,
3 E# ]$ l+ k6 X& I1 G* s! Z new FArgumentsImpl (this, sel)));( J2 g$ Z: N7 x a) ^3 q/ R
} catch (Exception e) {
: c1 U# |& P/ _; G* i7 r2 L+ K! y! y e.printStackTrace (System.err);/ [; J2 N1 V3 E: N
}
* Y3 F6 ]2 ?! n) i$ A
8 ~2 I. E6 Q( n; E. I syncUpdateOrder ();' |; [+ l2 T+ _( _" ?$ P
- {) p1 O0 f; Q/ ~0 `2 D/ a try {
' h$ F( w! v z, m. ^ modelActions.createActionTo$message
5 H; U! M" l/ r2 c (heat, new Selector (heat.getClass (), "updateLattice", false));
, Z1 v! ~4 Y7 f0 M& N1 | Z0 O } catch (Exception e) {
4 y+ m& Z' }" N/ X) i System.err.println("Exception updateLattice: " + e.getMessage ());
. x7 F$ `4 J5 p }
) o6 W3 i1 |* i6 l8 H2 G. _ 8 I1 c4 i3 a8 N; I" p& U
// Then we create a schedule that executes the
" m" z* p2 ]% I/ q* e5 K // modelActions. modelActions is an ActionGroup, by itself it/ x+ z- u/ p8 X1 C& u3 e4 s5 q
// has no notion of time. In order to have it executed in. J9 E: }5 z1 r/ j/ P/ H
// time, we create a Schedule that says to use the
( F7 c" V1 k2 ]! g/ n$ q3 W; P& E // modelActions ActionGroup at particular times. This n5 b2 `. ]8 M' _0 F( _# F$ \! ?
// schedule has a repeat interval of 1, it will loop every8 ]$ B, ?% d0 s* R
// time step. The action is executed at time 0 relative to
) ?* n+ n" D5 X // the beginning of the loop.
+ C* X1 W& N0 F1 }3 B) A% n2 f; ?
// This is a simple schedule, with only one action that is
+ K. @& \, S& E1 I4 _# n) ` // just repeated every time. See jmousetrap for more
. e; U9 j3 }+ d/ ]5 J // complicated schedules.
0 r* N+ c* O+ A" C ) T2 Z' E1 h2 i8 Q. m& L
modelSchedule = new ScheduleImpl (getZone (), 1);
8 x* |7 _$ f: D! E' v- o; a _# c modelSchedule.at$createAction (0, modelActions);' A# B2 ~5 Z) @! b
1 ]; k4 z- y" ~6 F) k1 j return this;9 l" U( ^6 D. K" C' m( i: ~
} |