HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:1 I4 O% Z8 G( A* t/ Q O8 t
8 f J. \' \- B) B" l
public Object buildActions () {
4 Y, b- ?. h' z% c super.buildActions();5 c" K( H) ]$ `1 l# g
$ M1 X1 F; G! ]: A
// Create the list of simulation actions. We put these in
! |' R$ X) _3 P/ y+ X* l5 @! x // an action group, because we want these actions to be) p) a( X+ z! n
// executed in a specific order, but these steps should0 y/ r# O8 E2 a5 A, r
// take no (simulated) time. The M(foo) means "The message
* X G1 Y" k0 c3 g* r) B$ l // called <foo>". You can send a message To a particular
: M2 m( R$ J! o // object, or ForEach object in a collection.
5 I o3 o- D" W6 t ; h: @8 R# H$ R! C9 A0 Q
// Note we update the heatspace in two phases: first run( |8 I+ I; D' s
// diffusion, then run "updateWorld" to actually enact the
k0 h: M) i9 t$ k: p6 ]! y+ A // changes the heatbugs have made. The ordering here is* U: d3 @/ x; {2 x, `& b* q
// significant!
: z$ U; k% E' J0 ^5 f# B
4 Z1 g3 O& c l$ V5 S // Note also, that with the additional
. }2 c a7 k6 l) N // `randomizeHeatbugUpdateOrder' Boolean flag we can
* R3 R2 e; l( \) d6 _* Z // randomize the order in which the bugs actually run" L, g8 w9 C, X; {7 Q: z0 A
// their step rule. This has the effect of removing any
5 f- K5 y x( ^, T! n2 z // systematic bias in the iteration throught the heatbug
6 B$ m$ O y( I // list from timestep to timestep( {; ]8 }& ^2 p- h1 q
% z& `+ g) t0 w d6 j$ Y
// By default, all `createActionForEach' modelActions have
, {2 X8 `; T, S+ Y // a default order of `Sequential', which means that the; j( _4 V0 P9 E. m
// order of iteration through the `heatbugList' will be
1 f# e$ P. {* _! P4 m9 U // identical (assuming the list order is not changed! W) s; e8 e% H& i
// indirectly by some other process).5 ?) n% f1 N, ^! g5 d/ {$ X2 p
( O4 {: W6 `/ G$ Y2 x2 m modelActions = new ActionGroupImpl (getZone ());
; [) t3 H% ^- D) Q
+ B X, k8 U+ L( Q- a0 m. H- A2 D try {
* b6 _, t( u2 N5 Q m# O modelActions.createActionTo$message% A- L$ t# v8 {' r$ m! b
(heat, new Selector (heat.getClass (), "stepRule", false));
9 e9 p, o2 f: N, f5 \ } catch (Exception e) {7 O" n) S9 B1 N8 P. W- v4 S
System.err.println ("Exception stepRule: " + e.getMessage ());! [6 O8 L1 ^' H9 [6 ?
}) u- q, \, E2 X: H8 J
& J& n+ b# d- N/ k: e3 W3 q5 t
try {
! d' Y' t' k2 a Heatbug proto = (Heatbug) heatbugList.get (0);& Z B/ X, Y# J7 x
Selector sel = / t! D( P9 c6 ~, t
new Selector (proto.getClass (), "heatbugStep", false);6 t5 E* v0 {* p4 Z$ q Z& l
actionForEach =
5 r* B8 F1 x( [+ g0 R modelActions.createFActionForEachHomogeneous$call6 `1 | @7 d6 [
(heatbugList,6 ^# K, @; x) l9 Q* \
new FCallImpl (this, proto, sel,
+ b' p7 O, ?: V' U new FArgumentsImpl (this, sel)));
Z' ~4 Q; [) y, N& f9 I, d3 X } catch (Exception e) {
* F( |5 e* s+ U5 M& c5 z) Y8 j e.printStackTrace (System.err);9 i( G8 K6 n2 G
}3 {* \3 `2 q2 J3 t' N7 {
! ?5 _( I, P) g, c6 p+ T3 h
syncUpdateOrder ();5 O/ r1 Y( p. T$ i9 I
% x1 @7 y7 S0 L( h# x' Z o; G
try {9 U" ^- @5 l5 E7 G8 s8 `
modelActions.createActionTo$message
" k+ w, z6 B0 r# E( i$ N, K3 f8 n. y (heat, new Selector (heat.getClass (), "updateLattice", false));% E a/ j7 y4 g: |
} catch (Exception e) {. z: q- Y3 I: O8 T2 A4 K; z; x
System.err.println("Exception updateLattice: " + e.getMessage ());7 }8 E8 }" Y+ A0 Y9 I2 t
}$ y- e+ n3 \5 S2 Y9 g) q
0 b5 s# m. E$ \! ^, t) t3 \
// Then we create a schedule that executes the8 l( W% w! ]" y5 _) Z1 W: x3 k
// modelActions. modelActions is an ActionGroup, by itself it
) o: o% N# C# A! S) h7 x% | // has no notion of time. In order to have it executed in
3 m2 V, f/ U( \% `7 ` // time, we create a Schedule that says to use the# w* ~: S' G& p$ C9 c
// modelActions ActionGroup at particular times. This
- V7 |; T% C7 F' G! Q: ~, C // schedule has a repeat interval of 1, it will loop every
; G' U# X3 r5 s1 i) z: y' y" T7 B // time step. The action is executed at time 0 relative to# T# b* u+ Z8 N: b% C7 B
// the beginning of the loop.. A; E$ H0 F t0 W1 v
N# e' I$ G: J( G& J( y
// This is a simple schedule, with only one action that is, }# K3 H5 [" O' m3 S
// just repeated every time. See jmousetrap for more
3 w! W+ Q7 Y0 Y0 i; Y" A" e5 c% D // complicated schedules.
) S* Z; z& E% O% L1 n
% }; e" C# l( ]3 Z5 R modelSchedule = new ScheduleImpl (getZone (), 1);7 a' A2 D$ ?' x
modelSchedule.at$createAction (0, modelActions);' e0 A; K9 a2 B- e1 g! S
6 ^+ \9 @; V: z2 W# M return this;0 G4 {; Y d8 y) e# Z/ E8 n
} |