HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:, M* ` G2 `: W) R2 `& ^' x
7 u9 j) Y- x2 @! z; S public Object buildActions () {
# v! o/ V5 b+ b& v4 e% m: d G/ L super.buildActions();
! A+ c9 |" ], G% F5 [+ c) G$ y
1 l6 p* W7 j$ v1 A // Create the list of simulation actions. We put these in
) K; Y$ O. C0 I/ n* Y // an action group, because we want these actions to be& K2 [! S, B/ M# b0 d8 E7 x6 d
// executed in a specific order, but these steps should/ R& M) n+ V: Y. \$ P% n* O
// take no (simulated) time. The M(foo) means "The message
" E8 n/ ]' n; N2 z Q- V // called <foo>". You can send a message To a particular
; V" S8 \# X+ M1 U* a! J, [ // object, or ForEach object in a collection.$ N8 u5 c& f4 R1 F) q; k7 u& D* }
/ r7 d( a2 k0 B% A
// Note we update the heatspace in two phases: first run
& e" @4 L1 q; p& N" V) i y // diffusion, then run "updateWorld" to actually enact the6 Y h3 j2 o4 Q& d: c
// changes the heatbugs have made. The ordering here is
: O& B" H/ {' b5 q: ?/ E% F; y // significant!
# p* ^5 v1 }, q' o / R/ S0 J T/ U& w0 W4 g
// Note also, that with the additional
9 e& C, z( G; m& q2 k3 u // `randomizeHeatbugUpdateOrder' Boolean flag we can# g/ P: W& L' L3 U
// randomize the order in which the bugs actually run
* [# v6 n' I3 s& q+ O. X+ m // their step rule. This has the effect of removing any( A5 ^7 v" s5 w, Q: T7 N
// systematic bias in the iteration throught the heatbug) }$ g3 Y/ d) k' d
// list from timestep to timestep/ K8 V. P- b" F; j+ A6 m
# g; H: j1 ?# g; A( d
// By default, all `createActionForEach' modelActions have
4 X# p4 v- `9 p) B2 I- P, m9 n // a default order of `Sequential', which means that the
3 m) y) `( z/ p( d9 g8 A& M4 K // order of iteration through the `heatbugList' will be- y7 o1 ~0 Z2 p. d7 [$ ^
// identical (assuming the list order is not changed
$ }/ f; j# Y' q, g9 e. m4 n // indirectly by some other process).
, v! D* S8 @+ ?3 X8 q9 I & I$ {9 [1 |$ i% w* U
modelActions = new ActionGroupImpl (getZone ());
6 R4 d- M4 j% e8 A; z- w( G
( g2 I* L' e8 V5 Y: g+ O! \ try {
D1 z: i& h2 w. a modelActions.createActionTo$message
# Z: n! c. H w& C8 j (heat, new Selector (heat.getClass (), "stepRule", false));
5 Y m7 J% a1 Q8 [# M } catch (Exception e) {# m" w2 u( v' W' |
System.err.println ("Exception stepRule: " + e.getMessage ());
) w) s7 C% ^9 l9 @ a: { }
s/ z# G2 _# s
: j* ?5 D5 Q+ ?5 d" l try {
/ I2 Z4 f& z9 Q2 X8 v/ J+ Y( A6 r. P0 o Heatbug proto = (Heatbug) heatbugList.get (0);9 w V* Y) h: t6 i
Selector sel =
% u" z" K" p+ y. d8 \9 |. x) j new Selector (proto.getClass (), "heatbugStep", false);/ S, |# K t7 w& j e) m/ T- W1 b
actionForEach =$ Q' m3 {0 U$ e' n2 M0 \$ l+ r
modelActions.createFActionForEachHomogeneous$call
. o! `5 i' L; n3 V9 t (heatbugList,* W) D) I2 N) `, F, X' d1 \+ Z5 P
new FCallImpl (this, proto, sel,
/ h/ T3 x. \0 d) `# Q6 k3 L new FArgumentsImpl (this, sel)));# `7 L5 Y0 Q1 o7 w! _( T* q1 i
} catch (Exception e) {5 K1 z/ R# E. \1 @& }. g3 t' D
e.printStackTrace (System.err);
. [& d* p, `; p2 c }) Z- C3 i9 x9 W* x X9 ^5 Y6 d
# F: ^2 a, e. \ syncUpdateOrder ();
5 }) l. d; r; v& {4 |
( R% |4 Z4 W0 ~0 q try {6 f. v2 Q8 a) f9 C+ _% g" _
modelActions.createActionTo$message
, N. H K+ k: ?3 O8 a, ^$ x; } (heat, new Selector (heat.getClass (), "updateLattice", false));
4 ]1 T4 s( r* ]( C% z5 T7 N } catch (Exception e) {
7 z9 K2 O+ T. }$ _, ?" _+ b System.err.println("Exception updateLattice: " + e.getMessage ());- w7 `: K! ]- Y/ I/ l+ J6 U! P) l: t1 {
}- c" G$ J) K8 h7 U1 a
& B1 d, J# n9 N0 r // Then we create a schedule that executes the2 P3 s; U9 ]$ u- J# K
// modelActions. modelActions is an ActionGroup, by itself it
: k5 b. T, h7 |; T; e! V& i3 B // has no notion of time. In order to have it executed in* J; f( G% _% z* y: U" _! _
// time, we create a Schedule that says to use the& T* E. I4 B7 J3 v, s
// modelActions ActionGroup at particular times. This
7 a7 W" m* B- Y8 z // schedule has a repeat interval of 1, it will loop every/ I& {6 i! j" ?2 y {- M
// time step. The action is executed at time 0 relative to0 Q! ]) e' n+ R2 Q a" t3 Q5 v6 P
// the beginning of the loop.9 K( g6 V. r3 l& q. s- V
1 T, Q$ @/ i% e( u3 k4 Q( M // This is a simple schedule, with only one action that is" T8 C. |- U1 s
// just repeated every time. See jmousetrap for more N+ [: e' V) S0 x `! ?! G% [' e
// complicated schedules.
2 }' ~1 S( H( p' ]
+ z$ L6 \- n# E; E9 P/ v) L0 W modelSchedule = new ScheduleImpl (getZone (), 1);
|! M, j" T# z4 H! u) c modelSchedule.at$createAction (0, modelActions);
" |* P( L! k# z. A& I$ `3 H 2 \4 C9 G/ }3 p- B
return this;
9 i! V4 X4 `, X$ e( d+ @* ?( N9 w } |