HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
2 o- p [+ k. q/ R' Q9 C0 H d& n! y9 s& K( s& A
public Object buildActions () {
P7 f6 o6 \7 @$ H+ [6 N- A; | super.buildActions();
: a0 b' L! K$ \/ L U
1 f2 E6 Q/ X6 p# M // Create the list of simulation actions. We put these in
) a+ d+ P+ N1 ]1 E. I* k7 ^ // an action group, because we want these actions to be
! w) Z1 Y% f2 ^# W // executed in a specific order, but these steps should
6 Y# O' x. i/ m/ f // take no (simulated) time. The M(foo) means "The message
# S# m0 l: n- k- q( m; V // called <foo>". You can send a message To a particular
5 R" F: R# m. k$ F7 a7 K3 Y/ r- O L // object, or ForEach object in a collection.
) x' B; ]( N! P" d8 \1 X 2 Z$ r( t8 V" p3 `# ^; t" J, ~
// Note we update the heatspace in two phases: first run: w" X2 k$ @; m% q; A1 W
// diffusion, then run "updateWorld" to actually enact the
/ k" T3 H# @' v5 f3 O# B // changes the heatbugs have made. The ordering here is/ f# K- |" n* f: C/ T0 I
// significant!% Z; ^$ ~4 r/ ^ Y
1 B; W$ X" _; { // Note also, that with the additional8 f! y) E$ p1 W. q( B4 g' b" {- z4 u, i
// `randomizeHeatbugUpdateOrder' Boolean flag we can
?. N6 l0 g- l% s* ] // randomize the order in which the bugs actually run
2 [% d1 d# c" a // their step rule. This has the effect of removing any
0 `3 \. B. C5 E. T3 G9 J // systematic bias in the iteration throught the heatbug
}$ i8 C$ B& o: H- B9 l" C8 E // list from timestep to timestep. _5 S/ H1 C3 M4 q" @
/ u- P3 \6 k; }' T) X. Q% U: C
// By default, all `createActionForEach' modelActions have
, \4 O, M4 N: ~9 C: y& j // a default order of `Sequential', which means that the7 [% R; E; `$ ^* i' L# ^4 R0 s, ^
// order of iteration through the `heatbugList' will be7 m" [) Z* W! t# P1 `% G1 D0 s# N! X
// identical (assuming the list order is not changed
' b4 H7 D% y* i1 k+ |3 a. R! Q7 M0 ] // indirectly by some other process).* A4 J; m3 |! J$ B
3 W( N$ A$ b5 t$ V+ x& I modelActions = new ActionGroupImpl (getZone ());# v/ `; h+ F9 }* t8 x1 s
% L9 \% q$ z9 A
try {& w8 v `& n& _) {! N; ]4 i
modelActions.createActionTo$message% \2 j0 ^& q% k6 V+ ~+ S, g
(heat, new Selector (heat.getClass (), "stepRule", false));! l) F. e: N b
} catch (Exception e) {. P) d+ D8 h g
System.err.println ("Exception stepRule: " + e.getMessage ());. R9 V' E0 v5 h1 \* K( P
}" b4 i! K# g' g# Q
" i6 y7 F) E# m7 `7 B3 f
try {
6 g. O9 a2 v6 v( W" N- k5 O# H+ I Heatbug proto = (Heatbug) heatbugList.get (0);
6 B5 D% ?" _! Q$ j/ r Selector sel =
# p: {" E$ m5 }! f% n new Selector (proto.getClass (), "heatbugStep", false);
2 d: a' A2 O+ \) R1 a* A actionForEach =
( @1 d4 L# u# Y1 W" F7 `7 f; `6 P modelActions.createFActionForEachHomogeneous$call
" \# y0 s4 z* H$ M. H+ O, |2 M v (heatbugList,
' ?. E$ f6 e7 r: S" ~" I new FCallImpl (this, proto, sel,0 @9 f6 _7 T4 `- N$ [$ X
new FArgumentsImpl (this, sel)));
; |1 Q: f# w, n0 h, Q( ]% j1 t } catch (Exception e) {
; ?8 M) Z* P* m& a e.printStackTrace (System.err);3 p' d/ }. ~+ H" F* H
}
0 d4 b& K" l& J2 _) I* G
' u$ a- C" Q" X5 R! @ syncUpdateOrder ();
; R' b8 V; ]3 D, s9 U! Q; k T! @
6 g' ^5 E% @; z8 Y+ W* Q' U try {
* W* Z2 @. V4 [( s- Y! r modelActions.createActionTo$message ( E5 w ^1 e( w; }
(heat, new Selector (heat.getClass (), "updateLattice", false));
$ W6 o! C4 |: J. L, j- w$ a } catch (Exception e) {* M3 m9 M* \, _' o# B
System.err.println("Exception updateLattice: " + e.getMessage ());. O4 K) b1 D8 i; D/ d6 y
}
$ P7 F; {6 c6 I 0 W% I4 Q4 m) |. T' q3 `1 z, `* i
// Then we create a schedule that executes the
, f/ x. v; \, I1 E, ?$ \ // modelActions. modelActions is an ActionGroup, by itself it6 C8 r- ]3 C Y7 U2 [; D
// has no notion of time. In order to have it executed in' R3 P, a' L) } f' I6 h' m0 v2 w2 p
// time, we create a Schedule that says to use the
! m& T9 L B! H% \" n& c. z- ` // modelActions ActionGroup at particular times. This
1 J/ d" D; N- L& E3 m' D // schedule has a repeat interval of 1, it will loop every
- j3 [5 [; N- i+ M // time step. The action is executed at time 0 relative to2 O- [5 k4 c9 J4 ]
// the beginning of the loop.
& g+ M& I; V/ c) Z; T, S
1 X$ T9 ?" b# i5 d0 { // This is a simple schedule, with only one action that is. f) e* W! P. u4 i T
// just repeated every time. See jmousetrap for more) C& J! ?8 ^# \ O7 u4 ^
// complicated schedules.* o8 L3 D& Z: `& K+ [% l; a
" v+ S k, L A2 y9 X8 K9 q D
modelSchedule = new ScheduleImpl (getZone (), 1);
" M, U7 ^- E3 E7 L" O modelSchedule.at$createAction (0, modelActions);) O# | B/ I* {# P$ _% _2 e
+ E7 U4 s; \, [
return this;
* L0 w" l g, T4 M" d } |