HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
2 S' e/ i5 }. Y6 o. T ?8 L7 I, _: [2 F
public Object buildActions () {0 O. g% y; |& f# d5 H
super.buildActions();
1 P% D# }4 C O) t- \ 3 m% v* M5 O* F3 E1 [
// Create the list of simulation actions. We put these in
( @# ^1 A1 m" E) R, e // an action group, because we want these actions to be# {3 p4 D- a) P# ~
// executed in a specific order, but these steps should
0 U4 K: j+ U9 }. b( J# N // take no (simulated) time. The M(foo) means "The message
3 d( T5 ~0 S3 \* |) S // called <foo>". You can send a message To a particular
/ W b2 h( H# O // object, or ForEach object in a collection.) k9 u2 o) Z2 L* Q6 R
- k* N5 \- b. n" w$ ^% R/ `
// Note we update the heatspace in two phases: first run6 n& P% }: R5 `) [# g1 t5 b' A
// diffusion, then run "updateWorld" to actually enact the4 J6 |( I! W4 C: ]- H
// changes the heatbugs have made. The ordering here is7 G3 M- s6 `3 F) B8 r# D
// significant!
! n9 L2 P# C* [/ ~5 l1 Z
9 B! p+ R- @9 l$ ~1 b! k* U7 v // Note also, that with the additional
! }7 d/ i4 k* u1 u0 } // `randomizeHeatbugUpdateOrder' Boolean flag we can
3 I; r7 N. i! d' d# ?$ r" @% q! L // randomize the order in which the bugs actually run5 c) _+ }: _3 i) d0 _7 f
// their step rule. This has the effect of removing any7 H5 j# j; Q8 J9 L/ F# I% o. F
// systematic bias in the iteration throught the heatbug! S0 x+ v7 C& Q; ]' {
// list from timestep to timestep
1 y7 A9 Q( G5 ]6 U
: k. P& T7 L; R6 p! m; D( K7 G6 ^ // By default, all `createActionForEach' modelActions have
2 _: _% H! y J // a default order of `Sequential', which means that the
- M4 j& H! t& k+ [% k // order of iteration through the `heatbugList' will be4 M+ `* b" [9 a5 i) f5 Z8 |
// identical (assuming the list order is not changed
& ] T1 Z/ h) e' m // indirectly by some other process).
) i `$ Q4 n& p7 _+ u7 g
J, [/ U! R% Q6 a- e( U modelActions = new ActionGroupImpl (getZone ());& n& B( v2 v! y) j0 r
: S. {, t2 C$ p, w- \+ q y6 O3 t try {
. X# E. G5 m' } modelActions.createActionTo$message
4 Y0 y; v7 ?6 n (heat, new Selector (heat.getClass (), "stepRule", false));' @* [5 `/ `2 O# d4 ?
} catch (Exception e) {
' |2 _7 t- i7 ?* d# H System.err.println ("Exception stepRule: " + e.getMessage ());
7 {+ v- F' A* L+ ~, r6 M* e. Q$ o# L }
% j( E j# q" `
; O0 r, p* s" e, j& ~8 d try {. d# q- `: ] G* q. E. |
Heatbug proto = (Heatbug) heatbugList.get (0);
0 N6 i! K9 p$ q) I2 c1 M$ g" @ Selector sel = * |) @, U% J3 [3 }% [) {0 R7 M
new Selector (proto.getClass (), "heatbugStep", false);
/ U2 ?2 i: e0 K) Q# N actionForEach =% n8 L/ ^/ d* o: a) v! M! N) r
modelActions.createFActionForEachHomogeneous$call
2 p' w, T$ o/ l6 Z3 R/ M& U/ Q (heatbugList,
3 e! X) S7 a; @- k new FCallImpl (this, proto, sel,& T) r/ i, V4 ]' ]1 `- e
new FArgumentsImpl (this, sel)));
1 s7 r5 k2 x+ L2 F" |! u4 @ } catch (Exception e) {
% E- g L8 _0 w; ~ e.printStackTrace (System.err);, f% A5 l! G! \2 Q% e: W% l! N% C
}+ k& X$ |& A; G, c( {) ~+ R
" w& s- D" q5 J& I( E
syncUpdateOrder ();' q P7 z5 {2 D- K
" d, s. b2 ?% g+ r. Y* n9 t* M try {
3 t9 s* T4 R1 Z6 n! A- k modelActions.createActionTo$message . \3 A7 u$ c0 {. V0 O3 ~; c
(heat, new Selector (heat.getClass (), "updateLattice", false));- ~$ A' h' d% D* d: j
} catch (Exception e) {, J8 y' L. L( ?7 p" ^
System.err.println("Exception updateLattice: " + e.getMessage ());
* J- c1 f4 y9 @( y }
) \6 m5 }( `( P, q. {* h
8 m% g$ _2 { l // Then we create a schedule that executes the
) d& u& E3 a/ f1 e& s+ I // modelActions. modelActions is an ActionGroup, by itself it
5 q% m1 h1 ?0 i' B // has no notion of time. In order to have it executed in2 m+ s5 j R1 H1 P! q
// time, we create a Schedule that says to use the7 v0 c0 o- \3 u2 a
// modelActions ActionGroup at particular times. This7 a* U+ X) l: z- X" v/ Z8 k5 Z
// schedule has a repeat interval of 1, it will loop every5 G! W) D" W$ T0 n& N
// time step. The action is executed at time 0 relative to
1 @. e/ O5 D/ z! N. c // the beginning of the loop.+ g7 _* {% G. I8 f$ T% ]6 H4 P
* I0 _, J" r2 y3 G0 a& z
// This is a simple schedule, with only one action that is
) G! h4 v) z0 a0 E& T$ _ // just repeated every time. See jmousetrap for more
' y/ Q3 t& y% x. E4 _ q2 ` // complicated schedules.
$ T3 H. S+ }. W6 B/ V% x
7 T: r% h1 s. i. ]4 Z" a. s1 h1 @ modelSchedule = new ScheduleImpl (getZone (), 1);% j1 \% p3 t* h8 I
modelSchedule.at$createAction (0, modelActions);
7 m' }( f2 | @2 q7 c ; C! I& _: q/ _2 H
return this;
3 @" L; N6 y- R2 f5 Q+ e } |