HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- \; `1 m3 M0 J7 N: ]6 K6 i8 K
" v7 D8 d) E. m- x* ~, m public Object buildActions () {7 i" y; @, H% R' T
super.buildActions();
' g M1 O( p+ H0 R! _( V 1 l3 k. R. d7 B# G$ Y
// Create the list of simulation actions. We put these in3 V# ]/ e1 m' p2 z9 ?7 ~& L1 }
// an action group, because we want these actions to be) ]6 ^, g) G" R' z: y1 `( ^
// executed in a specific order, but these steps should3 D# F, K& r9 r% u$ @, @
// take no (simulated) time. The M(foo) means "The message
# [( e: D0 c, ]. k" U; \1 l // called <foo>". You can send a message To a particular
* t/ r% A* i/ z! F+ l' m // object, or ForEach object in a collection.7 Y! O/ c- |# a7 j* `) I
! g. m, G( K t! M. b. f( ] // Note we update the heatspace in two phases: first run& c5 g0 Y; J/ z
// diffusion, then run "updateWorld" to actually enact the- o0 d2 M/ N3 m% ~6 r( @
// changes the heatbugs have made. The ordering here is
. ^ { h$ Y& ~& _& o, q t // significant!
& S/ }' e9 E+ e ~- g4 n
; N I& l6 X0 D( g% T // Note also, that with the additional9 ?9 u9 I9 J' p) V# `8 _& W2 F
// `randomizeHeatbugUpdateOrder' Boolean flag we can( J. N9 J/ s* c/ [$ u7 ]9 N0 A$ x
// randomize the order in which the bugs actually run
) |- X! v) s9 ~8 W // their step rule. This has the effect of removing any S7 Q' z# x0 P7 _& _( I
// systematic bias in the iteration throught the heatbug. x, @7 q2 P4 u9 x5 v# ^' c% e
// list from timestep to timestep* X) _. u! g4 F( ^8 V4 V5 W
" T: H6 [6 H* p" a
// By default, all `createActionForEach' modelActions have
3 `8 v! H* _+ ?; d8 h( a5 k // a default order of `Sequential', which means that the, q9 N, I/ z. `: K1 `
// order of iteration through the `heatbugList' will be
! f5 V3 S5 o3 e // identical (assuming the list order is not changed; i3 \# f5 ]5 ^# a% k) y6 U8 `
// indirectly by some other process).
) X( X4 m# |4 }3 W' `
7 g1 m7 B' {2 F: m: G# b modelActions = new ActionGroupImpl (getZone ());
& ^* s9 e) d3 D% S8 A
3 @: h3 K. q( b* e$ ` try {
s+ I$ a+ w7 k modelActions.createActionTo$message8 ?! B- w& r0 N/ M& i# w6 E
(heat, new Selector (heat.getClass (), "stepRule", false));, `$ _+ o1 R. U6 l
} catch (Exception e) {
( w1 n& D7 x+ T% F- D System.err.println ("Exception stepRule: " + e.getMessage ());
# x( k9 Z4 u [ }% q/ V3 F# v! t4 N8 ?* b/ `
9 v" x5 w1 u+ n2 d" G try {
4 M% B* L# t: T, V$ ^* v, c Heatbug proto = (Heatbug) heatbugList.get (0);% k) X$ |7 P' D+ x0 Z. Q0 P/ Y# ^9 l
Selector sel = . x2 o! i4 ?5 G& r+ `
new Selector (proto.getClass (), "heatbugStep", false);( W+ I* x; w& ~% P
actionForEach =/ U5 D' Y( W9 y0 n% t; i
modelActions.createFActionForEachHomogeneous$call2 H, x% U& I* `0 L& t, C/ k6 t# \' g
(heatbugList,3 v3 |/ I$ q) M7 @5 r* l7 h" Z
new FCallImpl (this, proto, sel,2 W+ d( y7 y4 i) [
new FArgumentsImpl (this, sel))); h- Z, a& m- y' A$ t$ B7 I
} catch (Exception e) {
3 }# t1 k0 u" h2 C/ t e.printStackTrace (System.err);
9 x& e6 `) {; w+ C" `& M1 D& Q. I }: @7 _8 V. ~ Q1 [7 R
3 V1 L7 l( Q5 \; Y7 q2 D
syncUpdateOrder ();7 q" k! X2 \7 Z8 \: [) w! [( b
4 q* |6 Q# e' }; i* j5 \! N& \. H
try {
$ F W0 H: w( I8 e( H modelActions.createActionTo$message , p, r4 s0 f/ o1 E1 v3 f8 T: T- Z9 j
(heat, new Selector (heat.getClass (), "updateLattice", false));0 I/ Z. t8 x {3 A% F$ S/ w0 P
} catch (Exception e) {
& S( W; i7 f2 h% Z1 t# p2 u System.err.println("Exception updateLattice: " + e.getMessage ());
/ _' J+ o; L: I6 R6 }( B. i }
, I2 o6 S' H& r' W
1 ^' w( M( {0 f. x; X // Then we create a schedule that executes the8 T! {% p u) y7 G/ ^5 V: N
// modelActions. modelActions is an ActionGroup, by itself it
/ b$ ?9 X* h- k$ |/ U // has no notion of time. In order to have it executed in
9 c& L5 L6 y0 D, T // time, we create a Schedule that says to use the- ~3 T% n# Z7 J3 S- p
// modelActions ActionGroup at particular times. This
6 Q* ] \0 |5 |7 g // schedule has a repeat interval of 1, it will loop every# _: m7 W3 x) Z/ w! g4 {. [8 W
// time step. The action is executed at time 0 relative to7 d ~/ l* v" }# e- ?9 P. A
// the beginning of the loop.
* N R7 Y p i( a6 g u$ v
- X$ f/ L* e0 ~7 b // This is a simple schedule, with only one action that is" Q; M P8 B6 X0 ~" I
// just repeated every time. See jmousetrap for more
' l+ Y* s- B0 ?1 [: L6 A // complicated schedules.
9 T0 [( M% O& G, D
! N# D5 o0 _' H4 v0 L5 g$ t modelSchedule = new ScheduleImpl (getZone (), 1);- X9 \$ N" m7 H7 V, `
modelSchedule.at$createAction (0, modelActions);5 P" o( D" }; c( J/ |
* z6 b- u# }1 X# O8 ^ return this;
5 Q# d" y" _1 z } |