HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# @! L5 Z1 @% B4 ^
) ]4 I7 t% _& G7 }' d+ Z9 R public Object buildActions () {9 p! N6 | k+ p! }: }0 ?. J
super.buildActions();
5 \: x% ` Z, w9 g
; U+ E3 h, H: `* w% K2 R* v5 y* M // Create the list of simulation actions. We put these in c; N1 [9 u0 z2 ], _
// an action group, because we want these actions to be
5 f0 K2 g8 A _+ S4 _! K) q( d // executed in a specific order, but these steps should! l' p+ O0 v; x1 E8 J8 e. _
// take no (simulated) time. The M(foo) means "The message# n" S3 s' n4 r! D; I" J) [6 f
// called <foo>". You can send a message To a particular
$ Z4 a8 W F/ ?, d- W: F( e# V // object, or ForEach object in a collection. H5 J- g, {: v8 q
# s. s! l, g9 J0 @1 S% s) O5 S // Note we update the heatspace in two phases: first run% i8 v6 ?+ Y, y1 h% B9 |
// diffusion, then run "updateWorld" to actually enact the
7 g9 C, a4 F8 Z& i) N7 l // changes the heatbugs have made. The ordering here is6 u8 c1 I3 n% \+ }% a! t
// significant!5 s6 i' ^$ B8 G6 Z% ^8 l# Z. O
! W* a& f; s* e+ o! K& @ // Note also, that with the additional
# ^/ p$ }- I( r // `randomizeHeatbugUpdateOrder' Boolean flag we can! ~5 ~: q" L* H h) `
// randomize the order in which the bugs actually run6 @ n0 G7 v7 Q' o, u/ u% O- b
// their step rule. This has the effect of removing any
& B: `" u! B0 A0 r: J // systematic bias in the iteration throught the heatbug
( n/ V) z% X9 ~: m" e // list from timestep to timestep
+ _0 _6 ]) G/ q0 i
6 w6 F. r2 y. V& |0 D7 k' X3 j // By default, all `createActionForEach' modelActions have
; ]" A3 }) l" {0 J. z // a default order of `Sequential', which means that the+ j6 b5 R- f2 Y$ U9 e: o. p8 N
// order of iteration through the `heatbugList' will be( ^* @) m3 u3 D7 L
// identical (assuming the list order is not changed
- N- [* J a# b0 T0 o3 u! G // indirectly by some other process)./ S$ o% Q4 p2 S
5 l6 }: ~- y0 x3 Z4 c8 z
modelActions = new ActionGroupImpl (getZone ());0 Z5 _( K! s+ x7 @' p) p
0 d. z0 c6 u' P6 y# b/ o+ t5 J. T
try {
/ `- q' }# p4 V9 K" N modelActions.createActionTo$message7 q( t4 a a! L( _& s' {
(heat, new Selector (heat.getClass (), "stepRule", false));# e7 ?( |0 E( V# m2 W2 o
} catch (Exception e) {
1 E9 p: b, X) \: [, o, b System.err.println ("Exception stepRule: " + e.getMessage ());4 @% v+ f2 o3 V+ h8 \+ ?' {4 R$ X/ p
}
6 a* d, \5 I3 V$ Y/ U4 p$ x2 \" j& Z! A1 w" w8 C8 e
try {) @6 y+ }4 l, \8 V2 M6 R, _
Heatbug proto = (Heatbug) heatbugList.get (0);
) W1 Q7 m$ m% Q }5 a Selector sel = : J; |. D& p1 C9 s4 ?) d5 i! h" r. w% S
new Selector (proto.getClass (), "heatbugStep", false);( G2 @2 ]6 z* Y$ _9 i9 E- R3 K
actionForEach =1 P8 I$ ]0 P. A5 S0 }0 q% f
modelActions.createFActionForEachHomogeneous$call0 h( Q; J A1 U: L
(heatbugList,& f' ]8 Q3 W6 a8 }
new FCallImpl (this, proto, sel,( L5 e' R, i5 w! ^: W" g: N. K
new FArgumentsImpl (this, sel)));
7 A: C& Z8 D4 S$ { } catch (Exception e) {
8 Y0 O- U4 ~/ s e.printStackTrace (System.err);
5 D2 |1 n* P0 G6 M' Y }7 i3 R) u9 w2 K$ ?( R
$ E! c4 X$ B+ e% C
syncUpdateOrder ();
/ ~0 o5 A$ M8 V6 f" }& Q5 C, L
5 V! i/ x' i. Y* q& P1 A: Z try {
0 b9 b- |8 o) U' Y/ R: ? modelActions.createActionTo$message 3 e6 @; v+ i" U4 l }# W
(heat, new Selector (heat.getClass (), "updateLattice", false));
) S( _( O* j. G# ]/ \ } catch (Exception e) {
0 V" `7 X1 ]: u& X+ q) `* `* } System.err.println("Exception updateLattice: " + e.getMessage ());
' S" j8 O; }4 F% X9 V }
7 j: O* q* J. x8 J( Q( Y6 I
. s+ }% x/ W$ `, ~8 A // Then we create a schedule that executes the
T5 |' T8 ?4 B5 d* ` // modelActions. modelActions is an ActionGroup, by itself it
& `, ]5 F8 g5 Q6 A; m // has no notion of time. In order to have it executed in
6 j2 q5 t, e/ y' |5 N1 p // time, we create a Schedule that says to use the' w8 e8 b, Z N+ w8 k
// modelActions ActionGroup at particular times. This7 B+ Y; e0 Q8 \& q9 b* o7 b+ u# ]
// schedule has a repeat interval of 1, it will loop every
; a; {& j9 K; y; d% y3 A // time step. The action is executed at time 0 relative to, I- e3 f1 ^& k$ k1 R1 \! e. A
// the beginning of the loop.
) q/ ^4 e3 G. d; b4 P9 u* i0 g) v. A. ]2 v
// This is a simple schedule, with only one action that is
" V8 ^7 D6 y5 {3 n4 D. S4 `; W // just repeated every time. See jmousetrap for more N" B# S" W! |, D5 f
// complicated schedules.( }- g" q# z, x# @/ L3 g$ j. c
9 O2 l+ W* e E, s" c modelSchedule = new ScheduleImpl (getZone (), 1);
' K7 A$ g) u) G) X1 k/ C/ v2 } modelSchedule.at$createAction (0, modelActions);7 @& K- ]4 j! H. e! r9 f
/ v8 n5 Q9 p; U6 r# O* E return this;8 [( q" y' _4 p" o
} |