HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! [; l7 M5 n+ @3 e
! e, Q& x' @9 B9 U public Object buildActions () {# Y( o# X" N& Y; K! D6 ~
super.buildActions();
2 X& E& T( n: u% f; @. K % {. J' l- Y' P8 B8 a3 p6 }
// Create the list of simulation actions. We put these in
( l) ]( i ~' b) R% r // an action group, because we want these actions to be
- A3 e* p0 N& ^/ A // executed in a specific order, but these steps should2 y; x# r) l( Q$ H& p% }- Q) V0 j
// take no (simulated) time. The M(foo) means "The message
6 q# F1 ]6 a; S% V5 n) \ // called <foo>". You can send a message To a particular0 K5 K: b. s# g! Y( q8 Q& r
// object, or ForEach object in a collection." O$ X9 f" b' ^' K& {; p& q
! i9 S/ o* H# @, ]2 s' }
// Note we update the heatspace in two phases: first run* _1 M3 A/ C( w5 C3 y+ d
// diffusion, then run "updateWorld" to actually enact the1 m% ]. E* t" |8 B7 O6 m1 Q
// changes the heatbugs have made. The ordering here is8 `' k! m F4 V8 ~( ~1 A
// significant!9 y4 a5 ~0 d8 v& T8 W! j9 g
; ?) k, H2 J# S- v2 @" T5 I // Note also, that with the additional" B- [3 V4 _& y" c' @) `7 R: a
// `randomizeHeatbugUpdateOrder' Boolean flag we can
" |7 C0 l/ z# F; p& P // randomize the order in which the bugs actually run5 U- i! e/ r) L9 f% j# C
// their step rule. This has the effect of removing any
' N( f. [" R/ a) ?1 w- j# z4 \ // systematic bias in the iteration throught the heatbug+ K* W& J9 \7 b
// list from timestep to timestep
/ f: x' {9 [5 b& k& d
3 h) X7 Y o! L( u3 k // By default, all `createActionForEach' modelActions have5 V( x; v. K5 j) M9 g1 o
// a default order of `Sequential', which means that the0 O) U- l* J! H4 o% @) Q, J6 L* K
// order of iteration through the `heatbugList' will be
* x! m0 ?7 Z$ T! M' p/ j) d8 S7 v // identical (assuming the list order is not changed3 L: p$ ^$ Q/ T
// indirectly by some other process).
4 r: A: s8 p# d* _ ]9 l* Y9 Q8 E 5 z. z3 h6 |& J; W' j
modelActions = new ActionGroupImpl (getZone ());7 Z4 U$ \9 R$ }- b1 H
1 `/ O: w, K0 W* m8 q4 I try {# n! [. o: Y9 G/ l" f
modelActions.createActionTo$message
' i2 F- y9 s6 }$ H) |% q (heat, new Selector (heat.getClass (), "stepRule", false));
" `" A; T3 [- E& ]8 b } catch (Exception e) {4 |6 i4 C0 x) m. Y3 l% e4 T' ^
System.err.println ("Exception stepRule: " + e.getMessage ());/ A& K; T0 \6 F0 K4 T: D! }5 j- v
}2 n! j- e, i( ^* X r7 |' {) `* T
, H- X- i$ j" x0 B5 t1 o7 }) P
try {) [* @! c. q: T- y& I) a
Heatbug proto = (Heatbug) heatbugList.get (0);
[7 J% Y( O' f$ I# |! |7 y* x Selector sel = % ]+ H$ A- B: c, _5 v( t; ] ~
new Selector (proto.getClass (), "heatbugStep", false);4 |% f$ S0 |& [/ _% O* L% w( `
actionForEach =
, \- H6 N0 I* _/ Y9 Y' w modelActions.createFActionForEachHomogeneous$call
8 {/ m: Y( `+ v% L (heatbugList,. z; f0 T$ Q' [. i: L4 n4 w* ]8 t
new FCallImpl (this, proto, sel,* C% `& l- T. h& q$ Y. E
new FArgumentsImpl (this, sel)));
6 r3 K+ Z2 `- S$ @5 L0 `8 S } catch (Exception e) {
& [- G' G+ w# {: j" \ e.printStackTrace (System.err);, q/ b3 e# R& @& A2 j% X2 }4 h
}1 O; h. i% `" X
3 e* K. G% ?3 F+ a) i syncUpdateOrder ();) G; J* d' i' C7 J4 D, r
$ p+ o; q8 _& A4 r8 i7 _4 b" ]
try {
4 O' ^: ]" B6 `4 D4 r& C modelActions.createActionTo$message " N9 f+ a4 Y( g" ]2 z6 C
(heat, new Selector (heat.getClass (), "updateLattice", false));, \9 U9 y8 y7 O6 `6 g
} catch (Exception e) {. |- u) Z' y. b$ }- A' ]' T
System.err.println("Exception updateLattice: " + e.getMessage ());
8 k* c% S7 \* H' h6 k7 f7 S }+ u. e7 o' `, K! ~/ }$ P, d
7 I' ^/ S* w2 K: u& Q
// Then we create a schedule that executes the
5 c5 y* b4 f% U/ F1 |( R# I9 \ // modelActions. modelActions is an ActionGroup, by itself it
, W p, p" L% |0 w R% i( l // has no notion of time. In order to have it executed in' }( J. N: @* x) B0 E
// time, we create a Schedule that says to use the8 [. L6 o; O' J% D
// modelActions ActionGroup at particular times. This3 c% y! C4 c6 r* X/ d
// schedule has a repeat interval of 1, it will loop every
v* z+ T9 H& P/ b // time step. The action is executed at time 0 relative to* ?8 b# b# s0 v- y: X: C- L$ ^4 J
// the beginning of the loop.# ~. |( {/ ~, B) Q2 B
* V5 ^/ p) O& x/ X5 {, X4 j2 u // This is a simple schedule, with only one action that is
2 b) I: ?' b% I5 e. _# k0 D# U9 r% X // just repeated every time. See jmousetrap for more
) z9 Y% Q Y" Z6 o: ~6 l // complicated schedules.
7 l/ _/ j, w# n9 ~, \: L - {0 @+ U) o9 {% a' [
modelSchedule = new ScheduleImpl (getZone (), 1);
7 Z$ v3 l* L* H! X) c modelSchedule.at$createAction (0, modelActions);
; S; I0 `) A8 W
) W0 J# A/ ?5 t. m return this;7 |: n8 H% x% J1 `
} |