HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% M7 k8 y/ \* V) ^9 v" ^! e
' v7 t7 C! H( O2 o& W6 u/ P2 m public Object buildActions () {
3 G2 |0 D: C% Z* b1 V" Y super.buildActions();
1 ^" C6 v3 f' P, R) F 5 T( y* W3 e+ [. C8 Q
// Create the list of simulation actions. We put these in
$ \, U- j% H, b7 o0 |. R // an action group, because we want these actions to be
8 r1 ~+ ?8 B* b- P8 s! ^ // executed in a specific order, but these steps should0 [) o5 Y8 W* B. j9 f2 d
// take no (simulated) time. The M(foo) means "The message
4 i6 q" e4 L2 U/ d // called <foo>". You can send a message To a particular
9 I$ \: g9 x1 { // object, or ForEach object in a collection.
/ K/ i" T0 q8 T8 }0 b H 2 {. i. m+ a1 ^; h% @2 I
// Note we update the heatspace in two phases: first run; f( O) t9 V- Q+ ~
// diffusion, then run "updateWorld" to actually enact the. I4 j4 g' l" v% ~
// changes the heatbugs have made. The ordering here is3 D2 \3 ^8 Z, \1 x
// significant!
. F' z9 R" m9 E3 r. i$ z 6 B! e8 O' d% S! Z% M* V
// Note also, that with the additional
* \6 t8 ?4 j! ^ // `randomizeHeatbugUpdateOrder' Boolean flag we can
[0 U/ }5 {1 Z1 m. T( U6 J6 v' Q // randomize the order in which the bugs actually run6 D# U! n7 u; h/ g0 }! u" ~
// their step rule. This has the effect of removing any
# D4 _. \1 k' K+ A/ S // systematic bias in the iteration throught the heatbug
1 w/ |! ]4 g4 I* `2 s5 ^* S // list from timestep to timestep" ?' t, I$ u2 W; @% r6 e5 I
# k( d: \& l* L; D' D
// By default, all `createActionForEach' modelActions have$ J* w9 ^* F$ D1 S
// a default order of `Sequential', which means that the& Y# [; f3 i' Y
// order of iteration through the `heatbugList' will be
3 m9 r9 f) P: U' q4 K: k1 B // identical (assuming the list order is not changed
5 j" W3 z+ T' [4 y5 Q // indirectly by some other process).9 v Q7 N; a( r% X
$ U# n4 g1 Z6 a6 y; o: }3 X
modelActions = new ActionGroupImpl (getZone ());
$ X+ t/ R1 V8 Y' {% ^* n( I" b6 {6 A1 }8 |3 j
try {1 y- b" ]5 j) e- B
modelActions.createActionTo$message
* d9 ~. u% W7 o- Y7 V* h (heat, new Selector (heat.getClass (), "stepRule", false));
5 S* }- T; ]7 K; }/ W% q- n } catch (Exception e) {% P7 a0 V9 j6 e
System.err.println ("Exception stepRule: " + e.getMessage ());( c a+ Q- z u. r7 `2 A
}9 p7 W. J. t: Q; l7 Y0 ^& l# W0 [
" j8 P( u5 w3 K3 k# { try {7 I4 L0 q4 F7 Z" Z5 w, ?, _
Heatbug proto = (Heatbug) heatbugList.get (0);: O0 j2 P; M# S3 }( ` r# w! [( \ \
Selector sel = ! u F4 _5 V1 r0 `8 b# n
new Selector (proto.getClass (), "heatbugStep", false);7 ]! r. C( `# T9 P
actionForEach =) C" X8 a. t( \. J
modelActions.createFActionForEachHomogeneous$call5 I& x6 y0 S8 v* ~7 w
(heatbugList,
: s" {; \4 P4 |; L# ~ new FCallImpl (this, proto, sel,: P4 G6 z9 M( s1 X) x+ ?
new FArgumentsImpl (this, sel)));
4 ^+ {0 V4 u# G3 A7 V. O5 N } catch (Exception e) {
" r' F- `+ b; m9 [$ h e.printStackTrace (System.err);3 f# h+ P' `* O, M* a- W, g- ^
}" _# J7 Y3 \0 P3 C0 _6 `
5 v& ]. @- [& Y' N( d
syncUpdateOrder ();
H" x8 I& k& O' K- j/ Y$ Z, s3 @6 \8 ?/ z
try {$ I, [# y+ Z, b/ z) B# ~
modelActions.createActionTo$message ( c ^# w; l$ N
(heat, new Selector (heat.getClass (), "updateLattice", false));
& P% q2 E7 X( _+ Z5 |/ q" f } catch (Exception e) {: A2 T: V! O0 V0 F$ j4 ^* m7 X
System.err.println("Exception updateLattice: " + e.getMessage ());
9 j8 s8 T+ h4 W8 S, a5 C }4 K; I W! U! F% H$ @0 O
, A0 C; S; f: j( L% B; o# I // Then we create a schedule that executes the/ S; G8 Q# f! U. N; v% G
// modelActions. modelActions is an ActionGroup, by itself it9 u) {0 @1 ]4 `5 `* `
// has no notion of time. In order to have it executed in. G% L5 m! u4 U( W# f
// time, we create a Schedule that says to use the7 Y$ l* g: L3 z5 {+ b8 k
// modelActions ActionGroup at particular times. This
+ s$ p! O* i4 P% K // schedule has a repeat interval of 1, it will loop every
, N( v- \2 J2 f; D7 J+ d$ J // time step. The action is executed at time 0 relative to
( i9 V* D! T @8 c0 W8 Q. } // the beginning of the loop.+ N9 u+ ?( \- q
/ T$ _$ H2 [7 D# a4 J% [: \ // This is a simple schedule, with only one action that is
# y, R- x5 _" |$ G5 X; N4 [ // just repeated every time. See jmousetrap for more v% O( v6 ~& J( P
// complicated schedules.' Z3 f8 k. ]! s
& a; X; i6 G2 S3 C modelSchedule = new ScheduleImpl (getZone (), 1);0 r3 n* j0 g9 C# O
modelSchedule.at$createAction (0, modelActions);
9 T2 S( V; s: `9 T8 d! J2 \& }/ E
0 _" o! \6 h* G) _1 }- O+ T! w! p& x return this;! Y0 ~6 E# c4 |! |
} |