HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# q$ W+ ` b7 b6 N) u
0 L" i7 [( ]3 e d# ] public Object buildActions () {# V; {& Q- l# a7 R! O
super.buildActions();4 h* d. U$ K, f( i8 Q
6 B' b6 f( w. r- F( _* d# X // Create the list of simulation actions. We put these in
1 ]1 s2 E5 I4 ^; j& h# i+ E // an action group, because we want these actions to be; O) e: L i( s4 R1 I
// executed in a specific order, but these steps should
# \' G2 f) R# N" b // take no (simulated) time. The M(foo) means "The message+ i* Y7 B: M# t- N/ K+ z5 v0 }
// called <foo>". You can send a message To a particular
( G8 w) b+ j' ]. R0 y$ N! ^* F // object, or ForEach object in a collection.9 M9 d5 Q: X) ]" _! S
# l& r) \) f4 A // Note we update the heatspace in two phases: first run b# w2 a! _ P- K# D! K
// diffusion, then run "updateWorld" to actually enact the
, z- M1 h x0 `" u // changes the heatbugs have made. The ordering here is: j& Z( o3 \: N% O& m
// significant!
- @9 T$ [, A- Y1 R 8 ]/ Q2 p% L2 |$ q9 ^
// Note also, that with the additional
; \: w9 Y; u; g0 s O6 P* N# T // `randomizeHeatbugUpdateOrder' Boolean flag we can6 @! T2 g3 B, }' T1 e9 I( @ e
// randomize the order in which the bugs actually run
- `/ f1 r$ j& h/ y# m% m // their step rule. This has the effect of removing any. D. T. O& Y' [$ F8 j* d, |
// systematic bias in the iteration throught the heatbug
" w8 t3 [4 g- Q. V( u- w // list from timestep to timestep
6 K3 n: X' j( r0 u5 Z
7 f# k- H& w7 X/ V, m R // By default, all `createActionForEach' modelActions have
6 l% I6 E8 s7 u* Q // a default order of `Sequential', which means that the
5 o+ M2 c$ |1 T+ d // order of iteration through the `heatbugList' will be
+ A8 [9 E5 L, _ // identical (assuming the list order is not changed
) y( [8 J0 l" w6 l. R, _% e // indirectly by some other process).
- c3 E, T" ^& v0 Q. F8 h ' }0 M5 s ]9 b
modelActions = new ActionGroupImpl (getZone ());& x9 `$ X% f0 F. ^9 c% x# `
& G* a' ^- G" l4 j8 ?7 L2 n1 |8 U
try {
- ~3 U2 D6 M; f0 e3 ?+ W modelActions.createActionTo$message/ i! f1 r1 l: R9 A! `/ r8 {
(heat, new Selector (heat.getClass (), "stepRule", false));2 ^: I' B7 `$ J% z; C7 h
} catch (Exception e) {! C! b2 M( `4 H8 I0 G; f
System.err.println ("Exception stepRule: " + e.getMessage ());
# G, a. ?6 N0 a }- l. @4 G2 F2 u i5 t p
9 ~, x) Y7 C( D
try {
2 k! a8 z4 Q* c Heatbug proto = (Heatbug) heatbugList.get (0);4 ]2 @7 H4 D9 ]) s# O' Y
Selector sel =
* D2 _7 ^# {! W6 X( k4 g new Selector (proto.getClass (), "heatbugStep", false);8 J$ W0 X( y( E4 u, { L
actionForEach =- R. }% e. D" o8 B# n# F
modelActions.createFActionForEachHomogeneous$call
; x1 l) B( j( B/ b (heatbugList,
) B# L% l& u3 V% d! n: ^* I: k new FCallImpl (this, proto, sel,
1 ~' H7 C% F2 |" S% l( e* t new FArgumentsImpl (this, sel)));
( k+ I) N, r* {6 @, P$ J } catch (Exception e) {/ l3 i. B' {* t0 x2 z
e.printStackTrace (System.err);2 x0 s# G. k* j% ?( a) ^" c- j% I5 ^
}, p: ^5 q' [7 j, u3 S
& u1 c( F. _$ i& m syncUpdateOrder ();- h5 B! e% I0 m* V9 `; t
$ p ~: m! V& A, t, g
try {
% n% \9 n. }( p8 o( J6 l modelActions.createActionTo$message
( h0 e6 o& t: m+ [" a9 v+ ] (heat, new Selector (heat.getClass (), "updateLattice", false));
' d. r% ^% R3 m$ H: Z) o2 B } catch (Exception e) {, u6 K- }5 B7 C' ]
System.err.println("Exception updateLattice: " + e.getMessage ());$ ]- F: H" a) [) ?" ^
} R5 T5 t* k( Z+ O. g
V' w! ?( B4 h ? // Then we create a schedule that executes the: Y4 M# B/ G+ O2 t! M
// modelActions. modelActions is an ActionGroup, by itself it9 W4 x# g! d1 r5 g
// has no notion of time. In order to have it executed in$ o3 [. {% z- N0 g$ O5 k
// time, we create a Schedule that says to use the4 l$ K, q' J# k# G
// modelActions ActionGroup at particular times. This
. M7 x; L8 E6 y; g1 T // schedule has a repeat interval of 1, it will loop every: Q' W$ o1 y( }$ b e/ B
// time step. The action is executed at time 0 relative to! \9 P# ]2 d5 ?8 p5 d# o' F
// the beginning of the loop.$ b) p1 `) f, S$ A2 o: C
/ |, C7 p% c, h" `* c4 H) ]
// This is a simple schedule, with only one action that is
+ |& s% n I9 e // just repeated every time. See jmousetrap for more @. ?& ]- N. k/ ]
// complicated schedules.
& h8 K) Q7 F1 `7 I- s* J) U. ^ 4 f, `* O5 f* ~" m- r# L
modelSchedule = new ScheduleImpl (getZone (), 1);* t% ?9 n; j+ i
modelSchedule.at$createAction (0, modelActions);
5 J4 n2 J9 N; C$ }, {/ ~5 |
* F. [" P- l6 z- I. Q( X9 Z1 H return this;
8 m* p: s c7 I7 m" I p } |