HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 }% k) \0 z- q, x0 d
& J% S0 k% d4 `% a
public Object buildActions () {' M; _" ]( p$ A Q" N& I
super.buildActions();( G7 l/ Q2 a+ y9 B4 | ~
' \3 r% p* a9 `4 ]
// Create the list of simulation actions. We put these in7 y; D# J0 H H( I
// an action group, because we want these actions to be
6 }5 e7 y: J9 A2 n1 ~7 B // executed in a specific order, but these steps should: K" w$ A+ r8 H
// take no (simulated) time. The M(foo) means "The message7 ^4 Z+ y1 ]6 q8 e: S N1 S
// called <foo>". You can send a message To a particular1 Y @9 ~/ \5 J4 a! G/ g
// object, or ForEach object in a collection.3 F5 g2 L! @: p3 _" _: X1 {! F
, {6 f( Q. g8 U2 R2 ^ // Note we update the heatspace in two phases: first run3 M2 b$ u3 d. d8 ]+ _2 i- i7 b
// diffusion, then run "updateWorld" to actually enact the' t6 V/ |, t1 N7 _
// changes the heatbugs have made. The ordering here is* m9 r8 T( ?/ q0 O5 u
// significant!: a$ u/ x6 e6 ?+ K; w) C. Q
: N D1 B& F7 a
// Note also, that with the additional
5 ~0 Z, M9 y8 v, t // `randomizeHeatbugUpdateOrder' Boolean flag we can" {- r' n1 Z9 u' h1 h, `! G7 L
// randomize the order in which the bugs actually run6 G) J* t8 K( R& d
// their step rule. This has the effect of removing any
7 y9 s1 o: m6 {; p5 p# {0 Q // systematic bias in the iteration throught the heatbug
, E& ^7 B! D" Q# O& ?$ W* | // list from timestep to timestep
+ m1 p1 M+ p8 a) @" r
1 ^& J4 h& x+ g- t4 W& L // By default, all `createActionForEach' modelActions have/ V2 y F Z* S! @% j
// a default order of `Sequential', which means that the
3 r3 {1 u/ r+ R. p8 _ // order of iteration through the `heatbugList' will be
9 r& u: V/ h6 C: \& [! Q7 N // identical (assuming the list order is not changed
8 V. O d; Q# w4 C // indirectly by some other process).7 @9 Q2 C% a1 X/ q3 n2 b
& I( w& Y- M2 Z modelActions = new ActionGroupImpl (getZone ()); J+ o s5 t4 o, e) D
9 J; b: U) [, u try {
# r9 r1 S$ \% p% M7 A# h+ B( V* d modelActions.createActionTo$message
9 u/ }6 Y+ }9 b; f B3 \ (heat, new Selector (heat.getClass (), "stepRule", false));
# ^2 m2 J5 K: _1 W3 s" d } catch (Exception e) {) K) y+ U0 g' S g
System.err.println ("Exception stepRule: " + e.getMessage ());: K9 p5 N; K9 b/ |; n; k
}0 ?/ T" s0 K4 H8 e/ H6 R( Y
# \' f) w0 v+ n# v3 ^
try {
K0 S9 N$ c2 j Heatbug proto = (Heatbug) heatbugList.get (0);" s( {9 K o$ o; |6 e1 X
Selector sel =
' E$ b: U9 ~8 ~0 u5 L) d- S& D0 |: \ new Selector (proto.getClass (), "heatbugStep", false);
1 d" S$ v7 r4 @ x actionForEach =
) }5 I2 _- u1 j; I& x: D& L$ r modelActions.createFActionForEachHomogeneous$call$ u/ K. P% R0 c) o* A
(heatbugList,
# W$ U; ^5 F" e+ N; z0 i, ^8 I new FCallImpl (this, proto, sel,
1 V. E/ E; b8 t& L% } new FArgumentsImpl (this, sel)));2 j* y% e' H F0 v. d
} catch (Exception e) {
9 y' n! q+ w- h e.printStackTrace (System.err);: y- Z1 B5 L, e; n2 O8 k T
}% T2 x( |5 _! b. }9 C0 u4 ?
! h, h# ^: r6 B7 d ~' e syncUpdateOrder ();' r* B1 i% @! E3 @% O$ @+ X
: p- a3 F1 ]; W" K, F4 j1 }
try {& @! G! V2 e; I3 Z
modelActions.createActionTo$message : O/ T! n8 G* z4 Z2 y+ V
(heat, new Selector (heat.getClass (), "updateLattice", false));% z5 b, F& M% e3 F, E
} catch (Exception e) {% t6 b% y: [8 O$ L
System.err.println("Exception updateLattice: " + e.getMessage ());
3 |2 _$ v2 O) ?, R# q }0 X; I' J9 M. I5 \6 n# A" }& E
$ \1 [1 O& H: G
// Then we create a schedule that executes the
8 P9 {* L0 Y) l4 F // modelActions. modelActions is an ActionGroup, by itself it! S8 i) ?9 d, a
// has no notion of time. In order to have it executed in) j: h: G. D4 ^2 I3 a4 @
// time, we create a Schedule that says to use the
: h0 v/ t) a. H+ i7 p // modelActions ActionGroup at particular times. This
$ i' l# d+ b2 Q7 H // schedule has a repeat interval of 1, it will loop every* |4 Q. ]. X' E% I
// time step. The action is executed at time 0 relative to# `/ Q/ M$ o/ l# e' ^) p" v$ x! m& [7 K
// the beginning of the loop.4 G: c% t* v8 }# r7 G z3 U; o% _
" d9 c9 I2 C9 w2 z5 _+ Q- h // This is a simple schedule, with only one action that is
9 W O& M# f4 u1 T1 Z" P // just repeated every time. See jmousetrap for more
/ l8 D& W7 s5 W; Y // complicated schedules.* V- \! _* ]1 C
$ N3 c4 x/ s+ i& y2 E
modelSchedule = new ScheduleImpl (getZone (), 1);
7 y6 `. Q) Y% G9 M modelSchedule.at$createAction (0, modelActions);
/ W8 z! W8 Y1 j+ h# K. Y$ i1 l$ T
3 t1 J' x8 \6 ~% E return this;
# ~9 x( u* ?8 j# K1 [ } |