HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 v4 q- t8 ]! \8 m# Q4 F% V7 B+ Y3 u9 B
public Object buildActions () {
& P0 W) @# P k) e) t' L4 @5 d super.buildActions();
7 Q$ h2 R7 ]4 p2 w: m! e ~ 1 s( p/ a, ?( z# U8 v1 p
// Create the list of simulation actions. We put these in
. T ~ f# p: _1 x& f+ M // an action group, because we want these actions to be
3 b, e3 x' Q6 w- A; ] // executed in a specific order, but these steps should
1 U3 P- N; {; U) r // take no (simulated) time. The M(foo) means "The message2 c& h) C" k0 v
// called <foo>". You can send a message To a particular" Z: W0 V- I: Z
// object, or ForEach object in a collection.
. i$ n8 e- O, k . {# C: \2 S- u1 j* u, v& K! m
// Note we update the heatspace in two phases: first run; G' Q7 Z& i; J7 `, m
// diffusion, then run "updateWorld" to actually enact the, @; j) V+ t E7 q8 I: p8 u
// changes the heatbugs have made. The ordering here is
# y) }- S. ?: P" [) t" n, V- I4 W // significant!
% [0 K% \- w, S+ G2 C8 H. }5 Q
* f+ ?$ _9 _# E i' X) x; u // Note also, that with the additional
1 [8 Y4 d& l9 |% I" Q# y // `randomizeHeatbugUpdateOrder' Boolean flag we can3 j- U+ D5 A1 x9 T3 P! b
// randomize the order in which the bugs actually run
2 t$ K6 o4 W6 G9 J {) j4 ?8 Y7 j // their step rule. This has the effect of removing any
- O, ^( z" } Q( A* _4 P // systematic bias in the iteration throught the heatbug6 ~( P% [* f6 z6 U% c
// list from timestep to timestep3 \6 H5 l( R6 I; a
* K2 c. Z& h+ o H
// By default, all `createActionForEach' modelActions have
5 t* k1 J5 K9 z // a default order of `Sequential', which means that the
! ~" v5 [- y. n$ k& b // order of iteration through the `heatbugList' will be$ ], W; X9 K4 H
// identical (assuming the list order is not changed) N7 v. ^$ {+ f5 z8 I6 ^2 v
// indirectly by some other process).6 W! `, r6 [( P4 u# w, o
* C0 @, u F) s" c) P/ _
modelActions = new ActionGroupImpl (getZone ());
; L* i/ G9 T9 m( V: ? _1 {' {4 A
1 S' B( M: I8 O- D3 ]9 L4 I- l try {% i0 E: i# l; `. ~3 \/ j
modelActions.createActionTo$message
; a3 W6 U, _0 p (heat, new Selector (heat.getClass (), "stepRule", false));2 W# N3 k2 s) ?/ r( N- Z5 T
} catch (Exception e) {
6 \3 i% u' S" W* t" L! q# q System.err.println ("Exception stepRule: " + e.getMessage ());
# U4 [" v) B/ x' T L; i, W1 i- }: V }, h% t7 n# w" e% H9 J
3 N! G0 K! E7 P" s, G
try {
0 R5 B E/ n5 e l9 t' O Heatbug proto = (Heatbug) heatbugList.get (0);
8 c' x9 o6 Z# [: H% I. ^ Selector sel = . A6 p+ s3 w) ]" w; r* p5 W
new Selector (proto.getClass (), "heatbugStep", false);
. \5 k7 X `) k# P7 d- n actionForEach =
! \3 ?+ P2 n7 c modelActions.createFActionForEachHomogeneous$call" L& ^- l" T( l+ k. N
(heatbugList,# z2 l7 l2 c- ^/ V& G) ^4 a* j" O3 {
new FCallImpl (this, proto, sel,
n0 T2 T3 F% l/ z m new FArgumentsImpl (this, sel)));& f8 z- k5 h2 f
} catch (Exception e) {
! v5 Q+ O f1 j) _ e.printStackTrace (System.err);
$ z5 V) q5 c: ^4 m1 B8 j0 U }% N# l* y" {3 `- A- [; Q4 d
5 G: M4 Z( l' N/ O) X1 J
syncUpdateOrder ();
' k$ U% o( h! o) w
2 M6 W, b' Q- s( ^% ?/ x4 l. ~ try {
: |. D7 L2 i4 n6 E1 k/ s modelActions.createActionTo$message : M. @# ~2 W& |( q
(heat, new Selector (heat.getClass (), "updateLattice", false));
8 H% F7 \ ]7 T, j } catch (Exception e) {) g) R; x) f2 S" [2 p
System.err.println("Exception updateLattice: " + e.getMessage ());
; v( O) H" ~! F, f1 m }
$ t6 V: R4 N0 g W# D E
i" r6 S/ j0 z // Then we create a schedule that executes the
3 F0 z, u7 g& k) m& @ // modelActions. modelActions is an ActionGroup, by itself it
* i% k( m& g3 X% I7 E // has no notion of time. In order to have it executed in6 v4 ~7 t" n6 }' r
// time, we create a Schedule that says to use the5 f6 b% |1 b( g6 E% r4 `( l
// modelActions ActionGroup at particular times. This
9 ]5 B3 J1 @- U F; c // schedule has a repeat interval of 1, it will loop every
7 n4 J0 |0 d9 X3 s6 c // time step. The action is executed at time 0 relative to
8 i" p% i! H6 e! k/ B: e // the beginning of the loop.
5 \2 z+ r+ {+ a- C* ?7 `- |* q7 r
// This is a simple schedule, with only one action that is
% {6 x; r9 K0 E0 X/ V // just repeated every time. See jmousetrap for more
8 H, I, b0 Y8 G0 K1 z; N- [ // complicated schedules.
' m+ q' }* J P0 q3 z: l ! d' ~- _3 U1 ?* {
modelSchedule = new ScheduleImpl (getZone (), 1);, }! v& s- h1 W3 F3 n# Q' P* q
modelSchedule.at$createAction (0, modelActions);
& x7 c" Q2 g* S0 }8 U2 M 7 m& b- |0 o& C
return this;
+ `6 y& l; y& W6 a } |