HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
; d5 {# h8 m1 H( n9 l9 Q; z5 h9 b/ T6 c* w: z/ M2 S( J6 P- u9 }2 c
public Object buildActions () {
1 N7 E; n& R# Y$ M% D' O super.buildActions();
! {8 K- A" K. k# e7 f7 |! V6 T7 r * ?" W: F' q+ X
// Create the list of simulation actions. We put these in
& K/ \+ `: N9 P8 y2 a // an action group, because we want these actions to be& X- _9 G; ^' d- I: i/ D& [- A( p
// executed in a specific order, but these steps should; l* Z7 K% P2 |# t4 D2 V
// take no (simulated) time. The M(foo) means "The message
6 P0 G9 N$ K+ s; C$ S! @" F. b // called <foo>". You can send a message To a particular9 T- d4 X, T0 Y( ~$ u" O
// object, or ForEach object in a collection.
2 {( j5 @& q3 ^% ~4 T/ Q7 i( y4 b 3 O; r' ?# }) r
// Note we update the heatspace in two phases: first run6 `" A$ \+ ^) A# a1 N
// diffusion, then run "updateWorld" to actually enact the2 C: M X* V' k {: C, v8 U4 Z1 U: [
// changes the heatbugs have made. The ordering here is
5 f+ m/ J6 k7 L9 J& V // significant!
- \. |, @) D; V4 ]+ {" @" ?% P 9 h" w9 O6 b" }$ ]
// Note also, that with the additional6 A+ i; ~) a; x
// `randomizeHeatbugUpdateOrder' Boolean flag we can6 h( W j: F( e3 I: S; l$ Z: }
// randomize the order in which the bugs actually run
* H. F4 i0 r; F; R0 { // their step rule. This has the effect of removing any
3 J$ P9 [+ E3 V9 s) U ~1 ^ // systematic bias in the iteration throught the heatbug+ a* d+ H7 {0 D% ^9 @8 [
// list from timestep to timestep
: U2 h( O }& `5 l, ~
; s7 v: s" A- h8 H0 [+ [! L( G // By default, all `createActionForEach' modelActions have
# v0 h# \9 n0 F6 l; y6 ?! [$ p0 D8 @ // a default order of `Sequential', which means that the+ \( |* _+ L' \3 Z
// order of iteration through the `heatbugList' will be1 ^) s$ ?+ k. _$ E
// identical (assuming the list order is not changed
9 o* a- W* D9 k. O: f // indirectly by some other process).3 O; P( m7 r5 u
$ `$ ~: Z% R# Q, W, m0 o modelActions = new ActionGroupImpl (getZone ());$ q( m% G+ B8 V p( i& H0 K
' p! ^" f. j! m3 w+ B try {
5 J1 M% _" o; k( d9 S3 ~1 [- z modelActions.createActionTo$message
, k* [1 z; P ^/ Q (heat, new Selector (heat.getClass (), "stepRule", false));
* F1 L: i/ f h" _ } catch (Exception e) {
: `4 ~: W5 C2 y. E System.err.println ("Exception stepRule: " + e.getMessage ());
/ p- K* Z+ U! I k8 D0 ?$ V" d9 L }
, Q3 Z* L% g% R3 p& x( d* s# B& k
. [( s! I- e) B/ t; B# `- C+ H try {
$ r( a& D8 n7 Q/ a4 F Heatbug proto = (Heatbug) heatbugList.get (0);
7 J7 O& ~" `0 ~' K: m Selector sel = 3 l5 s. E$ ~# }' }
new Selector (proto.getClass (), "heatbugStep", false);
n$ r$ [8 m5 [: t actionForEach =- a7 F6 V$ p! j9 g( S
modelActions.createFActionForEachHomogeneous$call
& ?. d, T7 G3 f (heatbugList,
- A" L. B3 S. N6 A1 t3 G new FCallImpl (this, proto, sel,7 d( J7 j* @$ X" F
new FArgumentsImpl (this, sel)));
4 V) \$ K5 K) w } catch (Exception e) {
9 c* q9 X; X1 w0 y; X# u+ v2 Q e.printStackTrace (System.err);
3 ^0 e. B' d O# {1 H" t }( W, H: ~7 O& G$ i- T4 z2 c
( D/ v$ {5 r, a3 O syncUpdateOrder ();
0 b# L i" A; a p) P
: Y% q! L; q" f5 C1 i! j) r: H try {. P3 f* U; |6 ~: g
modelActions.createActionTo$message 4 j$ I8 }' p. E- j" t& F2 D
(heat, new Selector (heat.getClass (), "updateLattice", false));
5 Y0 b4 H2 C2 F+ J9 U! S4 o1 W } catch (Exception e) {( U6 y8 M& m& z0 I5 `
System.err.println("Exception updateLattice: " + e.getMessage ());& w o* z6 a) k) i+ v; }
}
/ u" |, O' }3 b" V# z' a/ J
" @1 T: c1 k6 C$ P1 Q% T // Then we create a schedule that executes the
8 q0 m9 P: i4 e* L* t% m // modelActions. modelActions is an ActionGroup, by itself it0 F; c; N! l$ |% m( N. Q4 L8 I
// has no notion of time. In order to have it executed in
, i/ I% ?/ ?! x& | // time, we create a Schedule that says to use the
! l/ ^% n3 ?7 k8 p // modelActions ActionGroup at particular times. This# Z3 Z# Z3 @/ k6 o+ `+ o
// schedule has a repeat interval of 1, it will loop every# T+ g% k7 A3 m; A) Q E
// time step. The action is executed at time 0 relative to
7 _: l- T2 y7 k$ H0 A // the beginning of the loop.
, h9 ~5 L9 }" f* ^
5 i+ h) J* J& {( I // This is a simple schedule, with only one action that is
4 x% H* ~2 z. n: i! v; `) @ // just repeated every time. See jmousetrap for more
; I* Q2 x& Q" c7 v- z% x // complicated schedules.
% X4 v9 \. ^; A/ V5 _5 E A3 v$ n. ~8 S( J* V# j
modelSchedule = new ScheduleImpl (getZone (), 1);( p8 h* I& V. F1 A% @4 P6 i/ Z( p/ u: u
modelSchedule.at$createAction (0, modelActions);
N! Y7 `9 g1 }! a/ `0 b
a) P/ o! X( l; Y- V. w: c return this;
& C- R! v' `# z N e; m } |