HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# p- p1 |- G1 g
7 M9 G' p' Q6 R4 R* U8 @ public Object buildActions () {0 @" F6 h6 P; ~
super.buildActions();
( C F3 n0 r, N5 i9 ^* V% h4 H5 f6 ~ ' t$ E7 L z9 S T
// Create the list of simulation actions. We put these in2 F3 `; w$ T3 T+ Y
// an action group, because we want these actions to be2 e. P+ v, G4 k7 w
// executed in a specific order, but these steps should
) \: d7 M& c- Z* M3 T3 ~' f! V // take no (simulated) time. The M(foo) means "The message
6 B" Y1 N6 l; g# N // called <foo>". You can send a message To a particular l+ W+ J* t5 e) z% R( I6 l
// object, or ForEach object in a collection.
1 w: y; S. A: D" ?$ w! J! h3 R
- I* c$ h, A9 z# w6 d/ g // Note we update the heatspace in two phases: first run. G: l& Z: |3 \% i, q
// diffusion, then run "updateWorld" to actually enact the2 ` l5 I* r: \+ N. n% Y! p
// changes the heatbugs have made. The ordering here is
' u5 p- B- i$ R# I // significant!
" g; U' M( Z# m7 \, K 5 M! K+ L4 i* o' N
// Note also, that with the additional
) O$ k, f1 V& `/ z // `randomizeHeatbugUpdateOrder' Boolean flag we can; \* E. ^4 o* V G) ]5 }' s* A
// randomize the order in which the bugs actually run, Q! z, D2 p- X! m3 @' Q9 o
// their step rule. This has the effect of removing any
; }6 L0 Y$ Y# I) b/ V. M4 [ // systematic bias in the iteration throught the heatbug0 M% V0 B& k% x, A0 R
// list from timestep to timestep+ i3 M) s. Y, N4 R* |
f$ l7 X% v# [0 s" i& t, b( C n
// By default, all `createActionForEach' modelActions have
' c1 w; w6 b- B1 H0 N, Q2 a // a default order of `Sequential', which means that the, x9 E9 c7 V0 U+ A* I: p
// order of iteration through the `heatbugList' will be
9 d* F1 v9 n5 b+ E // identical (assuming the list order is not changed
& S! U0 R& F4 G( w4 i) w6 Q // indirectly by some other process).
& V8 V( H% D* O5 D" R1 @: A
+ O/ \8 c+ b6 K4 ]- ?8 G modelActions = new ActionGroupImpl (getZone ());% _ z, P0 O- C* ^8 {+ j
3 c. Z8 m4 ?2 {9 r6 n/ t! N try {
2 q8 Z6 O6 W+ x) \ modelActions.createActionTo$message
- W8 z T+ L& a; ? (heat, new Selector (heat.getClass (), "stepRule", false));
6 O$ n: |$ z/ Y1 k } catch (Exception e) {3 L0 i# i1 q/ \; V# Q
System.err.println ("Exception stepRule: " + e.getMessage ());
! e% Z O. h! W* b2 X }
o X" M# ?* X7 Q4 U% ?: x5 K0 C
try {' ` Q, ~( Z. J) B; `* v: {) Q- ]
Heatbug proto = (Heatbug) heatbugList.get (0);9 ^5 F3 b% n1 n, V7 X9 }
Selector sel =
j+ Q; e. E! {$ [4 ^9 P; ] new Selector (proto.getClass (), "heatbugStep", false);
4 O# L' f; }4 g actionForEach =
( A% n# b# g& z3 o6 p, m modelActions.createFActionForEachHomogeneous$call
6 [) E4 |' Y$ [! M* y (heatbugList,/ H+ _& t. E5 W& \
new FCallImpl (this, proto, sel,/ t$ @+ }: p( H) C
new FArgumentsImpl (this, sel)));
+ i' J- Y0 J# w } catch (Exception e) {
( ~% c* Q$ ~) Y' W3 P0 B' T7 G1 D) [" | e.printStackTrace (System.err);
' T% D7 G) I+ r. Y0 {& w }
/ c# U- y$ j% ~" I ?9 o
' H$ q. @4 s$ k7 V8 K e syncUpdateOrder ();
; F0 K% \3 d+ f) e- ^) T: S& e% ]0 a1 E5 n( C% v: t
try {5 ^* n3 d% [* J: Q) P5 `" t
modelActions.createActionTo$message 1 s2 U+ I8 a7 m
(heat, new Selector (heat.getClass (), "updateLattice", false));$ Z/ g% a8 X; @) K( t7 r1 T
} catch (Exception e) {( R) o2 ~( k5 Q4 Q6 y
System.err.println("Exception updateLattice: " + e.getMessage ());
* q* J5 I" s; x6 F! i% i1 O }
9 F' r i$ `+ [
! @; s7 F3 O( d" x- C0 }" I1 V4 ~" U7 l9 H // Then we create a schedule that executes the
4 `" \) ?5 D, D0 b // modelActions. modelActions is an ActionGroup, by itself it
1 x6 F& e$ b+ Q; V! E // has no notion of time. In order to have it executed in
: `7 X, u, _" t! l* {/ S // time, we create a Schedule that says to use the- W4 R( L0 A4 \" M1 U
// modelActions ActionGroup at particular times. This
- C" U1 H3 ~8 v9 ], I3 s2 o // schedule has a repeat interval of 1, it will loop every
: x% n6 X' m+ g // time step. The action is executed at time 0 relative to8 ~6 j- ~$ n. U: k, h; ]
// the beginning of the loop.
- N- I3 `) F; z" L7 g% K
( `8 m: `3 s7 D* Y% o5 Z // This is a simple schedule, with only one action that is
! Z) L# G* b* r4 P$ ~7 z) S9 ~ // just repeated every time. See jmousetrap for more
/ v+ e1 B; h+ z/ N7 y) C G- \ // complicated schedules.2 i5 ?) ~& }# y! f3 f
, o3 {* R* F4 j9 q- ~2 y& a
modelSchedule = new ScheduleImpl (getZone (), 1);
# ]* [! K4 h* d8 h7 t modelSchedule.at$createAction (0, modelActions);& v/ ^8 p- \6 J( Q" m# ~
) v0 O: A! M6 W* ^' k8 f! j% l, P
return this;( c/ f6 ~* S' E
} |