HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 n, |) j& e1 \' b2 [" Y
: \/ G" V8 N8 U5 i! W; y7 U public Object buildActions () { Z0 W9 O* d6 O& p9 s/ }3 \
super.buildActions();4 K6 ^/ E& ^ i. l) s0 `
% I$ O2 i- H& d }/ e0 y7 C // Create the list of simulation actions. We put these in
5 F. V9 V# X6 W# f // an action group, because we want these actions to be& \# @) Y1 Q3 K0 b( g0 g4 S4 G+ I
// executed in a specific order, but these steps should
+ S* x; T" R6 a" Y // take no (simulated) time. The M(foo) means "The message4 M5 Y* R4 `1 F
// called <foo>". You can send a message To a particular
/ b- ]$ h# h8 T; Z9 M6 l // object, or ForEach object in a collection.
4 E% l4 `/ B8 j, z; F% z( M6 c 8 f# ~# E, r4 q. K# G4 p/ B
// Note we update the heatspace in two phases: first run: Q, x( ^2 r( ?% B0 [ T
// diffusion, then run "updateWorld" to actually enact the1 z; o \& R5 M( I
// changes the heatbugs have made. The ordering here is/ N+ A/ _3 b( g" t
// significant!( @# K2 g' z7 T
& G; z9 ? R. T& p // Note also, that with the additional5 W6 R) t0 S/ t8 i4 J* D3 {8 l1 K# W
// `randomizeHeatbugUpdateOrder' Boolean flag we can- L- D1 r6 _4 g8 e5 _+ @- p
// randomize the order in which the bugs actually run0 ?6 s( b( s( m* X' D! K# O$ Z
// their step rule. This has the effect of removing any
) e0 N% T( X7 G" |$ {2 v$ U. L, A // systematic bias in the iteration throught the heatbug+ @& q: R4 R/ k, _) w; ?
// list from timestep to timestep8 ]9 y4 E" q- x% n9 F$ R# H5 U
& ^2 y" @8 w2 O( |0 d# X; V // By default, all `createActionForEach' modelActions have. j# U' _, [/ R1 x' Y
// a default order of `Sequential', which means that the N" R* r' _. A$ }
// order of iteration through the `heatbugList' will be
" ?0 p8 R) ~ _6 Y3 ~ // identical (assuming the list order is not changed
% n; i: a% G2 @9 M5 S* F3 I* z' x // indirectly by some other process).
' X z" r6 f0 q( H }1 R S. R3 E$ ~2 A% a3 ~1 [% N
modelActions = new ActionGroupImpl (getZone ());$ C/ j8 w. E! H6 M5 {" x0 d( d) I& N
9 P# Y! q. Z+ u) d t
try {
; c2 {& {* \$ b" C5 P: @, T modelActions.createActionTo$message1 {% y2 ^$ u: Y- b* H
(heat, new Selector (heat.getClass (), "stepRule", false));
2 ?6 G P1 L; c1 W9 \ | b% C } catch (Exception e) {, C+ y4 c$ T: g& W! `/ ^1 F
System.err.println ("Exception stepRule: " + e.getMessage ());
% `$ L: v* M- k& L! p* z7 m% f$ z }
- f* Z1 v: g1 q& O6 u+ T# H" o- T" K/ Z
try {
* T* c4 S1 u: G M5 ~ Heatbug proto = (Heatbug) heatbugList.get (0);
3 _2 _2 @( n" e# P' i8 p0 q, n- Z Selector sel =
. q! R* ?7 x% Z1 [! n; J' S! _- G new Selector (proto.getClass (), "heatbugStep", false);
4 R8 w' [! J( K4 X actionForEach =
+ ~, H, `7 x+ V7 F ~ modelActions.createFActionForEachHomogeneous$call, q0 K5 x! \; \
(heatbugList,2 y( a; e; ~! }5 s& {% `. M1 x: O
new FCallImpl (this, proto, sel,
8 R4 O$ b9 B( i) y9 G k new FArgumentsImpl (this, sel)));; |% t& k- X1 v/ ^" j( o
} catch (Exception e) {) [" f' z) P, k
e.printStackTrace (System.err);( X' m0 x6 M8 w. }/ a. y0 o/ ?
}
$ T* p4 _' O4 q8 D$ m" J
* K. N H/ }2 o- {9 x; e- z, ]; _ syncUpdateOrder ();
" P2 q+ t7 K* k- h( k1 g" b& ?; w5 v- p" w& G M
try {- V! s3 G1 J& N8 D" `
modelActions.createActionTo$message
; {5 t9 f% X' r$ ]$ X (heat, new Selector (heat.getClass (), "updateLattice", false));: M' x) q2 B W- _8 a* E
} catch (Exception e) {
J) F& s& O Y! c: f System.err.println("Exception updateLattice: " + e.getMessage ());
; ^2 g. p7 `9 J5 M5 d9 ]* y- p }" q+ c7 a+ [8 |# `' j: s+ |- W: H
( G4 O& G4 @" B( @) q! t6 e
// Then we create a schedule that executes the# B* \- K& b7 X+ J7 v5 r1 J9 M" w
// modelActions. modelActions is an ActionGroup, by itself it3 v$ {# w; }: X6 l9 j8 X
// has no notion of time. In order to have it executed in$ z. K5 |# f) Y$ r4 ^# p
// time, we create a Schedule that says to use the, K5 {. T* s1 |# ~& W
// modelActions ActionGroup at particular times. This1 N8 F5 Y7 R7 @3 H
// schedule has a repeat interval of 1, it will loop every
& G* ~5 v( n) I$ ?0 V: g // time step. The action is executed at time 0 relative to
$ \1 p: t- ?4 W, l0 w. @2 J2 O // the beginning of the loop.0 d' h) P: j6 h9 f
( b1 O7 o: h8 e; i
// This is a simple schedule, with only one action that is
; U+ W6 v! X1 j2 t% I7 e // just repeated every time. See jmousetrap for more; Z3 ]4 E6 W9 Z
// complicated schedules.
$ H0 @( N4 |0 G, ^9 ~
1 S% E8 C1 ]5 h" a0 s4 k6 r modelSchedule = new ScheduleImpl (getZone (), 1);2 e1 ?" F. @+ i; e6 c
modelSchedule.at$createAction (0, modelActions);
9 \1 E% i4 x! ~! V. J
# N7 {: r4 k& Q) P& m return this;
, }) |% D/ u6 i } |