HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& o: k9 `( N& x1 w
n# E7 j0 U {; ~+ M- G/ i+ D
public Object buildActions () {2 { A `! F$ O7 r) B
super.buildActions();
& z$ _2 M( n$ T, X' w1 U# V+ v: } ]3 X6 ]$ O O4 Q
// Create the list of simulation actions. We put these in
1 b# Z8 \2 g% l; H$ |9 o // an action group, because we want these actions to be
7 \* S0 W- n& Y7 a( t // executed in a specific order, but these steps should
! I4 X$ s# @1 m // take no (simulated) time. The M(foo) means "The message
# L/ i; q0 y! a) J- n% P // called <foo>". You can send a message To a particular
8 P1 u% M9 r! M% \5 W3 G+ | // object, or ForEach object in a collection.
- d- O" K) l3 D% e0 V
+ @, C/ G. O) u$ ~4 Z2 [$ q6 Y // Note we update the heatspace in two phases: first run0 y+ F5 X+ r8 L8 F- y' j
// diffusion, then run "updateWorld" to actually enact the
% s8 P, t+ I5 o // changes the heatbugs have made. The ordering here is
" F( o. @# o" B0 p# U } // significant!/ A: k/ d2 Y" y2 H' b# z
6 v' A# C& }4 |2 c# }1 P# X: m5 b // Note also, that with the additional* _6 l3 C2 l! n1 [
// `randomizeHeatbugUpdateOrder' Boolean flag we can6 X( t! l( C8 x& t- `
// randomize the order in which the bugs actually run
, \. q C- a0 ?4 Y$ s& c // their step rule. This has the effect of removing any
4 w5 N, h' e$ r) K+ z% f2 ]: c; ~ // systematic bias in the iteration throught the heatbug
2 @% C7 X/ g0 J% { // list from timestep to timestep
* \1 ?. F/ y4 C5 m) c9 I9 N) p
8 e0 x( @& L- E( e( A& S0 X+ @ // By default, all `createActionForEach' modelActions have! h! v- `4 b. U6 c
// a default order of `Sequential', which means that the( u7 F# \6 d, G2 {
// order of iteration through the `heatbugList' will be
3 E; I6 J) [& d, H+ P- F# l& q1 J // identical (assuming the list order is not changed
( [) C. `, v L // indirectly by some other process).5 z' Y8 R0 C4 W; X3 s7 W! ]
3 _& H' @+ v. I$ |
modelActions = new ActionGroupImpl (getZone ());- B; x% ] l1 \
5 K& Q5 R. T0 u" R8 W3 }4 E
try {
9 u# _# t! L8 o; x2 K modelActions.createActionTo$message
3 o2 [8 }' D. {4 y (heat, new Selector (heat.getClass (), "stepRule", false));
/ ^5 ?7 \7 y: A" w0 l+ J: } } catch (Exception e) {# H6 a! ?% B8 [3 {) u, D
System.err.println ("Exception stepRule: " + e.getMessage ());
& P3 p1 @( b- G' u/ p! @2 F }
# | R7 I2 Z; @- N2 _
3 y: i( K# c6 ^3 `) `3 r3 m1 c: S) x% ` try {& a, x- v& e1 n3 W
Heatbug proto = (Heatbug) heatbugList.get (0);! a9 b* F$ I6 |6 C! T: R, }
Selector sel =
% L3 U* K) E* g7 @ new Selector (proto.getClass (), "heatbugStep", false);* \, \6 }* k, N3 S# I; j
actionForEach =" Z* q: X; k' ~
modelActions.createFActionForEachHomogeneous$call
% ]; l' s; i \6 |1 j' R" E$ M g (heatbugList,
! d* P; t; y$ T! z: d new FCallImpl (this, proto, sel,
! x9 o* q& F; R new FArgumentsImpl (this, sel)));
, E4 ]2 ~- e+ c# m } catch (Exception e) {
$ O' A. t* X' u- K2 i- A! F e.printStackTrace (System.err);% Y- u! H8 `4 I+ Q& P, {: x; C
}
7 i; `1 Q7 _2 M6 j8 D - t0 G+ ^9 L2 ]! G
syncUpdateOrder ();
2 K/ {! _ B$ I# G3 A
$ q$ A7 a& y* ]6 U: M a try {
, q- l1 M% \/ J% N* Y2 P8 l modelActions.createActionTo$message * R! y# K: o$ O' L
(heat, new Selector (heat.getClass (), "updateLattice", false));- G/ @+ t W" `! i" V1 s* H9 p6 r
} catch (Exception e) {, O i) W4 x6 u& N- K
System.err.println("Exception updateLattice: " + e.getMessage ());
/ m) O$ k- W" n& U. J- Q5 m }
7 G" U: i+ S( F5 d0 H$ f5 {
% O$ M$ v8 \5 q // Then we create a schedule that executes the3 L1 L' I- X( f0 v8 t
// modelActions. modelActions is an ActionGroup, by itself it/ C/ w" _: A- n- J4 y
// has no notion of time. In order to have it executed in
; }( v5 F0 p4 M7 E: D* M5 s2 J! [ A // time, we create a Schedule that says to use the' s# A# R# e ~
// modelActions ActionGroup at particular times. This
# R, B) l( {5 l1 i5 a* J! g: s // schedule has a repeat interval of 1, it will loop every9 A6 R+ c3 Z; J) i: ]; ?
// time step. The action is executed at time 0 relative to
) S& a/ _* C- h$ @& G" @- s // the beginning of the loop./ c1 s, A) [& \
) u$ r4 W( {. y- w6 H* Q3 R& ~ // This is a simple schedule, with only one action that is
+ S9 \' J. n9 }+ g+ v // just repeated every time. See jmousetrap for more+ h2 _5 M' S4 b7 l$ o3 z6 C
// complicated schedules.
# Q; x7 ^- e+ m8 W2 E # M' s/ Z5 a ~0 }; i
modelSchedule = new ScheduleImpl (getZone (), 1);/ {% ~. b+ C2 O& ]" d4 k! r
modelSchedule.at$createAction (0, modelActions);) `& H" \9 l! \ {: G; m" F; c
4 J2 y, `2 z; q2 p0 A
return this;
/ O; \' i- y1 e8 o/ q, k7 H$ Z$ S } |