HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:" d% s S/ z0 `( v
# \' v+ r' Y+ h/ m
public Object buildActions () {
. A; _! u- Q% A$ z9 c- K0 f super.buildActions();& g7 V, o3 e: r% _
. X) B" v. `! \. I" {* |( a // Create the list of simulation actions. We put these in
R( \: m* L; d$ K // an action group, because we want these actions to be
! h# Y; r8 ]5 t) o3 t! D9 m7 a. U1 G // executed in a specific order, but these steps should0 }/ R0 Y# E. h b* I
// take no (simulated) time. The M(foo) means "The message: e9 N, N. R) M: n; h: ~$ Q
// called <foo>". You can send a message To a particular, x9 y7 [ X- T9 M& A! I
// object, or ForEach object in a collection.6 V1 S' J/ h$ x4 n* a2 ~5 [$ u9 _5 V
% B) U. k: K) `6 q7 G: U9 a // Note we update the heatspace in two phases: first run
/ D" X. B2 `5 O: }/ l/ V% P# l0 R // diffusion, then run "updateWorld" to actually enact the
- l: x9 q5 r/ X2 @5 p/ D // changes the heatbugs have made. The ordering here is7 C) U8 K7 j+ Y! c% V8 Q" S3 \
// significant!2 a( |4 H; E: O
0 X6 u( ?, V" Z5 Y- ^8 p& r" M // Note also, that with the additional' L& j( w3 r: V1 d9 u* b# q% @
// `randomizeHeatbugUpdateOrder' Boolean flag we can
3 T3 b0 X" \4 K v2 k' Y1 R" x // randomize the order in which the bugs actually run! f, ~# x, ?; Z" K; `, N
// their step rule. This has the effect of removing any* L0 E4 ~9 N! j. \* K
// systematic bias in the iteration throught the heatbug4 ?- t7 ]9 f# t. m
// list from timestep to timestep( H5 G1 K% s2 D# L0 J5 u8 u
9 |3 y0 I1 ^; X. P // By default, all `createActionForEach' modelActions have* l f5 a! U* d
// a default order of `Sequential', which means that the
0 W5 P' G* `% r4 X0 Y // order of iteration through the `heatbugList' will be
) `" y) Z7 ]; e" u // identical (assuming the list order is not changed- I9 B- S! Q9 Q& Q& R) O
// indirectly by some other process).: p$ z6 Y9 K. f! X3 K% F9 v
) c+ C3 [) y) u! Z9 t: |0 n
modelActions = new ActionGroupImpl (getZone ());. V, E6 D- K7 I$ m( i
2 D S1 x, J) ^" j3 v try {
0 M s; v8 Y Z# E modelActions.createActionTo$message; w" ^. i' }1 L T ]. x+ p1 T2 I
(heat, new Selector (heat.getClass (), "stepRule", false));, i0 F1 B5 {( E+ l
} catch (Exception e) {8 j% }, l% S7 G2 j6 I" E; \
System.err.println ("Exception stepRule: " + e.getMessage ());& Y$ j4 k3 N h. a( G
}
' C8 W- P# i0 z: @' v5 D" ^6 h3 N" S6 g& w
try {% n5 ~3 a+ ]: O' H! X, |
Heatbug proto = (Heatbug) heatbugList.get (0);
" o) v! c6 d8 {4 {# f8 l Selector sel =
# G# Z, M8 w5 C# W0 E8 l/ Z f new Selector (proto.getClass (), "heatbugStep", false);; \% {7 |% H7 L; p, i. u
actionForEach =( r) ^$ |- Y- t( e
modelActions.createFActionForEachHomogeneous$call
9 S3 I& J, T& a# B- l (heatbugList,4 q0 S3 ]; D. {8 h+ t6 V
new FCallImpl (this, proto, sel,
9 A0 D4 Y' ]8 m+ ` new FArgumentsImpl (this, sel)));4 o9 j! H( U" d- {) U
} catch (Exception e) {4 a& Z& P% t5 H& e7 ~4 c2 v
e.printStackTrace (System.err);0 m+ T2 b: X4 K3 C& z8 M
}
b* z. S% m1 G, Q ' p R1 \) v5 C- C# H
syncUpdateOrder ();
9 X: k( J" ^ a7 |5 U; K
$ m2 U# O6 h1 _ try {
7 v4 Q2 E, o- \ modelActions.createActionTo$message
% J& F, F# ~; ?; |* ?; g. { (heat, new Selector (heat.getClass (), "updateLattice", false));" i$ e' D" X; k* b4 W" S3 u7 Y
} catch (Exception e) {
; S4 D1 ?3 l1 Y' k. M" y! V% a2 I! @ System.err.println("Exception updateLattice: " + e.getMessage ());4 J" v4 n& i2 D: @6 o( p- C) O
}9 F5 M$ j" \! {6 S' A
/ t5 X$ u+ e g) ? // Then we create a schedule that executes the: V' u6 y$ g- j8 ]2 t- H
// modelActions. modelActions is an ActionGroup, by itself it
- j T- R2 e/ Z2 w0 J$ @ // has no notion of time. In order to have it executed in
8 P# j& T. H4 Y- A // time, we create a Schedule that says to use the) x% K. `# W9 B
// modelActions ActionGroup at particular times. This
* z5 ~% Q- x! t1 \2 S // schedule has a repeat interval of 1, it will loop every
7 h6 n! R% C+ e // time step. The action is executed at time 0 relative to8 ^1 L3 P% d1 _. q: O
// the beginning of the loop.
( |7 K* H0 U+ L9 D
" S* I6 c! @, t5 O [, n // This is a simple schedule, with only one action that is( m/ A: R$ g) L& q7 x
// just repeated every time. See jmousetrap for more* T9 r0 B- H- L! r A% [! f
// complicated schedules.
% M T; h# h' j0 g/ Y% }
- y6 ~5 V. V# ~, p modelSchedule = new ScheduleImpl (getZone (), 1);! [" _ ]. k p
modelSchedule.at$createAction (0, modelActions);
9 @8 t9 V6 r6 q3 \+ }2 J
* Q" T, Z* l+ C5 ~% @ return this;
1 [+ e7 r* I: H o% ?- K } |