HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- X Z+ n4 x0 C; Y2 ~& i4 m- k/ c. y
4 ^0 U. E' Z ~ public Object buildActions () {8 _8 B- Z- @( P5 B4 V, y
super.buildActions();
# _- {# E2 { i1 ? , A% J/ ^; @& w! R3 ~7 X! i3 j
// Create the list of simulation actions. We put these in, d3 @ k5 Y1 u5 S7 S5 N# X6 P
// an action group, because we want these actions to be" z5 D( s5 ]/ H! s- k
// executed in a specific order, but these steps should
. \9 k6 ?' g) E& p // take no (simulated) time. The M(foo) means "The message9 G/ M' a* p$ U- N' R$ _$ g+ p
// called <foo>". You can send a message To a particular
: ]* l4 c6 \2 f) v9 u: g // object, or ForEach object in a collection.2 S/ G. p6 H+ B* e Z
7 k- Q# p; o* v // Note we update the heatspace in two phases: first run
& s' P/ D8 Y% O5 i! ]! C+ i // diffusion, then run "updateWorld" to actually enact the: g0 @' _/ y; f7 R6 p$ V
// changes the heatbugs have made. The ordering here is
) K; f' p, ^! ^4 e8 Y* I // significant!
( G# v2 G+ {) v. ]9 n: X 2 g+ R+ [& r' l. o
// Note also, that with the additional
! a4 O- M. C7 f$ J! `, d9 P7 E // `randomizeHeatbugUpdateOrder' Boolean flag we can1 S2 R. L1 { u/ M2 z
// randomize the order in which the bugs actually run
, R1 `3 ^% z- i- A // their step rule. This has the effect of removing any
+ @, y1 `9 Z4 g/ r // systematic bias in the iteration throught the heatbug# ~; f8 W! O) k
// list from timestep to timestep
; [! q- n% j( w- \7 O* z$ M7 Q: u
! z; x- w2 |5 Q" @3 w+ s& p // By default, all `createActionForEach' modelActions have) H- o: o% ?1 E
// a default order of `Sequential', which means that the
/ t2 K, Q7 U) z% x+ n // order of iteration through the `heatbugList' will be" `) k" h- p' x& |0 L
// identical (assuming the list order is not changed
6 m: A4 W! U: p; r& @2 j6 U // indirectly by some other process).
' X# W4 i# y9 C5 n) z ; f. h+ O+ m3 [7 e, q
modelActions = new ActionGroupImpl (getZone ()); |" K+ n: {' B5 J' _
# Y( N7 t2 g* o9 V, V$ Z try {
3 \+ J4 d7 Y" w: V9 n {/ O modelActions.createActionTo$message9 ]1 f! S) S. ~: `- {: N; G) B. y
(heat, new Selector (heat.getClass (), "stepRule", false));
( e7 s1 h$ e! J; d5 g } catch (Exception e) {: |$ N0 N0 a% H! z; _. T6 j
System.err.println ("Exception stepRule: " + e.getMessage ());
% z( t% i: N- Z! u, k) g }# d9 T" f5 ]( b' {) {
0 j) T) y# O$ o/ R
try {+ j2 ], M# O' c* @ a
Heatbug proto = (Heatbug) heatbugList.get (0);
. t+ V6 D" x2 a1 l2 p( }8 s Selector sel =
. d$ ]" c" c* u k E7 z new Selector (proto.getClass (), "heatbugStep", false);" A& X0 t9 O* O% \0 y
actionForEach =
7 q5 ]$ v' t+ v& T; d2 C modelActions.createFActionForEachHomogeneous$call. x6 t4 q5 a+ t7 E/ `
(heatbugList,
- B8 w* m6 a8 w8 z/ n3 s5 ]* ? new FCallImpl (this, proto, sel,2 o/ P! t& |$ z* N+ {7 x5 {
new FArgumentsImpl (this, sel)));
4 `4 b" Z: M# T, I } catch (Exception e) {0 L' O0 d7 b5 M( ~- v! ^: y
e.printStackTrace (System.err);
2 a& ?7 ~6 a! v) C3 e5 m7 H }
$ @) ~9 [ Q4 [8 c& u( i
9 C! | @1 {3 F6 M S- ?3 Y syncUpdateOrder ();: L7 @# ^; c8 c% N) U
- _, ]+ _0 ~) {/ A& {
try {
+ Y p( |( ]) `0 D/ x2 U modelActions.createActionTo$message
( T0 w. g' ?. A (heat, new Selector (heat.getClass (), "updateLattice", false));
, {9 M. y: ~6 O0 u" A5 g | } catch (Exception e) {
, J. |& s/ l+ P; U4 d System.err.println("Exception updateLattice: " + e.getMessage ());
1 x1 s8 i" E* q( t/ u }& z F. x, U }
" D! V1 g! i* I8 J& T
4 U f8 K! d3 r Z) c // Then we create a schedule that executes the9 V E9 o, Q1 M7 F
// modelActions. modelActions is an ActionGroup, by itself it
4 H- O/ z/ j- ]1 R: N+ S; T: X // has no notion of time. In order to have it executed in/ m6 k, A, y; p7 |
// time, we create a Schedule that says to use the! P+ L* p* \9 S. O2 ]
// modelActions ActionGroup at particular times. This
; @, k' [3 ~. I2 e' H // schedule has a repeat interval of 1, it will loop every
! ]6 G8 p- w% F. \* q% U1 E // time step. The action is executed at time 0 relative to$ B" @8 T) a8 s- Z
// the beginning of the loop.5 K: A& `7 ~9 M0 S
& f! O0 g4 h0 m' a2 Q; F // This is a simple schedule, with only one action that is
; q; U& v7 l+ U2 Y n/ P3 f // just repeated every time. See jmousetrap for more# p& s6 [. k* y$ s, S, N
// complicated schedules.8 a4 S" _ c, K# i" \
* ^& y( I2 G. y' }6 |0 ~
modelSchedule = new ScheduleImpl (getZone (), 1);) V3 b) n$ M, `, I5 L- Z
modelSchedule.at$createAction (0, modelActions);* ~" s m* l! Q }6 I
- N" X) ~0 `5 G, H' A5 v
return this;. K* y+ p7 D/ V8 @4 a
} |