HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:( d4 k) V( q f
1 p8 o( C; V% _3 z# A$ v( A! n' r public Object buildActions () {
' o( l6 {% T% X y+ E8 o$ H super.buildActions();9 j8 s% N8 M$ u3 J, n2 N
: O6 m% |6 n) I" P2 D9 A // Create the list of simulation actions. We put these in- M; {! K) u4 `$ p4 T# i# I5 D9 D
// an action group, because we want these actions to be; }% y$ j# }3 d; Z& s2 [, @ a. a& Y
// executed in a specific order, but these steps should D$ I& `: i/ G% f' U9 [6 n! \% p$ x
// take no (simulated) time. The M(foo) means "The message3 `1 `. ]- O, f& ?4 u
// called <foo>". You can send a message To a particular
- c" {! H t1 }' {( @4 \5 `" b+ ^ // object, or ForEach object in a collection.) S. V+ o7 a$ O! B; w% O% ]' H
* a) w3 T6 Z; c9 T' O // Note we update the heatspace in two phases: first run" Y0 w9 X$ K: [2 X
// diffusion, then run "updateWorld" to actually enact the
* @: n# a& i7 w // changes the heatbugs have made. The ordering here is
, }2 F( M, o, k0 J // significant!& C3 q4 D1 `+ I6 J7 w5 z3 q
, R9 B9 G5 w2 X
// Note also, that with the additional) H) \$ a0 s M4 |# _
// `randomizeHeatbugUpdateOrder' Boolean flag we can, e1 Q7 F$ t$ Q& J8 E: \/ X; j
// randomize the order in which the bugs actually run
" J B% `7 x$ v- z; T+ m" x // their step rule. This has the effect of removing any
0 |+ D0 d3 |' S0 G5 |( _7 I0 t0 Y! { // systematic bias in the iteration throught the heatbug
+ `3 J6 D/ P0 D F // list from timestep to timestep
( [2 @# ]9 z3 p+ _8 P
$ w5 B- f* h/ J4 G& l // By default, all `createActionForEach' modelActions have9 b ^, V$ u9 ?% p3 p+ g3 c9 j
// a default order of `Sequential', which means that the
/ M! B3 a5 j$ m! ^4 [ // order of iteration through the `heatbugList' will be) Q0 H. k) n2 W
// identical (assuming the list order is not changed( F$ {, A( o- ?1 G/ n/ i% n
// indirectly by some other process).
3 k# J! m# }) e9 C8 O4 v& c0 i3 m " Q9 w7 q D6 ?% V% U" I
modelActions = new ActionGroupImpl (getZone ());
( \* R2 ]% o$ S! S* a# _8 d+ p {* u- K" H+ ]
try {/ l4 ]# B# Q W+ l' x) @
modelActions.createActionTo$message) V" ^) W7 ~& @5 [
(heat, new Selector (heat.getClass (), "stepRule", false));
& }! A" \' n6 l2 i3 k } catch (Exception e) {$ I: ~* I( X. J7 S" O+ ^
System.err.println ("Exception stepRule: " + e.getMessage ());
+ Z4 Q3 }4 Q2 d6 {* ]* W, o3 [$ N }) O1 [: X' T; s/ j: F
: `7 e( C. ]" ]. S4 h- n
try {# H7 X0 _# U! N; T
Heatbug proto = (Heatbug) heatbugList.get (0);0 R0 K" ?+ ] N- W
Selector sel =
% r6 h$ W7 x5 e/ y new Selector (proto.getClass (), "heatbugStep", false);
9 e7 j3 t- ~" A# j! w actionForEach =' N4 Z. [ ]% T: g$ p
modelActions.createFActionForEachHomogeneous$call
3 m% c) n, s6 B7 v) ` (heatbugList,
x8 L" y7 K0 i& \# B i8 [& ~ new FCallImpl (this, proto, sel,; D- i( u" Q! G8 U$ z6 C H
new FArgumentsImpl (this, sel)));
( ?$ {$ Q( ?" D" b/ D: j } catch (Exception e) {7 s% F3 |* ^7 n
e.printStackTrace (System.err);* \, }9 @ V- N8 X. x
}3 q! h' S5 n0 f2 S6 N
: i5 M3 ]" z% ?) z+ o5 h7 D: `2 B syncUpdateOrder ();+ R0 S* [: O6 \
) Y: R' d+ r/ d U4 F( ] e- {
try {; i+ f6 p/ U5 ~6 n9 V
modelActions.createActionTo$message
+ C) A( B" v. h6 X9 Y: t (heat, new Selector (heat.getClass (), "updateLattice", false));+ @' S+ n- s9 t O/ O2 Z
} catch (Exception e) { G0 \" W# H, Q5 b3 d( M- m
System.err.println("Exception updateLattice: " + e.getMessage ());
: b& U0 Q' K& G2 e }7 ~7 B% o0 O7 K( S _; A: G; K
, }1 L! I- w$ X/ b0 Z2 H // Then we create a schedule that executes the
0 M! R7 E( M- F* Q L0 {8 W // modelActions. modelActions is an ActionGroup, by itself it- u' j# V/ k8 }) |/ w! k, E
// has no notion of time. In order to have it executed in V0 M$ \7 \5 y- j' S. ?1 \
// time, we create a Schedule that says to use the
) k+ }5 }6 C+ Y$ j1 G( C$ ? // modelActions ActionGroup at particular times. This
: m7 t* t( k8 g2 X$ b' | // schedule has a repeat interval of 1, it will loop every
& u# A7 q5 I* k# _3 J4 u9 i5 G/ N Q4 b4 a // time step. The action is executed at time 0 relative to# z7 N4 r: r1 x
// the beginning of the loop.
& z( i/ z' h5 v0 T# p& `# s$ n, S: B* Y' b( c( j6 m* X
// This is a simple schedule, with only one action that is
9 a) }8 d& @2 D' o. O3 `& ]& ? // just repeated every time. See jmousetrap for more5 B* p: U1 c; [4 p
// complicated schedules.2 h. n. [6 A8 D( B9 n5 u) V
1 H/ ]( S; M$ ?) s, q6 U+ g
modelSchedule = new ScheduleImpl (getZone (), 1);
% ~' ~7 x' _& d l5 `1 t+ c, M; v( V modelSchedule.at$createAction (0, modelActions);- I/ @4 J- `& y; L6 {
! k& S3 N: |8 u; ?' W2 G
return this;8 j+ D# b! m D/ }7 ^( n
} |