HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:/ G7 z# b" S- D! f }! o
$ P1 }5 ]; t: \; a3 o( B9 `$ c public Object buildActions () {
6 N, s9 H* O' |6 J1 F, z- M, b3 v super.buildActions();0 f9 R# Z! U6 a+ X* V
9 Z" Y; ?* t* n5 }! H& ?7 s2 ~8 K
// Create the list of simulation actions. We put these in
7 c& u2 J/ [! f4 k; `' O6 A5 d, t/ g // an action group, because we want these actions to be
% n) `' w. ]/ \) h( z: e. L% w // executed in a specific order, but these steps should
' O* G- B( G o( @9 M6 n' q // take no (simulated) time. The M(foo) means "The message
6 I7 l( {. `5 z0 G // called <foo>". You can send a message To a particular+ P- \0 c& X9 u
// object, or ForEach object in a collection.
+ ~& L; y- F2 m6 h) D2 ~" ^
4 ?( [" r( _# H# m3 G // Note we update the heatspace in two phases: first run
) h5 g0 b3 Q' G# n6 c Q, E& k // diffusion, then run "updateWorld" to actually enact the
! M2 T/ W/ v8 o% k& D" d. w$ | // changes the heatbugs have made. The ordering here is; }5 T' p9 n: F& t0 [& Z# a# h
// significant!
3 [5 \, E( S" |6 w$ ^1 q1 O# B ( L, ^/ w2 R# J9 }& E0 T5 ^
// Note also, that with the additional
9 Z" l4 |+ J+ ~4 S; Y // `randomizeHeatbugUpdateOrder' Boolean flag we can
6 G% \' q0 W7 F5 S$ J. C // randomize the order in which the bugs actually run3 H9 M6 a" v: O5 o
// their step rule. This has the effect of removing any3 g: J0 N4 a" N
// systematic bias in the iteration throught the heatbug
- B+ H7 X, Y7 `/ k7 { // list from timestep to timestep
. M0 k/ c6 t* t" t + z" G5 l v) U
// By default, all `createActionForEach' modelActions have
# b! _2 L1 ^; W+ g0 f( | // a default order of `Sequential', which means that the
7 W& Q6 _8 u. p' ?: l5 [& e1 x // order of iteration through the `heatbugList' will be4 L; [% I! p3 I
// identical (assuming the list order is not changed9 L8 n& O$ E2 v; ^9 W
// indirectly by some other process).
3 h6 f. z x9 `: g
/ T* N! b( a- @$ t: z9 Q modelActions = new ActionGroupImpl (getZone ());* x3 Y0 X0 q7 c. Q/ w
& U" l/ }6 n; n8 |5 \
try {
0 m T) x4 _2 ^6 R( G modelActions.createActionTo$message
' l- i- e+ h1 d8 d# @ T$ ]' ~: p6 L (heat, new Selector (heat.getClass (), "stepRule", false));
& f8 C \) Q% |" i5 [ } catch (Exception e) {
1 _& k* o* d* L& w% U0 F System.err.println ("Exception stepRule: " + e.getMessage ());6 Z2 N8 S- E; [, i. e" n" m0 A
}
5 T( b; }" D; O9 m5 }) R& u/ W# v0 }3 X: r/ ^9 {
try {
. c8 L' L9 W5 | R Heatbug proto = (Heatbug) heatbugList.get (0);
* M7 W: A1 s4 U% N* c8 [+ K5 y. x Selector sel =
# s5 ~3 L0 R1 F i" C new Selector (proto.getClass (), "heatbugStep", false);
$ N) P) ~) m- ^3 ]5 f# l, c3 y actionForEach =$ c& c; i- S6 H& w$ ~
modelActions.createFActionForEachHomogeneous$call3 Q* v: G7 S) i" J$ L
(heatbugList,, l9 q/ O6 M9 N b+ R
new FCallImpl (this, proto, sel,
! V: e4 t2 m7 _' | new FArgumentsImpl (this, sel)));3 W5 m ]4 u/ m
} catch (Exception e) {0 ~. m, { w; h! g
e.printStackTrace (System.err);
7 N0 F# I! O) i6 _2 r$ s }5 X/ Q% |( I$ C. b' J% T& @% z
% ], r' p- L x- J1 q$ b syncUpdateOrder ();! \* t# P( C7 _( @. B: O
" t" B9 K: R9 q- ]- } try {1 P- j/ [0 u# S+ Z k
modelActions.createActionTo$message # g- c& A: F3 H9 A k
(heat, new Selector (heat.getClass (), "updateLattice", false));% v5 b5 C/ I( y; y/ L
} catch (Exception e) {
2 a3 j% q, h! c System.err.println("Exception updateLattice: " + e.getMessage ());
) q/ V* F% n/ U( t* j; Z }% i# w8 `. Z q0 @
- n) q% x: y3 M- {5 O Z, Q/ @
// Then we create a schedule that executes the
0 K1 }2 I! t( L" | f7 s% S( t // modelActions. modelActions is an ActionGroup, by itself it/ ]1 h# x" @+ M( d+ \
// has no notion of time. In order to have it executed in {* v5 E/ Z, Y! G# u; j8 Q
// time, we create a Schedule that says to use the( ^4 l3 x6 p( m( |& b+ [5 j- V0 T
// modelActions ActionGroup at particular times. This
$ Z! N4 ?! d4 ^2 d // schedule has a repeat interval of 1, it will loop every4 l* _8 q4 M8 I; E8 L
// time step. The action is executed at time 0 relative to7 f; J$ I3 s/ s2 g/ |
// the beginning of the loop.
]) e; f, S" A9 h: `' P' w: n! q0 f) O# s m8 b; z6 W
// This is a simple schedule, with only one action that is0 {. E4 Y: n: u7 R; H- n. U
// just repeated every time. See jmousetrap for more
% _* ~) D9 T* \0 p. T, \3 _. f // complicated schedules., U7 ?1 X4 w* `! y" k, O& w
; g9 o/ O& y; \/ J. H+ i7 x
modelSchedule = new ScheduleImpl (getZone (), 1);
$ ?6 C$ C' M) |( G+ Q/ j modelSchedule.at$createAction (0, modelActions);& k' I8 N: L& f# O. N: A9 l
) y1 T( T2 f9 `+ \ return this;% Z1 z/ N/ F( V' V+ U
} |