HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ x2 }( Q3 \8 k" m, L% S
7 u/ z( U/ n3 v, V. Q Z: R4 I
public Object buildActions () {/ w, s4 l0 w8 M; } Z, K% P. B ?
super.buildActions();( x: Y9 y6 S: f
. c1 C y" P9 f B3 E // Create the list of simulation actions. We put these in
/ C# a+ l4 [7 K& x1 y' X' C // an action group, because we want these actions to be( J/ v; G i/ a: O# ~ S) E
// executed in a specific order, but these steps should) i( a( m6 q( [: j/ c! A& D
// take no (simulated) time. The M(foo) means "The message
9 t2 j( J1 r7 B' m" K1 D // called <foo>". You can send a message To a particular
- B U* a/ i, g5 T, W) j // object, or ForEach object in a collection.
0 ?( c; F/ X2 t. n+ j
4 p: H* d) x) \6 j, F. G5 K // Note we update the heatspace in two phases: first run
' c0 d: Y6 j( E) S4 U0 b* X // diffusion, then run "updateWorld" to actually enact the
- N: j$ Y) S1 O) `+ ~8 X' p% [ // changes the heatbugs have made. The ordering here is
; ^- A3 f, m$ A9 ^4 {; r // significant!
+ p4 z6 ]+ }! N- V
( I. x+ v4 I% S4 }- |8 z // Note also, that with the additional1 ]2 F) k1 h/ x3 |4 ?
// `randomizeHeatbugUpdateOrder' Boolean flag we can x9 D% |6 ]4 l+ @
// randomize the order in which the bugs actually run. _' Q0 e$ T8 f- n
// their step rule. This has the effect of removing any6 }$ x: R$ I( B; i
// systematic bias in the iteration throught the heatbug
/ p" J9 E6 O9 [+ r/ J0 c // list from timestep to timestep/ x! w# U5 G, t
* s4 D6 ?% Y8 R, l) `
// By default, all `createActionForEach' modelActions have+ l' U- c1 P9 T# U# i8 I7 v
// a default order of `Sequential', which means that the( Z+ t/ i/ q! | T+ X
// order of iteration through the `heatbugList' will be
. ?5 P$ @ [" p( } // identical (assuming the list order is not changed
2 y, y5 Q$ i* ^+ W5 @# [ // indirectly by some other process).' E. W, u; m D+ O1 ?. N
9 @& n. {: ^4 {* H7 x
modelActions = new ActionGroupImpl (getZone ());
$ F' c' V: G y* h# z# v
: D: G2 I; P8 `2 h9 D) k try {
1 r0 f1 ^$ a, n7 F7 e/ X4 \8 e modelActions.createActionTo$message
2 R0 b, z/ d5 ?) q" u (heat, new Selector (heat.getClass (), "stepRule", false));
5 l/ x3 f& o6 E: R9 ] } catch (Exception e) {
4 V3 C6 X4 O- m1 U! p4 S5 z System.err.println ("Exception stepRule: " + e.getMessage ());* E- R5 F7 o3 m
}
n3 d8 h: E$ X# }/ P
& }7 E) |" H: S$ M3 g. I try { {0 p5 W o4 Z- _& u6 d7 ?- a3 J! S
Heatbug proto = (Heatbug) heatbugList.get (0);
7 y5 @ ^ {2 y. J9 R+ {( R Selector sel = ) d) L) A2 b. p m8 X D4 I
new Selector (proto.getClass (), "heatbugStep", false);+ [# f$ f; {1 Q6 Q- ]0 N! P8 f
actionForEach =8 h+ ~- q& ?$ d& Q' t
modelActions.createFActionForEachHomogeneous$call5 A* I5 ] Z( [9 U1 {: r
(heatbugList,
! B' v* I7 n# W9 y8 L4 y new FCallImpl (this, proto, sel,- S7 `; z7 q* Z& \$ l3 u0 E/ f- C0 m( Q
new FArgumentsImpl (this, sel)));$ H# v( D; z5 F6 T; T4 n
} catch (Exception e) {* |- i$ r0 h* g8 d1 C
e.printStackTrace (System.err);
) V$ t4 c6 a2 S/ A. n7 k" m/ C }
' n; j/ f/ m3 L; q2 c" i
: J2 D5 u* w8 [! J syncUpdateOrder ();
( r* W6 E3 e9 T) Z3 Y& c( d1 W4 D) [3 ?/ }+ {7 s5 U) h* h
try {9 G u6 U& w6 l% H- w
modelActions.createActionTo$message
# s4 r2 V7 {4 ]( O3 H; n (heat, new Selector (heat.getClass (), "updateLattice", false));
6 f: K7 ?0 q6 |7 c$ c+ p } catch (Exception e) {2 P. y7 I1 Z& M" i# l- T+ r
System.err.println("Exception updateLattice: " + e.getMessage ());
7 G# w3 I& S) w# ]: E) c; Y A1 e }4 t( b" e- i' |/ ] b1 c
7 w. |7 ]. D% U+ @% w0 X+ F" D // Then we create a schedule that executes the
. M8 V2 a, A2 N2 N0 t/ ? // modelActions. modelActions is an ActionGroup, by itself it
p& Q# G* v( q$ i" a. w5 F6 Y // has no notion of time. In order to have it executed in
2 Z* ]) u2 l6 x1 Y+ y" J+ q# E // time, we create a Schedule that says to use the7 E6 K( d" ~# \/ v% w& L2 c, e
// modelActions ActionGroup at particular times. This
0 S* I6 c7 }. C, E% v, e( e // schedule has a repeat interval of 1, it will loop every& Z8 e! `0 N3 _
// time step. The action is executed at time 0 relative to3 W- X2 T7 i1 P. x1 l
// the beginning of the loop.
7 Q) D7 w0 K; {2 B3 ?, N" N* m$ v
' `7 r9 t" ^. m- S // This is a simple schedule, with only one action that is
2 ~' K& }2 A7 [0 }% Q7 X // just repeated every time. See jmousetrap for more9 \% S; a7 D8 l5 L- w' v
// complicated schedules.# b" T$ Q W* ~( x* f
. W/ m3 C7 s/ X$ v modelSchedule = new ScheduleImpl (getZone (), 1);. D; Q- M. j2 k
modelSchedule.at$createAction (0, modelActions);6 ~& `* [! P5 r+ h* ?0 v8 ^+ i( f' r
' e: n) @6 g! H J return this;
/ |; ], [6 E& L* y) U+ G } |