HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 _! ~; J) U+ ]5 D6 o
0 L Q) b6 C* U9 Z @5 m; y public Object buildActions () {+ N. n3 N/ c; X; @' T( J
super.buildActions();( V! w( Z3 J1 {. F
5 _; J( M- W$ v Z5 g" b$ |
// Create the list of simulation actions. We put these in1 Z% w% ?2 B3 o$ B. `
// an action group, because we want these actions to be
. i1 b' s o; @# N' p; M, O // executed in a specific order, but these steps should8 H! b0 Q! Z- w" Q6 S) N
// take no (simulated) time. The M(foo) means "The message
4 {5 {7 g/ d; p q4 U" `0 \ // called <foo>". You can send a message To a particular
" x \) L# f; t8 ?$ D0 W // object, or ForEach object in a collection.7 T% j$ Q9 L' n2 S6 |) x
7 \# {% K t) R) Z0 w3 v
// Note we update the heatspace in two phases: first run
2 Z" ^6 t: b# [ // diffusion, then run "updateWorld" to actually enact the/ {( U0 L, D: Y
// changes the heatbugs have made. The ordering here is
N" \1 R3 s9 Y; [1 g // significant!
" k0 ~! w7 n G+ W' u+ W" ] + `' m4 x$ w' A- |7 v/ c
// Note also, that with the additional' G9 C8 h6 H; p; ] |
// `randomizeHeatbugUpdateOrder' Boolean flag we can! X5 q% Y4 ?" y( i
// randomize the order in which the bugs actually run* ^3 X9 q& A1 p. t' m" I
// their step rule. This has the effect of removing any
6 j/ j4 `. x. o5 e7 ^ // systematic bias in the iteration throught the heatbug4 c& }8 s/ _8 ^0 C! u6 c- s# c
// list from timestep to timestep" R$ j) B* h1 z4 B, x& p% s9 C
: d B; z" I; A( G" M0 s3 I
// By default, all `createActionForEach' modelActions have% A/ F# c5 G% l
// a default order of `Sequential', which means that the& f* `3 Z' l) L* X6 c1 o* O, }
// order of iteration through the `heatbugList' will be
# u! P& G" u$ p" v3 l" I% B // identical (assuming the list order is not changed
% X- ~$ Q0 m8 ? // indirectly by some other process).
8 l+ I% U) j: ?( u3 i! ] * ?! _ |0 |. ]1 H9 m8 p
modelActions = new ActionGroupImpl (getZone ());
) f3 F6 G+ R1 Z9 y/ K/ I- b) T e* s5 v) |$ u4 C. {- X
try {
+ f B. ^7 [) `' a1 A3 w6 Q modelActions.createActionTo$message
2 b; n$ G. ~3 k5 h3 D9 B (heat, new Selector (heat.getClass (), "stepRule", false));
3 R5 s W, J- e" Q. D$ N } catch (Exception e) {. P! A# t Q [* n3 {. C. l
System.err.println ("Exception stepRule: " + e.getMessage ());
4 y3 ]& K3 y2 p8 V4 M5 r% `$ C }( H( I( ~4 c, E$ e; V2 a
u+ Y8 U& l; {8 @7 T
try {
/ Y6 Z7 c+ R) Z ~9 }3 j2 E Heatbug proto = (Heatbug) heatbugList.get (0);: o+ Q( ?' T( X
Selector sel = 5 y; A L2 v) e" i' b2 Z7 T, I
new Selector (proto.getClass (), "heatbugStep", false);) ]! r L! k9 v" Y) K
actionForEach =2 `7 n- {" H0 ]( ]: @+ |
modelActions.createFActionForEachHomogeneous$call/ J* Q z+ s: V8 M/ Z
(heatbugList," G6 T; C4 r: q2 D- r$ s) j
new FCallImpl (this, proto, sel,
! |" \1 M# U$ a q% b& H8 H. D new FArgumentsImpl (this, sel)));
5 p0 @# H( E* v# G* h; m7 V' S } catch (Exception e) {6 j3 c2 `0 n1 D5 {1 e
e.printStackTrace (System.err);
9 ^8 ]5 ]! Q. F* I" h6 E! _* @ }& w3 Z5 K! ?8 o$ w
, g- q' z7 [! H7 z( B# S) }" H8 {
syncUpdateOrder ();# {0 S W$ e* l4 L, B7 V q
) }; I2 R* N; u( L/ j5 W try {
& f% w& G0 _5 j: ?* s$ A5 p" Q7 N4 t modelActions.createActionTo$message
$ a' t+ j$ H; ~9 d (heat, new Selector (heat.getClass (), "updateLattice", false));
& x! e% P& V( u' a+ R } catch (Exception e) {
+ V& C1 A" f5 e( I/ \$ Q: A1 o System.err.println("Exception updateLattice: " + e.getMessage ());
0 `9 Z' Z9 x) ?) _' x }( v1 i4 r& W5 v, _ L
, p1 K" n' A/ r- t( l. Y) U& e // Then we create a schedule that executes the
7 ` {! L$ A! V( g // modelActions. modelActions is an ActionGroup, by itself it
$ ~1 ]) X. O7 D // has no notion of time. In order to have it executed in
! y0 J, U2 `. ?1 |. M( Y5 } // time, we create a Schedule that says to use the
8 }9 S3 x9 t& c // modelActions ActionGroup at particular times. This% o' Q$ Z5 x! h2 `% X
// schedule has a repeat interval of 1, it will loop every
. {: q' v/ Q6 E1 `1 T5 T* q // time step. The action is executed at time 0 relative to
0 C5 @ n( `) [ // the beginning of the loop.
, x2 { w: j0 g. f1 P
2 `1 x9 Z0 Q+ o( a: ^6 v: p // This is a simple schedule, with only one action that is
# ]4 ^1 U7 w9 V/ M } // just repeated every time. See jmousetrap for more
; ~1 M4 z$ p r% g // complicated schedules.
, {7 G' h* P: I# ^5 N4 b$ B7 h * V; t' P* r8 H8 v7 r
modelSchedule = new ScheduleImpl (getZone (), 1);
3 R! A3 A0 ` q( c4 w/ u modelSchedule.at$createAction (0, modelActions); V/ ^# f! C3 c
& r8 |+ ]9 d8 Q: b q8 Y return this;' H) d2 ~& j0 y- p% I
} |