HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
) e. U% ?9 f; J4 j
5 Q/ ?5 U1 ]1 x# t public Object buildActions () {6 J( O9 b+ o( T5 F) d
super.buildActions();
9 ?" e) m. q! y, x; o( r% X! p
1 n1 ^2 h z+ r0 \( X4 P* j1 o0 g // Create the list of simulation actions. We put these in% O( W0 d$ H; B6 }) t$ n
// an action group, because we want these actions to be# D, D/ v) W/ O' H
// executed in a specific order, but these steps should" s, e: U3 {5 X5 K( O% M. j
// take no (simulated) time. The M(foo) means "The message
( B n7 g. ?; p' N+ b6 o // called <foo>". You can send a message To a particular
4 l+ y. f4 o, a) S // object, or ForEach object in a collection.! e$ Q+ g) p$ j$ w6 |1 L
7 M$ h e* ?9 v L$ @) @
// Note we update the heatspace in two phases: first run% {! t! ?+ _0 j |$ }; L: W
// diffusion, then run "updateWorld" to actually enact the* t+ l% d; G7 |4 l
// changes the heatbugs have made. The ordering here is5 V: q" M: A- |6 g1 d6 R
// significant!
) q$ p( A0 p2 R0 O J8 q 5 R( \ Z/ f1 C( u/ i) B5 B/ R5 J G
// Note also, that with the additional+ v2 @ d. \6 Y; N3 b9 C. z, s/ |, @
// `randomizeHeatbugUpdateOrder' Boolean flag we can
8 Y5 h4 X6 q$ M& ~ // randomize the order in which the bugs actually run+ d3 q! Q/ o: ]: q
// their step rule. This has the effect of removing any
: z. Y( X: k9 L' ? // systematic bias in the iteration throught the heatbug
+ x- G% V' \( Q // list from timestep to timestep+ R Y" r& ]( h C' F( ~+ H
5 D3 `8 j: N! M9 Q7 {* j% B
// By default, all `createActionForEach' modelActions have
, x' y1 s/ L# @$ i* q; S" N$ X // a default order of `Sequential', which means that the
9 D0 {6 ?, x5 c // order of iteration through the `heatbugList' will be' M R& @ O% t" f- {6 y5 U1 Q- I
// identical (assuming the list order is not changed
3 g- S+ s) [5 j3 y // indirectly by some other process).2 Y* `% C8 v" {/ P
# ]; D. N6 [: R# g+ I* M* N modelActions = new ActionGroupImpl (getZone ());* }/ K/ Y0 }, y6 {* z
- V& X( K* T6 O4 Q4 P0 a try {' T, ?+ i* f% [* s/ x. B. S& s
modelActions.createActionTo$message3 w- h" H5 E/ C9 Q% A2 i1 T
(heat, new Selector (heat.getClass (), "stepRule", false));. s4 L F' h2 l/ b: n% M! J- }5 x4 b2 C
} catch (Exception e) {2 i7 T- l! h% Y
System.err.println ("Exception stepRule: " + e.getMessage ());
$ g( i" L) c" C& v }5 u, H3 E: n1 j; Z; P
1 E: f# C; t9 `$ C! u try {
% \' i+ l$ L- ` Heatbug proto = (Heatbug) heatbugList.get (0);% f+ @+ K, {& o: E9 ?+ v/ C o
Selector sel = 6 Y3 a; D& F8 L5 l
new Selector (proto.getClass (), "heatbugStep", false);
1 [+ S: X8 x8 C, N1 |3 L7 \. C actionForEach =# Q4 o' q2 ^0 M
modelActions.createFActionForEachHomogeneous$call
$ h# \/ y. y, L! x (heatbugList,
) O9 C! b& Y% i' X: h7 L new FCallImpl (this, proto, sel,, I( B4 N: B, C% Q2 e
new FArgumentsImpl (this, sel))); H3 J' a# a% L2 u, u7 G o# I
} catch (Exception e) {
: l# D; {+ u- f$ J) P# `& c. Y e.printStackTrace (System.err);' K+ |0 @: ]: \
}" {' F3 Q' j; O# g/ e6 I
0 E) Z: @: |+ Z. ?+ a) z6 W1 M
syncUpdateOrder ();
+ ~0 z! A6 ^' H( z! D8 p0 X; {( R
5 ]3 M$ g! Y Y/ p5 v try {
' j5 Y* S" Z+ U modelActions.createActionTo$message - Y* q8 X) X+ T* r3 s2 K# h; u
(heat, new Selector (heat.getClass (), "updateLattice", false));
/ A+ U) X% b# q4 f } catch (Exception e) {4 D: E' l+ R3 G
System.err.println("Exception updateLattice: " + e.getMessage ());2 F) v' _ E8 N6 k1 i ?
}
3 I' Z) m3 h4 z8 P
5 ~6 f7 G1 T, l) W$ | // Then we create a schedule that executes the
. h. |4 Y. Z: Q9 e- D // modelActions. modelActions is an ActionGroup, by itself it% f5 g H- g4 k) Q+ o$ a
// has no notion of time. In order to have it executed in9 q3 K( r3 F8 G: |; R
// time, we create a Schedule that says to use the
5 i1 O0 d, d1 e) S // modelActions ActionGroup at particular times. This. q2 F" p7 W4 l4 F
// schedule has a repeat interval of 1, it will loop every# X1 o( Q6 J; m$ u7 S9 S
// time step. The action is executed at time 0 relative to
; `4 `- u$ v- c! V // the beginning of the loop.% y, I: P$ A. p- Y; o
5 i% b, a4 y/ c& _/ {' `) w: h // This is a simple schedule, with only one action that is
* A* E( j1 v; E( f; ~6 ^! i, r // just repeated every time. See jmousetrap for more
- w6 X2 k( _7 T( B // complicated schedules.
& @0 A8 I# h0 \0 l
* [. m' z( B! z9 E( B4 \5 J+ f' F modelSchedule = new ScheduleImpl (getZone (), 1);
a) V2 D6 g. Q. { modelSchedule.at$createAction (0, modelActions);
1 \# V( U, z' y 6 ^, [8 d. z* E& B" W$ ^1 r; f
return this;4 u1 F* L% P. g8 A" `
} |