HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 _8 |6 ?4 L& K2 ~: L4 P9 ~: |: Z3 i: s3 O
public Object buildActions () {( A6 E' C: J o7 A$ k
super.buildActions();
! \: _7 G- Y' T2 @8 i : M/ t& l! G7 _6 \: h
// Create the list of simulation actions. We put these in
5 v* @+ N/ y, Q, w. j3 J // an action group, because we want these actions to be. T, N; d. F, Y; X8 E) E8 c" W/ `' Q
// executed in a specific order, but these steps should0 L, T2 a& f0 _: |# U; F
// take no (simulated) time. The M(foo) means "The message3 X% F9 j7 ^$ H. ^+ j' \
// called <foo>". You can send a message To a particular
( f+ }6 U" [8 L# Z5 O // object, or ForEach object in a collection.- V6 x1 W/ L. y* ~5 P5 f
7 Q8 F4 `* C2 a // Note we update the heatspace in two phases: first run% |! o3 J5 c% n b' l/ @
// diffusion, then run "updateWorld" to actually enact the
5 v7 a6 o* T' h& [ // changes the heatbugs have made. The ordering here is
! ^; n. S6 b' v8 b" Z, T0 e // significant!
8 ]: X+ Z9 J2 o8 l( L" p" s- i - A2 `: R% b- U/ r
// Note also, that with the additional
0 Q, R' v: d' d2 I/ M // `randomizeHeatbugUpdateOrder' Boolean flag we can. P9 V- [, y* z0 N; _ ]
// randomize the order in which the bugs actually run0 ^3 m9 Y9 N0 k9 z( n! d
// their step rule. This has the effect of removing any4 k; G. u2 [: J. _: w1 C
// systematic bias in the iteration throught the heatbug
Q) x$ q& R9 b+ Z, V" n7 ] // list from timestep to timestep4 n2 ]3 R1 e5 k2 m
" m. d3 T0 V ~* g( w# `9 z // By default, all `createActionForEach' modelActions have# S! D) o' N/ ?" x) q" ^' @
// a default order of `Sequential', which means that the, B) G9 z2 w9 F* ~
// order of iteration through the `heatbugList' will be
6 E- F2 g- ]7 p // identical (assuming the list order is not changed
+ z5 H" z$ r$ W) _" I+ a // indirectly by some other process).6 ^1 Q- Q [- ?! [, i! o
" T0 \) y$ y! H O+ f) A modelActions = new ActionGroupImpl (getZone ());
# T T4 g. S. n
; ?/ W2 o! L) t* x4 Y( A try {: _6 X6 _: l$ A& b- s' N
modelActions.createActionTo$message
5 X/ [% I+ R3 u/ O2 u/ x (heat, new Selector (heat.getClass (), "stepRule", false));
/ o" c+ W8 ?5 i } catch (Exception e) {
1 T$ M/ E8 Y+ U, I System.err.println ("Exception stepRule: " + e.getMessage ());* \! t3 K; f/ z- P
}
, s- V! G( U) A! T2 _, h$ T& g3 z, w; _" x
try {
+ L1 p& L4 U" r% u) R# ~ Heatbug proto = (Heatbug) heatbugList.get (0);3 R4 h! c/ o1 |
Selector sel = 5 G/ y6 l; m5 _( [: S6 R
new Selector (proto.getClass (), "heatbugStep", false);# S+ q# c- _5 U7 P3 |
actionForEach =6 x8 n0 u. i% y
modelActions.createFActionForEachHomogeneous$call4 [2 @6 ^- \# ]& A" @
(heatbugList,
' s9 \' E$ |: v, H2 t new FCallImpl (this, proto, sel,
1 u- g0 s' X; b/ B o3 o( x% k: T5 V new FArgumentsImpl (this, sel)));
s8 @" @/ @) S4 u3 g0 x% i } catch (Exception e) {
/ I9 {, c4 \2 w- R }' l e.printStackTrace (System.err);
- p' E: P; z1 \# b5 H# n& Y+ n }. a' D3 O; ~6 X t2 D
3 M+ X5 O8 t) @+ J$ R8 E5 ]% j
syncUpdateOrder ();
* p/ f9 V1 V; @4 I
, ^* F9 c# A |- k3 \. H try {
5 y: p! z! O; F w1 J" c( T, } modelActions.createActionTo$message 0 V7 ^: Z1 ^- K* T7 j: [
(heat, new Selector (heat.getClass (), "updateLattice", false));
' b5 a! ^6 ~- l& H m } catch (Exception e) {- Z( v R- O- K! Z( ]2 J; q! g& O
System.err.println("Exception updateLattice: " + e.getMessage ());
" S) {3 J7 E, I6 X5 I( Z: L% M }3 M7 W) c) }3 M( Y' L d
1 y- b$ m3 S( B" o- J0 v // Then we create a schedule that executes the
' g3 W7 x `1 c" S$ s9 b9 g // modelActions. modelActions is an ActionGroup, by itself it; h9 F! e- q$ G& S, Q
// has no notion of time. In order to have it executed in5 y5 G5 L/ _5 ?/ d$ ]# ]" `: s$ I
// time, we create a Schedule that says to use the
7 j4 k; Z: S! i0 k1 K# k4 ~ B // modelActions ActionGroup at particular times. This
+ b# ~+ w4 ?, \- F+ K- q7 j // schedule has a repeat interval of 1, it will loop every3 D! w" H5 K" h. O: x8 K6 R/ `
// time step. The action is executed at time 0 relative to2 i& R% a5 ^- Q3 v2 i/ _) `; c
// the beginning of the loop.
& P! H9 P/ H( r( m! q9 w' a* J) w6 F
// This is a simple schedule, with only one action that is! h" j: [% V3 {/ Q3 T8 Q) G
// just repeated every time. See jmousetrap for more
' R$ \1 F0 P% ^% `9 ], `2 a // complicated schedules.* B7 P+ l( w. v j4 G* h
% ^# B9 V8 U3 o: B* S7 p& q* R- W* S
modelSchedule = new ScheduleImpl (getZone (), 1);
! _4 q ^& r0 P/ r, j5 b6 G: B modelSchedule.at$createAction (0, modelActions);
8 g1 `0 C) [7 S+ L: K0 N3 _ ! {3 T* X1 o8 l# l" R
return this;
1 M9 v- Z3 S6 d* z } |