HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# ?7 y1 C* D1 v% R
0 m& u1 _5 |4 F8 C, o public Object buildActions () {
$ ~3 o+ u: R6 O2 ?5 w6 U5 a super.buildActions();
# z+ c/ J3 o# E, ]/ g 2 l& M" V9 {+ [, E1 `) A
// Create the list of simulation actions. We put these in
) [& M. Y/ t* O% a // an action group, because we want these actions to be+ G! y3 `+ h2 A( c
// executed in a specific order, but these steps should# } k0 ~, b1 X# a% p
// take no (simulated) time. The M(foo) means "The message0 T# x/ |$ P& }
// called <foo>". You can send a message To a particular" |, M& E, w6 a F2 r
// object, or ForEach object in a collection.* L% p& Q; d+ R. {* [! P
) @( H2 x Q* i
// Note we update the heatspace in two phases: first run
4 E6 u, V6 M# ]' b // diffusion, then run "updateWorld" to actually enact the
3 F4 _8 Y- g5 P+ U2 e1 s+ g // changes the heatbugs have made. The ordering here is7 h. X7 B8 ~6 S2 ]! F& F
// significant!% h' S) f5 ?$ f2 u
5 z! B; a$ m) S7 X. Q+ g // Note also, that with the additional
2 `0 I' k! P) F% U // `randomizeHeatbugUpdateOrder' Boolean flag we can" F2 ~; Q9 {+ s+ ^
// randomize the order in which the bugs actually run
7 j5 S8 `8 A5 D$ ~' [ // their step rule. This has the effect of removing any5 F7 t' p- Y: j
// systematic bias in the iteration throught the heatbug
, ]+ ` |, \ ?, ~' _ // list from timestep to timestep" L% Q- l- u% T* }3 Q; V* \- p0 _
1 s+ v+ ]' z; @6 z" e // By default, all `createActionForEach' modelActions have
& l& t( ]& U( @) L1 Y // a default order of `Sequential', which means that the' f) m$ y6 z9 o
// order of iteration through the `heatbugList' will be
5 s; O! G% U" [+ R // identical (assuming the list order is not changed
6 j, n0 [: ^: U // indirectly by some other process).
, z, j6 T l) v7 Q5 M! o; [ ' N/ x8 u* \+ W
modelActions = new ActionGroupImpl (getZone ());- [. v2 |+ v) a% M* u; |7 J
0 o5 d- i C* o$ M9 n( O0 M try {
* \3 w9 r, s0 }: d6 n% \5 K modelActions.createActionTo$message1 \! g! @' O/ [/ U
(heat, new Selector (heat.getClass (), "stepRule", false));, `0 u' T v; h3 ^; S5 b
} catch (Exception e) {4 a X/ X+ W D% p2 C9 F1 k
System.err.println ("Exception stepRule: " + e.getMessage ());
, H$ U: \3 O, H$ Z0 N }
/ u! \2 w% X# V' @) } {9 _% M# b/ {& ^: ~, M; K6 `0 o
try {7 q1 r& p7 h) ~+ ?( y* x
Heatbug proto = (Heatbug) heatbugList.get (0);
$ U2 w1 g d7 Q9 j Selector sel = / `- y( P- f. @# r4 p/ ], G0 M
new Selector (proto.getClass (), "heatbugStep", false);
, n7 D/ R t5 k: }+ v actionForEach =
% e! T0 J! l+ C; `, z2 b1 i1 W3 Q, } modelActions.createFActionForEachHomogeneous$call: D$ t' U% e& c0 V
(heatbugList,+ ?0 o# K0 E1 G6 u
new FCallImpl (this, proto, sel,% W8 Z: g8 ?& |2 `0 F, Y
new FArgumentsImpl (this, sel)));
2 Z n& T0 b' p" \ |% ~ i } catch (Exception e) {) V! N7 ]) p2 H' h; F7 G
e.printStackTrace (System.err);! W6 R: X8 s6 w m* }
}
0 Y- K5 N- ], n% f
+ `" F! a8 l6 l& O9 w/ x3 s: o syncUpdateOrder ();
8 ~$ t R& p9 L8 L7 h. e! l7 R7 L! O
try {# ?, q- _; q: ~( {# ?& C" H+ g
modelActions.createActionTo$message 4 I# I" d- W* Y; _0 _
(heat, new Selector (heat.getClass (), "updateLattice", false));
4 L7 K6 `7 m, J! e5 m; F } catch (Exception e) {( N8 i F1 h: b* _0 `: G
System.err.println("Exception updateLattice: " + e.getMessage ());
/ O) r! M2 X; ]6 x5 }8 M# O! X }1 B5 }3 S& i7 g9 _7 ~
" }: q! B# s; b- d
// Then we create a schedule that executes the/ c+ t. l$ i# [, i. t
// modelActions. modelActions is an ActionGroup, by itself it5 m3 q3 A7 j" _! s, s8 S& A
// has no notion of time. In order to have it executed in
$ F i2 D P4 M. R8 _6 T. C // time, we create a Schedule that says to use the
+ z$ x# M2 r1 y( J // modelActions ActionGroup at particular times. This
5 H d: S. S# V // schedule has a repeat interval of 1, it will loop every
T. q* k g. v. g6 V // time step. The action is executed at time 0 relative to; K) V7 V8 ]/ j8 O
// the beginning of the loop.- S5 y; q6 T2 }1 C
/ H! m% g# v7 v" ~: Q2 L
// This is a simple schedule, with only one action that is
/ }( A( e& t: |" X( w // just repeated every time. See jmousetrap for more
2 G1 Y2 d# v% [ J1 |/ d$ z4 |5 i // complicated schedules.
. e: P4 i6 P9 @. J1 F6 n2 m+ t
" `9 U! T3 t, }. @1 R. x/ {" h modelSchedule = new ScheduleImpl (getZone (), 1);
2 Z& N! r: d( n" X: _ \0 h( c modelSchedule.at$createAction (0, modelActions);
4 e* t$ @8 t/ F1 R: J 3 i9 c1 V1 s. m6 g9 a
return this;
5 Z; D* Z' s4 a9 L" v, U } |