HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
' ]5 l! S5 q: a* E
& }& {& }- m7 k public Object buildActions () {
, Y9 ]+ d1 r- y% S1 H) W" E super.buildActions();. Y5 x! Q# v) Z
% \2 s2 U0 o% i T // Create the list of simulation actions. We put these in
9 P/ ~5 T9 [7 z+ G // an action group, because we want these actions to be$ V" U1 C5 Y, v5 T
// executed in a specific order, but these steps should
/ |8 W% H; X- Q @* m- l/ t$ H // take no (simulated) time. The M(foo) means "The message
2 D2 N' x' b3 |% ]. T // called <foo>". You can send a message To a particular
: H! \" w! e8 H. ?+ U // object, or ForEach object in a collection.
# w4 S% J) R; _# S4 i5 Z ) \; L$ i5 {6 z5 j% z( K1 @$ y
// Note we update the heatspace in two phases: first run5 @1 [. v. @% t( `6 h4 W' s8 Y4 \
// diffusion, then run "updateWorld" to actually enact the, O- p; }. d1 z; q5 D$ @ S
// changes the heatbugs have made. The ordering here is
( z) I5 r; W7 }6 |# u( ] // significant!# P" |% r: {4 K
" T, i' j( Y$ N3 ]9 ~0 ? // Note also, that with the additional
) n% V9 ` ?8 t3 y4 M // `randomizeHeatbugUpdateOrder' Boolean flag we can7 O) b$ s9 ?7 W; U9 ^- b+ B+ \
// randomize the order in which the bugs actually run0 \2 E6 [9 R+ N( i. ~# `3 Q7 M
// their step rule. This has the effect of removing any
1 V+ [- y( i+ O" N# t // systematic bias in the iteration throught the heatbug
: w" a# g$ b3 `8 x // list from timestep to timestep
3 R! y2 X" ^! p' |; s ! W# g- f" V$ u& N1 C0 A: x" w
// By default, all `createActionForEach' modelActions have6 k9 |( D3 K3 ^8 i; t
// a default order of `Sequential', which means that the
' `; j# J1 j1 p& c" Y' _) i2 I // order of iteration through the `heatbugList' will be/ F9 s9 J8 S& B$ H
// identical (assuming the list order is not changed
4 v7 Z x2 D3 K" @ // indirectly by some other process).+ @( p0 U( _1 X# H% n
$ K! _ h2 |; O, J
modelActions = new ActionGroupImpl (getZone ());5 _7 c4 b6 L: O6 I' \% k: q
$ f* }7 J- Y7 f5 y2 P
try {
' U# Z' y9 _; z! k7 C8 O8 l modelActions.createActionTo$message4 m, }, t2 O+ I5 G' y- G
(heat, new Selector (heat.getClass (), "stepRule", false));
% }# c1 [4 b4 I7 | } catch (Exception e) {
0 _% M U+ X& u8 F System.err.println ("Exception stepRule: " + e.getMessage ());
- p; ]0 Y+ D B' p. Y" O8 d }
+ _ W; q1 L* B# t* w4 U0 j* D+ V' _
try {
}! M' Y! p M' i! a' l Heatbug proto = (Heatbug) heatbugList.get (0);( c! j4 k% a- `' f, u* P c: B
Selector sel = 9 Y0 M1 S# l6 Z; U& n8 }
new Selector (proto.getClass (), "heatbugStep", false);
6 f, o9 {/ ?9 _: h. I7 L actionForEach =
) Y3 n' F7 y# h modelActions.createFActionForEachHomogeneous$call
9 Q7 ^4 |! q# a# ^+ M1 { (heatbugList,' T3 O9 I+ S6 `$ V1 d' W- s* f0 }
new FCallImpl (this, proto, sel,9 ~ u4 }& N* e8 f' E' i' y
new FArgumentsImpl (this, sel)));" h2 [, y- {; a
} catch (Exception e) {6 A* P" K3 H- J- C7 Q5 J) K$ E. b- B
e.printStackTrace (System.err);
/ @0 e$ L9 l, k }, G3 v& y0 X! r. Y7 D
9 K0 Z8 {3 h% P) \* m. e1 F syncUpdateOrder ();3 r( E- _) f9 k& G- `$ t
/ }1 Q: G) z9 R5 @$ d! q try {. q0 q% a, X# W* K# u0 }
modelActions.createActionTo$message
6 W9 m0 h5 W& ~/ W) B' e (heat, new Selector (heat.getClass (), "updateLattice", false));' D4 ^9 D4 Z8 S: ^6 z9 T
} catch (Exception e) {
. ?' @% Z+ T4 Y1 n! N# } System.err.println("Exception updateLattice: " + e.getMessage ());
4 }8 ~: q. _) r% o }2 j; _7 i- _ A+ K% S
& Q* R' S: x9 J3 }& ~" S
// Then we create a schedule that executes the
( v* }$ q. p4 ~+ Q* F, W // modelActions. modelActions is an ActionGroup, by itself it
( T# _6 |8 S4 c7 s' Y; I5 N+ w% A' \ // has no notion of time. In order to have it executed in
) S- o8 ~. K2 E9 ?: u+ f9 W5 v$ L // time, we create a Schedule that says to use the5 H7 h9 P- k+ L: F5 l7 N
// modelActions ActionGroup at particular times. This
0 _; ^3 T. k' B. Y" j+ Z; N // schedule has a repeat interval of 1, it will loop every
7 S! V6 k' a7 r/ G0 `1 D // time step. The action is executed at time 0 relative to: o! q. a4 \ O( N2 N8 d9 M
// the beginning of the loop.
# u5 n: X5 v D2 q0 _9 r# D. D J/ M
// This is a simple schedule, with only one action that is5 P6 i. _+ c( ]5 e. ~" a- {& {" C
// just repeated every time. See jmousetrap for more
8 q( v3 f5 x, u6 T* c% [' h1 C // complicated schedules.; @) t& x* w! D9 c+ l
) U5 R0 s% E8 a; G- {% V" |# [- x1 g# w modelSchedule = new ScheduleImpl (getZone (), 1);
. u' L0 q4 J& R& Z+ f modelSchedule.at$createAction (0, modelActions);
2 {& s. [* v; \" c & q6 }8 {; R+ |$ ^
return this;
6 l) l2 N7 }* L" b, X } |