HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 Y: K: O0 B5 ?& F% k/ o! p
" Z/ e, S. |9 ^( w. E* m8 a3 S
public Object buildActions () {
; q( A: J2 t P6 L super.buildActions();0 w0 O# \2 z* ]9 v, H2 d, i' p
4 b: Z; I( P: ]+ v // Create the list of simulation actions. We put these in
! q+ W$ o4 K0 d0 c5 S // an action group, because we want these actions to be7 t5 m7 a$ y5 ^3 U$ A$ V
// executed in a specific order, but these steps should1 V& n% q2 L% w. ^* R) o# e
// take no (simulated) time. The M(foo) means "The message
, P: x& n. q) f1 n // called <foo>". You can send a message To a particular
- P0 Q1 a2 N3 A$ ~ R2 F // object, or ForEach object in a collection.9 @5 x( W+ v f" g# j
! ]* D, [& T6 `6 |* z' i& f
// Note we update the heatspace in two phases: first run
% ]4 s0 ~8 j, o4 R1 `) t4 T // diffusion, then run "updateWorld" to actually enact the
/ P* E4 L; h0 k; l4 a# b: S // changes the heatbugs have made. The ordering here is
* M$ B; Q2 S, Q/ N' r! o ]0 t // significant!" z, W1 k, T; Z, J
1 |- p; C( p$ z# k) j0 h1 j! `0 J // Note also, that with the additional
3 ~; F R, |. [# P2 h+ w // `randomizeHeatbugUpdateOrder' Boolean flag we can
! W' L+ U/ u- E* y' N8 d; I // randomize the order in which the bugs actually run* @: _: p/ a, I9 S" R- t
// their step rule. This has the effect of removing any- L5 o: b' Q1 I9 U A5 t) l% B: q9 u
// systematic bias in the iteration throught the heatbug: u: H0 \% Z# J" N8 Q
// list from timestep to timestep
~" w6 H/ h- z! Z( A: N ! @6 B) B6 l, C3 n. n4 {; J' }
// By default, all `createActionForEach' modelActions have9 M I6 N; a; i
// a default order of `Sequential', which means that the
. @0 @+ l, Q5 c6 U# A // order of iteration through the `heatbugList' will be( ~8 E) [) E6 b5 y7 ?8 v' W
// identical (assuming the list order is not changed* S. Q$ P7 [. M5 k+ I9 ~2 |
// indirectly by some other process).
& Y! \: R3 g5 b% ~8 k/ J' w1 n % M+ V. t l. E! B" }/ f8 `6 C
modelActions = new ActionGroupImpl (getZone ());' v# |, T! p9 ?9 |) G$ R) f; q4 `
U, U% S" R+ P
try {! E2 B4 o6 b& l; {- \- [- b
modelActions.createActionTo$message" I3 O; C0 E. {6 j* K
(heat, new Selector (heat.getClass (), "stepRule", false));, {4 R( \. _" V5 g
} catch (Exception e) {5 z* X: Z: J% z _/ i5 ^4 X: }( o
System.err.println ("Exception stepRule: " + e.getMessage ());2 l% |$ Q1 _! r+ Z
} e. e- L! _; K
) w4 }+ v& \- F* N8 H% x try {
: I6 C( x& K) x5 U& j1 D6 I Heatbug proto = (Heatbug) heatbugList.get (0);2 {- b# j9 T8 `" H! ^" ~" d
Selector sel =
! l4 R% d+ _( x( X" y) ~' S new Selector (proto.getClass (), "heatbugStep", false);
W# C4 |5 k# g% }3 [ actionForEach =. e/ ^; \; n5 ^9 O2 \, }6 G
modelActions.createFActionForEachHomogeneous$call
9 w0 j$ s: h4 j1 c( y (heatbugList,
* S5 z9 v8 u; m8 i% ~ new FCallImpl (this, proto, sel,: X: C% l5 u& I h5 L$ U
new FArgumentsImpl (this, sel)));+ [1 Y# d' Z: ]- \0 d6 X1 l1 L
} catch (Exception e) {2 R; \; o8 |4 e5 p/ ~) ~2 x* @6 ^
e.printStackTrace (System.err);
8 |4 u2 J& ]3 y/ z3 i$ w }0 g! `9 }" Q( D+ A
) K2 M5 X2 i, a U/ Z
syncUpdateOrder ();, M8 L% k" U; P9 z8 G
! Y9 u1 o9 d- h$ j( r g5 k* [3 T
try {
! i4 B" ~0 V9 h5 u modelActions.createActionTo$message 6 m: X$ p. M* u; w* f8 Z5 _% ^
(heat, new Selector (heat.getClass (), "updateLattice", false));) L" P/ o3 k) m: q3 N
} catch (Exception e) {* h, z% Y5 n, S8 x1 w: \0 z0 s/ q8 B
System.err.println("Exception updateLattice: " + e.getMessage ());/ o2 B( t$ G# q
}
) @5 n/ i8 p) p# M" d; H + F9 p' B9 N! V! [% c
// Then we create a schedule that executes the# n- c2 ^$ S' T" [; S
// modelActions. modelActions is an ActionGroup, by itself it
: {! [% ? g4 W/ ]/ |6 x6 a, G // has no notion of time. In order to have it executed in
2 l I9 ?2 W/ Y // time, we create a Schedule that says to use the) K& f; S7 b$ x" l. l- r
// modelActions ActionGroup at particular times. This- V* M& G; Y; X: N G+ r
// schedule has a repeat interval of 1, it will loop every; N, ^; y& B& H) U0 l7 c
// time step. The action is executed at time 0 relative to
1 a9 |2 q; K7 w/ M. J, P" c9 M- V // the beginning of the loop.! {. U9 F# i' O* z5 v# M% j! B: [
& p) j! D' g! R/ y+ ^
// This is a simple schedule, with only one action that is
$ Z' [( A9 m: Z( E9 P( c // just repeated every time. See jmousetrap for more6 f4 {8 r' |! i0 O1 G5 r6 ~( O
// complicated schedules.
/ Y- U/ C1 @: `& P8 }6 q) v$ c7 P * i* i! {1 T* i+ v
modelSchedule = new ScheduleImpl (getZone (), 1);/ m; J2 _+ z5 }7 ]5 i
modelSchedule.at$createAction (0, modelActions);( N% e. m! s U% n+ O
8 s% C/ h0 a0 C6 ?7 b0 A return this;
" d2 ~, v# u/ P/ ]" M$ a } |