HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& t) `8 c* |, t: J
, m& U- _! y3 W3 X2 r: g0 `* e public Object buildActions () {
0 q, ]$ y7 p8 X# F9 d# D super.buildActions();- p. p: I3 }" E% a+ X. f+ {5 Z
1 _/ y6 e% F0 z: V- Z6 g // Create the list of simulation actions. We put these in* B: t/ t5 G0 m
// an action group, because we want these actions to be
9 x: p, u1 E. v: u+ q6 Q0 K) n // executed in a specific order, but these steps should
$ T/ l6 F( L( y! p // take no (simulated) time. The M(foo) means "The message
* L' A# o3 M! D4 H, Z7 I // called <foo>". You can send a message To a particular+ Z; _' ?! X; X, \9 j- Y- ^2 J
// object, or ForEach object in a collection.
. K! w4 b! q$ D2 a
$ Z" {& c/ V! }: J // Note we update the heatspace in two phases: first run
6 K1 l; `# |7 O3 m // diffusion, then run "updateWorld" to actually enact the0 K" U# y2 G; x: K+ M
// changes the heatbugs have made. The ordering here is) {* }& j# \7 G7 U+ ]5 M% d
// significant!5 y* M0 W4 `, F' {1 x$ \/ \( }( F
# T3 f- ~9 n+ j
// Note also, that with the additional, j5 R9 v" Z4 W1 B- b7 [
// `randomizeHeatbugUpdateOrder' Boolean flag we can z4 V" `# { l. N- L' \, O$ F( J
// randomize the order in which the bugs actually run9 t; T6 x; G f; `- t! l; X
// their step rule. This has the effect of removing any
7 W( F" G0 ~& c5 P; P // systematic bias in the iteration throught the heatbug
2 h: z) S5 S- e0 `8 j( R$ ? // list from timestep to timestep! p) S+ S, t" O: n4 }& |1 M
" y7 n9 g/ O6 ^; |- Z/ m+ i // By default, all `createActionForEach' modelActions have" d# q0 ?. n% i, R
// a default order of `Sequential', which means that the
4 i3 Q, U0 |2 \; j# d% a: M // order of iteration through the `heatbugList' will be) a9 }0 j7 `$ O" @ O
// identical (assuming the list order is not changed
1 ]2 [* V. `' ^* S // indirectly by some other process).
" l' H* L8 C* D* P3 o . j" U6 J3 z$ {" w: t6 \) ?( t
modelActions = new ActionGroupImpl (getZone ());& V0 \& w% I) ^. e! E( Q
# i* r" ?; E0 } try {$ U2 O* o7 Z* f: x1 g s1 o" K
modelActions.createActionTo$message
* L A8 S. y5 `1 p) ] (heat, new Selector (heat.getClass (), "stepRule", false));
( T$ [: `, ?4 m' P7 m } catch (Exception e) {& ~ j9 d6 D: _$ U+ C* L4 `+ g
System.err.println ("Exception stepRule: " + e.getMessage ());
2 R$ G& y4 _' d% t5 J* q }
6 x1 [$ D2 G9 H2 B: \! g; Z1 S. v' Z6 K0 V8 P% k
try {4 Y' o$ Q$ u( W
Heatbug proto = (Heatbug) heatbugList.get (0);2 K/ w- s# C: w' w6 ^7 ^+ j
Selector sel =
/ A0 t) w; h, j3 `0 g! q7 ?; \) } new Selector (proto.getClass (), "heatbugStep", false);
d2 x7 i% p! |. M7 I actionForEach =
( i* }2 r& n: a) w modelActions.createFActionForEachHomogeneous$call* u1 f6 C" C! P- M
(heatbugList,
6 ]$ A, L3 O' B/ u T3 U new FCallImpl (this, proto, sel,
x& P0 L% }+ I5 a new FArgumentsImpl (this, sel)));
7 N0 \4 _5 T6 _- G+ i } catch (Exception e) {# H, P; |( E: Y
e.printStackTrace (System.err);' t) ~* j% h& O0 L9 ]
}
: s8 w% w" i0 `! h
' y ^2 h& C1 {) s: P syncUpdateOrder ();
3 t9 w3 V' o& v
0 a- z% E8 @# V7 \0 H k+ e try {
# [# W" M$ C9 A' q& }- E modelActions.createActionTo$message
: F. ?) V, E5 m: n% z2 h( g( m (heat, new Selector (heat.getClass (), "updateLattice", false));9 a! m8 W+ Q5 S7 i) y
} catch (Exception e) {
; n4 n+ U! c, n System.err.println("Exception updateLattice: " + e.getMessage ());
& ^" @0 V. ^ ^ }
+ ~! k& q. Q# h {3 i
$ D/ g5 z! o/ m // Then we create a schedule that executes the; f- ~& ^! G+ e7 c2 [, {, x
// modelActions. modelActions is an ActionGroup, by itself it
; @) f# i+ r% b; E6 [ | // has no notion of time. In order to have it executed in* F9 ]3 O" q8 T) ~4 ^. g) P
// time, we create a Schedule that says to use the; ~% Y" a: X$ K8 d. r' O8 I4 p7 b
// modelActions ActionGroup at particular times. This! `% o7 k' E: H# f6 E% ]
// schedule has a repeat interval of 1, it will loop every. `1 s6 }$ C- ~% [, f. d7 A
// time step. The action is executed at time 0 relative to7 l7 \" Z5 |+ h5 H6 F& n4 J, J
// the beginning of the loop.
) U, @6 v3 b: ^! a- _3 \, Y9 {2 }9 |0 d3 ~
// This is a simple schedule, with only one action that is, {. q1 r, j' @8 l
// just repeated every time. See jmousetrap for more3 Z6 G1 n" O1 ^9 s! Z& y9 {0 `
// complicated schedules.
: Q5 W2 \5 h' I, I8 q. Q" h ) D8 P8 H, Q* j, v4 v4 e2 e! r* a
modelSchedule = new ScheduleImpl (getZone (), 1);
0 d$ i# K# c* C% O/ r" ]- o/ e. ? modelSchedule.at$createAction (0, modelActions);
3 i5 ~1 q/ g3 n4 P; p6 j $ A# v) v; D3 l% N9 E
return this;* o- h2 Z3 F- `1 m0 U" T
} |