HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* c% I+ o9 a6 V7 m8 z5 O2 ~* ]% A
' [& Y8 j0 {" Y! _7 ?+ s
public Object buildActions () {
4 h: x: K6 Y2 u# p) o$ X( ~ super.buildActions();) r! Y. ~' A( }. z% B2 v# h
4 Q" D, l* d/ z m // Create the list of simulation actions. We put these in
% {$ q- |* U9 H // an action group, because we want these actions to be9 R, \- v4 I' G- E) T5 E. W r! A( D- |
// executed in a specific order, but these steps should
f% B3 Y1 u! B: P* d // take no (simulated) time. The M(foo) means "The message
2 ^$ [2 @6 k a; O& j5 A, |0 h // called <foo>". You can send a message To a particular+ [2 p9 [! }* @/ u# {
// object, or ForEach object in a collection.6 c+ O" \( ?% c: w
% L) r0 g0 M" e7 ^9 u
// Note we update the heatspace in two phases: first run
* J2 K' I' j! \* l i7 B2 w // diffusion, then run "updateWorld" to actually enact the/ ?% [/ l/ w5 l
// changes the heatbugs have made. The ordering here is) N& m ?! r9 t2 h2 }
// significant!
8 S8 J) a: o( ?1 [. j ; O+ I- J) ?3 R; r+ ?& t2 |" m
// Note also, that with the additional& K% w& g# ~, U& ]8 F/ z7 e5 X
// `randomizeHeatbugUpdateOrder' Boolean flag we can& D; j/ O9 B, y! }& }5 R* V" ~0 g
// randomize the order in which the bugs actually run3 L( C2 G# R& f
// their step rule. This has the effect of removing any
' {- F" X# f1 A7 `' N // systematic bias in the iteration throught the heatbug& r7 W& Z1 R, i! l
// list from timestep to timestep7 ?( Q6 Z" o1 ~5 x4 H
: \2 h) |* B- T
// By default, all `createActionForEach' modelActions have8 N+ v" L3 T0 k, l* ?3 w/ e
// a default order of `Sequential', which means that the
9 ]* z. \/ b0 C4 w) w# V // order of iteration through the `heatbugList' will be
+ t \, p7 T3 L9 j0 g" } // identical (assuming the list order is not changed
7 ?; U+ A5 L* a" w( H // indirectly by some other process).* K T9 q2 j8 L* K% Q" \. ]
2 U8 P- R5 | I$ s. k/ y8 I
modelActions = new ActionGroupImpl (getZone ());+ S7 D% O' [: g$ P. e
" I# d; |6 k! I# [2 _ try {
' W* {$ }' J( {1 O8 f$ y) u modelActions.createActionTo$message
6 H, n! { @2 A. P (heat, new Selector (heat.getClass (), "stepRule", false));5 C! y1 X1 I. e8 Y4 f
} catch (Exception e) {
) z) Q( _4 i: C( B! H) i( S' \2 v% H System.err.println ("Exception stepRule: " + e.getMessage ());* U, M7 C. q4 o. ^( P
}6 q: |6 Z% b7 C$ d
& ?, v) o' S4 o+ _4 r try {1 n6 I& h G8 P
Heatbug proto = (Heatbug) heatbugList.get (0);. \3 S2 M1 N8 S6 x2 V; N- B
Selector sel = * M9 b4 ?' a: l9 ?3 L
new Selector (proto.getClass (), "heatbugStep", false);* A# `8 l) n4 \) b5 K
actionForEach = A, H# o B+ B
modelActions.createFActionForEachHomogeneous$call( U( A3 y3 ?% }
(heatbugList,2 Z4 e8 \+ }7 p; `3 i
new FCallImpl (this, proto, sel,5 t1 ]4 m9 W$ `) \/ a/ h" e
new FArgumentsImpl (this, sel)));
% ~7 p+ C5 ]% u; j" r( k } catch (Exception e) {& T9 } p) M; J; G8 ~0 S
e.printStackTrace (System.err);9 I2 ~$ V% s9 s. M
}
- x& }5 u9 z4 i1 P/ K- b X# O
9 V3 |) l$ M6 O( m9 a0 E syncUpdateOrder ();
/ x' f4 G$ r' a
! ]8 V$ {' {( |' _ try {8 v* v# P1 M. ]! N
modelActions.createActionTo$message
9 J+ i, G& o+ J) i3 M, F+ w (heat, new Selector (heat.getClass (), "updateLattice", false));3 }) U9 a3 {* Z/ C% K
} catch (Exception e) {- q0 w c( t: T, H1 K% {, }6 T
System.err.println("Exception updateLattice: " + e.getMessage ());
- n+ }7 k( m- \/ T- g }( K# g) r: v+ l$ I, Y9 v/ }1 `# f8 v. `
& l0 G; v+ G' k% Z q7 J7 t // Then we create a schedule that executes the+ [& I7 G2 Z: N; ^3 b" W$ R
// modelActions. modelActions is an ActionGroup, by itself it/ T9 x1 e' b, S% X+ M1 j1 J
// has no notion of time. In order to have it executed in0 u: X1 ? V: q/ a
// time, we create a Schedule that says to use the
3 h) S D3 v* Z d, q% V0 k4 h# D, i2 M: L // modelActions ActionGroup at particular times. This
* ?" w8 X3 A3 e // schedule has a repeat interval of 1, it will loop every
" j7 H4 g9 ~9 |$ H! f, e // time step. The action is executed at time 0 relative to
: V) u0 H3 _: T4 D) v // the beginning of the loop.6 n+ N/ i; k: l
+ {% |! B5 A" ~" m6 g" s
// This is a simple schedule, with only one action that is: N, a7 }5 h. B5 T7 b& y( E
// just repeated every time. See jmousetrap for more; |- U, t$ {: k* Q: z0 ]
// complicated schedules.
' J8 r8 @0 e) r' E# o8 ^/ l5 B
; Q4 h8 c( \5 u" P$ y modelSchedule = new ScheduleImpl (getZone (), 1);
7 N8 [, `/ Q, b) Y5 Y5 C modelSchedule.at$createAction (0, modelActions);, S6 H9 }, r! D+ r3 N3 p$ I
2 D, ]4 ?: m7 U. i
return this;: u6 S: M3 h& x' v0 }
} |