HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:8 B3 F' m) c. d D
$ u) ^8 D& a6 q" D* G/ S6 ~5 A
public Object buildActions () {
4 Z8 \- c# B2 `5 {$ N3 z- U6 C super.buildActions();- i4 X# p% q$ |" @
; P0 r- J% K. Z7 }+ ]2 z
// Create the list of simulation actions. We put these in
2 a3 e" C$ X8 D, z% L9 j // an action group, because we want these actions to be; R4 Z ~ ^2 n/ l' z# L
// executed in a specific order, but these steps should+ a- U0 m0 y5 q4 ^
// take no (simulated) time. The M(foo) means "The message
5 ^3 r) {8 n q/ T; L W // called <foo>". You can send a message To a particular6 F" q9 ^ [. w8 [/ D
// object, or ForEach object in a collection.
{1 J& t: z: m3 h/ l5 z* ?
% X5 W% W* @. F: Z6 Y& o // Note we update the heatspace in two phases: first run
0 A) j6 S, L0 O/ ^ H8 J% x // diffusion, then run "updateWorld" to actually enact the
! Q$ p) [8 ^+ { // changes the heatbugs have made. The ordering here is
9 e$ f% Z6 F( B( x // significant!, n8 v* @; ?, q% H4 `8 u% G, f8 \
/ u6 w) k9 s8 f) q9 ` // Note also, that with the additional
F8 }7 A! d q% L // `randomizeHeatbugUpdateOrder' Boolean flag we can& k/ t9 p; P4 ~- C6 K
// randomize the order in which the bugs actually run
4 v) w5 y8 t5 E, d2 T; o( L" i0 J // their step rule. This has the effect of removing any1 k8 h: S6 Y( C
// systematic bias in the iteration throught the heatbug
" Y- g% x5 C& J) E( \" S // list from timestep to timestep
& v4 {6 c1 ^+ [" s) | . |, P' R. ]* X/ M$ `6 X' Y! N
// By default, all `createActionForEach' modelActions have0 x3 J5 [% U; b, n' S
// a default order of `Sequential', which means that the6 n3 |3 ?. }' H& s2 H
// order of iteration through the `heatbugList' will be1 K- F$ N! u- e v: l3 v0 `
// identical (assuming the list order is not changed
& ]1 ?) {' j6 _ // indirectly by some other process).
" |# a% e' p& j* R4 w- C
3 {( M% P( o& `: {' n; D3 k5 Q modelActions = new ActionGroupImpl (getZone ());! ?- V! P7 s! s7 k( {2 l4 B
. v$ m0 C6 G1 C) Y try {0 O7 [/ {4 m Q/ C2 H* c, H' `/ @
modelActions.createActionTo$message+ {0 r J. H/ M6 a% T! b
(heat, new Selector (heat.getClass (), "stepRule", false));
7 X$ u! q/ I* Q4 I ?% x. m9 m+ ` } catch (Exception e) {/ Q& Y2 ~) Q0 t% u0 f$ ^+ [
System.err.println ("Exception stepRule: " + e.getMessage ());
. J& v3 ~& D( }) o7 z }
& G# b* A# k+ |+ d5 O; B; K1 d/ b) [9 `& L& }/ { P
try {6 u% _0 v" I) t9 {+ K
Heatbug proto = (Heatbug) heatbugList.get (0);5 [. K- U9 V0 D; i$ ?7 }
Selector sel = & P3 v7 v2 P. P z) r
new Selector (proto.getClass (), "heatbugStep", false);
7 \9 @ T5 s% V3 s' z actionForEach =
: h3 z) M+ z6 t7 l2 o: E$ j9 q Q modelActions.createFActionForEachHomogeneous$call7 b: c) [& G6 f2 A" K4 L
(heatbugList, M$ ^- ~3 k: k% b7 G
new FCallImpl (this, proto, sel,/ S4 ?/ L' l2 m9 |
new FArgumentsImpl (this, sel)));! D! Q( P" h; M4 s% P/ K( y
} catch (Exception e) {
( E3 ?( W- T# n& i }8 z7 G- h e.printStackTrace (System.err);
6 Y1 [8 j( f" ]. n: H }
& t7 l e* B% y% c7 O, L! I ]( A . {) g4 a& ]0 Q( j
syncUpdateOrder ();9 \' L6 Z s8 b+ t; \6 I. Q
6 S4 Q8 | C- u4 Q try {
# ^! D5 n+ x4 W7 I modelActions.createActionTo$message ) D# i9 |: f* ]$ i
(heat, new Selector (heat.getClass (), "updateLattice", false));
) `0 b+ k# p3 b5 {% U } catch (Exception e) {
* j+ g$ R: ^0 D% c System.err.println("Exception updateLattice: " + e.getMessage ());4 m8 V! G/ }2 H( {
}
8 p* m2 k* z8 r* \. _1 F; \! C/ O 8 O6 A, u2 |0 N
// Then we create a schedule that executes the' _! J4 g- o v, K+ S4 t( r
// modelActions. modelActions is an ActionGroup, by itself it
5 ^1 M4 k, d: _% h0 D6 w! \/ S. \ // has no notion of time. In order to have it executed in
; `; C1 p1 L% n# J: p* H# I7 x // time, we create a Schedule that says to use the
; w7 o' X) ^9 C% q. ~$ _' O7 L // modelActions ActionGroup at particular times. This: T2 ?; p$ c" g0 E2 w: J
// schedule has a repeat interval of 1, it will loop every
* R5 l! m. t6 e; p // time step. The action is executed at time 0 relative to
! |7 Y( O1 A" H- J2 |! }& c) i# u- L // the beginning of the loop.
0 F9 R+ g/ Y4 O- e# U, { I$ i) |7 |/ n% c. X& M
// This is a simple schedule, with only one action that is
! ] X& d9 ?7 p5 r# I# h5 G, _ // just repeated every time. See jmousetrap for more
8 x Z# f7 {% _& h // complicated schedules.
" h, \: d1 K+ e. ~ $ R9 v% ?- u9 r3 ?: \; s
modelSchedule = new ScheduleImpl (getZone (), 1);/ \- {+ j# Z0 t* x% {8 P
modelSchedule.at$createAction (0, modelActions);* M( w- J* g; b* c
8 H0 P, C5 E$ n return this;4 o0 I" o- Z9 k _# i- H1 Q1 F7 x
} |