HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- w5 Z- E- E$ B4 \# k2 J" k
# A2 K; a* J4 Z+ h public Object buildActions () {& q1 @% \( ^# d( g3 G" S
super.buildActions();2 p: O" g# ^( @# @
9 Z/ l0 i R9 m" A& c! f% X2 f( Q
// Create the list of simulation actions. We put these in$ _. |$ j0 Y, P( l: h
// an action group, because we want these actions to be
+ G8 t- S2 n7 D; p' x+ q2 s+ k // executed in a specific order, but these steps should
) d% B) \5 D+ _3 H // take no (simulated) time. The M(foo) means "The message
( ^- k, Z$ F$ Q" `8 u) v! z, ^& V // called <foo>". You can send a message To a particular1 Z5 _- F, ]6 |5 v
// object, or ForEach object in a collection.1 t& n' N3 y6 Z* v; X& U) {9 B
: ]- `: n' [& A' m // Note we update the heatspace in two phases: first run
1 d! E6 Z0 ^0 f) U* q) I // diffusion, then run "updateWorld" to actually enact the7 R1 n6 S8 t1 K5 n
// changes the heatbugs have made. The ordering here is
8 ~8 @7 S3 E: h' u/ ~ // significant!
9 I9 O' v* Y& k3 B) [/ Y , B, i1 K Z( n# I
// Note also, that with the additional
: @& c, y- z3 s4 a# @' A' R // `randomizeHeatbugUpdateOrder' Boolean flag we can
( K: j' b6 s7 ^, I // randomize the order in which the bugs actually run
& ?2 N( Z( \" {" u7 j h! D* O- ? // their step rule. This has the effect of removing any9 s6 H0 w9 @% `( b, o4 p* ?1 h
// systematic bias in the iteration throught the heatbug
- M5 A. i" c5 V \0 m // list from timestep to timestep
5 t! b3 R% L1 |$ U: y+ D
+ m6 h4 |* I7 |2 `: ? // By default, all `createActionForEach' modelActions have
' `& v; Z8 P1 N1 x/ T8 E // a default order of `Sequential', which means that the
# j, Z2 o! A. g. y$ S // order of iteration through the `heatbugList' will be5 I7 A" v! d0 x D
// identical (assuming the list order is not changed
9 G2 Q7 K* U- @" p9 j' f: I // indirectly by some other process).
9 T! h& R+ c2 R8 V8 z
9 _- o( `9 T% B modelActions = new ActionGroupImpl (getZone ());7 T8 p7 y6 i; D6 n0 k o
" l, [; Q6 {$ X% ~$ O
try {
/ M; _7 ?( `( i modelActions.createActionTo$message: A' {# H3 S3 b2 q
(heat, new Selector (heat.getClass (), "stepRule", false));9 n, M3 |* j. `! v; Q1 |
} catch (Exception e) {: n7 g$ S* a( N' K
System.err.println ("Exception stepRule: " + e.getMessage ());
* X; ^4 J; p* G( A/ j! b! z8 R1 z" f# _ }8 s% T* }( }5 X& O o/ \5 d+ Y
$ B0 w {, |3 E* r
try {
% ^8 c) g1 p( J6 @3 h5 n Heatbug proto = (Heatbug) heatbugList.get (0);
7 _$ R# }7 o. c" [: \ Selector sel = E3 y5 K1 _8 n8 {; w# v" p
new Selector (proto.getClass (), "heatbugStep", false);
6 c6 I2 q4 R; ?- ]4 z3 [4 _3 V actionForEach =1 p, C! N# D$ J# x
modelActions.createFActionForEachHomogeneous$call' Y0 a( P# n- r$ E- h( N2 ^7 \
(heatbugList,
9 x! o4 `9 b9 g" T$ ?+ H new FCallImpl (this, proto, sel,: f/ ]; Y+ v) Q, b. ^
new FArgumentsImpl (this, sel)));" Q: V2 ^' W9 u1 A4 ]( S$ H
} catch (Exception e) {
3 E4 N$ X7 ^: T6 s7 S% }8 S e.printStackTrace (System.err);& h- F3 R7 Z3 }! R4 o0 _5 |
}8 K m0 u: e( P8 m( m
& Y! _. s6 d" T- ?; @9 s3 Y/ z* E
syncUpdateOrder ();
) o4 J* E1 [7 V. K8 Z% u& p9 \1 K( m2 A q; ~' u9 E
try {
& @0 t5 Z, `+ Z( F p modelActions.createActionTo$message 0 w r2 q- c" O7 s9 `$ {* V
(heat, new Selector (heat.getClass (), "updateLattice", false));, t' Y: }. q* B' H0 g- G9 J n
} catch (Exception e) {+ \1 w) C9 n. Z% ^3 |2 J
System.err.println("Exception updateLattice: " + e.getMessage ());% h3 n) f% l, x7 V2 `
}
" U5 N0 t; H. Y# i3 v& `, Z3 k + I$ S% n! \7 i* N/ c4 W
// Then we create a schedule that executes the7 y4 X% `9 T0 Q' i
// modelActions. modelActions is an ActionGroup, by itself it
5 P/ F. G+ T# n3 [6 V" G$ ^! q // has no notion of time. In order to have it executed in
7 s; {: M% ^4 k) g" e; { // time, we create a Schedule that says to use the0 `/ s. |* Q& A. ?4 {/ A$ \$ _
// modelActions ActionGroup at particular times. This7 T. ?5 u# r) s8 Z
// schedule has a repeat interval of 1, it will loop every. \* w$ O! k: i0 I" L
// time step. The action is executed at time 0 relative to: [* l, B+ L: Y4 s* y. g4 c
// the beginning of the loop.( t- L4 Y3 _2 T
1 {9 W$ X# q5 u; d; \5 d // This is a simple schedule, with only one action that is$ E7 J! D3 C3 k5 n3 W/ U
// just repeated every time. See jmousetrap for more3 N; n7 g0 @& T9 Q) g6 u7 P( T
// complicated schedules." L( R) d3 y+ B7 j; [
" h7 f! _6 B4 N9 m, _" b; ~
modelSchedule = new ScheduleImpl (getZone (), 1);
% l' P- G+ T# T* Q7 f( F8 I modelSchedule.at$createAction (0, modelActions);
7 \3 a2 B) o. r" Z* ^ " M" s8 S8 f& k z! V% Y
return this;8 o5 t0 v& H2 A4 _/ E
} |