HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# [1 u5 O9 u4 O+ {) O: ~2 N/ ]
) ]! p3 s% K4 d public Object buildActions () {
) T4 k% o7 S2 v" X; z! ^ super.buildActions();- T0 L% y b/ x! [' B
: X/ f9 w- z/ o- |6 m
// Create the list of simulation actions. We put these in! x* r. l! M* ?/ H9 q3 i
// an action group, because we want these actions to be
" M' f! R0 g6 l, ~8 K& a // executed in a specific order, but these steps should
; N6 r1 ^7 ? V# e( C // take no (simulated) time. The M(foo) means "The message5 h" I' H8 @% U6 H6 T
// called <foo>". You can send a message To a particular- |+ D& l8 J/ ?7 q8 ]: y
// object, or ForEach object in a collection.
! e5 b* C5 Q4 u# S+ L 7 F: v, A/ f. Q/ j$ f
// Note we update the heatspace in two phases: first run/ [/ d- m# P0 ?6 b& k U) e2 t
// diffusion, then run "updateWorld" to actually enact the9 f( N) ^# ?+ g; L
// changes the heatbugs have made. The ordering here is
# Q' X4 k3 L& k& H // significant!
) n }" r. C9 |1 p1 P M5 |
9 J% U, d1 k4 j1 Q3 n" X, b // Note also, that with the additional
6 E% ^3 _/ f3 I: V5 i8 |4 V) }! k0 n // `randomizeHeatbugUpdateOrder' Boolean flag we can( C5 j1 O2 T8 B1 x, Q: S, _
// randomize the order in which the bugs actually run
& R" V" d* {9 @/ U/ ?4 T7 U- B# V // their step rule. This has the effect of removing any
! i N$ b1 `3 w- s( g1 m/ a // systematic bias in the iteration throught the heatbug7 Y+ N/ B( q8 t4 K7 V
// list from timestep to timestep
) _% N8 ^6 B3 } B 5 q, J4 @* y9 R
// By default, all `createActionForEach' modelActions have
! o! r) j' I5 [$ J" F( l) s // a default order of `Sequential', which means that the' c4 z$ L8 K, H+ i3 w
// order of iteration through the `heatbugList' will be/ d+ }/ C* E( H8 y% G0 N; @1 q
// identical (assuming the list order is not changed
1 _2 j+ e3 Z( \ // indirectly by some other process).
( E$ D% u' X' Y& w
) v$ _8 |7 l* {* `* V% j modelActions = new ActionGroupImpl (getZone ());2 }, I; E' c% O
2 f' }( E3 p: k try {: _% F) k2 `$ m1 G0 N& ]4 x
modelActions.createActionTo$message! x# k1 ]% Q3 f& j/ a
(heat, new Selector (heat.getClass (), "stepRule", false));
. W) l( e8 p0 n" ]& T, {5 x- F5 J } catch (Exception e) {
5 c& J/ E; w) _3 D8 w+ }' I System.err.println ("Exception stepRule: " + e.getMessage ());
7 ~0 U8 `( ^( | }% r+ D& N: I4 H2 }2 @' G
2 @6 Q" a! E. U- | try {
* u- m( H* H+ z! t* G# L Heatbug proto = (Heatbug) heatbugList.get (0);! a6 n! g' \$ L. |9 [
Selector sel =
( s( b. {% X2 [4 U new Selector (proto.getClass (), "heatbugStep", false);
9 I/ Z0 D$ f- V; ] actionForEach =3 d/ E! a" T$ W
modelActions.createFActionForEachHomogeneous$call x( ^9 K6 k! I. D" X
(heatbugList,
4 x; D& R) q; m# g/ A/ s4 O% C new FCallImpl (this, proto, sel,3 g9 q* R5 [8 R0 _& u
new FArgumentsImpl (this, sel)));8 A$ Q5 \, Z* ]2 v) H
} catch (Exception e) {* j' `# a# W4 Q" X9 P( b
e.printStackTrace (System.err);
I" `2 M( U( F }. f( Q( \, I' N; Y. C; U/ H
! n1 m7 [" |# M1 Q syncUpdateOrder ();
4 N* z" T3 ^4 R3 Q( k
' k, \' N7 M" @5 V: m* T try {
7 V0 C$ Y& z! v b0 l4 Q: Q modelActions.createActionTo$message
, w2 x" U) M m (heat, new Selector (heat.getClass (), "updateLattice", false));6 a" f$ F# e' n x& Z" T
} catch (Exception e) {7 j7 l; P' E2 A$ T8 t
System.err.println("Exception updateLattice: " + e.getMessage ());
, X) [8 O2 {% _# c; x+ i: X }
% P+ C, U, h7 J , y8 N+ H! O0 L$ [
// Then we create a schedule that executes the
( s$ A2 `- }9 f* f$ } // modelActions. modelActions is an ActionGroup, by itself it: L* J' {' [1 x* s' Z6 z
// has no notion of time. In order to have it executed in/ V& K8 n0 B7 i
// time, we create a Schedule that says to use the% a/ Z. z+ J7 v
// modelActions ActionGroup at particular times. This% L' `8 Q+ J& E
// schedule has a repeat interval of 1, it will loop every
6 w4 W9 n4 B3 w, C // time step. The action is executed at time 0 relative to1 F6 C! a. ~& d
// the beginning of the loop.
. _8 l+ h% h1 x3 U, L8 b# F) Y
1 l* |2 B. ~( x6 J4 Q // This is a simple schedule, with only one action that is, [0 J* n. p4 D
// just repeated every time. See jmousetrap for more
& u! y* T9 s5 A/ o& e // complicated schedules.
& u3 Y! p- C' x1 ]' L9 i
- T$ r C4 l* g modelSchedule = new ScheduleImpl (getZone (), 1);* a* G2 \5 H. V: [5 o5 w- \! K4 W
modelSchedule.at$createAction (0, modelActions);+ ]& x+ t2 k- [) T/ s
; [, J4 w6 k# R: `8 z3 N6 Y return this;
! ^ o& e6 `& }, Q' |: Y7 {6 R } |