HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
2 y$ w% V1 A5 g+ c2 D
8 _$ n s/ U: f2 A' G, I public Object buildActions () {
, S9 d4 l$ x, i3 x. V/ w* b* a super.buildActions();. W8 u$ p/ K. A0 g( _% Q
9 q# f0 [2 @* B // Create the list of simulation actions. We put these in
/ t+ u& d6 d# U6 D+ |8 X7 W // an action group, because we want these actions to be' O+ I6 w+ a- J+ D; A" {
// executed in a specific order, but these steps should8 E4 Q9 F# G# [4 x: G% {4 o/ ]) t8 r8 d
// take no (simulated) time. The M(foo) means "The message
! `! Q0 f! ?, ] // called <foo>". You can send a message To a particular
' y5 ^, `5 u* U9 A/ R // object, or ForEach object in a collection.: G5 y" B4 E7 o2 ^' q2 A
) K. {+ c4 L% [4 Y // Note we update the heatspace in two phases: first run
' k4 F+ [6 _* [! B' M. H# h // diffusion, then run "updateWorld" to actually enact the
; w& ^+ K4 {6 k' c- J // changes the heatbugs have made. The ordering here is5 H( ]1 n2 `, f3 A
// significant!
/ c( ?, ?( ^' K3 b* ~$ ?0 e1 B2 {
# v% \* p1 {, u F // Note also, that with the additional
* N; H0 Z. v2 Y# A // `randomizeHeatbugUpdateOrder' Boolean flag we can8 q# u$ ` R. ]: |/ f& M8 R
// randomize the order in which the bugs actually run
8 L! R m. }8 e% O' O! \0 S; G // their step rule. This has the effect of removing any
+ a. l* `& G- S4 D6 o // systematic bias in the iteration throught the heatbug
a" v2 E$ o4 z" B- E // list from timestep to timestep) N2 B5 y+ I* F/ r
: t" b9 ^" {# Z8 t" ?% J/ z // By default, all `createActionForEach' modelActions have
" \, c! s! h' H$ N; ]0 s8 c. K // a default order of `Sequential', which means that the
& H0 z, J4 c K+ v: t) o8 p( g // order of iteration through the `heatbugList' will be; V. {9 k' L) ~) W0 _ R( K
// identical (assuming the list order is not changed6 u/ z# ^+ j) M& H4 D
// indirectly by some other process).
+ a) ?1 q+ z0 A* p8 X9 F
; k) _1 z7 d8 Q5 l, F5 Y modelActions = new ActionGroupImpl (getZone ());
* Y& B0 l: @( A+ X
% ]0 m5 F- {7 R& t2 ]2 w- n try {
; A7 M" I" X& y, Y. I- U9 l1 D7 | modelActions.createActionTo$message
$ U0 @* S/ V- M& \+ o' _ (heat, new Selector (heat.getClass (), "stepRule", false));
) h, w" c3 \6 O/ K5 ^, w: G! { } catch (Exception e) {& }4 D2 c, E5 `6 a9 r5 H% i' S5 O; E
System.err.println ("Exception stepRule: " + e.getMessage ());6 k) x4 v. ?% Z9 ^, e) x
}" t2 g! r5 r! i, A+ n1 {8 I7 P
& z, V ]) h2 ?+ R2 O3 a0 n try {
2 M! a0 F2 U0 c) B Heatbug proto = (Heatbug) heatbugList.get (0);$ c# w5 Y1 T6 K. A; s5 ~
Selector sel = 6 x6 ]2 y5 n: N! a0 D
new Selector (proto.getClass (), "heatbugStep", false);
- }0 S h0 T& z actionForEach =
- e6 G" J) ]" w) E3 ~9 i0 { modelActions.createFActionForEachHomogeneous$call/ Z( k3 t# U; x' y+ n0 r5 G2 {- D
(heatbugList,
+ ]% A6 b/ y% j9 L$ m new FCallImpl (this, proto, sel,
) j% e7 J$ e; d, T2 B3 o: ?6 m new FArgumentsImpl (this, sel)));8 q- ?* b3 i* ?( a% N
} catch (Exception e) {, Z+ m+ y k5 J+ y
e.printStackTrace (System.err);
! C& u( p& m$ f. p! M }
- P' I, o( A. h4 q' z7 v u4 j3 u4 s0 Z& i
syncUpdateOrder ();
( J; N9 p* {3 l) E. T$ i/ U# a3 n2 x7 m7 C6 x9 U
try {
- m; |1 i3 s" T% @! p modelActions.createActionTo$message
+ [% |8 w! x% A9 ~- D (heat, new Selector (heat.getClass (), "updateLattice", false));
" T. k& a2 H4 C% q x& g } catch (Exception e) {9 z+ d. F2 l$ y4 A+ i% H
System.err.println("Exception updateLattice: " + e.getMessage ());# `. |- ]( y; q0 `; H2 ~1 ^- @& T
}
! Y5 T) Z( e5 a% ^7 ^& o
$ ]# ~3 g+ E! Y( B* J# ], [ u // Then we create a schedule that executes the
+ u4 I( A' o( K/ e8 _ // modelActions. modelActions is an ActionGroup, by itself it _( l; u7 L3 M
// has no notion of time. In order to have it executed in
6 M w+ X% r8 R/ x; x // time, we create a Schedule that says to use the9 A$ B. V; Y/ x4 J4 }1 W! |% S
// modelActions ActionGroup at particular times. This4 [ O0 c; `" V- J; ~* s
// schedule has a repeat interval of 1, it will loop every: o3 r' U6 F( g' A
// time step. The action is executed at time 0 relative to
: f( ]1 |3 r& T' q // the beginning of the loop.! e( d$ [- s/ o/ v0 j7 H
+ C. W7 ` i* a0 O5 ` // This is a simple schedule, with only one action that is
1 a9 \# C4 @+ L; ?# u+ G3 i // just repeated every time. See jmousetrap for more
2 p: U7 Q" _8 F // complicated schedules.
( E; B' i: W$ M" B5 Y! D 5 L- A( i ?- A4 X7 ~4 R5 c& o
modelSchedule = new ScheduleImpl (getZone (), 1);" L( M/ M7 R7 x" f; I
modelSchedule.at$createAction (0, modelActions);
' b L5 B% W h1 ~; n# d
) T! B. S7 V! k/ t7 b( F: ]- ] return this;
( ^- ^1 v! x# f6 @: k4 G. `1 x# T } |