HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
* x) _5 ^6 \# i! q% Q) g
+ A! _) x4 P* N& e) W- ~7 | public Object buildActions () {
3 g% X m# ?5 G% g4 ^6 c5 K: d, k super.buildActions();' \* L3 F3 C8 r! B2 }
; `- Y: J5 g- t2 E0 l+ M- _
// Create the list of simulation actions. We put these in
" s3 E8 B2 p7 w8 X+ D5 A: J0 C5 w7 u5 x // an action group, because we want these actions to be( Z! z# R, j7 W9 a q3 U
// executed in a specific order, but these steps should* o+ f X5 Q: v) A& t4 b
// take no (simulated) time. The M(foo) means "The message- r9 A" G* k6 S$ c5 `
// called <foo>". You can send a message To a particular ?+ c, B; m/ c
// object, or ForEach object in a collection.
: V( ~. g- }3 b2 c4 e) O 4 e! S ^. O/ D' s* d
// Note we update the heatspace in two phases: first run
, f6 t4 W7 q+ d! R3 l8 E' B // diffusion, then run "updateWorld" to actually enact the
, s0 e- s" H/ ^" [/ P4 g2 p0 h/ } // changes the heatbugs have made. The ordering here is( x! F3 n& I" d/ p, q V
// significant!
( D+ @* a' g. M3 x
% b: P+ O7 L4 [# w // Note also, that with the additional7 v1 f1 p9 c0 n8 J; v& c
// `randomizeHeatbugUpdateOrder' Boolean flag we can+ F7 X& ~' z2 j/ I/ E& b8 S, d# h! X# w
// randomize the order in which the bugs actually run
7 y; L( }9 T, a; G. M // their step rule. This has the effect of removing any
b, c. S% ^2 m# b // systematic bias in the iteration throught the heatbug
' h7 t" ^( F% s/ _1 |, Z // list from timestep to timestep
* }: u1 H6 L. h 5 _! c+ U6 z2 P/ ~; y
// By default, all `createActionForEach' modelActions have, R" U1 e6 t q
// a default order of `Sequential', which means that the
) ^5 u: Q. J* E+ L) j0 T) M* y6 [/ b // order of iteration through the `heatbugList' will be" H* ?7 H3 k1 w5 s
// identical (assuming the list order is not changed+ q7 `; s9 g( n
// indirectly by some other process).
6 q5 Q! p( j7 O9 [
" M/ @/ L. I5 M, Y( k8 [ modelActions = new ActionGroupImpl (getZone ());( z/ D: j" ^# n+ a+ W: h
?7 f k8 t, H- q! O try {- |: h4 d7 Y& p" t
modelActions.createActionTo$message
# a0 R Q# u' u; U# U! F8 n" M (heat, new Selector (heat.getClass (), "stepRule", false));
1 m: `% F$ Z7 b h! c } catch (Exception e) {8 ?6 }4 M! ]3 u2 B/ k
System.err.println ("Exception stepRule: " + e.getMessage ());$ j( D8 g; K" ]7 G. k$ E
}
0 B, b( t2 q) n$ k$ d8 \: _+ j7 v L5 L3 v
try {! K* Q% `3 y( `/ C% y" _
Heatbug proto = (Heatbug) heatbugList.get (0);9 l) G& v" t7 Y8 \" x1 Y! a
Selector sel =
$ b. }6 d0 }9 K0 r0 u p new Selector (proto.getClass (), "heatbugStep", false);9 {+ |" @5 E+ D, n! d8 D5 @9 c3 U# X
actionForEach =( @* c! D1 b. C
modelActions.createFActionForEachHomogeneous$call. X9 w& c- b; [- _8 f
(heatbugList,
5 y2 ^. R G( ?) h new FCallImpl (this, proto, sel, d% x% H) I. w" s
new FArgumentsImpl (this, sel)));
" [: r9 M$ _4 s5 _% m( _3 N } catch (Exception e) {
8 h; ^* z. K7 G, s) L7 z e.printStackTrace (System.err);
! w7 r% a7 K: \% R2 J# ]) q }
2 J7 _! l; v; O, H9 i6 Y4 {. v # i, o* S' w9 \& [8 z
syncUpdateOrder ();6 \0 `4 {3 j' j- G
1 K8 I) W% T& E try {- B( O2 }; b& m* S
modelActions.createActionTo$message / x |% c* w3 v
(heat, new Selector (heat.getClass (), "updateLattice", false));
9 y; n8 m8 T% @6 _; I! c% h- R# P } catch (Exception e) {
% z$ A/ ?; z- O/ ? System.err.println("Exception updateLattice: " + e.getMessage ());" p* ]2 W) Z7 W: C
}! Z: J5 K2 I& g7 d1 }
0 |# m q& Z0 L. {3 _
// Then we create a schedule that executes the
( @2 P V$ c8 O# h; x s/ ~6 [0 a // modelActions. modelActions is an ActionGroup, by itself it5 J1 H* A* o; ?9 R" g
// has no notion of time. In order to have it executed in
, B& c4 z; d& F: J0 H" U( Z) E. ] // time, we create a Schedule that says to use the. `, ]$ D) s; t' {
// modelActions ActionGroup at particular times. This4 ` ? u: b+ [7 R% K$ }. j
// schedule has a repeat interval of 1, it will loop every
4 X9 H, j% U; A F( [ // time step. The action is executed at time 0 relative to
; [: P3 k( t! w! K+ B9 e // the beginning of the loop.* ~# B$ g4 {, C. g3 M
" V5 R; t- G( W // This is a simple schedule, with only one action that is1 B+ k0 T; i1 U; ^4 C5 @" [% m0 U
// just repeated every time. See jmousetrap for more
' _! L5 v! {+ x J' q // complicated schedules.! B# d5 l& p6 ]/ j+ N
5 m: W$ H7 c+ ^ modelSchedule = new ScheduleImpl (getZone (), 1);5 {; x4 |1 K( N" R. B
modelSchedule.at$createAction (0, modelActions);
' F7 b8 J5 v+ _* w
& m5 }! U. G. ~' ~$ J' \8 l return this;$ a/ @9 }" [; G, q6 ]& ~
} |