HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 {& Z& q2 Q/ ~
4 D1 T- _; e% {/ X public Object buildActions () {
# W7 F8 x3 F* B super.buildActions();
( T0 K' h8 J$ f; x: T1 Y/ b9 Q U% \ + n/ j6 d9 a0 H4 g7 C7 C8 q
// Create the list of simulation actions. We put these in, b5 C4 F( d; |4 d* S+ }
// an action group, because we want these actions to be6 D' w: D0 _, T. ?" N
// executed in a specific order, but these steps should
4 Y- @# `& p, ` // take no (simulated) time. The M(foo) means "The message9 k+ R! a: S9 q
// called <foo>". You can send a message To a particular
4 b' M4 K: t4 m" m- P" u // object, or ForEach object in a collection.# f2 K" s& ~# P. M% Q
6 z1 G0 x$ E0 M // Note we update the heatspace in two phases: first run
6 H# _ G) K4 q. `" ~2 t( K$ i // diffusion, then run "updateWorld" to actually enact the
7 A( r+ ^, X: d" Z: m0 ` // changes the heatbugs have made. The ordering here is
b( ~8 p3 ]" q' b5 b# `* q // significant!
# L' w9 J" ^1 M" j$ R 9 |0 A- q6 K# a' W- A
// Note also, that with the additional, U& i2 M/ p3 o' a i. d5 Z3 }
// `randomizeHeatbugUpdateOrder' Boolean flag we can) \- J; y' {0 l. u/ l g2 A
// randomize the order in which the bugs actually run
; ^+ R* g* Q1 @$ K7 C- } // their step rule. This has the effect of removing any
8 h6 R g: E2 m8 m$ Y4 K // systematic bias in the iteration throught the heatbug
, C8 F6 k6 C. G- R4 V // list from timestep to timestep
, w2 r2 {7 }3 ~, \ / w% g1 d) B9 O' ~6 `
// By default, all `createActionForEach' modelActions have: x. Y" ~ z7 L
// a default order of `Sequential', which means that the
) v( U. H3 R% U b* S/ ~" | // order of iteration through the `heatbugList' will be
0 E* V6 x1 S! V // identical (assuming the list order is not changed0 h0 n7 g/ C: Y! v% O; L
// indirectly by some other process).
3 v+ N- _6 n9 u! s: u; V+ Z* V0 X* n 9 g6 y1 k. [$ m- t6 o7 N+ q
modelActions = new ActionGroupImpl (getZone ());; p1 i9 m& W& i% i7 R$ L/ i
( {. Z' K5 |& L try {
( {: O3 Q, F) C+ @9 ]/ @0 S/ r modelActions.createActionTo$message
( k( i8 ?2 O3 v3 i" S1 d (heat, new Selector (heat.getClass (), "stepRule", false));+ {$ l9 w0 D' E; [9 O
} catch (Exception e) {7 c- L" u. ^- O; l
System.err.println ("Exception stepRule: " + e.getMessage ());
9 ^' x6 T5 `. W% C/ j) g3 V- J8 y }0 b' v9 T0 G! { h
! |, s# f# D+ I
try {8 _. n' b4 q- P$ S1 ^ ^+ n1 B
Heatbug proto = (Heatbug) heatbugList.get (0);/ e5 h& J, U* e5 ?+ f
Selector sel = . P) W$ T' M) g0 Q C; q- ~
new Selector (proto.getClass (), "heatbugStep", false);
8 b" v' J, z0 K actionForEach =
* _0 `! O9 ?. T$ y1 ~- m modelActions.createFActionForEachHomogeneous$call
' `) B3 B4 d% b8 j' P- ^2 W (heatbugList,
) T1 p8 J# u B% Y9 x$ m4 s s new FCallImpl (this, proto, sel,! W4 s; q0 {* z7 k5 L
new FArgumentsImpl (this, sel)));
3 U' k# e: F& U' W7 c$ p( O4 ? | } catch (Exception e) {
8 o6 q B( K1 u2 c4 f e.printStackTrace (System.err); E/ q/ a6 K$ ~# {
}4 a7 H k1 B+ i: r# }& v) L
9 c( ?) B1 Z2 G+ H: T, {
syncUpdateOrder ();/ z* ^- z6 F5 B/ H; t
% g# z, a( x' e, K, L. ~ try {# G8 q9 Z/ I( [4 T( _
modelActions.createActionTo$message 0 M) C7 {( x1 v9 u* Z8 i
(heat, new Selector (heat.getClass (), "updateLattice", false));
4 i' z. \$ j. Q) l# b5 i! u$ g/ y: N } catch (Exception e) {
# |5 Z4 \: d; [' J- p; [9 G& e- ]9 G System.err.println("Exception updateLattice: " + e.getMessage ());: q# u& |: W$ K0 k* O
}" T$ c" P. k" P. b* r
E7 {8 o2 y0 }4 a! j+ t // Then we create a schedule that executes the
+ N- i/ D+ m* W& N // modelActions. modelActions is an ActionGroup, by itself it2 y' `( C4 c8 Z3 J5 `3 I& u9 A3 A2 {
// has no notion of time. In order to have it executed in
/ D' m& g% t/ u" @, j, ` // time, we create a Schedule that says to use the
- u& Q% c9 R$ d2 D3 Y" ^# | // modelActions ActionGroup at particular times. This5 r$ q, W: d; M2 ~% a
// schedule has a repeat interval of 1, it will loop every
7 }4 y& p* c( l9 w& Y( p // time step. The action is executed at time 0 relative to6 V6 x2 ?( B! f8 I1 ]; ?; `
// the beginning of the loop.
4 |( Y" A& P/ o. D4 B0 U# A4 O% j+ B# S
// This is a simple schedule, with only one action that is
+ ?6 Z% H7 R( s, [4 A // just repeated every time. See jmousetrap for more1 b* c! N2 ~9 b `+ _7 g
// complicated schedules.
2 p! Y( ?8 J" A/ c7 H( A
. L, a7 a" B4 E! Q8 R8 [ modelSchedule = new ScheduleImpl (getZone (), 1);* O" d# H0 {( u; D% h6 |
modelSchedule.at$createAction (0, modelActions);
9 o% [1 O) M/ \' v7 t$ ~* f" [" m- W
; \; Y n5 m f* ~4 J7 `$ Z return this;) I! r R7 p! G0 t( x/ w# W* L: t4 w& J
} |