HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* a+ _+ }/ d0 Y. |+ B
* I: N: {3 u7 R0 X1 Q public Object buildActions () {8 ^( J- m& m0 n. x+ b L6 p8 s, Z5 k+ k
super.buildActions();0 `4 H1 Z% _( U. f/ w9 I, U) [
: _9 o! @3 V& s( {- g( ~' v+ } // Create the list of simulation actions. We put these in3 l( H2 m; k; b
// an action group, because we want these actions to be
. ]8 r! Q* L" S; |3 B1 q // executed in a specific order, but these steps should! T( O4 E' u4 W5 K* o6 D$ X
// take no (simulated) time. The M(foo) means "The message
( B! j# s1 ?+ Z4 w // called <foo>". You can send a message To a particular3 d3 _2 w/ o! l; u4 _6 W. W- U$ w
// object, or ForEach object in a collection.3 ]! R( m% c4 I8 V0 @+ u/ L& i7 P( v
; m) N) l! w0 y6 @ // Note we update the heatspace in two phases: first run1 A9 |" q4 k) x5 r0 |5 o1 i7 F' c* d' s
// diffusion, then run "updateWorld" to actually enact the( }8 u7 u9 G( j4 E/ D! t
// changes the heatbugs have made. The ordering here is
( r- |9 L' b5 K% k* ?# a6 y+ r, } // significant!5 O; D! Y1 L4 {% u8 E
0 T# W( |, o5 A7 j- B v // Note also, that with the additional& R# z% E! X8 |0 f+ Q- q& f, h4 {% i; g( f
// `randomizeHeatbugUpdateOrder' Boolean flag we can
8 ?1 e4 R9 I6 k // randomize the order in which the bugs actually run/ ]9 d9 a5 j/ w/ r
// their step rule. This has the effect of removing any$ q: C/ y" T) X
// systematic bias in the iteration throught the heatbug
- r! |. g- P$ t5 z8 C$ Q5 p // list from timestep to timestep3 D) F$ f7 ?) V) j
; T# y8 ^3 O; X+ j
// By default, all `createActionForEach' modelActions have% Q' I T& D+ n- \* z3 `/ {3 q
// a default order of `Sequential', which means that the
# t: d+ l/ o; m* i // order of iteration through the `heatbugList' will be3 o% V! _" B R8 ~% O* V9 ?1 T q
// identical (assuming the list order is not changed
& H* \ e; b& e# Z0 f7 K, b1 A3 ^ // indirectly by some other process).
6 U0 q* _3 E' `) D: ^4 M % k% P0 @/ m6 M7 [# }8 H0 t4 z, \6 v# f
modelActions = new ActionGroupImpl (getZone ());! [5 @% c/ J5 ]( `
" `+ R6 ? f# I: _) \1 U' H try {0 a/ f0 `$ k& c) N3 ^2 f/ R
modelActions.createActionTo$message7 ?) a- X5 O; |# J. h
(heat, new Selector (heat.getClass (), "stepRule", false));; m& h9 H2 P( f3 O% s4 Q
} catch (Exception e) {
6 H3 F& p2 n& S1 w8 m4 c" U System.err.println ("Exception stepRule: " + e.getMessage ()); c6 b& R6 c- r- j0 [
}" R4 ]3 l* w, x3 A j
# T# e2 |; j+ N* C" q: D4 I# H% U try {/ Q# ]/ F; b# @7 q1 F" l6 J
Heatbug proto = (Heatbug) heatbugList.get (0);1 Z. c8 @% c9 Y4 d. L
Selector sel =
# t. G( B& L0 m Q F+ [ new Selector (proto.getClass (), "heatbugStep", false);
) R6 n. w% U n. ^3 K actionForEach =7 t& V! I& Y8 ?+ }
modelActions.createFActionForEachHomogeneous$call
6 f- P0 N1 a) B. G (heatbugList,
' U- O" e' U; `6 \) P& B3 T8 m new FCallImpl (this, proto, sel,; R" k- Z- f5 s I3 h
new FArgumentsImpl (this, sel)));
) g9 a; R# I% L0 x, i4 r } catch (Exception e) {$ x% g& V! y0 w' d5 F8 @
e.printStackTrace (System.err);
/ t( r% Z7 i1 c5 Z! i( B1 R }
, D7 y) B* ]7 C' G 9 k" a0 X$ \( ?2 {
syncUpdateOrder ();
' A8 R. C3 B r, g
8 A( t8 K# j0 n" G7 P( e try {: F8 v, m, N/ G1 I
modelActions.createActionTo$message 3 a S0 q5 }% N8 I- {# L6 _3 V
(heat, new Selector (heat.getClass (), "updateLattice", false));4 n, x' b. w% [) i% e
} catch (Exception e) {. k& O! b5 V; t2 X6 N! ^: U
System.err.println("Exception updateLattice: " + e.getMessage ());/ t' ]! M7 {' i% Q! s
}9 \7 y; ~% L+ `8 F) C- A: N# I
) C e! z* s0 d, b" L1 m; O3 f // Then we create a schedule that executes the
9 ?. l. W3 {$ p4 n: p8 @ // modelActions. modelActions is an ActionGroup, by itself it
1 O" `2 l+ g' E) k // has no notion of time. In order to have it executed in1 R" O, N: }- g
// time, we create a Schedule that says to use the
- P: C# _% C. u$ D: v# E // modelActions ActionGroup at particular times. This6 K; r" c. m& Y; b2 K
// schedule has a repeat interval of 1, it will loop every- D3 [0 L) F& Q$ ]* p" {4 Y' k
// time step. The action is executed at time 0 relative to
0 \; @2 j% k+ D: C // the beginning of the loop.
+ f* N/ i- |7 T2 U6 G3 Y
% ~. `' e0 e1 n# e2 Y+ s // This is a simple schedule, with only one action that is
! K$ p$ H+ [# W) _8 f // just repeated every time. See jmousetrap for more
. T, n: f! T4 }2 w4 S" B9 w3 l // complicated schedules.
; F2 a2 G2 G( `- A
( @5 b4 S; B8 Z4 ?9 x modelSchedule = new ScheduleImpl (getZone (), 1);6 r" Z! e& I" |7 B6 X/ D8 K0 w! ~
modelSchedule.at$createAction (0, modelActions);
4 @! d& @& a' i7 a
$ L1 f, m/ y5 p return this;& U0 S9 M# H$ ]: d9 J. O5 {2 `
} |